Class: AppCommand::SSH

Inherits:
Convoy::ActionCommand::Base
  • Object
show all
Defined in:
lib/routes/ssh.rb

Constant Summary collapse

CONFIGURATION_PREFIX =
'ssh_'

Instance Method Summary collapse

Instance Method Details

#executeObject



7
8
9
10
11
12
13
14
15
# File 'lib/routes/ssh.rb', line 7

def execute

    @opts = command_options
    @args = arguments

    opts_validate
    opts_routing

end

#opts_routingObject



62
63
64
65
66
# File 'lib/routes/ssh.rb', line 62

def opts_routing

    ssh_into_server

end

#opts_validateObject



17
18
19
20
21
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
57
58
59
60
# File 'lib/routes/ssh.rb', line 17

def opts_validate

    if @args[0].nil?
        App::Terminal::error('You must specify what server you want to SSH into', "This must be specified in: #{App::Terminal::format_directory(ConfigUnique::CONFIG_FILE)}", true)
    end

    if @args[0] =~ /\Assh_\S+\z/i
        config_param = @args[0]
    else
        config_param = "#{CONFIGURATION_PREFIX}#{@args[0]}"
    end

    unless App::Config::param_exists(config_param)
        App::Terminal::error('Invalid Config Parameter', "The #{App::Terminal::format_highlight('config parameter')} #{App::Terminal::format_invalid(config_param)} doesn't exist in: #{App::Terminal::format_directory(ConfigUnique::CONFIG_FILE)}", true)
    end

    ssh_parts = App::Config.param(config_param).split('|')

    @ssh_user = ssh_parts[0]
    @ssh_host = ssh_parts[1]
    @ssh_cert = ssh_parts[2].to_s.strip.length == 0 ? nil : ssh_parts[2]

    help_message = [
        "The file: #{App::Terminal::format_directory(ConfigUnique::CONFIG_FILE)} must contain a line similar to the following:",
        nil,
        'ssh_ec2=ec2-user|ec2-XX-XX-XX-XX.eu-west-1.compute.amazonaws.com|~/.ssh/pem-key.pem',
        'ssh_ec2=ec2-user|ec2-XX-XX-XX-XX.eu-west-1.compute.amazonaws.com',
    ]

    if @ssh_user.nil? || @ssh_user == ''
        App::Terminal::error("SSH #{App::Terminal::format_highlight('user')} required", help_message, true)
    end

    if @ssh_host.nil? || @ssh_host == ''
        App::Terminal::error("SSH #{App::Terminal::format_highlight('host')} required", help_message, true)
    end

    unless @ssh_cert.nil?
        unless App::UtilsFiles::file_exists(File.expand_path(@ssh_cert))
            App::Terminal::error("PEM key not found: #{App::Terminal::format_directory(@ssh_cert)}")
        end
    end

end

#ssh_into_serverObject



68
69
70
71
72
73
74
75
76
# File 'lib/routes/ssh.rb', line 68

def ssh_into_server

    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