Class: Kamaze::DockerImage::SSH

Inherits:
Hash
  • Object
show all
Includes:
Concern::Containers
Defined in:
lib/kamaze/docker_image/ssh.rb

Overview

Runner provide methods to connect into image using ssh.

Sample of use:

require 'kamaze/docker_image'

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

Constant Summary collapse

Command =

noinspection RubyConstantNamingConvention

Kamaze::DockerImage::Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ SSH

Returns a new instance of SSH.



37
38
39
40
41
# 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

Instance Attribute Details

#configHash (readonly)

Returns:

  • (Hash)


32
33
34
# File 'lib/kamaze/docker_image/ssh.rb', line 32

def config
  @config
end

Instance Method Details

#call(cmd = nil, &block) ⇒ Object

Connect to ssh (executing optional command cmd).

Parameters:

  • cmd (Array<String|Object>) (defaults to: nil)

Raises:

  • (Errno::ENONET)

See Also:



49
50
51
52
53
54
55
# 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) ⇒ Command

Returns:



82
83
84
85
86
87
88
89
# 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

#containers(all: true) ⇒ Hash{String => Hash} (protected) Originally defined in module Concern::Containers

List containers.

Containers are indexed by name.

Returns:

  • (Hash{String => Hash})

#containers_makerProc (protected) Originally defined in module Concern::Containers

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add some methods on retrieved info.

Returns:

  • (Proc)

#defaultsHash{Symbol => Object}

Get defaults for config.

Returns:

  • (Hash{Symbol => Object})


77
78
79
# File 'lib/kamaze/docker_image/ssh.rb', line 77

def defaults
  YAML.safe_load(Pathname.new(__dir__).join('ssh.yml').read, [Symbol])
end

#enabled?Boolean

Denote SSH is enabled.

Returns:

  • (Boolean)


120
121
122
# File 'lib/kamaze/docker_image/ssh.rb', line 120

def enabled?
  config.fetch(:ssh)[:enabled]
end

#executableString|Object

Get absolute path for executable.

Returns:

  • (String|Object)


109
110
111
112
113
114
115
# 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

#networkArray<String>

Get ip addresses.

Returns:

  • (Array<String>)


127
128
129
130
131
132
133
134
135
136
137
# 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?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/kamaze/docker_image/ssh.rb', line 140

def network?
  !network.empty?
end

#paramsHash{Symbol => Object}

Params used to shape command.

Returns:

  • (Hash{Symbol => Object})


94
95
96
97
98
99
100
101
102
103
104
# 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

#waitself

Wait until ssh is available.

Returns:

  • (self)


60
61
62
63
64
65
66
67
68
69
70
71
72
# 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