Skip to content

Commit 57251ed

Browse files
committed
Add ondemand flag and api v2 support to floaty ssh
1 parent 4103fde commit 57251ed

File tree

4 files changed

+115
-34
lines changed

4 files changed

+115
-34
lines changed

Diff for: 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

Diff for: 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)

Diff for: lib/vmfloaty/ssh.rb

+27-9
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,45 @@ 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+
hosts = service.check_ondemandvm(verbose, requestid, service.url)
32+
if hosts['domain'].nil?
33+
hostname = hosts[host_os]['hostname']
34+
hostname = hosts[host_os]['hostname'][0] if hosts[host_os]['hostname'].is_a?(Array)
35+
else
36+
# Provides backwards compatibility with VMPooler API v1
37+
hostname = "#{hosts[host_os]['hostname']}.#{hosts['domain']}"
38+
hostname = "#{hosts[host_os]['hostname'][0]}.#{hosts['domain']}" if hosts[host_os]['hostname'].is_a?(Array)
39+
end
40+
else
41+
if response['domain'].nil?
42+
hostname = response[host_os]['hostname']
43+
hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
44+
else
45+
# Provides backwards compatibility with VMPooler API v1
46+
hostname = "#{response[host_os]['hostname']}.#{response['domain']}"
47+
hostname = "#{response[host_os]['hostname'][0]}.#{response['domain']}" if response[host_os]['hostname'].is_a?(Array)
48+
end
49+
end
3250

3351
"#{ssh_path} #{user}@#{hostname}"
3452
end
3553

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

Diff for: spec/vmfloaty/ssh_spec.rb

+85-22
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,109 @@
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-
end
1413

15-
{
16-
os_types.keys[0] => { 'hostname' => 'vmpooler-hostname' },
17-
'domain' => 'delivery.puppetlabs.net',
18-
'ok' => true
19-
}
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+
}
19+
20+
else
21+
return {
22+
os_types.keys[0] => { 'hostname' => 'vmpooler-v1-hostname' },
23+
'domain' => 'delivery.puppetlabs.net',
24+
'ok' => true
25+
}
26+
end
2027
end
2128

2229
def type
2330
return 'abs' if os_types == 'abs_host_string'
24-
return 'vmpooler' if os_types == 'vmpooler_host_string'
31+
return 'vmpooler' if os_types == 'vmpooler_api_v1_host_string' || os_types == 'vmpooler_api_v2_host_string'
32+
end
33+
34+
def wait_for_request(verbose, requestid)
35+
return true
2536
end
2637
end
2738

2839
describe Ssh do
2940
before :each do
3041
end
3142

32-
it 'gets a hostname string for abs' do
33-
verbose = false
34-
service = ServiceStub.new
35-
host_os = 'abs_host_string'
36-
use_token = false
37-
cmd = Ssh.command_string(verbose, service, host_os, use_token)
38-
expect(cmd).to match(/ssh [email protected]/)
43+
context "for pooled requests" do
44+
it 'gets a hostname string for abs' do
45+
verbose = false
46+
service = ServiceStub.new
47+
host_os = 'abs_host_string'
48+
use_token = false
49+
cmd = Ssh.command_string(verbose, service, host_os, use_token)
50+
expect(cmd).to match(/ssh [email protected]/)
51+
end
52+
53+
it 'gets a hostname string for vmpooler api v1' do
54+
verbose = true
55+
service = ServiceStub.new
56+
host_os = 'vmpooler_api_v1_host_string'
57+
use_token = false
58+
cmd = Ssh.command_string(verbose, service, host_os, use_token)
59+
expect(cmd).to match(/ssh [email protected]/)
60+
end
61+
62+
it 'gets a hostname string for vmpooler api v2' do
63+
verbose = false
64+
service = ServiceStub.new
65+
host_os = 'vmpooler_api_v2_host_string'
66+
use_token = false
67+
cmd = Ssh.command_string(verbose, service, host_os, use_token)
68+
expect(cmd).to match(/ssh [email protected]/)
69+
end
3970
end
4071

41-
it 'gets a hostname string for vmpooler' do
42-
verbose = false
43-
service = ServiceStub.new
44-
host_os = 'vmpooler_host_string'
45-
use_token = false
46-
cmd = Ssh.command_string(verbose, service, host_os, use_token)
47-
expect(cmd).to match(/ssh [email protected]/)
72+
context "for ondemend requests" do
73+
let(:service) { ServiceStub.new }
74+
let(:url) { 'http://pooler.example.com' }
75+
76+
it 'gets a hostname string for abs' do
77+
verbose = false
78+
host_os = 'abs_host_string'
79+
use_token = false
80+
ondemand = true
81+
response = {'abs_host_string' => { 'hostname' => ['abs-hostname.delivery.puppetlabs.net']}}
82+
allow(service).to receive(:url)
83+
allow(service).to receive(:check_ondemandvm).and_return(response)
84+
cmd = Ssh.command_string(verbose, service, host_os, use_token, ondemand)
85+
expect(cmd).to match(/ssh [email protected]/)
86+
end
87+
88+
it 'gets a hostname string for abs' do
89+
verbose = false
90+
host_os = 'vmpooler_api_v1_host_string'
91+
use_token = false
92+
ondemand = true
93+
response = {'vmpooler_api_v1_host_string' => { 'hostname' => ['vmpooler_api_v1_host_string.delivery.puppetlabs.net']}}
94+
allow(service).to receive(:url)
95+
allow(service).to receive(:check_ondemandvm).and_return(response)
96+
cmd = Ssh.command_string(verbose, service, host_os, use_token, ondemand)
97+
expect(cmd).to match(/ssh root@vmpooler_api_v1_host_string.delivery.puppetlabs.net/)
98+
end
99+
100+
it 'gets a hostname string for abs' do
101+
verbose = false
102+
host_os = 'vmpooler_api_v2_host_string'
103+
use_token = false
104+
ondemand = true
105+
response = {'vmpooler_api_v2_host_string' => { 'hostname' => ['vmpooler_api_v2_host_string.delivery.puppetlabs.net']}}
106+
allow(service).to receive(:url)
107+
allow(service).to receive(:check_ondemandvm).and_return(response)
108+
cmd = Ssh.command_string(verbose, service, host_os, use_token, ondemand)
109+
expect(cmd).to match(/ssh root@vmpooler_api_v2_host_string.delivery.puppetlabs.net/)
110+
end
48111
end
49112
end

0 commit comments

Comments
 (0)