Class: Hocho::Host
- Inherits:
-
Object
- Object
- Hocho::Host
- Defined in:
- lib/hocho/host.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#properties ⇒ Object
Returns the value of attribute properties.
-
#providers ⇒ Object
readonly
Returns the value of attribute providers.
-
#shmdir ⇒ Object
readonly
Returns the value of attribute shmdir.
- #sudo_password ⇒ Object
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#tmpdir ⇒ Object
readonly
Returns the value of attribute tmpdir.
-
#use_alternate_ssh_options ⇒ Object
Returns the value of attribute use_alternate_ssh_options.
Instance Method Summary collapse
- #alternate_ssh_options ⇒ Object
- #alternate_ssh_options_available? ⇒ Boolean
- #apply_property_providers(providers) ⇒ Object
- #attributes ⇒ Object
- #bundler_cmd ⇒ Object
- #compress? ⇒ Boolean
- #hostname ⇒ Object
-
#initialize(name, provider: nil, providers: nil, properties: {}, tags: {}, ssh_options: nil, tmpdir: nil, shmdir: nil, sudo_password: nil) ⇒ Host
constructor
A new instance of Host.
- #make_ssh_connection ⇒ Object
- #merge!(other) ⇒ Object
- #nopasswd_sudo? ⇒ Boolean
- #normal_ssh_options ⇒ Object
- #openssh_config(separator = '=') ⇒ Object
- #preferred_driver ⇒ Object
- #run_list ⇒ Object
- #ssh_connection ⇒ Object
- #ssh_name ⇒ Object
- #ssh_options ⇒ Object
- #ssh_port ⇒ Object
- #sudo_required? ⇒ Boolean
- #to_h ⇒ Object
- #use_alternate_ssh_options? ⇒ Boolean
- #user ⇒ Object
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 = @override_ssh_options = @tmpdir = tmpdir @shmdir = shmdir @sudo_password = sudo_password @use_alternate_ssh_options = false end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/hocho/host.rb', line 26 def name @name end |
#properties ⇒ Object
Returns the value of attribute properties.
26 27 28 |
# File 'lib/hocho/host.rb', line 26 def properties @properties end |
#providers ⇒ Object (readonly)
Returns the value of attribute providers.
26 27 28 |
# File 'lib/hocho/host.rb', line 26 def providers @providers end |
#shmdir ⇒ Object (readonly)
Returns the value of attribute shmdir.
26 27 28 |
# File 'lib/hocho/host.rb', line 26 def shmdir @shmdir end |
#sudo_password ⇒ Object
72 73 74 |
# File 'lib/hocho/host.rb', line 72 def sudo_password @sudo_password || properties[:sudo_password] || ENV['SUDO_PASSWORD'] end |
#tags ⇒ Object
Returns the value of attribute tags.
28 29 30 |
# File 'lib/hocho/host.rb', line 28 def @tags end |
#tmpdir ⇒ Object (readonly)
Returns the value of attribute tmpdir.
26 27 28 |
# File 'lib/hocho/host.rb', line 26 def tmpdir @tmpdir end |
#use_alternate_ssh_options ⇒ Object
Returns the value of attribute use_alternate_ssh_options.
28 29 30 |
# File 'lib/hocho/host.rb', line 28 def @use_alternate_ssh_options end |
Instance Method Details
#alternate_ssh_options ⇒ Object
92 93 94 |
# File 'lib/hocho/host.rb', line 92 def .merge(Hocho::Utils::Symbolize.keys_of(properties.fetch(:alternate_ssh_options, {}))) end |
#alternate_ssh_options_available? ⇒ Boolean
96 97 98 |
# File 'lib/hocho/host.rb', line 96 def !!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 |
#attributes ⇒ Object
68 69 70 |
# File 'lib/hocho/host.rb', line 68 def attributes properties[:attributes] || {} end |
#bundler_cmd ⇒ Object
202 203 204 |
# File 'lib/hocho/host.rb', line 202 def bundler_cmd properties[:bundler_cmd] || 'bundle' end |
#compress? ⇒ Boolean
231 232 233 |
# File 'lib/hocho/host.rb', line 231 def compress? properties.fetch(:compress, true) end |
#hostname ⇒ Object
186 187 188 |
# File 'lib/hocho/host.rb', line 186 def hostname [:host_name] || name end |
#make_ssh_connection ⇒ Object
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. = unless Net::SSH::VALID_OPTIONS.include?(:strict_host_key_checking) .delete(:strict_host_key_checking) end Net::SSH.start(name, nil, ) rescue Net::SSH::Exception, Errno::ECONNREFUSED, Net::SSH::Proxy::ConnectError => e raise if alt raise unless puts "[#{name}] Trying alternate_ssh_options due to #{e.inspect}" self. = 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.) if other. @tmpdir = other.tmpdir if other.tmpdir @shmdir = other.shmdir if other.shmdir @properties.merge!(other.properties) end |
#nopasswd_sudo? ⇒ Boolean
80 81 82 |
# File 'lib/hocho/host.rb', line 80 def nopasswd_sudo? !!properties[:nopasswd_sudo] end |
#normal_ssh_options ⇒ Object
88 89 90 |
# File 'lib/hocho/host.rb', line 88 def (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='=') .flat_map do |key, value| case key when :encryption [["Ciphers", [*value].join(?,)]] when :compression [["Compression", value]] 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 ⇒ Object
198 199 200 |
# File 'lib/hocho/host.rb', line 198 def preferred_driver properties[:preferred_driver] && properties[:preferred_driver].to_sym end |
#run_list ⇒ Object
64 65 66 |
# File 'lib/hocho/host.rb', line 64 def run_list properties[:run_list] || [] end |
#ssh_connection ⇒ Object
206 207 208 |
# File 'lib/hocho/host.rb', line 206 def ssh_connection @ssh ||= make_ssh_connection end |
#ssh_name ⇒ Object
60 61 62 |
# File 'lib/hocho/host.rb', line 60 def ssh_name properties[:ssh_name] || name end |
#ssh_options ⇒ Object
84 85 86 |
# File 'lib/hocho/host.rb', line 84 def ? : end |
#ssh_port ⇒ Object
194 195 196 |
# File 'lib/hocho/host.rb', line 194 def ssh_port [:port] end |
#sudo_required? ⇒ Boolean
76 77 78 |
# File 'lib/hocho/host.rb', line 76 def sudo_required? properties.fetch(:sudo_required, true) end |
#to_h ⇒ Object
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: .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
100 101 102 |
# File 'lib/hocho/host.rb', line 100 def @use_alternate_ssh_options end |
#user ⇒ Object
190 191 192 |
# File 'lib/hocho/host.rb', line 190 def user [:user] end |