Sha256: e52ef04401232727a4fc3bd8ed94ee07c667736fb71967d96aac8af796615681

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'open3'

module Percheron
  module Container
    module Actions
      class Build

        def initialize(container, nocache: false)
          @container = container
          @nocache = nocache
        end

        def execute!
          in_working_directory(base_dir) do
            execute_pre_build_scripts!
            $logger.debug "Building '#{container.image}'"
            Docker::Image.build_from_dir(base_dir, build_opts) do |out|
              $logger.debug '%s' % [ out.strip ]
            end
          end
        end

        private

          attr_reader :container, :nocache

          def build_opts
            {
              'dockerfile'  => container.dockerfile.basename.to_s,
              't'           => container.image,
              'forcerm'     => true,
              'nocache'     => nocache
            }
          end

          def base_dir
            container.dockerfile.dirname.to_s
          end

          def in_working_directory(new_dir)
            old_dir = Dir.pwd
            Dir.chdir(new_dir)
            yield
            Dir.chdir(old_dir)
          end

          def execute_pre_build_scripts!
            container.pre_build_scripts.each do |script|
              in_working_directory(base_dir) do
                command = '/bin/bash -x %s 2>&1' % Pathname.new(File.expand_path(script))
                $logger.debug "Executing '#{command}' for '#{container.name}' container"
                Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
                  while line = stdout_err.gets
                    $logger.debug line.strip
                  end
                end
              end
            end
          end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
percheron-0.4.0 lib/percheron/container/actions/build.rb