22
23
24
25
26
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
|
# File 'lib/pointer.rb', line 22
def run!(options)
@options = options
@options[:ssh_password] = Kernel.password("ssh password for user #{@options[:ssh_user]}: ")
variables = ERB.new(IO.read(File.dirname(__FILE__) + '/../config/variables.sh.erb'))
var_file = Tempfile.new('sh')
var_file.write(variables.result(binding))
var_file.rewind
Net::SSH.start(@options[:ssh_host], @options[:ssh_user], password: @options[:ssh_password], paranoid: false) do |ssh|
puts ssh.scp.upload!(var_file.path, '/tmp/variables.sh')
puts ssh.scp.upload!(File.expand_path(@options[:public_key]), remote_public_key_file)
puts ssh.scp.upload!(File.dirname(__FILE__) + '/../config/1_as_root.sh', '/tmp/1.sh')
puts ssh.scp.upload!(File.dirname(__FILE__) + '/../config/2_as_rails.sh', '/tmp/2.sh')
run_stream('bash -l -e /tmp/1.sh', ssh)
puts ssh.exec!("chown #{@options[:rails_user]}:#{@options[:rails_user]} /tmp/variables.sh")
puts ssh.exec!("chown #{@options[:rails_user]}:#{@options[:rails_user]} " + remote_public_key_file)
puts ssh.exec!("chown #{@options[:rails_user]}:#{@options[:rails_user]} /tmp/1.sh")
puts ssh.exec!("chown #{@options[:rails_user]}:#{@options[:rails_user]} /tmp/2.sh")
end
Net::SSH.start(@options[:ssh_host], @options[:rails_user], keys: File.expand_path(@options[:private_key]), paranoid: false) do |ssh|
run_stream('bash -l -e /tmp/2.sh', ssh)
puts "ok"
end
ensure
Net::SSH.start(@options[:ssh_host], @options[:ssh_user], password: @options[:ssh_password], paranoid: false) do |ssh|
puts ssh.exec!('rm /tmp/variables.sh')
puts ssh.exec!('rm ' + remote_public_key_file)
puts ssh.exec!('rm /tmp/1.sh')
puts ssh.exec!('rm /tmp/2.sh')
end
end
|