Class: Jisota::SSHSession
- Inherits:
-
Object
- Object
- Jisota::SSHSession
- Defined in:
- lib/jisota/ssh_session.rb
Defined Under Namespace
Classes: FileNotFoundError
Instance Method Summary collapse
- #command(command, logger = nil) ⇒ Object
-
#initialize(session = nil, options = {}) ⇒ SSHSession
constructor
A new instance of SSHSession.
- #upload(file, logger = nil) ⇒ Object
Constructor Details
#initialize(session = nil, options = {}) ⇒ SSHSession
Returns a new instance of SSHSession.
8 9 10 11 |
# File 'lib/jisota/ssh_session.rb', line 8 def initialize(session = nil, = {}) @session = session @scp_engine = .fetch(:scp_engine) { Net::SCP } end |
Instance Method Details
#command(command, logger = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jisota/ssh_session.rb', line 13 def command(command, logger = nil) exit_code = nil exit_signal = nil = "" logger.command(command) if logger @session.open_channel do |channel| channel.exec(command) do channel.on_data { |_, data| logger.info(data) if logger } channel.on_extended_data { |_, _, data| logger.warn(data) if logger; << data } channel.on_request("exit-status") { |_, data| exit_code = data.read_long } channel.on_request("exit-signal") { |_, data| exit_signal = data.read_long } end end @session.loop if exit_code == 0 true else logger.error("Error running #{command}:") logger.error() false end end |
#upload(file, logger = nil) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/jisota/ssh_session.rb', line 39 def upload(file, logger = nil) raise FileNotFoundError, "Upload file not found: #{file.from}" unless File.exist?(file.from) tmp_file = "tmp/jisota/#{SecureRandom.hex}" command("mkdir -p tmp/jisota", logger) logger.upload(from: file.from, to: tmp_file) if logger @scp_engine.new(@session).upload!(file.from, tmp_file, recursive: false) command("sudo mkdir -p `dirname #{file.to}` && sudo mv #{tmp_file} #{file.to}", logger) end |