mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
Rebasing fixed tests
This commit is contained in:
parent
de7d0fdeab
commit
a77ea84092
11 changed files with 217 additions and 111 deletions
|
|
@ -84,6 +84,7 @@ class Vmfloaty
|
|||
c.option '--url STRING', String, 'URL of pooler service'
|
||||
c.action do |args, options|
|
||||
verbose = options.verbose || config['verbose']
|
||||
|
||||
service = Service.new(options, config)
|
||||
filter = args[0]
|
||||
|
||||
|
|
|
|||
|
|
@ -6,134 +6,197 @@ require 'faraday'
|
|||
require 'json'
|
||||
|
||||
class ABS
|
||||
# List active VMs in ABS
|
||||
#
|
||||
#
|
||||
# {
|
||||
# "state":"filled",
|
||||
# "last_processed":"2019-10-31 20:59:33 +0000",
|
||||
# "allocated_resources": [
|
||||
# {
|
||||
# "hostname":"h3oyntawjm7xdch.delivery.puppetlabs.net",
|
||||
# "type":"centos-7.2-tmpfs-x86_64",
|
||||
# "engine":"vmpooler"}
|
||||
# ],
|
||||
# "audit_log":{
|
||||
# "2019-10-30 20:33:12 +0000":"Allocated h3oyntawjm7xdch.delivery.puppetlabs.net for job 1572467589"
|
||||
# },
|
||||
# "request":{
|
||||
# "resources":{
|
||||
# "centos-7.2-tmpfs-x86_64":1
|
||||
# },
|
||||
# "job": {
|
||||
# "id":1572467589,
|
||||
# "tags": {
|
||||
# "user":"mikker",
|
||||
# "url_string":"floaty://mikker/1572467589"
|
||||
# },
|
||||
# "user":"mikker",
|
||||
# "time-received":1572467589
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
#
|
||||
def self.list_active(verbose, url, _token, user)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
res = conn.get 'status/queue'
|
||||
requests = JSON.parse(res.body)
|
||||
|
||||
requests.each do |req|
|
||||
reqHash = JSON.parse(req)
|
||||
next unless user == reqHash['request']['job']['user']
|
||||
|
||||
puts '------------------------------------'
|
||||
puts "State: #{reqHash['state']}"
|
||||
puts "Job ID: #{reqHash['request']['job']['id']}"
|
||||
reqHash['request']['resources'].each do |vm_template, i|
|
||||
puts "--VMRequest: #{vm_template}: #{i}"
|
||||
end
|
||||
if reqHash['state'] == 'allocated' || reqHash['state'] == 'filled'
|
||||
reqHash['allocated_resources'].each do |vm_name, i|
|
||||
puts "----VM: #{vm_name}: #{i}"
|
||||
end
|
||||
end
|
||||
puts "User: #{reqHash['request']['job']['user']}"
|
||||
puts ''
|
||||
end
|
||||
|
||||
sleep(100)
|
||||
end
|
||||
|
||||
# List available VMs in ABS
|
||||
def self.list(verbose, url, os_filter = nil)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
os_list = []
|
||||
|
||||
response = conn.get 'status/platforms/vmpooler'
|
||||
response_body = JSON.parse(response.body)
|
||||
os_list << "*** VMPOOLER Pools ***"
|
||||
os_list = os_list + JSON.parse(response_body["vmpooler_platforms"])
|
||||
res = conn.get 'status/platforms/vmpooler'
|
||||
|
||||
response = conn.get 'status/platforms/nspooler'
|
||||
response_body = JSON.parse(response.body)
|
||||
os_list << ""
|
||||
os_list << "*** NSPOOLER Pools ***"
|
||||
os_list = os_list + JSON.parse(response_body["nspooler_platforms"])
|
||||
res_body = JSON.parse(res.body)
|
||||
os_list << '*** VMPOOLER Pools ***'
|
||||
os_list += JSON.parse(res_body['vmpooler_platforms'])
|
||||
|
||||
response = conn.get 'status/platforms/aws'
|
||||
response_body = JSON.parse(response.body)
|
||||
os_list << ""
|
||||
os_list << "*** AWS Pools ***"
|
||||
os_list = os_list + JSON.parse(response_body["aws_platforms"])
|
||||
res = conn.get 'status/platforms/nspooler'
|
||||
res_body = JSON.parse(res.body)
|
||||
os_list << ''
|
||||
os_list << '*** NSPOOLER Pools ***'
|
||||
os_list += JSON.parse(res_body['nspooler_platforms'])
|
||||
|
||||
res = conn.get 'status/platforms/aws'
|
||||
res_body = JSON.parse(res.body)
|
||||
os_list << ''
|
||||
os_list << '*** AWS Pools ***'
|
||||
os_list += JSON.parse(res_body['aws_platforms'])
|
||||
|
||||
os_list.delete 'ok'
|
||||
|
||||
puts os_list
|
||||
|
||||
os_filter ? os_list.select { |i| i[/#{os_filter}/] } : os_list
|
||||
end
|
||||
|
||||
# List active VMs from ABS
|
||||
def self.list_active(verbose, url, token)
|
||||
status = Auth.token_status(verbose, url, token)
|
||||
status['reserved_hosts'] || []
|
||||
end
|
||||
# Retrieve an OS from ABS.
|
||||
def self.retrieve(verbose, os_types, token, url, user)
|
||||
#
|
||||
# Contents of post must be:j
|
||||
#
|
||||
# {
|
||||
# "resources": {
|
||||
# "centos-7-i386": 1,
|
||||
# "ubuntu-1404-x86_64": 2
|
||||
# },
|
||||
# "job": {
|
||||
# "id": "12345",
|
||||
# "tags": {
|
||||
# "user": "jenkins",
|
||||
# "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
|
||||
def self.retrieve(verbose, os_type, token, url)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
conn.headers['X-AUTH-TOKEN'] = token if token
|
||||
|
||||
os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
|
||||
raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
|
||||
saved_job_id = Time.now.to_i
|
||||
|
||||
response = conn.post "host/#{os_string}"
|
||||
reqObj = {
|
||||
:resources => os_types,
|
||||
:job => {
|
||||
:id => saved_job_id,
|
||||
:tags => {
|
||||
:user => user,
|
||||
:url_string => "floaty://#{user}/#{saved_job_id}",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res_body = JSON.parse(response.body)
|
||||
# os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
|
||||
# raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
|
||||
puts "Requesting VMs with job_id: #{saved_job_id}. Will retry for up to an hour."
|
||||
res = conn.post 'api/v2/request', reqObj.to_json
|
||||
|
||||
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}"
|
||||
else
|
||||
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/host/#{os_string}. #{res_body}"
|
||||
i = 0
|
||||
retries = 360
|
||||
|
||||
raise AuthError, "HTTP #{res.status}: The token provided could not authenticate to the pooler.\n#{res_body}" if res.status == 401
|
||||
|
||||
(1..retries).each do |i|
|
||||
queue_place, res_body = check_queue(conn, saved_job_id, reqObj)
|
||||
return translated(res_body) if res_body
|
||||
|
||||
puts "Waiting 10 seconds to check if ABS request has been filled. Queue Position: #{queue_place}... (x#{i})"
|
||||
sleep(10)
|
||||
end
|
||||
nil
|
||||
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?
|
||||
#
|
||||
# We should fix the ABS API to be more like the vmpooler or nspooler api, but for now
|
||||
#
|
||||
def self.translated(res_body)
|
||||
vmpooler_formatted_body = {}
|
||||
|
||||
modify_hash.each do |key, _value|
|
||||
raise ModifyError, "Configured service type does not support modification of #{key}" unless %i[reason reserved_for_reason].include? key
|
||||
res_body.each do |host|
|
||||
if vmpooler_formatted_body[host['type']] && vmpooler_formatted_body[host['type']]['hostname'].class == Array
|
||||
vmpooler_formatted_body[host['type']]['hostname'] << host['hostname']
|
||||
else
|
||||
vmpooler_formatted_body[host['type']] = { 'hostname' => [host['hostname']] }
|
||||
end
|
||||
end
|
||||
vmpooler_formatted_body['ok'] = true
|
||||
|
||||
if modify_hash[:reason]
|
||||
# "reason" is easier to type than "reserved_for_reason", but nspooler needs the latter
|
||||
modify_hash[:reserved_for_reason] = modify_hash.delete :reason
|
||||
end
|
||||
|
||||
conn = Http.get_conn(verbose, url)
|
||||
conn.headers['X-AUTH-TOKEN'] = token
|
||||
|
||||
response = conn.put do |req|
|
||||
req.url "host/#{hostname}"
|
||||
req.body = modify_hash.to_json
|
||||
end
|
||||
|
||||
response.body.empty? ? {} : JSON.parse(response.body)
|
||||
vmpooler_formatted_body
|
||||
end
|
||||
|
||||
def self.disk(_verbose, _url, _hostname, _token, _disk)
|
||||
raise ModifyError, 'Configured service type does not support modification of disk space'
|
||||
end
|
||||
def self.check_queue(conn, job_id, reqObj)
|
||||
queue_info_res = conn.get "/status/queue/info/#{job_id}"
|
||||
queue_info = JSON.parse(queue_info_res.body)
|
||||
|
||||
def self.snapshot(_verbose, _url, _hostname, _token)
|
||||
raise ModifyError, 'Configured service type does not support snapshots'
|
||||
end
|
||||
res = conn.post 'api/v2/request', reqObj.to_json
|
||||
|
||||
def self.revert(_verbose, _url, _hostname, _token, _snapshot_sha)
|
||||
raise ModifyError, 'Configured service type does not support snapshots'
|
||||
end
|
||||
|
||||
def self.delete(verbose, url, hosts, token)
|
||||
raise TokenError, 'Token provided was nil; Request cannot be made to delete VM' if token.nil?
|
||||
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
conn.headers['X-AUTH-TOKEN'] = token if token
|
||||
|
||||
response_body = {}
|
||||
|
||||
hosts = hosts.split(',') unless hosts.is_a? Array
|
||||
hosts.each do |host|
|
||||
response = conn.delete "host/#{host}"
|
||||
res_body = JSON.parse(response.body)
|
||||
response_body[host] = res_body
|
||||
unless res.body.empty?
|
||||
res_body = JSON.parse(res.body)
|
||||
return queue_info['queue_place'], res_body
|
||||
end
|
||||
|
||||
response_body
|
||||
[queue_info['queue_place'], nil]
|
||||
end
|
||||
|
||||
def self.status(verbose, url)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
response = conn.get '/status'
|
||||
JSON.parse(response.body)
|
||||
res = conn.get '/status'
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
def self.summary(verbose, url)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
response = conn.get '/summary'
|
||||
JSON.parse(response.body)
|
||||
res = conn.get '/summary'
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
def self.query(verbose, url, hostname)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
response = conn.get "host/#{hostname}"
|
||||
JSON.parse(response.body)
|
||||
res = conn.get "host/#{hostname}"
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -17,12 +17,12 @@ class NonstandardPooler
|
|||
os_filter ? os_list.select { |i| i[/#{os_filter}/] } : os_list
|
||||
end
|
||||
|
||||
def self.list_active(verbose, url, token)
|
||||
def self.list_active(verbose, url, token, _user)
|
||||
status = Auth.token_status(verbose, url, token)
|
||||
status['reserved_hosts'] || []
|
||||
end
|
||||
|
||||
def self.retrieve(verbose, os_type, token, url)
|
||||
def self.retrieve(verbose, os_type, token, url, _user)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
conn.headers['X-AUTH-TOKEN'] = token if token
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class Pooler
|
|||
hosts
|
||||
end
|
||||
|
||||
def self.list_active(verbose, url, token)
|
||||
def self.list_active(verbose, url, token, _user)
|
||||
status = Auth.token_status(verbose, url, token)
|
||||
vms = []
|
||||
vms = status[token]['vms']['running'] if status[token] && status[token]['vms']
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class Service
|
|||
|
||||
def user
|
||||
unless @config['user']
|
||||
puts 'Enter your pooler service username:'
|
||||
puts "Enter your #{@config['url']} service username:"
|
||||
@config['user'] = STDIN.gets.chomp
|
||||
end
|
||||
@config['user']
|
||||
|
|
@ -52,13 +52,13 @@ class Service
|
|||
|
||||
def get_new_token(verbose)
|
||||
username = user
|
||||
pass = Commander::UI.password "Enter your #{@config["url"]} service password:", '*'
|
||||
pass = Commander::UI.password "Enter your #{@config['url']} service password:", '*'
|
||||
Auth.get_token(verbose, url, username, pass)
|
||||
end
|
||||
|
||||
def delete_token(verbose, token_value = @config['token'])
|
||||
username = user
|
||||
pass = Commander::UI.password 'Enter your pooler service password:', '*'
|
||||
pass = Commander::UI.password "Enter your #{@config['url']} service password:", '*'
|
||||
Auth.delete_token(verbose, url, username, pass, token_value)
|
||||
end
|
||||
|
||||
|
|
@ -72,13 +72,13 @@ class Service
|
|||
end
|
||||
|
||||
def list_active(verbose)
|
||||
@service_object.list_active verbose, url, token
|
||||
@service_object.list_active verbose, url, token, user
|
||||
end
|
||||
|
||||
def retrieve(verbose, os_types, use_token = true)
|
||||
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
|
||||
@service_object.retrieve verbose, os_types, token_value, url, user
|
||||
end
|
||||
|
||||
def ssh(verbose, host_os, use_token = true)
|
||||
|
|
|
|||
|
|
@ -31,6 +31,13 @@ class Utils
|
|||
# }
|
||||
# }
|
||||
|
||||
# abs pooler response body example when `floaty get` arguments are :
|
||||
# {
|
||||
# "hostname"=>"thin-soutane.delivery.puppetlabs.net",
|
||||
# "type"=>"centos-7.2-tmpfs-x86_64",
|
||||
# "engine"=>"vmpooler"
|
||||
# }
|
||||
|
||||
raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" unless response_body.delete('ok')
|
||||
|
||||
# vmpooler reports the domain separately from the hostname
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue