Skip to content

Commit 7d65d3d

Browse files
committed
Add ondemand flag and api v2 support to floaty ssh
1 parent 0667f0a commit 7d65d3d

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

lib/vmfloaty.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def run # rubocop:disable Metrics/AbcSize
484484

485485
FloatyLogger.info "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1
486486

487-
service.ssh(verbose, host_os, use_token)
487+
service.ssh(verbose, host_os, use_token, options.ondemand)
488488
exit 0
489489
end
490490
end

lib/vmfloaty/service.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def wait_for_request(verbose, requestid)
8787
@service_object.wait_for_request verbose, requestid, url
8888
end
8989

90-
def ssh(verbose, host_os, use_token = true)
90+
def ssh(verbose, host_os, use_token = true, ondemand = nil)
9191
token_value = nil
9292
if use_token
9393
begin
@@ -97,7 +97,7 @@ def ssh(verbose, host_os, use_token = true)
9797
FloatyLogger.info 'Could not get token... requesting vm without a token anyway...'
9898
end
9999
end
100-
Ssh.ssh(verbose, self, host_os, token_value)
100+
Ssh.ssh(verbose, self, host_os, token_value, ondemand)
101101
end
102102

103103
def query(verbose, hostname)

lib/vmfloaty/ssh.rb

+28-9
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,46 @@ def self.which(cmd)
1414
nil
1515
end
1616

17-
def self.command_string(verbose, service, host_os, use_token)
17+
def self.command_string(verbose, service, host_os, use_token, ondemand = nil)
1818
ssh_path = which('ssh')
1919
raise 'Could not determine path to ssh' unless ssh_path
20-
21-
os_types = {}
20+
os_types = Utils.generate_os_hash([host_os])
2221
os_types[host_os] = 1
2322

24-
response = service.retrieve(verbose, os_types, use_token)
23+
response = service.retrieve(verbose, os_types, use_token, ondemand)
2524
raise "Could not get vm from #{service.type}:\n #{response}" unless response['ok']
2625

2726
user = /win/.match?(host_os) ? 'Administrator' : 'root'
2827

29-
hostname = response[host_os]['hostname']
30-
hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
31-
hostname = "#{hostname}.#{response['domain']}" unless hostname.end_with?('puppetlabs.net')
28+
if ondemand
29+
requestid = response['request_id']
30+
service.wait_for_request(verbose, requestid)
31+
response = service.retrieve(verbose, os_types, use_token, ondemand)
32+
hosts = service.check_ondemandvm(verbose, requestid, service.url)
33+
if hosts['domain'].nil?
34+
hostname = hosts[host_os]['hostname']
35+
hostname = hosts[host_os]['hostname'][0] if hosts[host_os]['hostname'].is_a?(Array)
36+
else
37+
# Provides backwards compatibility with VMPooler API v1
38+
hostname = "#{hosts[host_os]['hostname'][0]}.#{hosts['domain']}"
39+
hostname = "#{hosts[host_os]['hostname'][0]}.#{hosts['domain']}" if hosts[host_os]['hostname'].is_a?(Array)
40+
end
41+
else
42+
if response['domain'].nil?
43+
hostname = response[host_os]['hostname']
44+
hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
45+
else
46+
# Provides backwards compatibility with VMPooler API v1
47+
hostname = "#{response[host_os]['hostname']}.#{response['domain']}"
48+
hostname = "#{response[host_os]['hostname'][0]}.#{response['domain']}" if response[host_os]['hostname'].is_a?(Array)
49+
end
50+
end
3251

3352
"#{ssh_path} #{user}@#{hostname}"
3453
end
3554

36-
def self.ssh(verbose, service, host_os, use_token)
37-
cmd = command_string(verbose, service, host_os, use_token)
55+
def self.ssh(verbose, service, host_os, use_token, ondemand)
56+
cmd = command_string(verbose, service, host_os, use_token, ondemand)
3857
# TODO: Should this respect more ssh settings? Can it be configured
3958
# by users ssh config and does this respect those settings?
4059
Kernel.exec(cmd)

spec/vmfloaty/ssh_spec.rb

+21-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@
44
require 'vmfloaty/ssh'
55

66
class ServiceStub
7-
def retrieve(_verbose, os_types, _use_token)
7+
def retrieve(_verbose, os_types, _use_token, ondemand)
88
if os_types.keys[0] == 'abs_host_string'
99
return {
1010
os_types.keys[0] => { 'hostname' => ['abs-hostname.delivery.puppetlabs.net'] },
1111
'ok' => true
1212
}
13+
14+
elsif os_types.keys[0] == 'vmpooler_api_v2_host_string'
15+
return {
16+
os_types.keys[0] => { 'hostname' => ['vmpooler-v2-hostname.delivery.puppetlabs.net'] },
17+
'ok' => true
18+
}
1319
end
1420

1521
{
16-
os_types.keys[0] => { 'hostname' => 'vmpooler-hostname' },
22+
os_types.keys[0] => { 'hostname' => 'vmpooler-v1-hostname.delivery.puppetlabs.net' },
1723
'domain' => 'delivery.puppetlabs.net',
1824
'ok' => true
1925
}
2026
end
2127

2228
def type
2329
return 'abs' if os_types == 'abs_host_string'
24-
return 'vmpooler' if os_types == 'vmpooler_host_string'
30+
return 'vmpooler' if os_types == 'vmpooler_api_v1_host_string' || os_types == 'vmpooler_api_v2_host_string'
2531
end
2632
end
2733

@@ -38,12 +44,21 @@ def type
3844
expect(cmd).to match(/ssh [email protected]/)
3945
end
4046

41-
it 'gets a hostname string for vmpooler' do
47+
it 'gets a hostname string for vmpooler api v1' do
48+
verbose = false
49+
service = ServiceStub.new
50+
host_os = 'vmpooler_api_v1_host_string'
51+
use_token = false
52+
cmd = Ssh.command_string(verbose, service, host_os, use_token)
53+
expect(cmd).to match(/ssh [email protected]/)
54+
end
55+
56+
it 'gets a hostname string for vmpooler api v2' do
4257
verbose = false
4358
service = ServiceStub.new
44-
host_os = 'vmpooler_host_string'
59+
host_os = 'vmpooler_api_v2_host_string'
4560
use_token = false
4661
cmd = Ssh.command_string(verbose, service, host_os, use_token)
47-
expect(cmd).to match(/ssh [email protected]/)
62+
expect(cmd).to match(/ssh root@vmpooler-v2-hostname.delivery.puppetlabs.net/)
4863
end
4964
end

0 commit comments

Comments
 (0)