class RemoteCall

Public Class Methods

new() click to toggle source
# File lib/canzea/core/ssh-base-cmd-class.rb, line 10
def initialize ()
    @log = Logger.new(Canzea::config[:logging_root] + '/plans.log')
end

Public Instance Methods

decrypt(contents, privateKey) click to toggle source
# File lib/canzea/core/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) click to toggle source
# File lib/canzea/core/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 = "") click to toggle source
# File lib/canzea/core/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, :paranoid => false, :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) click to toggle source
# File lib/canzea/core/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) click to toggle source

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

# File lib/canzea/core/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