Class: Cyclid::API::Plugins::Ssh
- Defined in:
- app/cyclid/plugins/transport/ssh.rb
Overview
SSH based transport
Instance Attribute Summary collapse
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
-
#exit_signal ⇒ Object
readonly
Returns the value of attribute exit_signal.
Instance Method Summary collapse
-
#close ⇒ Object
Close the SSH connection.
-
#download(io, path) ⇒ Object
Copy a data from remote file to a local IO object.
-
#exec(cmd, path = nil) ⇒ Object
Execute a command via.
-
#initialize(args = {}) ⇒ Ssh
constructor
A new instance of Ssh.
-
#upload(io, path) ⇒ Object
Copy data from a local IO object to a remote file via.
Methods inherited from Transport
Methods inherited from Base
config?, config_schema, default_config, get_config, human_name, register_plugin, set_config, update_config
Constructor Details
#initialize(args = {}) ⇒ Ssh
Returns a new instance of Ssh.
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 |
# File 'app/cyclid/plugins/transport/ssh.rb', line 30 def initialize(args = {}) args.symbolize_keys! # Hostname, username & a log target are required return false unless args.include? :host and \ args.include? :user and \ args.include? :log password = args[:password] if args.include? :password keys = [args[:key]] if args.include? :key @log = args[:log] # Create an SSH channel Cyclid.logger.debug 'waiting for SSH...' start = Time.now loop do begin @session = Net::SSH.start(args[:host], args[:user], password: password, keys: keys, timeout: 5) break unless @session.nil? rescue Net::SSH::Exception Cyclid.logger.debug 'SSH authentication failed' rescue StandardError => ex Cyclid.logger.debug "SSH connection failed: #{ex}" end sleep 5 raise 'timed out waiting for SSH' \ if (Time.now - start) >= 30 end end |
Instance Attribute Details
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
28 29 30 |
# File 'app/cyclid/plugins/transport/ssh.rb', line 28 def exit_code @exit_code end |
#exit_signal ⇒ Object (readonly)
Returns the value of attribute exit_signal.
28 29 30 |
# File 'app/cyclid/plugins/transport/ssh.rb', line 28 def exit_signal @exit_signal end |
Instance Method Details
#close ⇒ Object
Close the SSH connection
124 125 126 |
# File 'app/cyclid/plugins/transport/ssh.rb', line 124 def close @session.close end |
#download(io, path) ⇒ Object
Copy a data from remote file to a local IO object
118 119 120 121 |
# File 'app/cyclid/plugins/transport/ssh.rb', line 118 def download(io, path) channel = @session.scp.download path, io channel.wait end |
#exec(cmd, path = nil) ⇒ Object
Execute a command via. SSH
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/cyclid/plugins/transport/ssh.rb', line 69 def exec(cmd, path = nil) command = build_command(cmd, path, @env) Cyclid.logger.debug "command=#{command}" @session.open_channel do |channel| channel.on_open_failed do |_ch, _code, desc| # XXX raise abort "failed to open channel: #{desc}" end # STDOUT channel.on_data do |_ch, data| # Send to Log Buffer @log.write data end # STDERR channel.on_extended_data do |_ch, _type, data| # Send to Log Buffer @log.write data end # Capture return value from commands channel.on_request 'exit-status' do |_ch, data| @exit_code = data.read_long end # Capture a command exiting with a signal channel.on_request 'exit-signal' do |_ch, data| @exit_signal = data.read_long end channel.exec command do |_ch, _success| end end # Run the SSH even loop; this blocks until the command has completed @session.loop @exit_code.zero? && @exit_signal.nil? ? true : false end |
#upload(io, path) ⇒ Object
Copy data from a local IO object to a remote file via. SCP
112 113 114 115 |
# File 'app/cyclid/plugins/transport/ssh.rb', line 112 def upload(io, path) channel = @session.scp.upload io, path channel.wait end |