class Hocho::Host

Attributes

name[R]
properties[R]
providers[R]
shmdir[R]
sudo_password[W]
tags[RW]
tmpdir[R]
use_alternate_ssh_options[RW]

Public Class Methods

new(name, provider: nil, providers: nil, properties: {}, tags: {}, ssh_options: nil, tmpdir: nil, shmdir: nil, sudo_password: nil) click to toggle source
# File lib/hocho/host.rb, line 9
def initialize(name, provider: nil, providers: nil, properties: {}, tags: {}, ssh_options: nil, tmpdir: nil, shmdir: nil, sudo_password: nil)
  if provider
    warn "DEPRECATION WARNING: #{caller[1]}: Hocho::Host.new(provider:) is deprecated. Use providers: instead "
  end

  @name = name
  @providers = [*provider, *providers]
  self.properties = properties
  @tags = tags
  @override_ssh_options = ssh_options
  @tmpdir = tmpdir
  @shmdir = shmdir
  @sudo_password = sudo_password

  @use_alternate_ssh_options = false
end

Public Instance Methods

alternate_ssh_options() click to toggle source
# File lib/hocho/host.rb, line 92
def alternate_ssh_options
  normal_ssh_options.merge(Hocho::Utils::Symbolize.keys_of(properties.fetch(:alternate_ssh_options, {})))
end
alternate_ssh_options_available?() click to toggle source
# File lib/hocho/host.rb, line 96
def alternate_ssh_options_available?
  !!properties[:alternate_ssh_options]
end
apply_property_providers(providers) click to toggle source
# File lib/hocho/host.rb, line 54
def apply_property_providers(providers)
  providers.each do |provider|
    provider.determine(self)
  end
end
attributes() click to toggle source
# File lib/hocho/host.rb, line 68
def attributes
  properties[:attributes] || {}
end
bundler_cmd() click to toggle source
# File lib/hocho/host.rb, line 202
def bundler_cmd
  properties[:bundler_cmd] || 'bundle'
end
compress?() click to toggle source
# File lib/hocho/host.rb, line 231
def compress?
  properties.fetch(:compress, true)
end
hostname() click to toggle source
# File lib/hocho/host.rb, line 186
def hostname
  ssh_options[:host_name] || name
end
make_ssh_connection() click to toggle source
# File lib/hocho/host.rb, line 210
def make_ssh_connection
  alt = false
  begin
    # A workaround for a bug on net-ssh: https://github.com/net-ssh/net-ssh/issues/764
    # :strict_host_key_checking is translated from ssh config. However, Net::SSH.start does not accept
    # the option as valid one. Remove this part when net-ssh fixes the bug.
      options = ssh_options
    unless Net::SSH::VALID_OPTIONS.include?(:strict_host_key_checking)
      options.delete(:strict_host_key_checking)
    end
    Net::SSH.start(name, nil, options)
  rescue Net::SSH::Exception, Errno::ECONNREFUSED, Net::SSH::Proxy::ConnectError => e
    raise if alt
    raise unless alternate_ssh_options_available?
    puts "[#{name}] Trying alternate_ssh_options due to #{e.inspect}"
    self.use_alternate_ssh_options = true
    alt = true
    retry
  end
end
merge!(other) click to toggle source
# File lib/hocho/host.rb, line 47
def merge!(other)
  @tags.merge!(other.tags) if other.tags
  @tmpdir = other.tmpdir if other.tmpdir
  @shmdir = other.shmdir if other.shmdir
  @properties.merge!(other.properties)
end
nopasswd_sudo?() click to toggle source
# File lib/hocho/host.rb, line 80
def nopasswd_sudo?
  !!properties[:nopasswd_sudo]
end
normal_ssh_options() click to toggle source
# File lib/hocho/host.rb, line 88
def normal_ssh_options
  (Net::SSH::Config.for(ssh_name) || {}).merge(Hocho::Utils::Symbolize.keys_of(properties[:ssh_options] || {})).merge(@override_ssh_options || {})
end
openssh_config(separator='=') click to toggle source
# File lib/hocho/host.rb, line 104
def openssh_config(separator='=')
  ssh_options.flat_map do |key, value|
    case key
    when :encryption
     [["Ciphers", [*value].join(?,)]]
    when :compression
     [["Compression", value ? 'yes' : 'no']]
    when :compression_level
     [["CompressionLevel", value]]
    when :timeout
     [["ConnectTimeout", value]]
    when :forward_agent
     [["ForwardAgent", value ? 'yes' : 'no']]
    when :keys_only
     [["IdentitiesOnly", value ? 'yes' : 'no']]
    when :global_known_hosts_file
     [["GlobalKnownHostsFile", value]]
    when :auth_methods
      [].tap do |lines|
        methods = value.dup
        value.each  do |val|
          case val
          when 'hostbased'
            lines << ["HostBasedAuthentication", "yes"]
          when 'password'
            lines << ["PasswordAuthentication", "yes"]
          when 'publickey'
            lines << ["PubkeyAuthentication", "yes"]
          end
        end
        unless methods.empty?
          lines << ["PreferredAuthentications", methods.join(?,)]
        end
      end
    when :host_key
     [["HostKeyAlgorithms", [*value].join(?,)]]
    when :host_key_alias
     [["HostKeyAlias", value]]
    when :host_name
     [["HostName", value]]
    when :keys
      [*value].map do |val|
       ["IdentityFile", val]
      end
    when :hmac
     [["Macs", [*value].join(?,)]]
    when :port
     [["Port", value]]
    when :proxy
      case value
      when Net::SSH::Proxy::Jump
        [["ProxyJump", value.jump_proxies]]
      when Net::SSH::Proxy::Command
       [["ProxyCommand", value.command_line_template]]
      when false
       [["ProxyCommand", 'none']]
      else
       [["ProxyCommand", value]]
      end
    when :rekey_limit
     [["RekeyLimit", value]]
    when :user
     [["User", value]]
    when :user_known_hosts_file
     [["UserKnownHostsFile", value]]
    when :verify_host_key
      case value
      when :never
        [["StrictHostKeyChecking", "no"]]
      when :accept_new_or_local_tunnel
        [["StrictHostKeyChecking", "accept-new"]]
      when :accept_new
        [["StrictHostKeyChecking", "accept-new"]]
      when :always
        [["StrictHostKeyChecking", "yes"]]
      end
    end
  end.compact.map do |keyval|
    keyval.join(separator)
  end
end
preferred_driver() click to toggle source
# File lib/hocho/host.rb, line 198
def preferred_driver
  properties[:preferred_driver] && properties[:preferred_driver].to_sym
end
properties=(other) click to toggle source
# File lib/hocho/host.rb, line 43
def properties=(other)
  @properties = Hashie::Mash.new(other)
end
run_list() click to toggle source
# File lib/hocho/host.rb, line 64
def run_list
  properties[:run_list] || []
end
ssh_connection() click to toggle source
# File lib/hocho/host.rb, line 206
def ssh_connection
  @ssh ||= make_ssh_connection
end
ssh_name() click to toggle source
# File lib/hocho/host.rb, line 60
def ssh_name
  properties[:ssh_name] || name
end
ssh_options() click to toggle source
# File lib/hocho/host.rb, line 84
def ssh_options
  use_alternate_ssh_options? ? alternate_ssh_options : normal_ssh_options
end
ssh_port() click to toggle source
# File lib/hocho/host.rb, line 194
def ssh_port
  ssh_options[:port]
end
sudo_password() click to toggle source
# File lib/hocho/host.rb, line 72
def sudo_password
  @sudo_password || properties[:sudo_password] || ENV['SUDO_PASSWORD']
end
sudo_required?() click to toggle source
# File lib/hocho/host.rb, line 76
def sudo_required?
  properties.fetch(:sudo_required, true)
end
to_h() click to toggle source
# File lib/hocho/host.rb, line 30
def to_h
  {
    name: name,
    providers: providers,
    tags: tags.to_h,
    properties: properties.to_h,
  }.tap do |h|
    h[:tmpdir] = tmpdir if tmpdir
    h[:shmdir] = shmdir if shmdir
    h[:ssh_options] = @override_ssh_options if @override_ssh_options
  end
end
use_alternate_ssh_options?() click to toggle source
# File lib/hocho/host.rb, line 100
def use_alternate_ssh_options?
  @use_alternate_ssh_options
end
user() click to toggle source
# File lib/hocho/host.rb, line 190
def user
  ssh_options[:user]
end