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
@return [Hash]
Public Class Methods
@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
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
@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
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
Denote SSH
is enabled.
@return [Boolean]
# File lib/kamaze/docker_image/ssh.rb, line 120 def enabled? config.fetch(:ssh)[:enabled] end
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
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
@return [Boolean]
# File lib/kamaze/docker_image/ssh.rb, line 140 def network? !network.empty? end
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 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