Skip to content

Commit d1fc70f

Browse files
authored
Merge pull request #67 from mattkirby/pooler_158
Add support for vmpooler on demand provisioning
2 parents 482d432 + 7b0a767 commit d1fc70f

File tree

8 files changed

+96
-9
lines changed

8 files changed

+96
-9
lines changed

Diff for: .dockerignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
**/*.yml
2+
**/*.yaml
3+
**/*.md
4+
**/*example
5+
**/Dockerfile*
6+
Gemfile.lock
7+
Rakefile
8+
coverage
9+
spec
10+
examples
11+
scripts
12+
vendor

Diff for: Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM ruby:2.7
2+
3+
COPY ./ ./
4+
5+
RUN apt-get update && apt-get install -y less
6+
RUN gem install bundler && bundle install && gem build vmfloaty.gemspec && gem install vmfloaty*.gem

Diff for: lib/vmfloaty.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def run # rubocop:disable Metrics/AbcSize
3737
c.option '--notoken', 'Makes a request without a token'
3838
c.option '--force', 'Forces vmfloaty to get requested vms'
3939
c.option '--json', 'Prints retrieved vms in JSON format'
40+
c.option '--ondemand', 'Requested vms are provisioned upon receival of the request, tracked by a request ID'
4041
c.action do |args, options|
4142
verbose = options.verbose || config['verbose']
4243
service = Service.new(options, config)
@@ -63,9 +64,13 @@ def run # rubocop:disable Metrics/AbcSize
6364
exit 1
6465
end
6566

66-
response = service.retrieve(verbose, os_types, use_token)
67+
response = service.retrieve(verbose, os_types, use_token, options.ondemand)
68+
request_id = response['request_id'] if options.ondemand
69+
response = service.wait_for_request(verbose, request_id) if options.ondemand
70+
6771
hosts = Utils.standardize_hostnames(response)
68-
if options.json
72+
73+
if options.json || options.ondemand
6974
puts JSON.pretty_generate(hosts)
7075
else
7176
puts Utils.format_host_output(hosts)

Diff for: lib/vmfloaty/abs.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ def self.list(verbose, url, os_filter = nil)
150150
os_list << '*** VMPOOLER Pools ***'
151151
os_list += JSON.parse(res_body['vmpooler_platforms'])
152152

153+
res = conn.get 'status/platforms/ondemand_vmpooler'
154+
res_body = JSON.parse(res.body)
155+
unless res_body['ondemand_vmpooler_platforms'] == '[]'
156+
os_list << ''
157+
os_list << '*** VMPOOLER ONDEMAND Pools ***'
158+
os_list += JSON.parse(res_body['ondemand_vmpooler_platforms'])
159+
end
160+
153161
res = conn.get 'status/platforms/nspooler'
154162
res_body = JSON.parse(res.body)
155163
os_list << ''
@@ -168,7 +176,7 @@ def self.list(verbose, url, os_filter = nil)
168176
end
169177

170178
# Retrieve an OS from ABS.
171-
def self.retrieve(verbose, os_types, token, url, user, options)
179+
def self.retrieve(verbose, os_types, token, url, user, options, _ondemand = nil)
172180
#
173181
# Contents of post must be like:
174182
#

Diff for: lib/vmfloaty/pooler.rb

+33-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def self.list_active(verbose, url, token, _user)
2828
vms
2929
end
3030

31-
def self.retrieve(verbose, os_type, token, url, _user, _options)
31+
def self.retrieve(verbose, os_type, token, url, _user, _options, ondemand = nil)
3232
# NOTE:
3333
# Developers can use `Utils.generate_os_hash` to
3434
# generate the os_type param.
@@ -38,19 +38,49 @@ def self.retrieve(verbose, os_type, token, url, _user, _options)
3838
os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
3939
raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
4040

41-
response = conn.post "vm/#{os_string}"
41+
response = conn.post "vm/#{os_string}" unless ondemand
42+
response ||= conn.post "ondemandvm/#{os_string}"
4243

4344
res_body = JSON.parse(response.body)
4445

4546
if res_body['ok']
4647
res_body
4748
elsif response.status == 401
4849
raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
50+
elsif response.status == 403
51+
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. Request exceeds the configured per pool maximum. #{res_body}"
4952
else
50-
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
53+
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}" unless ondemand
54+
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/ondemandvm/#{os_string}. #{res_body}"
5155
end
5256
end
5357

58+
def self.wait_for_request(verbose, request_id, url, timeout = 300)
59+
start_time = Time.now
60+
while check_ondemandvm(verbose, request_id, url) == false
61+
return false if (Time.now - start_time).to_i > timeout
62+
63+
STDOUT.puts "waiting for request #{request_id} to be fulfilled"
64+
sleep 5
65+
end
66+
STDOUT.puts "The request has been fulfilled"
67+
check_ondemandvm(verbose, request_id, url)
68+
end
69+
70+
def self.check_ondemandvm(verbose, request_id, url)
71+
conn = Http.get_conn(verbose, url)
72+
73+
response = conn.get "ondemandvm/#{request_id}"
74+
res_body = JSON.parse(response.body)
75+
return res_body if response.status == 200
76+
77+
return false if response.status == 202
78+
79+
raise "HTTP #{response.status}: The request cannot be found, or an unknown error occurred" if response.status == 404
80+
81+
false
82+
end
83+
5484
def self.modify(verbose, url, hostname, token, modify_hash)
5585
raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil?
5686

Diff for: lib/vmfloaty/service.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ def list_active(verbose)
7575
@service_object.list_active verbose, url, token, user
7676
end
7777

78-
def retrieve(verbose, os_types, use_token = true)
78+
def retrieve(verbose, os_types, use_token = true, ondemand = nil)
7979
puts 'Requesting a vm without a token...' unless use_token
8080
token_value = use_token ? token : nil
81-
@service_object.retrieve verbose, os_types, token_value, url, user, @config
81+
@service_object.retrieve verbose, os_types, token_value, url, user, @config, ondemand
82+
end
83+
84+
def wait_for_request(verbose, requestid)
85+
@service_object.wait_for_request verbose, requestid, url
8286
end
8387

8488
def ssh(verbose, host_os, use_token = true)

Diff for: lib/vmfloaty/utils.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def self.standardize_hostnames(response_body)
4545

4646
result = {}
4747

48-
response_body.each do |os, value|
48+
STDOUT.puts "response body is #{response_body}"
49+
filtered_response_body = response_body.reject { |key, _| key == 'request_id' || key == 'ready' }
50+
filtered_response_body.each do |os, value|
4951
hostnames = Array(value['hostname'])
5052
hostnames.map! { |host| "#{host}.#{domain}" } if domain
5153
result[os] = hostnames

Diff for: spec/vmfloaty/pooler_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@
8484
expect(vm_req['debian-7-i386']['hostname']).to eq %w[sc0o4xqtodlul5w 4m4dkhqiufnjmxy]
8585
expect(vm_req['centos-7-x86_64']['hostname']).to eq 'zb91y9qbrbf6d3q'
8686
end
87+
88+
context 'with ondemand provisioning' do
89+
let(:ondemand_response) { '{"ok":true,"request_id":"1234"}' }
90+
it 'retreives the vm with a token' do
91+
stub_request(:post, "#{@vmpooler_url}/ondemandvm/debian-7-i386")
92+
.with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
93+
.to_return(:status => 200, :body => ondemand_response, :headers => {})
94+
95+
stub_request(:get, "#{@vmpooler_url}/ondemandvm/1234")
96+
.to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {})
97+
98+
vm_hash = {}
99+
vm_hash['debian-7-i386'] = 1
100+
Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user', {}, true)
101+
vm_req = Pooler.check_ondemandvm(false, '1234', @vmpooler_url)
102+
expect(vm_req).to be_an_instance_of Hash
103+
expect(vm_req['ok']).to equal true
104+
expect(vm_req['debian-7-i386']['hostname']).to eq 'fq6qlpjlsskycq6'
105+
end
106+
end
87107
end
88108

89109
describe '#modify' do

0 commit comments

Comments
 (0)