require "proboscis_cli/version" require 'httparty' require 'json' require "base64" class Proboscis def self.host(environment) ENV["proboscis_#{environment}"] end def self.param(environment, lookup) ENV["proboscis_#{environment}_#{lookup}"] end def self.connect(environment, subdomain, target) self.login environment self.client_list environment client_id = @client_list.select {|c| c["domain"] == subdomain} if client_id.empty? puts "Unable to find client with subdomain #{subdomain}" exit -1 end self.client environment, client_id.first if(!@infra_info["#{target.upcase}_IP"]) puts "Unable to find target #{target.upcase}_IP in #{@infra_info}. Is the machine up?" exit -1 end ip = @infra_info["#{target.upcase}_IP"] user = ENV["proboscis_#{environment}_target_user"] || "root" port = ENV["proboscis_#{environment}_target_port"] || "22" puts "Executing ssh #{user}@#{ip} -p #{port}" system "ssh #{user}@#{ip} -p #{port}" end def self.client(environment, client_id) response = HTTParty.get("#{self.host(environment)}/api/api/clients/#{client_id["id"]}/products", headers: {"X-Authorization" => @auth_code}) if response.code != 200 puts "Unable to fetch details of client with id #{client_id}" exit -1 end infra_info = JSON::parse(response.body) if infra_info.empty? puts "No Products Mapped!" exit -1 end @infra_info = JSON::parse(infra_info.first["infrastructureInfo"]) end def self.client_list(environment) response = HTTParty.get("#{self.host(environment)}/api/api/clients", headers: {"X-Authorization" => @auth_code}) if response.code != 200 puts "Unable to get the client list.." exit -1 end @client_list = JSON::parse(response.body)["content"] end def self.login(environment) response = HTTParty.post("#{self.host(environment)}/api/login", body: {phoneNumber: self.param(environment, 'user'), password: self.param(environment, 'pass')}.to_json, headers: { 'Content-Type' => 'application/json' }) if response.code != 200 puts "Unable to login to proboscis.." exit -1 end @auth_code = Base64.strict_encode64(JSON::parse(response.body)["accessToken"]) end end