Class: RemoteCall

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh-base-cmd-class.rb

Instance Method Summary collapse

Constructor Details

#initializeRemoteCall

Returns a new instance of RemoteCall.



10
11
12
# File 'lib/ssh-base-cmd-class.rb', line 10

def initialize ()
    @log = Logger.new(Canzea::config[:logging_root] + '/plans.log')
end

Instance Method Details

#decrypt(contents, privateKey) ⇒ Object



69
70
71
72
73
74
# File 'lib/ssh-base-cmd-class.rb', line 69

def decrypt (contents, privateKey)
    privkey_pem = File.read privateKey
    key = OpenSSL::PKey::RSA.new privkey_pem
    output = key.private_decrypt Base64.urlsafe_decode64 contents
    puts output
end

#encrypt(contents, publicKey) ⇒ Object



62
63
64
65
66
67
# File 'lib/ssh-base-cmd-class.rb', line 62

def encrypt (contents, publicKey)
    pubkey_pem = File.read publicKey
    key = OpenSSL::PKey::RSA.new pubkey_pem
    output = Base64.urlsafe_encode64 key.public_encrypt contents
    puts output
end

#exec(hostname, privateKey, cmd, ref = "") ⇒ Object



14
15
16
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/ssh-base-cmd-class.rb', line 14

def exec (hostname, privateKey, cmd, ref = "")

    @username = "root"

    @log.info("    R [#{ref}] COMMAND: #{cmd}")

    begin
        Net::SSH.start(hostname, @username, :verify_host_key => :never, :keys => [privateKey]) do |ssh|

            chan = ssh.open_channel do |channel|
                channel.request_pty
                channel.env("DIGITAL_OCEAN_API_KEY", ENV['DIGITAL_OCEAN_API_KEY'])
                channel.env("VAULT_TOKEN", ENV['VAULT_TOKEN'])
                channel.env("CONSUL_URL", ENV['CONSUL_URL'])
                channel.env("WORK_DIR", ENV['WORK_DIR'])
                channel.env("ES_REF", ref)
                channel.exec(cmd) do |ch, success|
                    abort "could not execute command" unless success

                    channel.on_data do |ch, data|
                      puts data

                      data.sub(/\n/, '').scan(/.{1,80}/).each do | line |
                          @log.info("    R [#{ref}]  STDOUT: #{line}")
                      end
                    end

                    channel.on_request("exit-status") do |ch, data|
                        exit_code = data.read_long
                        @log.info("    R [#{ref}] Exit status: #{exit_code}")
                        if (exit_code == 0)
                        else
                            abort()
                        end
                    end

                    channel.on_close do |ch|
                    end
                end
            end
            chan.wait
        end
    rescue
        @log.info("    R ABORTED!")
        raise
    end
end

#get(hostname, privateKey, remoteFile, localFile = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ssh-base-cmd-class.rb', line 90

def get (hostname, privateKey, remoteFile, localFile = nil)

    @username = "root"

    @log.info("    R : Getting from #{hostname}")
    @log.info("    R : Getting file #{remoteFile}")

    Net::SSH.start(hostname, @username, :paranoid => false, :keys => [privateKey]) do |ssh|
        ssh.sftp.download!(remoteFile, localFile)
    end
    @log.info("    R : Saved to #{localFile}")
end

#put(hostname, privateKey, localFile, remoteFile = nil) ⇒ Object

Secure copy - use the public key to encrypt the contents before sent across



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ssh-base-cmd-class.rb', line 78

def put (hostname, privateKey, localFile, remoteFile = nil)

    @username = "root"

    puts "Uploading #{localFile} to #{remoteFile}"
    @log.info("    R : Uploading to #{hostname}")
    @log.info("    R : Uploading #{localFile} to #{remoteFile}")
    Net::SSH.start(hostname, @username, :paranoid => false, :keys => [privateKey]) do |ssh|
        ssh.sftp.upload!(localFile, remoteFile)
    end
end