Add ondemand flag and api v2 support to floaty ssh

This commit is contained in:
Jake Spain 2022-04-04 08:57:23 -04:00
parent 4103fdeccc
commit 667dacbcea
No known key found for this signature in database
GPG key ID: BC1C4DA0A085E113
4 changed files with 115 additions and 34 deletions

View file

@ -484,7 +484,7 @@ class Vmfloaty
FloatyLogger.info "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1
service.ssh(verbose, host_os, use_token)
service.ssh(verbose, host_os, use_token, options.ondemand)
exit 0
end
end

View file

@ -87,7 +87,7 @@ class Service
@service_object.wait_for_request verbose, requestid, url
end
def ssh(verbose, host_os, use_token = true)
def ssh(verbose, host_os, use_token = true, ondemand = nil)
token_value = nil
if use_token
begin
@ -97,7 +97,7 @@ class Service
FloatyLogger.info 'Could not get token... requesting vm without a token anyway...'
end
end
Ssh.ssh(verbose, self, host_os, token_value)
Ssh.ssh(verbose, self, host_os, token_value, ondemand)
end
def query(verbose, hostname)

View file

@ -14,27 +14,45 @@ class Ssh
nil
end
def self.command_string(verbose, service, host_os, use_token)
def self.command_string(verbose, service, host_os, use_token, ondemand = nil)
ssh_path = which('ssh')
raise 'Could not determine path to ssh' unless ssh_path
os_types = {}
os_types = Utils.generate_os_hash([host_os])
os_types[host_os] = 1
response = service.retrieve(verbose, os_types, use_token)
response = service.retrieve(verbose, os_types, use_token, ondemand)
raise "Could not get vm from #{service.type}:\n #{response}" unless response['ok']
user = /win/.match?(host_os) ? 'Administrator' : 'root'
hostname = response[host_os]['hostname']
hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
hostname = "#{hostname}.#{response['domain']}" unless hostname.end_with?('puppetlabs.net')
if ondemand
requestid = response['request_id']
service.wait_for_request(verbose, requestid)
hosts = service.check_ondemandvm(verbose, requestid, service.url)
if hosts['domain'].nil?
hostname = hosts[host_os]['hostname']
hostname = hosts[host_os]['hostname'][0] if hosts[host_os]['hostname'].is_a?(Array)
else
# Provides backwards compatibility with VMPooler API v1
hostname = "#{hosts[host_os]['hostname']}.#{hosts['domain']}"
hostname = "#{hosts[host_os]['hostname'][0]}.#{hosts['domain']}" if hosts[host_os]['hostname'].is_a?(Array)
end
else
if response['domain'].nil?
hostname = response[host_os]['hostname']
hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
else
# Provides backwards compatibility with VMPooler API v1
hostname = "#{response[host_os]['hostname']}.#{response['domain']}"
hostname = "#{response[host_os]['hostname'][0]}.#{response['domain']}" if response[host_os]['hostname'].is_a?(Array)
end
end
"#{ssh_path} #{user}@#{hostname}"
end
def self.ssh(verbose, service, host_os, use_token)
cmd = command_string(verbose, service, host_os, use_token)
def self.ssh(verbose, service, host_os, use_token, ondemand)
cmd = command_string(verbose, service, host_os, use_token, ondemand)
# TODO: Should this respect more ssh settings? Can it be configured
# by users ssh config and does this respect those settings?
Kernel.exec(cmd)