Class: Ridley::HostConnector::SSH
- Defined in:
- lib/ridley-connectors/host_connector/ssh.rb
Constant Summary collapse
- DEFAULT_PORT =
22- EMBEDDED_RUBY_PATH =
'/opt/chef/embedded/bin/ruby'.freeze
Instance Method Summary collapse
-
#bootstrap(host, options = {}) ⇒ HostConnector::Response
Bootstrap a node.
-
#chef_client(host, options = {}) ⇒ HostConnector::Response
Perform a chef client run on a node.
-
#put_secret(host, secret, options = {}) ⇒ HostConnector::Response
Write your encrypted data bag secret on a node.
-
#ruby_script(host, command_lines, options = {}) ⇒ HostConnector::Response
Execute line(s) of Ruby code on a node using Chef’s embedded Ruby.
-
#run(host, command, options = {}) ⇒ HostConnector::Response
Execute a shell command on a node.
-
#uninstall_chef(host, options = {}) ⇒ HostConnector::Response
Uninstall Chef from a node.
-
#update_omnibus(host, options = {}) ⇒ HostConnector::Response
Update a node’s Omnibus installation of Chef.
Instance Method Details
#bootstrap(host, options = {}) ⇒ HostConnector::Response
Bootstrap a node
95 96 97 98 99 100 101 102 103 |
# File 'lib/ridley-connectors/host_connector/ssh.rb', line 95 def bootstrap(host, = {}) = .reverse_merge(ssh: Hash.new) [:ssh].reverse_merge!(sudo: true, timeout: 5.0) context = BootstrapContext::Unix.new() log.info "Bootstrapping host: #{host}" log.filter_param(context.boot_command) run(host, context.boot_command, ) end |
#chef_client(host, options = {}) ⇒ HostConnector::Response
Perform a chef client run on a node
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ridley-connectors/host_connector/ssh.rb', line 118 def chef_client(host, = {}) log_level = case log.level when 0 "debug" when 1 "info" when 2 "warn" else "info" end run(host, "chef-client -l #{log_level}", ) end |
#put_secret(host, secret, options = {}) ⇒ HostConnector::Response
Write your encrypted data bag secret on a node
147 148 149 150 151 |
# File 'lib/ridley-connectors/host_connector/ssh.rb', line 147 def put_secret(host, secret, = {}) log.filter_param(secret) cmd = "echo '#{secret}' > /etc/chef/encrypted_data_bag_secret; chmod 0600 /etc/chef/encrypted_data_bag_secret" run(host, cmd, ) end |
#ruby_script(host, command_lines, options = {}) ⇒ HostConnector::Response
Execute line(s) of Ruby code on a node using Chef’s embedded Ruby
168 169 170 |
# File 'lib/ridley-connectors/host_connector/ssh.rb', line 168 def ruby_script(host, command_lines, = {}) run(host, "#{EMBEDDED_RUBY_PATH} -e \"#{command_lines.join(';')}\"", ) end |
#run(host, command, options = {}) ⇒ HostConnector::Response
Execute a shell command on a node
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ridley-connectors/host_connector/ssh.rb', line 23 def run(host, command, = {}) = .reverse_merge(ssh: Hash.new) [:ssh].reverse_merge!(port: DEFAULT_PORT, paranoid: false, sudo: false) command = "sudo -E #{command}" if [:ssh][:sudo] Ridley::HostConnector::Response.new(host).tap do |response| begin log.info "Running SSH command: '#{command}' on: '#{host}' as: '#{options[:ssh][:user]}'" defer { Net::SSH.start(host, [:ssh][:user], [:ssh].slice(*Net::SSH::VALID_OPTIONS)) do |ssh| ssh.open_channel do |channel| if [:sudo] channel.request_pty do |channel, success| unless success raise "Could not aquire pty: A pty is required for running sudo commands." end channel_exec(channel, command, host, response) end else channel_exec(channel, command, host, response) end end ssh.loop end } rescue Net::SSH::AuthenticationFailed => ex response.exit_code = -1 response.stderr = "Authentication failure for user #{ex}" rescue Net::SSH::ConnectionTimeout, Timeout::Error response.exit_code = -1 response.stderr = "Connection timed out" rescue SocketError, Errno::EHOSTUNREACH response.exit_code = -1 response.stderr = "Host unreachable" rescue Errno::ECONNREFUSED response.exit_code = -1 response.stderr = "Connection refused" rescue Net::SSH::Exception => ex response.exit_code = -1 response.stderr = ex.inspect rescue => ex response.exit_code = -1 response.stderr = "An unknown error occurred: #{ex.class} - #{ex.message}" end case response.exit_code when 0 log.info "Successfully ran SSH command on: '#{host}' as: '#{options[:ssh][:user]}'" when -1 log.info "Failed to run SSH command on: '#{host}' as: '#{options[:ssh][:user]}'" else log.info "Successfully ran SSH command on: '#{host}' as: '#{options[:ssh][:user]}' but it failed" end end end |
#uninstall_chef(host, options = {}) ⇒ HostConnector::Response
Uninstall Chef from a node
189 190 191 192 193 194 195 |
# File 'lib/ridley-connectors/host_connector/ssh.rb', line 189 def uninstall_chef(host, = {}) = .reverse_merge(ssh: Hash.new) [:ssh].reverse_merge!(sudo: true, timeout: 5.0) log.info "Uninstalling Chef from host: #{host}" run(host, CommandContext::UnixUninstall.command(), ) end |
#update_omnibus(host, options = {}) ⇒ HostConnector::Response
Update a node’s Omnibus installation of Chef
220 221 222 223 224 225 226 |
# File 'lib/ridley-connectors/host_connector/ssh.rb', line 220 def update_omnibus(host, = {}) = .reverse_merge(ssh: Hash.new) [:ssh].reverse_merge!(sudo: true, timeout: 5.0) log.info "Updating Omnibus installation on host: #{host}" run(host, CommandContext::UnixUpdateOmnibus.command(), ) end |