Sha256: e73189801828afcccdf0ee06618e6410dedf1e61bf3825afeedbe3b8e2d9ec2c

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Specinfra
  class Backend
    class Docker < Exec
      def initialize
        @images = []
        ::Docker.url = @config[:docker_url]
      end

      def run_command(cmd, opts={})
        cmd = build_command(cmd)
        cmd = add_pre_command(cmd)
        docker_run!(cmd)
      end

      def build_command(cmd)
        cmd
      end

      def add_pre_command(cmd)
        cmd
      end

      def copy_file(from, to)
        @images << current_image.insert_local('localPath' => from, 'outputPath' => to)
      end

      private

      def base_image
        @base_image ||= ::Docker::Image.get(@config[:docker_image])
      end

      def current_image
        @images.last || base_image
      end

      def docker_run!(cmd, opts={})
        opts = {
          'Image' => current_image.id,
          'Cmd' => %W{/bin/sh -c #{cmd}},
        }.merge(opts)

        if path = @config[:path]
          (opts['Env'] ||= {})['PATH'] = path
        end

        container = ::Docker::Container.create(opts)
        begin
          container.start
          begin
            stdout, stderr = container.attach
            result = container.wait
            return CommandResult.new :stdout => stdout.join, :stderr => stderr.join,
              :exit_status => result['StatusCode']
          rescue
            container.kill
          end
        ensure
          container.delete
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
specinfra-2.0.0.beta24 lib/specinfra/backend/docker.rb