Class: Hocho::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/hocho/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, provider: nil, providers: nil, properties: {}, tags: {}, ssh_options: nil, tmpdir: nil, shmdir: nil, sudo_password: nil) ⇒ Host

Returns a new instance of Host.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/hocho/host.rb', line 26

def name
  @name
end

#propertiesObject

Returns the value of attribute properties.



26
27
28
# File 'lib/hocho/host.rb', line 26

def properties
  @properties
end

#providersObject (readonly)

Returns the value of attribute providers.



26
27
28
# File 'lib/hocho/host.rb', line 26

def providers
  @providers
end

#shmdirObject (readonly)

Returns the value of attribute shmdir.



26
27
28
# File 'lib/hocho/host.rb', line 26

def shmdir
  @shmdir
end

#sudo_passwordObject



72
73
74
# File 'lib/hocho/host.rb', line 72

def sudo_password
  @sudo_password || properties[:sudo_password] || ENV['SUDO_PASSWORD']
end

#tagsObject

Returns the value of attribute tags.



28
29
30
# File 'lib/hocho/host.rb', line 28

def tags
  @tags
end

#tmpdirObject (readonly)

Returns the value of attribute tmpdir.



26
27
28
# File 'lib/hocho/host.rb', line 26

def tmpdir
  @tmpdir
end

#use_alternate_ssh_optionsObject

Returns the value of attribute use_alternate_ssh_options.



28
29
30
# File 'lib/hocho/host.rb', line 28

def use_alternate_ssh_options
  @use_alternate_ssh_options
end

Instance Method Details

#alternate_ssh_optionsObject



92
93
94
# 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?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/hocho/host.rb', line 96

def alternate_ssh_options_available?
  !!properties[:alternate_ssh_options]
end

#apply_property_providers(providers) ⇒ Object



54
55
56
57
58
# File 'lib/hocho/host.rb', line 54

def apply_property_providers(providers)
  providers.each do |provider|
    provider.determine(self)
  end
end

#attributesObject



68
69
70
# File 'lib/hocho/host.rb', line 68

def attributes
  properties[:attributes] || {}
end

#bundler_cmdObject



202
203
204
# File 'lib/hocho/host.rb', line 202

def bundler_cmd
  properties[:bundler_cmd] || 'bundle'
end

#compress?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/hocho/host.rb', line 231

def compress?
  properties.fetch(:compress, true)
end

#hostnameObject



186
187
188
# File 'lib/hocho/host.rb', line 186

def hostname
  ssh_options[:host_name] || name
end

#make_ssh_connectionObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# 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) ⇒ Object



47
48
49
50
51
52
# 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?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/hocho/host.rb', line 80

def nopasswd_sudo?
  !!properties[:nopasswd_sudo]
end

#normal_ssh_optionsObject



88
89
90
# 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 = '=') ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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_driverObject



198
199
200
# File 'lib/hocho/host.rb', line 198

def preferred_driver
  properties[:preferred_driver] && properties[:preferred_driver].to_sym
end

#run_listObject



64
65
66
# File 'lib/hocho/host.rb', line 64

def run_list
  properties[:run_list] || []
end

#ssh_connectionObject



206
207
208
# File 'lib/hocho/host.rb', line 206

def ssh_connection
  @ssh ||= make_ssh_connection
end

#ssh_nameObject



60
61
62
# File 'lib/hocho/host.rb', line 60

def ssh_name
  properties[:ssh_name] || name
end

#ssh_optionsObject



84
85
86
# File 'lib/hocho/host.rb', line 84

def ssh_options
  use_alternate_ssh_options? ? alternate_ssh_options : normal_ssh_options
end

#ssh_portObject



194
195
196
# File 'lib/hocho/host.rb', line 194

def ssh_port
  ssh_options[:port]
end

#sudo_required?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/hocho/host.rb', line 76

def sudo_required?
  properties.fetch(:sudo_required, true)
end

#to_hObject



30
31
32
33
34
35
36
37
38
39
40
41
# 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?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/hocho/host.rb', line 100

def use_alternate_ssh_options?
  @use_alternate_ssh_options
end

#userObject



190
191
192
# File 'lib/hocho/host.rb', line 190

def user
  ssh_options[:user]
end