Sha256: 6ab1850273726909ace986e06aff5fce4331b71d6ef372402de7593f1014c284

Contents?: true

Size: 1.44 KB

Versions: 5

Compression:

Stored size: 1.44 KB

Contents

module Specinfra
  module Backend
    class Docker < Exec
      def initialize
        @images = []
        ::Docker.url = Specinfra.configuration.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(Specinfra.configuration.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 = Specinfra::configuration::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

5 entries across 5 versions & 1 rubygems

Version Path
specinfra-2.0.0.beta28 lib/specinfra/backend/docker.rb
specinfra-2.0.0.beta27 lib/specinfra/backend/docker.rb
specinfra-2.0.0.beta26 lib/specinfra/backend/docker.rb
specinfra-2.0.0.beta25 lib/specinfra/backend/docker.rb
specinfra-2.0.0.beta23 lib/specinfra/backend/docker.rb