class Kamaze::DockerImage::SSH

Runner provide methods to connect into image using ssh.

Sample of use:

“`ruby require 'kamaze/docker_image'

ssh = Kamaze::DockerImage::SSH.new(run_as: 'kamaze_sample_image') “`

Constants

Command

Describe a command

Command is able to run itself.

Attributes

config[R]

@return [Hash]

Public Class Methods

new(image) click to toggle source

@param [Kamaze::DockerImage] image

@see Kamaze::DockerImage::Concern::Setup#default_commands

# File lib/kamaze/docker_image/ssh.rb, line 37
def initialize(image)
  defaults.merge(image.to_h[:ssh].to_h).tap do |ssh|
    @config = image.to_h.merge(ssh: ssh).freeze
  end.each { |k, v| self[k] = v }
end

Public Instance Methods

call(cmd = nil, &block) click to toggle source

Connect to ssh (executing optional command “cmd“).

@param [Array<String|Object>] cmd @raise [Errno::ENONET]

@see command

# File lib/kamaze/docker_image/ssh.rb, line 49
def call(cmd = nil, &block)
  network? ? wait : (raise Errno::ENONET)
rescue Timeout::Error
  nil
ensure
  command(cmd).run(&block)
end
command(cmd = nil) click to toggle source

@return [Command]

# File lib/kamaze/docker_image/ssh.rb, line 82
def command(cmd = nil)
  cmd = Shellwords.split(cmd) if cmd.is_a?(String)

  config.fetch(:ssh).fetch(:command)
        .map { |w| w % params }
        .push(*cmd.to_a)
        .tap { |command| return Command.new(command, config) }
end
defaults() click to toggle source

Get defaults for config.

@return [Hash{Symbol => Object}]

# File lib/kamaze/docker_image/ssh.rb, line 77
def defaults
  YAML.safe_load(Pathname.new(__dir__).join('ssh.yml').read, [Symbol])
end
enabled?() click to toggle source

Denote SSH is enabled.

@return [Boolean]

# File lib/kamaze/docker_image/ssh.rb, line 120
def enabled?
  config.fetch(:ssh)[:enabled]
end
executable() click to toggle source

Get absolute path for executable.

@return [String|Object]

# File lib/kamaze/docker_image/ssh.rb, line 109
def executable
  config.fetch(:ssh).fetch(:executable).tap do |executable|
    Cliver.detect(executable).tap do |s|
      return (s || executable).freeze
    end
  end
end
network() click to toggle source

Get ip addresses.

@return [Array<String>]

# File lib/kamaze/docker_image/ssh.rb, line 127
def network
  # container = fetch_containers(config.fetch(:run_as), [:running])[0]
  containers[config.fetch(:run_as)].yield_self do |container|
    return [] if container.nil?
    return [] unless container.running?

    container.info
             .fetch('NetworkSettings').fetch('Networks')
             .values.keep_if { |v| v.to_h['IPAddress'] }.map { |v| v['IPAddress'] }.compact
  end
end
network?() click to toggle source

@return [Boolean]

# File lib/kamaze/docker_image/ssh.rb, line 140
def network?
  !network.empty?
end
params() click to toggle source

Params used to shape command.

@return [Hash{Symbol => Object}]

# File lib/kamaze/docker_image/ssh.rb, line 94
def params
  # rubocop:disable Style/TernaryParentheses
  {
    executable: executable,
    port: config.fetch(:ssh).fetch(:port),
    user: config.fetch(:ssh).fetch(:user),
    host: network.fetch(0),
    opt_pty: ($stdout.tty? and $stderr.tty?) ? '-t' : '-T',
  }
  # rubocop:enable Style/TernaryParentheses
end
wait() { |self| ... } click to toggle source

Wait until ssh is available.

@return [self]

# File lib/kamaze/docker_image/ssh.rb, line 60
def wait
  Timeout.timeout(config.fetch(:ssh).fetch(:timeout)) do
    loop do
      command(config.fetch(:ssh).fetch(:test)).tap do |command|
        if command.execute
          return block_given? ? yield(self) : self
        end

        sleep(0.5)
      end
    end
  end
end