Module: Pointer::SshHelpers

Included in:
EasyDeploy
Defined in:
lib/pointer/ssh_helpers.rb

Instance Method Summary collapse

Instance Method Details

#ensure_file_contains(file_name, string) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pointer/ssh_helpers.rb', line 19

def ensure_file_contains(file_name, string)
  contents = file_absent(file_name) ? '' : get_file_contents(file_name).to_s
  unless contents.include? string
    unless contents.end_with? "\n"
      contents += "\n"
    end

    contents += string

    put_file_contents(file_name, contents)
  end
end

#ensure_file_not_contains(file_name, string) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/pointer/ssh_helpers.rb', line 32

def ensure_file_not_contains(file_name, string)
  contents = file_absent(file_name) ? '' : get_file_contents(file_name).to_s

  if contents and contents.include? string
    contents.gsub!(string, '')
    put_file_contents(file_name, contents)
  end
end

#expect_empty(string) ⇒ Object



49
50
51
52
53
54
# File 'lib/pointer/ssh_helpers.rb', line 49

def expect_empty(string)
  if string
    puts string
    exit
  end
end

#file_absent(file_name) ⇒ Object



41
42
43
# File 'lib/pointer/ssh_helpers.rb', line 41

def file_absent(file_name)
  @ssh.exec!("ls #{file_name.shellescape}") =~ /No such file or directory/
end

#file_exists(file_name) ⇒ Object



45
46
47
# File 'lib/pointer/ssh_helpers.rb', line 45

def file_exists(file_name)
  not file_absent(file_name)
end

#get_file_contents(file_name) ⇒ Object



8
9
10
# File 'lib/pointer/ssh_helpers.rb', line 8

def get_file_contents(file_name)
  @ssh.exec!("cat #{file_name.shellescape}")
end

#put_file_contents(file_name, string) ⇒ Object



12
13
14
15
16
17
# File 'lib/pointer/ssh_helpers.rb', line 12

def put_file_contents(file_name, string)
  file = Tempfile.new('ssh')
  file.write(string)
  file.rewind
  @ssh.scp.upload!(file.path, file_name)
end

#test_connectionObject



72
73
74
75
76
77
78
79
80
# File 'lib/pointer/ssh_helpers.rb', line 72

def test_connection
  connection_test = `ssh -o StrictHostKeyChecking=no #{rails_user}@#{host} -p #{port} -i #{private_key} "echo OK"`
  if connection_test.strip == "OK"
    what "Connected via private key OK"
  else
    puts connection_test.on_red
    raise "I cannot connect via private key!"
  end
end

#with_root_sshObject



64
65
66
67
68
69
70
# File 'lib/pointer/ssh_helpers.rb', line 64

def with_root_ssh
  Net::SSH.start(host, user, port: port, :password => password, :paranoid => false) do |ssh|
    @ssh = ssh
    yield
    @ssh = 'Connection closed'
  end
end

#with_sshObject



56
57
58
59
60
61
62
# File 'lib/pointer/ssh_helpers.rb', line 56

def with_ssh
  Net::SSH.start(host, rails_user, port: port, :keys => [private_key], :paranoid => false) do |ssh|
    @ssh = ssh
    yield
    @ssh = 'Connection closed'
  end
end