Module: GitPusshuTen::Helpers::Environment::SSH

Included in:
Environment
Defined in:
lib/gitpusshuten/helpers/environment/ssh.rb

Instance Method Summary collapse

Instance Method Details

#directory?(path) ⇒ Boolean

Tests if the specified directory exists

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/gitpusshuten/helpers/environment/ssh.rb', line 8

def directory?(path)
  return true if execute_as_root("if [[ -d '#{path}' ]]; then exit; else echo 1; fi").nil?
  false
end

#execute_as_root(command) ⇒ Object

Performs a command as root



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gitpusshuten/helpers/environment/ssh.rb', line 50

def execute_as_root(command)
  while true
    begin
      Net::SSH.start(c.ip, 'root', {
        :password   => @root_password,
        :passphrase => c.passphrase,
        :port       => c.port
      }) do |ssh|
        response = ssh.exec!(command)
        GitPusshuTen::Log.silent response 
        return response
      end              
    rescue Net::SSH::AuthenticationFailed
      if @root_attempted
        GitPusshuTen::Log.error "Password incorrect. Please retry."
      else
        GitPusshuTen::Log.message "Please provide the password for #{'root'.color(:yellow)} (#{c.ip.color(:yellow)})."
        @root_attempted = true
      end
      @root_password = ask('') { |q| q.echo = false }
    end
  end
end

#execute_as_user(command) ⇒ Object

Performs a single command on the remote environment as a user



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
# File 'lib/gitpusshuten/helpers/environment/ssh.rb', line 22

def execute_as_user(command)
  @user_password ||= c.password
  
  while true
    begin
      Net::SSH.start(c.ip, c.user, {
        :password   => @user_password,
        :passphrase => c.passphrase,
        :port       => c.port
      }) do |ssh|
        response = ssh.exec!(command)
        GitPusshuTen::Log.silent response 
        return response
      end
    rescue Net::SSH::AuthenticationFailed
      if @user_attempted
        GitPusshuTen::Log.error "Password incorrect. Please retry."
      else
        GitPusshuTen::Log.message "Please provide the password for #{c.user.to_s.color(:yellow)} (#{c.ip.color(:yellow)})."
        @user_attempted = true
      end
      @user_password = ask('') { |q| q.echo = false }
    end
  end
end

#file?(path) ⇒ Boolean

Tests if the specified file exists

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/gitpusshuten/helpers/environment/ssh.rb', line 15

def file?(path)
  return true if execute_as_root("if [[ -f '#{path}' ]]; then exit; else echo 1; fi").nil?
  false
end