Class: App::SSH

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

Constant Summary collapse

SCRIPT_PATH =
'vm-scripts/'
SSH_PREFIX =
'ssh_'

Class Method Summary collapse

Class Method Details

.run_script_on_remote(config_key, script_name, args = nil, run_as_root = false) ⇒ Object

SCPs a script to a remote host and runs it.

Returns:

  • void


52
53
54
55
# File 'lib/core/ssh.rb', line 52

def self.run_script_on_remote(config_key, script_name, args = nil, run_as_root = false)
    ssh_user, ssh_host, ssh_cert = get_ssh_credentials(config_key)
    run_script_on_remote_credentials(ssh_user, ssh_host, ssh_cert, script_name, args, run_as_root)
end

.run_script_on_remote_credentials(ssh_user, ssh_host, ssh_cert, script_name, args = nil, run_as_root = false) ⇒ Object

SCPs a script to remote and runs it with pre-supplied credentials.

Returns:

  • void


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

def self.run_script_on_remote_credentials(ssh_user, ssh_host, ssh_cert, script_name, args = nil, run_as_root = false)
    script_path_local = "#{App::UtilsTools::get_base_path}/#{App::SSH::SCRIPT_PATH}#{script_name}"
    script_path_remote = "/tmp/#{script_name}" # Time.now.to_i
    check_script_exists(script_name)
    scp_script_to_vm(script_path_local, script_path_remote, ssh_user, ssh_host, ssh_cert)
    args = args.nil? ? [] : args
    command("chmod 0755 #{script_path_remote}", ssh_user, ssh_host, ssh_cert)
    command_execute_message("#{script_path_remote} #{args.join(' ')}")
    display_script_content(script_path_local)
    command("#{script_path_remote} #{args.join(' ')}", ssh_user, ssh_host, ssh_cert, false)
    command("rm #{script_path_remote}", ssh_user, ssh_host, ssh_cert)
end

.scp_to_remote(config_key, source, target) ⇒ Object

Source can be a file or directory but whatever it is, target must be the same (a file or directory).

Returns:

  • void


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/core/ssh.rb', line 31

def self.scp_to_remote(config_key, source, target)

    if config_key.nil?
        show_available_ssh_hosts
    else

        ssh_user, ssh_host, ssh_cert = get_ssh_credentials(config_key)

        unless App::UtilsFiles::file_exists(source)
            App::Terminal::error("The file doesn't exist: #{App::Terminal::format_directory(source)}", nil, true)
        end


    end

    exit

end

.ssh_into_remote(config_key) ⇒ Object

SSH into remote host.

Returns:

  • void


10
11
12
13
14
15
16
17
# File 'lib/core/ssh.rb', line 10

def self.ssh_into_remote(config_key)
    if config_key.nil?
        show_available_ssh_hosts
    else
        ssh_user, ssh_host, ssh_cert = get_ssh_credentials(config_key)
        ssh_into_remote_credentials(ssh_user, ssh_host, ssh_cert)
    end
end

.ssh_into_remote_credentials(ssh_user, ssh_host, ssh_cert = nil) ⇒ Object

SSH into remote with pre-supplied credentials.

Returns:

  • void


21
22
23
24
25
26
27
# File 'lib/core/ssh.rb', line 21

def self.ssh_into_remote_credentials(ssh_user, ssh_host, ssh_cert = nil)
    if ssh_cert.nil?
        App::Terminal::command("ssh #{ssh_user}@#{ssh_host}", nil, false, false)
    else
        App::Terminal::command("ssh -i #{ssh_cert} #{ssh_user}@#{ssh_host}", nil, false, false)
    end
end