Module: Crew::Context::SSH
Instance Method Summary collapse
- #cd(target_dir) ⇒ Object
- #current_dir ⇒ Object
- #reconnect! ⇒ Object
- #save_data(data, target_path) ⇒ Object
- #save_file(local_path, target_path) ⇒ Object
- #sh_raw(cmd, stdin = nil) ⇒ Object
- #sh_with_code(cmd, sh_opts) ⇒ Object
Instance Method Details
#cd(target_dir) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/crew/context/ssh.rb', line 12 def cd(target_dir) with_callbacks do previous_current_dir = current_dir @current_dir = File.(target_dir, previous_current_dir) @home.logger.cd @current_dir if block_given? begin yield ensure @home.logger.cd previous_current_dir cd previous_current_dir end end end end |
#current_dir ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/crew/context/ssh.rb', line 4 def current_dir @current_dir ||= begin result = sh_raw("pwd") raise unless result[2] == 0 result[0].chomp end end |
#reconnect! ⇒ Object
97 98 99 |
# File 'lib/crew/context/ssh.rb', line 97 def reconnect! @_shell = nil end |
#save_data(data, target_path) ⇒ Object
86 87 88 |
# File 'lib/crew/context/ssh.rb', line 86 def save_data(data, target_path) sh "echo -n #{escape(data)} > #{escape(target_path)}", stdin_data: data end |
#save_file(local_path, target_path) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/crew/context/ssh.rb', line 90 def save_file(local_path, target_path) with_callbacks do raise "#{local_path} does not exist" unless File.exist?(local_path) _shell.scp.upload! local_path, target_path end end |
#sh_raw(cmd, stdin = nil) ⇒ Object
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 81 82 83 84 |
# File 'lib/crew/context/ssh.rb', line 51 def sh_raw(cmd, stdin = nil) with_callbacks do stdout_data = "" stderr_data = "" exit_code = nil exit_signal = nil _shell.open_channel do |channel| channel.exec(cmd) do |ch, success| channel.on_data do |ch,data| stdout_data << data end channel.on_extended_data do |ch,type,data| stderr_data << data end channel.on_request("exit-status") do |ch,data| exit_code = data.read_long end channel.on_request("exit-signal") do |ch, data| exit_signal = data.read_long end if stdin channel.send_data stdin channel.eof! end end end _shell.loop [stdout_data, stderr_data, exit_code, exit_signal] end end |
#sh_with_code(cmd, sh_opts) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/crew/context/ssh.rb', line 28 def sh_with_code(cmd, sh_opts) original_cmd = cmd runner = "bash -l -c %s" stdin = nil cmd = "cd #{escape(current_dir)} && #{cmd}" if sh_opts[:sudo] enter_sudo_password if @sudo_password && !opts[:dont_use_sudo_password] raw_cmd = cmd @home.logger.sh "SUDO (using password) #{original_cmd}" cmd = "sudo -S #{runner % escape(cmd)}" stdin = "#{@sudo_password}\n" else @home.logger.sh "SUDO (using not password) #{original_cmd}" cmd = "sudo #{runner % escape(cmd)}" end else @home.logger.sh original_cmd cmd = runner % escape(cmd) end sh_raw cmd, stdin end |