Sha256: 8185b0001ad96312e89b8c635ce330daace090f34387d91bc3cbb68ba97fec93

Contents?: true

Size: 1.99 KB

Versions: 42

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module EacDocker
  class Container
    enable_immutable
    immutable_accessor :interactive, :temporary, :tty, type: :boolean
    immutable_accessor :env, type: :hash
    immutable_accessor :capability, :command_arg, :volume, type: :array
    attr_reader :id
    common_constructor :image

    def immutable_constructor_args
      [image]
    end

    alias immutable_volume volume

    def hostname
      ::EacDocker::Executables.docker.command(
        'inspect', '--format={{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}',
        id
      ).execute!.strip
    end

    # @return [String]
    def logs
      ::EacDocker::Executables.docker.command('logs', id).execute!
    end

    def on_detached
      command = ::EacDocker::Executables.docker.command(*(%w[run --detach] + run_command_args))
      self.id = command.execute!.strip
      begin
        yield(self)
      ensure
        stop
      end
    end

    def volume(left_part, right_part = nil)
      immutable_volume(right_part.if_present(left_part) { |v| "#{left_part}:#{v}" })
    end

    def run_command
      ::EacDocker::Executables.docker.command('run', *run_command_args)
    end

    def run_command_args
      %w[boolean capabilities envs volumes]
        .inject([]) { |a, e| a + send("run_command_#{e}_args") } +
        [image.provide.id] + command_args
    end

    def stop
      ::EacDocker::Executables.docker.command('stop', id).execute!
    end

    private

    attr_writer :id

    def run_command_boolean_args
      r = []
      r << '--interactive' if interactive?
      r << '--tty' if tty?
      r << '--rm' if temporary?
      r
    end

    def run_command_capabilities_args
      capabilities.flat_map { |capability| ['--cap-add', capability] }
    end

    def run_command_volumes_args
      volumes.flat_map { |volume| ['--volume', volume] }
    end

    def run_command_envs_args
      envs.flat_map { |name, value| ['--env', "#{name}=#{value}"] }
    end
  end
end

Version data entries

42 entries across 42 versions & 2 rubygems

Version Path
eac_tools-0.76.1 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.76.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.75.2 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.75.1 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.75.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.74.1 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.74.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.73.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.72.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.70.1 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.70.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.69.1 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.69.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.68.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.67.1 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.67.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.66.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.65.1 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.65.0 sub/eac_docker/lib/eac_docker/container.rb
eac_tools-0.64.0 sub/eac_docker/lib/eac_docker/container.rb