Class: Kamaze::DockerImage::SSH
- Inherits:
-
Hash
- Object
- Hash
- Kamaze::DockerImage::SSH
- 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
- #config ⇒ Hash readonly
Instance Method Summary collapse
-
#call(cmd = nil, &block) ⇒ Object
Connect to ssh (executing optional command
cmd). - #command(cmd = nil) ⇒ Command
-
#containers(all: true) ⇒ Hash{String => Hash}
included
from Concern::Containers
protected
List containers.
-
#containers_maker ⇒ Proc
included
from Concern::Containers
protected
private
Add some methods on retrieved info.
-
#defaults ⇒ Hash{Symbol => Object}
Get defaults for config.
-
#enabled? ⇒ Boolean
Denote SSH is enabled.
-
#executable ⇒ String|Object
Get absolute path for executable.
-
#initialize(image) ⇒ SSH
constructor
A new instance of SSH.
-
#network ⇒ Array<String>
Get ip addresses.
- #network? ⇒ Boolean
-
#params ⇒ Hash{Symbol => Object}
Params used to shape command.
-
#wait ⇒ self
Wait until ssh is available.
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
#config ⇒ Hash (readonly)
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).
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
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.
#containers_maker ⇒ Proc (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.
#defaults ⇒ Hash{Symbol => Object}
Get defaults for config.
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.
120 121 122 |
# File 'lib/kamaze/docker_image/ssh.rb', line 120 def enabled? config.fetch(:ssh)[:enabled] end |
#executable ⇒ String|Object
Get absolute path for executable.
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 |
#network ⇒ Array<String>
Get ip addresses.
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
140 141 142 |
# File 'lib/kamaze/docker_image/ssh.rb', line 140 def network? !network.empty? end |
#params ⇒ Hash{Symbol => Object}
Params used to shape command.
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 |
#wait ⇒ self
Wait until ssh is available.
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 |