Sha256: c65e8c03d3aa33dc05d7a759b6182b0d5f248792d847a5474ed7b1653f7e9262

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

module Specinfra
  module Backend
    class Docker < Exec
      def initialize
        @images = []
      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)
        begin
          @images << current_image.insert_local('localPath' => from, 'outputPath' => to)
        rescue ::Docker::Error::DockerError
          return false
        end
        true
      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

3 entries across 3 versions & 1 rubygems

Version Path
specinfra-2.0.0.beta4 lib/specinfra/backend/docker.rb
specinfra-2.0.0.beta3 lib/specinfra/backend/docker.rb
specinfra-2.0.0.beta2 lib/specinfra/backend/docker.rb