Class: Backup::Remote::Command
- Inherits:
-
Object
- Object
- Backup::Remote::Command
- Includes:
- SSHKit::DSL
- Defined in:
- lib/backup/remote/command.rb
Class Method Summary collapse
- .build_sshkit_host(hostname, ssh_options) ⇒ Object
- .interaction_handler_pwd(user, pwd, host = '') ⇒ Object
Instance Method Summary collapse
- #run_ssh_cmd(hostname, ssh_options, cmd) ⇒ Object
- #run_ssh_cmd_sudo(hostname, ssh_user, ssh_pass, cmd, handler = nil) ⇒ Object
- #ssh_download_file(hostname, ssh_options, remote_filename, dest_filename) ⇒ Object
- #ssh_download_file_scp(hostname, ssh_options, remote_filename, dest_filename) ⇒ Object
-
#ssh_download_file_sshkit(hostname, ssh_options, remote_filename, dest_filename) ⇒ Object
!! NOT work on big files > 4Gb.
- #ssh_upload_file(hostname, ssh_options, source_file, dest_file, handler = nil) ⇒ Object
Class Method Details
.build_sshkit_host(hostname, ssh_options) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/backup/remote/command.rb', line 14 def self.build_sshkit_host(hostname, ) ssh_user = [:user] ssh_pass = [:password] ssh_key = [:key] host = SSHKit::Host.new({hostname: hostname, user: ssh_user}) host.port = [:port] || 22 host.key = ssh_key if ssh_key host.password = ssh_pass if ssh_pass host end |
.interaction_handler_pwd(user, pwd, host = '') ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/backup/remote/command.rb', line 83 def self.interaction_handler_pwd(user, pwd, host='') { "#{user}@#{host}'s password:" => "#{pwd}\n", /#{user}@#{host}'s password: */ => "#{pwd}\n", "password: " => "#{pwd}\n", "password:" => "#{pwd}\n", "Password: " => "#{pwd}\n", } end |
Instance Method Details
#run_ssh_cmd(hostname, ssh_options, cmd) ⇒ Object
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 |
# File 'lib/backup/remote/command.rb', line 27 def run_ssh_cmd(hostname, , cmd) #puts "run ssh cmd: #{hostname}, #{ssh_user}, #{ssh_pass}, #{cmd}" host = Command.build_sshkit_host(hostname, ) #srv = ssh_user+'@'+hostname all_servers = [host] output = '' SSHKit::Coordinator.new(host).each in: :sequence do output = capture cmd end =begin on all_servers do |srv| as(user: ssh_user) do #execute(cmd) output = capture(cmd) end end =end puts "output: #{output}" # return { res: 1, output: output } rescue => e puts "ssh error: #{e.}, #{e.backtrace}" { res: 0, output: output, error: e. } end |
#run_ssh_cmd_sudo(hostname, ssh_user, ssh_pass, cmd, handler = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/backup/remote/command.rb', line 65 def run_ssh_cmd_sudo(hostname, ssh_user, ssh_pass, cmd, handler=nil) host = SSHKit::Host.new("#{ssh_user}@#{hostname}") host.password = ssh_pass on host do |host| execute("#{cmd}", interaction_handler: handler) end # return {res: 1, output: ""} rescue => e { res: 0, error: e. } end |
#ssh_download_file(hostname, ssh_options, remote_filename, dest_filename) ⇒ Object
95 96 97 98 |
# File 'lib/backup/remote/command.rb', line 95 def ssh_download_file(hostname, , remote_filename, dest_filename) return ssh_download_file_sshkit(hostname, , remote_filename, dest_filename) #return ssh_download_file_scp(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) end |
#ssh_download_file_scp(hostname, ssh_options, remote_filename, dest_filename) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/backup/remote/command.rb', line 100 def ssh_download_file_scp(hostname, , remote_filename, dest_filename) ssh_user = [:ssh_user] ssh_pass = [:ssh_password] ssh_key = [:ssh_key] # work Net::SCP.download!(hostname, ssh_user, remote_filename, dest_filename, :ssh => { :password => ssh_pass }) # return {res: 1, output: ""} rescue => e { res: 0, error: e. } end |
#ssh_download_file_sshkit(hostname, ssh_options, remote_filename, dest_filename) ⇒ Object
!! NOT work on big files > 4Gb
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/backup/remote/command.rb', line 119 def ssh_download_file_sshkit(hostname, , remote_filename, dest_filename) host = Command.build_sshkit_host(hostname, ) on host do |host| download! remote_filename, dest_filename end # return {res: 1, output: ""} rescue => e { res: 0, error: e. } end |
#ssh_upload_file(hostname, ssh_options, source_file, dest_file, handler = nil) ⇒ Object
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 |
# File 'lib/backup/remote/command.rb', line 136 def ssh_upload_file(hostname, , source_file, dest_file, handler=nil) host = Command.build_sshkit_host(hostname, ) # scp f_temp = "/tmp/#{SecureRandom.uuid}" # sshkit SSHKit::Coordinator.new(host).each in: :sequence do # upload to temp file upload! source_file, f_temp # upload to dest execute("cp #{f_temp} #{dest_file}", interaction_handler: handler) end =begin on host do |host| # NOT WORK with sudo #upload! source_file, dest_file as(user: ssh_user) do # upload to temp file upload! source_file, f_temp # upload to dest execute("cp #{f_temp} #{dest_file}", interaction_handler: handler) end end =end # return {res: 1, output: ""} rescue => e { res: 0, error: e. } end |