Skip to content

Add support for vmpooler on demand provisioning #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
**/*.yml
**/*.yaml
**/*.md
**/*example
**/Dockerfile*
Gemfile.lock
Rakefile
coverage
spec
examples
scripts
vendor
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM ruby:2.7

COPY ./ ./

RUN apt-get update && apt-get install -y less
RUN gem install bundler && bundle install && gem build vmfloaty.gemspec && gem install vmfloaty*.gem
9 changes: 7 additions & 2 deletions lib/vmfloaty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def run # rubocop:disable Metrics/AbcSize
c.option '--notoken', 'Makes a request without a token'
c.option '--force', 'Forces vmfloaty to get requested vms'
c.option '--json', 'Prints retrieved vms in JSON format'
c.option '--ondemand', 'Requested vms are provisioned upon receival of the request, tracked by a request ID'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
Expand All @@ -63,9 +64,13 @@ def run # rubocop:disable Metrics/AbcSize
exit 1
end

response = service.retrieve(verbose, os_types, use_token)
response = service.retrieve(verbose, os_types, use_token, options.ondemand)
request_id = response['request_id'] if options.ondemand
response = service.wait_for_request(verbose, request_id) if options.ondemand

hosts = Utils.standardize_hostnames(response)
if options.json

if options.json || options.ondemand
puts JSON.pretty_generate(hosts)
else
puts Utils.format_host_output(hosts)
Expand Down
10 changes: 9 additions & 1 deletion lib/vmfloaty/abs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ def self.list(verbose, url, os_filter = nil)
os_list << '*** VMPOOLER Pools ***'
os_list += JSON.parse(res_body['vmpooler_platforms'])

res = conn.get 'status/platforms/ondemand_vmpooler'
res_body = JSON.parse(res.body)
unless res_body['ondemand_vmpooler_platforms'] == '[]'
os_list << ''
os_list << '*** VMPOOLER ONDEMAND Pools ***'
os_list += JSON.parse(res_body['ondemand_vmpooler_platforms'])
end

res = conn.get 'status/platforms/nspooler'
res_body = JSON.parse(res.body)
os_list << ''
Expand All @@ -168,7 +176,7 @@ def self.list(verbose, url, os_filter = nil)
end

# Retrieve an OS from ABS.
def self.retrieve(verbose, os_types, token, url, user, options)
def self.retrieve(verbose, os_types, token, url, user, options, _ondemand = nil)
#
# Contents of post must be like:
#
Expand Down
36 changes: 33 additions & 3 deletions lib/vmfloaty/pooler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.list_active(verbose, url, token, _user)
vms
end

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

response = conn.post "vm/#{os_string}"
response = conn.post "vm/#{os_string}" unless ondemand
response ||= conn.post "ondemandvm/#{os_string}"

res_body = JSON.parse(response.body)

if res_body['ok']
res_body
elsif response.status == 401
raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
elsif response.status == 403
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}"
else
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}" unless ondemand
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/ondemandvm/#{os_string}. #{res_body}"
end
end

def self.wait_for_request(verbose, request_id, url, timeout = 300)
start_time = Time.now
while check_ondemandvm(verbose, request_id, url) == false
return false if (Time.now - start_time).to_i > timeout

STDOUT.puts "waiting for request #{request_id} to be fulfilled"
sleep 5
end
STDOUT.puts "The request has been fulfilled"
check_ondemandvm(verbose, request_id, url)
end

def self.check_ondemandvm(verbose, request_id, url)
conn = Http.get_conn(verbose, url)

response = conn.get "ondemandvm/#{request_id}"
res_body = JSON.parse(response.body)
return res_body if response.status == 200

return false if response.status == 202

raise "HTTP #{response.status}: The request cannot be found, or an unknown error occurred" if response.status == 404

false
end

def self.modify(verbose, url, hostname, token, modify_hash)
raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil?

Expand Down
8 changes: 6 additions & 2 deletions lib/vmfloaty/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ def list_active(verbose)
@service_object.list_active verbose, url, token, user
end

def retrieve(verbose, os_types, use_token = true)
def retrieve(verbose, os_types, use_token = true, ondemand = nil)
puts 'Requesting a vm without a token...' unless use_token
token_value = use_token ? token : nil
@service_object.retrieve verbose, os_types, token_value, url, user, @config
@service_object.retrieve verbose, os_types, token_value, url, user, @config, ondemand
end

def wait_for_request(verbose, requestid)
@service_object.wait_for_request verbose, requestid, url
end

def ssh(verbose, host_os, use_token = true)
Expand Down
4 changes: 3 additions & 1 deletion lib/vmfloaty/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def self.standardize_hostnames(response_body)

result = {}

response_body.each do |os, value|
STDOUT.puts "response body is #{response_body}"
filtered_response_body = response_body.reject { |key, _| key == 'request_id' || key == 'ready' }
filtered_response_body.each do |os, value|
hostnames = Array(value['hostname'])
hostnames.map! { |host| "#{host}.#{domain}" } if domain
result[os] = hostnames
Expand Down
20 changes: 20 additions & 0 deletions spec/vmfloaty/pooler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@
expect(vm_req['debian-7-i386']['hostname']).to eq %w[sc0o4xqtodlul5w 4m4dkhqiufnjmxy]
expect(vm_req['centos-7-x86_64']['hostname']).to eq 'zb91y9qbrbf6d3q'
end

context 'with ondemand provisioning' do
let(:ondemand_response) { '{"ok":true,"request_id":"1234"}' }
it 'retreives the vm with a token' do
stub_request(:post, "#{@vmpooler_url}/ondemandvm/debian-7-i386")
.with(:headers => { 'X-Auth-Token' => 'mytokenfile' })
.to_return(:status => 200, :body => ondemand_response, :headers => {})

stub_request(:get, "#{@vmpooler_url}/ondemandvm/1234")
.to_return(:status => 200, :body => @retrieve_response_body_single, :headers => {})

vm_hash = {}
vm_hash['debian-7-i386'] = 1
Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user', {}, true)
vm_req = Pooler.check_ondemandvm(false, '1234', @vmpooler_url)
expect(vm_req).to be_an_instance_of Hash
expect(vm_req['ok']).to equal true
expect(vm_req['debian-7-i386']['hostname']).to eq 'fq6qlpjlsskycq6'
end
end
end

describe '#modify' do
Expand Down