Class: Pointer::EasyDeploy

Inherits:
Object
  • Object
show all
Defined in:
lib/pointer.rb

Instance Method Summary collapse

Instance Method Details

#bitbucketObject



18
19
20
# File 'lib/pointer.rb', line 18

def bitbucket
  @options[:git_repo].include? '@bitbucket.org:'
end

#remote_private_key_fileObject



10
11
12
# File 'lib/pointer.rb', line 10

def remote_private_key_file
  "/tmp/tmp_private_key"
end

#remote_public_key_fileObject



14
15
16
# File 'lib/pointer.rb', line 14

def remote_public_key_file
  "/tmp/tmp_private_key"
end

#run!(options) ⇒ Object



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

#run_stream(cmd, ssh) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pointer.rb', line 58

def run_stream(cmd, ssh)
  ssh.exec! cmd do |ch, success|
    raise "could not execute command" unless success

    ch.on_data do |c, data|
      STDOUT.print data
    end

    ch.on_extended_data do |c, type, data|
      STDERR.print data
    end
  end
end