Class: Backup::Remote::Command

Inherits:
Object
  • Object
show all
Includes:
SSHKit::DSL
Defined in:
lib/backup/remote/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.interaction_handler_pwd(user, pwd, host = '') ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/backup/remote/command.rb', line 60

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_user, ssh_pass, cmd) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/backup/remote/command.rb', line 14

def run_ssh_cmd(hostname, ssh_user, ssh_pass, cmd)
  host = SSHKit::Host.new({hostname: hostname, user: ssh_user})
  host.password = ssh_pass

  #srv = ssh_user+'@'+hostname
  all_servers = [host]

  output = ''

  on all_servers do |srv|
  #SSHKit::Coordinator.new(host).each in: :sequence do
    as(user: ssh_user) do
      #execute(cmd)
      output = capture(cmd)
    end
  end

  #
  return {     res: 1,      output: output   }

rescue => e
  {
      res: 0,
      output: output,
      error: e.message
  }
end

#run_ssh_cmd_sudo(hostname, ssh_user, ssh_pass, cmd, handler = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/backup/remote/command.rb', line 42

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.message
  }
end

#ssh_download_file(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) ⇒ Object



72
73
74
75
# File 'lib/backup/remote/command.rb', line 72

def ssh_download_file(hostname, ssh_user, ssh_pass, remote_filename, dest_filename)
  return ssh_download_file_sshkit(hostname, ssh_user, ssh_pass, 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_user, ssh_pass, remote_filename, dest_filename) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/backup/remote/command.rb', line 77

def ssh_download_file_scp(hostname, ssh_user, ssh_pass, remote_filename, dest_filename)
  Net::SCP.download!(hostname, ssh_user, remote_filename, dest_filename, :ssh => { :password => ssh_pass })

  #
  return {res: 1, output: ""}

rescue => e
  {
      res: 0,
      error: e.message
  }
end

#ssh_download_file_sshkit(hostname, ssh_user, ssh_pass, remote_filename, dest_filename) ⇒ Object

!! NOT work on big files > 4Gb



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/backup/remote/command.rb', line 91

def ssh_download_file_sshkit(hostname, ssh_user, ssh_pass, remote_filename, dest_filename)
  host = SSHKit::Host.new("#{ssh_user}@#{hostname}")
  host.password = ssh_pass

  on host do |host|
    download! remote_filename, dest_filename
  end

  #
  return {res: 1, output: ""}

rescue => e
  {
      res: 0,
      error: e.message
  }
end

#ssh_upload_file(hostname, ssh_user, ssh_pass, source_file, dest_file, handler = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/backup/remote/command.rb', line 109

def ssh_upload_file(hostname, ssh_user, ssh_pass, source_file, dest_file, handler=nil)
  host = SSHKit::Host.new("#{ssh_user}@#{hostname}")
  host.password = ssh_pass

  # scp
  f_temp = "/tmp/#{SecureRandom.uuid}"

  # sshkit
  on host do |host|
    as(user: ssh_user) do

    end

    # NOT WORK with sudo
    #upload! source_file, dest_file

    # upload to temp file
    upload! source_file, f_temp

    # upload to dest
    execute("cp #{f_temp} #{dest_file}", interaction_handler: handler)

  end

  #
  return     {res: 1, output: ""}
rescue => e
  {
      res: 0,
      error: e.message
  }
end