mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-25 21:28:40 -05:00
Run the rubocop auto_correct
fixed 27 files inspected, 975 offenses detected, 804 offenses corrected
This commit is contained in:
parent
7041df82f0
commit
eb99ba1dec
21 changed files with 516 additions and 511 deletions
106
lib/vmfloaty.rb
106
lib/vmfloaty.rb
|
|
@ -20,7 +20,8 @@ class Vmfloaty
|
|||
|
||||
def run # rubocop:disable Metrics/AbcSize
|
||||
program :version, Vmfloaty::VERSION
|
||||
program :description, "A CLI helper tool for Puppet's vmpooler to help you stay afloat.\n\nConfiguration may be placed in a ~/.vmfloaty.yml file."
|
||||
program :description,
|
||||
"A CLI helper tool for Puppet's vmpooler to help you stay afloat.\n\nConfiguration may be placed in a ~/.vmfloaty.yml file."
|
||||
|
||||
config = Conf.read_config
|
||||
|
||||
|
|
@ -43,9 +44,7 @@ class Vmfloaty
|
|||
c.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
|
||||
c.action do |args, options|
|
||||
verbose = options.verbose || config['verbose']
|
||||
if options.loglevel
|
||||
FloatyLogger.setlevel = options.loglevel
|
||||
end
|
||||
FloatyLogger.setlevel = options.loglevel if options.loglevel
|
||||
service = Service.new(options, config)
|
||||
use_token = !options.notoken
|
||||
force = options.force
|
||||
|
|
@ -100,21 +99,19 @@ class Vmfloaty
|
|||
c.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
|
||||
c.action do |args, options|
|
||||
verbose = options.verbose || config['verbose']
|
||||
if options.loglevel
|
||||
FloatyLogger.setlevel = options.loglevel
|
||||
end
|
||||
FloatyLogger.setlevel = options.loglevel if options.loglevel
|
||||
|
||||
service = Service.new(options, config)
|
||||
filter = args[0]
|
||||
|
||||
if options.active
|
||||
# list active vms
|
||||
if service.type == "ABS"
|
||||
# this is actually job_ids
|
||||
running_vms = service.list_active_job_ids(verbose, service.url, service.user)
|
||||
else
|
||||
running_vms = service.list_active(verbose)
|
||||
end
|
||||
running_vms = if service.type == 'ABS'
|
||||
# this is actually job_ids
|
||||
service.list_active_job_ids(verbose, service.url, service.user)
|
||||
else
|
||||
service.list_active(verbose)
|
||||
end
|
||||
host = URI.parse(service.url).host
|
||||
if running_vms.empty?
|
||||
if options.json
|
||||
|
|
@ -122,17 +119,15 @@ class Vmfloaty
|
|||
else
|
||||
FloatyLogger.info "You have no running VMs on #{host}"
|
||||
end
|
||||
else
|
||||
if options.json
|
||||
puts Utils.get_host_data(verbose, service, running_vms).to_json
|
||||
elsif options.hostnameonly
|
||||
Utils.get_host_data(verbose, service, running_vms).each do |hostname, host_data|
|
||||
Utils.print_fqdn_for_host(service, hostname, host_data)
|
||||
end
|
||||
else
|
||||
puts "Your VMs on #{host}:"
|
||||
Utils.pretty_print_hosts(verbose, service, running_vms)
|
||||
elsif options.json
|
||||
puts Utils.get_host_data(verbose, service, running_vms).to_json
|
||||
elsif options.hostnameonly
|
||||
Utils.get_host_data(verbose, service, running_vms).each do |hostname, host_data|
|
||||
Utils.print_fqdn_for_host(service, hostname, host_data)
|
||||
end
|
||||
else
|
||||
puts "Your VMs on #{host}:"
|
||||
Utils.pretty_print_hosts(verbose, service, running_vms)
|
||||
end
|
||||
else
|
||||
# list available vms from pooler
|
||||
|
|
@ -164,7 +159,8 @@ class Vmfloaty
|
|||
c.syntax = 'floaty modify hostname [options]'
|
||||
c.summary = 'Modify a VM\'s tags, time to live, disk space, or reservation reason'
|
||||
c.description = 'This command makes modifications to the virtual machines state in the pooler service. You can either append tags to the vm, increase how long it stays active for, or increase the amount of disk space.'
|
||||
c.example 'Modifies myhost1 to have a TTL of 12 hours and adds a custom tag', 'floaty modify myhost1 --lifetime 12 --url https://myurl --token mytokenstring --tags \'{"tag":"myvalue"}\''
|
||||
c.example 'Modifies myhost1 to have a TTL of 12 hours and adds a custom tag',
|
||||
'floaty modify myhost1 --lifetime 12 --url https://myurl --token mytokenstring --tags \'{"tag":"myvalue"}\''
|
||||
c.option '--verbose', 'Enables verbose output'
|
||||
c.option '--service STRING', String, 'Configured pooler service name'
|
||||
c.option '--url STRING', String, 'URL of pooler service'
|
||||
|
|
@ -185,18 +181,18 @@ class Vmfloaty
|
|||
exit 1
|
||||
end
|
||||
running_vms =
|
||||
if modify_all
|
||||
service.list_active(verbose)
|
||||
else
|
||||
hostname.split(',')
|
||||
end
|
||||
if modify_all
|
||||
service.list_active(verbose)
|
||||
else
|
||||
hostname.split(',')
|
||||
end
|
||||
|
||||
tags = options.tags ? JSON.parse(options.tags) : nil
|
||||
modify_hash = {
|
||||
:lifetime => options.lifetime,
|
||||
:disk => options.disk,
|
||||
:tags => tags,
|
||||
:reason => options.reason,
|
||||
lifetime: options.lifetime,
|
||||
disk: options.disk,
|
||||
tags: tags,
|
||||
reason: options.reason
|
||||
}
|
||||
modify_hash.delete_if { |_, value| value.nil? }
|
||||
|
||||
|
|
@ -204,12 +200,10 @@ class Vmfloaty
|
|||
ok = true
|
||||
modified_hash = {}
|
||||
running_vms.each do |vm|
|
||||
begin
|
||||
modified_hash[vm] = service.modify(verbose, vm, modify_hash)
|
||||
rescue ModifyError => e
|
||||
FloatyLogger.error e
|
||||
ok = false
|
||||
end
|
||||
modified_hash[vm] = service.modify(verbose, vm, modify_hash)
|
||||
rescue ModifyError => e
|
||||
FloatyLogger.error e
|
||||
ok = false
|
||||
end
|
||||
if ok
|
||||
if modify_all
|
||||
|
|
@ -241,9 +235,7 @@ class Vmfloaty
|
|||
c.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
|
||||
c.action do |args, options|
|
||||
verbose = options.verbose || config['verbose']
|
||||
if options.loglevel
|
||||
FloatyLogger.setlevel = options.loglevel
|
||||
end
|
||||
FloatyLogger.setlevel = options.loglevel if options.loglevel
|
||||
|
||||
service = Service.new(options, config)
|
||||
hostnames = args[0]
|
||||
|
|
@ -254,17 +246,17 @@ class Vmfloaty
|
|||
successes = []
|
||||
|
||||
if delete_all
|
||||
if service.type == "ABS"
|
||||
# this is actually job_ids
|
||||
running_vms = service.list_active_job_ids(verbose, service.url, service.user)
|
||||
else
|
||||
running_vms = service.list_active(verbose)
|
||||
end
|
||||
running_vms = if service.type == 'ABS'
|
||||
# this is actually job_ids
|
||||
service.list_active_job_ids(verbose, service.url, service.user)
|
||||
else
|
||||
service.list_active(verbose)
|
||||
end
|
||||
if running_vms.empty?
|
||||
if options.json
|
||||
puts {}.to_json
|
||||
else
|
||||
FloatyLogger.info "You have no running VMs."
|
||||
FloatyLogger.info 'You have no running VMs.'
|
||||
end
|
||||
else
|
||||
confirmed = true
|
||||
|
|
@ -328,7 +320,8 @@ class Vmfloaty
|
|||
c.syntax = 'floaty snapshot hostname [options]'
|
||||
c.summary = 'Takes a snapshot of a given vm'
|
||||
c.description = 'Will request a snapshot be taken of the given hostname in the pooler service. This command is known to take a while depending on how much load is on the pooler service.'
|
||||
c.example 'Takes a snapshot for a given host', 'floaty snapshot myvm.example.com --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
|
||||
c.example 'Takes a snapshot for a given host',
|
||||
'floaty snapshot myvm.example.com --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
|
||||
c.option '--verbose', 'Enables verbose output'
|
||||
c.option '--service STRING', String, 'Configured pooler service name'
|
||||
c.option '--url STRING', String, 'URL of pooler service'
|
||||
|
|
@ -354,7 +347,8 @@ class Vmfloaty
|
|||
c.syntax = 'floaty revert hostname snapshot [options]'
|
||||
c.summary = 'Reverts a vm to a specified snapshot'
|
||||
c.description = 'Given a snapshot SHA, vmfloaty will request a revert to the pooler service to go back to a previous snapshot.'
|
||||
c.example 'Reverts to a snapshot for a given host', 'floaty revert myvm.example.com n4eb4kdtp7rwv4x158366vd9jhac8btq --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
|
||||
c.example 'Reverts to a snapshot for a given host',
|
||||
'floaty revert myvm.example.com n4eb4kdtp7rwv4x158366vd9jhac8btq --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
|
||||
c.option '--verbose', 'Enables verbose output'
|
||||
c.option '--service STRING', String, 'Configured pooler service name'
|
||||
c.option '--url STRING', String, 'URL of pooler service'
|
||||
|
|
@ -366,7 +360,9 @@ class Vmfloaty
|
|||
hostname = args[0]
|
||||
snapshot_sha = args[1] || options.snapshot
|
||||
|
||||
FloatyLogger.info "Two snapshot arguments were given....using snapshot #{snapshot_sha}" if args[1] && options.snapshot
|
||||
if args[1] && options.snapshot
|
||||
FloatyLogger.info "Two snapshot arguments were given....using snapshot #{snapshot_sha}"
|
||||
end
|
||||
|
||||
begin
|
||||
revert_req = service.revert(verbose, hostname, snapshot_sha)
|
||||
|
|
@ -391,9 +387,7 @@ class Vmfloaty
|
|||
c.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
|
||||
c.action do |_, options|
|
||||
verbose = options.verbose || config['verbose']
|
||||
if options.loglevel
|
||||
FloatyLogger.setlevel = options.loglevel
|
||||
end
|
||||
FloatyLogger.setlevel = options.loglevel if options.loglevel
|
||||
service = Service.new(options, config)
|
||||
if options.json
|
||||
pp service.status(verbose)
|
||||
|
|
@ -527,7 +521,7 @@ class Vmfloaty
|
|||
c.example 'Print a list of the valid service types', 'floaty service types'
|
||||
c.example 'Print a sample config file with multiple services', 'floaty service examples'
|
||||
c.example 'list vms from the service named "nspooler-prod"', 'floaty list --service nspooler-prod'
|
||||
c.action do |args, options|
|
||||
c.action do |args, _options|
|
||||
action = args.first
|
||||
|
||||
example_config = Utils.strip_heredoc(<<-CONFIG)
|
||||
|
|
|
|||
|
|
@ -53,10 +53,10 @@ class ABS
|
|||
def self.list_active(verbose, url, _token, user)
|
||||
hosts = []
|
||||
get_active_requests(verbose, url, user).each do |req_hash|
|
||||
if req_hash.key?('allocated_resources')
|
||||
req_hash['allocated_resources'].each do |onehost|
|
||||
hosts.push(onehost['hostname'])
|
||||
end
|
||||
next unless req_hash.key?('allocated_resources')
|
||||
|
||||
req_hash['allocated_resources'].each do |onehost|
|
||||
hosts.push(onehost['hostname'])
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -116,7 +116,7 @@ class ABS
|
|||
ret_status = {}
|
||||
hosts.each do |host|
|
||||
ret_status[host] = {
|
||||
'ok' => false,
|
||||
'ok' => false
|
||||
}
|
||||
end
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ class ABS
|
|||
if hosts.include? vm_name['hostname']
|
||||
if all_job_resources_accounted_for(req_hash['allocated_resources'], hosts)
|
||||
ret_status[vm_name['hostname']] = {
|
||||
'ok' => true,
|
||||
'ok' => true
|
||||
}
|
||||
jobs_to_delete.push(req_hash)
|
||||
else
|
||||
|
|
@ -147,7 +147,7 @@ class ABS
|
|||
jobs_to_delete.each do |job|
|
||||
req_obj = {
|
||||
'job_id' => job['request']['job']['id'],
|
||||
'hosts' => job['allocated_resources'],
|
||||
'hosts' => job['allocated_resources']
|
||||
}
|
||||
|
||||
FloatyLogger.info "Deleting #{req_obj}" if verbose
|
||||
|
|
@ -172,11 +172,11 @@ class ABS
|
|||
res_body = JSON.parse(res.body)
|
||||
if res_body.key?('vmpooler_platforms')
|
||||
os_list << '*** VMPOOLER Pools ***'
|
||||
if res_body['vmpooler_platforms'].is_a?(String)
|
||||
os_list += JSON.parse(res_body['vmpooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
|
||||
else
|
||||
os_list += res_body['vmpooler_platforms']
|
||||
end
|
||||
os_list += if res_body['vmpooler_platforms'].is_a?(String)
|
||||
JSON.parse(res_body['vmpooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
|
||||
else
|
||||
res_body['vmpooler_platforms']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -200,11 +200,11 @@ class ABS
|
|||
if res_body.key?('nspooler_platforms')
|
||||
os_list << ''
|
||||
os_list << '*** NSPOOLER Pools ***'
|
||||
if res_body['nspooler_platforms'].is_a?(String)
|
||||
os_list += JSON.parse(res_body['nspooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
|
||||
else
|
||||
os_list += res_body['nspooler_platforms']
|
||||
end
|
||||
os_list += if res_body['nspooler_platforms'].is_a?(String)
|
||||
JSON.parse(res_body['nspooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
|
||||
else
|
||||
res_body['nspooler_platforms']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -214,11 +214,11 @@ class ABS
|
|||
if res_body.key?('aws_platforms')
|
||||
os_list << ''
|
||||
os_list << '*** AWS Pools ***'
|
||||
if res_body['aws_platforms'].is_a?(String)
|
||||
os_list += JSON.parse(res_body['aws_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
|
||||
else
|
||||
os_list += res_body['aws_platforms']
|
||||
end
|
||||
os_list += if res_body['aws_platforms'].is_a?(String)
|
||||
JSON.parse(res_body['aws_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
|
||||
else
|
||||
res_body['aws_platforms']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -248,20 +248,20 @@ class ABS
|
|||
conn = Http.get_conn(verbose, supported_abs_url(url))
|
||||
conn.headers['X-AUTH-TOKEN'] = token if token
|
||||
|
||||
if continue.nil?
|
||||
saved_job_id = user + "-" + DateTime.now.strftime('%Q')
|
||||
else
|
||||
saved_job_id = continue
|
||||
end
|
||||
saved_job_id = if continue.nil?
|
||||
"#{user}-#{DateTime.now.strftime('%Q')}"
|
||||
else
|
||||
continue
|
||||
end
|
||||
|
||||
req_obj = {
|
||||
:resources => os_types,
|
||||
:job => {
|
||||
:id => saved_job_id,
|
||||
:tags => {
|
||||
:user => user,
|
||||
},
|
||||
},
|
||||
resources: os_types,
|
||||
job: {
|
||||
id: saved_job_id,
|
||||
tags: {
|
||||
user: user
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if config['vmpooler_fallback'] # optional and not available as cli flag
|
||||
|
|
@ -271,11 +271,12 @@ class ABS
|
|||
end
|
||||
|
||||
if config['priority']
|
||||
req_obj[:priority] = if config['priority'] == 'high'
|
||||
req_obj[:priority] = case config['priority']
|
||||
when 'high'
|
||||
1
|
||||
elsif config['priority'] == 'medium'
|
||||
when 'medium'
|
||||
2
|
||||
elsif config['priority'] == 'low'
|
||||
when 'low'
|
||||
3
|
||||
else
|
||||
config['priority'].to_i
|
||||
|
|
@ -291,14 +292,12 @@ class ABS
|
|||
|
||||
retries = 360
|
||||
|
||||
status = validate_queue_status_response(res.status, res.body, "Initial request", verbose)
|
||||
status = validate_queue_status_response(res.status, res.body, 'Initial request', verbose)
|
||||
|
||||
begin
|
||||
(1..retries).each do |i|
|
||||
res_body = check_queue(conn, saved_job_id, req_obj, verbose)
|
||||
if res_body && res_body.is_a?(Array) # when we get a response with hostnames
|
||||
return translated(res_body, saved_job_id)
|
||||
end
|
||||
return translated(res_body, saved_job_id) if res_body.is_a?(Array) # when we get a response with hostnames
|
||||
|
||||
sleep_seconds = 10 if i >= 10
|
||||
sleep_seconds = i if i < 10
|
||||
|
|
@ -317,10 +316,10 @@ class ABS
|
|||
# We should fix the ABS API to be more like the vmpooler or nspooler api, but for now
|
||||
#
|
||||
def self.translated(res_body, job_id)
|
||||
vmpooler_formatted_body = {'job_id' => job_id}
|
||||
vmpooler_formatted_body = { 'job_id' => job_id }
|
||||
|
||||
res_body.each do |host|
|
||||
if vmpooler_formatted_body[host['type']] && vmpooler_formatted_body[host['type']]['hostname'].class == Array
|
||||
if vmpooler_formatted_body[host['type']] && vmpooler_formatted_body[host['type']]['hostname'].instance_of?(Array)
|
||||
vmpooler_formatted_body[host['type']]['hostname'] << host['hostname']
|
||||
else
|
||||
vmpooler_formatted_body[host['type']] = { 'hostname' => [host['hostname']] }
|
||||
|
|
@ -331,9 +330,9 @@ class ABS
|
|||
vmpooler_formatted_body
|
||||
end
|
||||
|
||||
def self.check_queue(conn, job_id, req_obj, verbose)
|
||||
def self.check_queue(conn, _job_id, req_obj, verbose)
|
||||
res = conn.post 'request', req_obj.to_json
|
||||
status = validate_queue_status_response(res.status, res.body, "Check queue request", verbose)
|
||||
status = validate_queue_status_response(res.status, res.body, 'Check queue request', verbose)
|
||||
unless res.body.empty? || !valid_json?(res.body)
|
||||
res_body = JSON.parse(res.body)
|
||||
return res_body
|
||||
|
|
@ -353,7 +352,7 @@ class ABS
|
|||
res.body == 'OK'
|
||||
end
|
||||
|
||||
def self.summary(verbose, url)
|
||||
def self.summary(_verbose, _url)
|
||||
raise NoMethodError, 'summary is not defined for ABS'
|
||||
end
|
||||
|
||||
|
|
@ -405,17 +404,17 @@ class ABS
|
|||
|
||||
def self.valid_json?(json)
|
||||
JSON.parse(json)
|
||||
return true
|
||||
true
|
||||
rescue TypeError, JSON::ParserError => e
|
||||
return false
|
||||
false
|
||||
end
|
||||
|
||||
# when missing, adds the required api/v2 in the url
|
||||
def self.supported_abs_url(url)
|
||||
expected_ending = "api/v2"
|
||||
if !url.include?(expected_ending)
|
||||
expected_ending = 'api/v2'
|
||||
unless url.include?(expected_ending)
|
||||
# add a slash if missing
|
||||
expected_ending = "/#{expected_ending}" if url[-1] != "/"
|
||||
expected_ending = "/#{expected_ending}" if url[-1] != '/'
|
||||
url = "#{url}#{expected_ending}"
|
||||
end
|
||||
url
|
||||
|
|
|
|||
|
|
@ -21,13 +21,11 @@ class Http
|
|||
|
||||
url = "https://#{url}" unless url?(url)
|
||||
|
||||
conn = Faraday.new(:url => url, :ssl => { :verify => false }) do |faraday|
|
||||
Faraday.new(url: url, ssl: { verify: false }) do |faraday|
|
||||
faraday.request :url_encoded
|
||||
faraday.response :logger if verbose
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
conn
|
||||
end
|
||||
|
||||
def self.get_conn_with_auth(verbose, url, user, password)
|
||||
|
|
@ -37,13 +35,11 @@ class Http
|
|||
|
||||
url = "https://#{url}" unless url?(url)
|
||||
|
||||
conn = Faraday.new(:url => url, :ssl => { :verify => false }) do |faraday|
|
||||
Faraday.new(url: url, ssl: { verify: false }) do |faraday|
|
||||
faraday.request :url_encoded
|
||||
faraday.request :basic_auth, user, password
|
||||
faraday.response :logger if verbose
|
||||
faraday.adapter Faraday.default_adapter
|
||||
end
|
||||
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'logger'
|
||||
|
||||
class FloatyLogger < ::Logger
|
||||
|
|
@ -19,22 +21,23 @@ class FloatyLogger < ::Logger
|
|||
|
||||
def self.setlevel=(level)
|
||||
level = level.downcase
|
||||
if level == "debug"
|
||||
self.logger.level = ::Logger::DEBUG
|
||||
elsif level == "info"
|
||||
self.logger.level = ::Logger::INFO
|
||||
elsif level == "error"
|
||||
self.logger.level = ::Logger::ERROR
|
||||
case level
|
||||
when 'debug'
|
||||
logger.level = ::Logger::DEBUG
|
||||
when 'info'
|
||||
logger.level = ::Logger::INFO
|
||||
when 'error'
|
||||
logger.level = ::Logger::ERROR
|
||||
else
|
||||
error("set loglevel to debug, info or error")
|
||||
error('set loglevel to debug, info or error')
|
||||
end
|
||||
end
|
||||
|
||||
def initialize
|
||||
super(STDERR)
|
||||
super($stderr)
|
||||
self.level = ::Logger::INFO
|
||||
self.formatter = proc do |severity, datetime, progname, msg|
|
||||
"#{msg}\n"
|
||||
self.formatter = proc do |_severity, _datetime, _progname, msg|
|
||||
"#{msg}\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ class NonstandardPooler
|
|||
status['reserved_hosts'] || []
|
||||
end
|
||||
|
||||
def self.retrieve(verbose, os_type, token, url, _user, _options, ondemand = nil, _continue = nil)
|
||||
def self.retrieve(verbose, os_type, token, url, _user, _options, _ondemand = nil, _continue = nil)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
conn.headers['X-AUTH-TOKEN'] = token if token
|
||||
|
||||
|
|
@ -46,7 +46,8 @@ class NonstandardPooler
|
|||
raise TokenError, 'Token provided was nil; Request cannot be made to modify VM' if token.nil?
|
||||
|
||||
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
|
||||
raise ModifyError, "Configured service type does not support modification of #{key}" unless %i[reason
|
||||
reserved_for_reason].include? key
|
||||
end
|
||||
|
||||
if modify_hash[:reason]
|
||||
|
|
|
|||
|
|
@ -12,13 +12,11 @@ class Pooler
|
|||
response = conn.get 'vm'
|
||||
response_body = JSON.parse(response.body)
|
||||
|
||||
hosts = if os_filter
|
||||
response_body.select { |i| i[/#{os_filter}/] }
|
||||
else
|
||||
response_body
|
||||
end
|
||||
|
||||
hosts
|
||||
if os_filter
|
||||
response_body.select { |i| i[/#{os_filter}/] }
|
||||
else
|
||||
response_body
|
||||
end
|
||||
end
|
||||
|
||||
def self.list_active(verbose, url, token, _user)
|
||||
|
|
@ -50,7 +48,10 @@ class Pooler
|
|||
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}" unless ondemand
|
||||
unless ondemand
|
||||
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
|
||||
end
|
||||
|
||||
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/ondemandvm/#{os_string}. #{res_body}"
|
||||
end
|
||||
end
|
||||
|
|
@ -63,7 +64,7 @@ class Pooler
|
|||
FloatyLogger.info "waiting for request #{request_id} to be fulfilled"
|
||||
sleep 5
|
||||
end
|
||||
FloatyLogger.info "The request has been fulfilled"
|
||||
FloatyLogger.info 'The request has been fulfilled'
|
||||
check_ondemandvm(verbose, request_id, url)
|
||||
end
|
||||
|
||||
|
|
@ -84,8 +85,9 @@ class Pooler
|
|||
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?
|
||||
|
||||
modify_hash.keys.each do |key|
|
||||
raise ModifyError, "Configured service type does not support modification of #{key}." unless %i[tags lifetime disk].include? key
|
||||
modify_hash.each_key do |key|
|
||||
raise ModifyError, "Configured service type does not support modification of #{key}." unless %i[tags lifetime
|
||||
disk].include? key
|
||||
end
|
||||
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
|
@ -120,8 +122,7 @@ class Pooler
|
|||
|
||||
response = conn.post "vm/#{hostname}/disk/#{disk}"
|
||||
|
||||
res_body = JSON.parse(response.body)
|
||||
res_body
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
def self.delete(verbose, url, hosts, token, _user)
|
||||
|
|
@ -146,25 +147,21 @@ class Pooler
|
|||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
response = conn.get '/status'
|
||||
res_body = JSON.parse(response.body)
|
||||
res_body
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
def self.summary(verbose, url)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
response = conn.get '/summary'
|
||||
res_body = JSON.parse(response.body)
|
||||
res_body
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
def self.query(verbose, url, hostname)
|
||||
conn = Http.get_conn(verbose, url)
|
||||
|
||||
response = conn.get "vm/#{hostname}"
|
||||
res_body = JSON.parse(response.body)
|
||||
|
||||
res_body
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
def self.snapshot(verbose, url, hostname, token)
|
||||
|
|
@ -174,8 +171,7 @@ class Pooler
|
|||
conn.headers['X-AUTH-TOKEN'] = token
|
||||
|
||||
response = conn.post "vm/#{hostname}/snapshot"
|
||||
res_body = JSON.parse(response.body)
|
||||
res_body
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
def self.revert(verbose, url, hostname, token, snapshot_sha)
|
||||
|
|
@ -187,7 +183,6 @@ class Pooler
|
|||
raise "Snapshot SHA provided was nil, could not revert #{hostname}" if snapshot_sha.nil?
|
||||
|
||||
response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}"
|
||||
res_body = JSON.parse(response.body)
|
||||
res_body
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class Service
|
|||
def user
|
||||
unless @config['user']
|
||||
FloatyLogger.info "Enter your #{@config['url']} service username:"
|
||||
@config['user'] = STDIN.gets.chomp
|
||||
@config['user'] = $stdin.gets.chomp
|
||||
end
|
||||
@config['user']
|
||||
end
|
||||
|
|
@ -140,8 +140,8 @@ class Service
|
|||
# some methods do not exist for ABS, and if possible should target the Pooler service
|
||||
def maybe_use_vmpooler
|
||||
if @service_object == ABS # this is not an instance
|
||||
if !self.silent
|
||||
FloatyLogger.info "The service in use is ABS, but the requested method should run against vmpooler directly, using fallback_vmpooler config from ~/.vmfloaty.yml"
|
||||
unless silent
|
||||
FloatyLogger.info 'The service in use is ABS, but the requested method should run against vmpooler directly, using fallback_vmpooler config from ~/.vmfloaty.yml'
|
||||
self.silent = true
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,10 @@ class Utils
|
|||
# "engine"=>"vmpooler"
|
||||
# }
|
||||
|
||||
raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" unless response_body.delete('ok')
|
||||
unless response_body.delete('ok')
|
||||
raise ArgumentError,
|
||||
"Bad GET response passed to format_hosts: #{response_body.to_json}"
|
||||
end
|
||||
|
||||
# vmpooler reports the domain separately from the hostname
|
||||
domain = response_body.delete('domain')
|
||||
|
|
@ -50,7 +53,7 @@ class Utils
|
|||
abs_job_id = response_body.delete('job_id')
|
||||
result['job_id'] = abs_job_id unless abs_job_id.nil?
|
||||
|
||||
filtered_response_body = response_body.reject { |key, _| key == 'request_id' || key == 'ready' }
|
||||
filtered_response_body = response_body.reject { |key, _| %w[request_id ready].include?(key) }
|
||||
filtered_response_body.each do |os, value|
|
||||
hostnames = Array(value['hostname'])
|
||||
hostnames.map! { |host| "#{host}.#{domain}" } if domain
|
||||
|
|
@ -106,7 +109,7 @@ class Utils
|
|||
def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr = false, indent = 0)
|
||||
output_target = print_to_stderr ? $stderr : $stdout
|
||||
|
||||
fetched_data = self.get_host_data(verbose, service, hostnames)
|
||||
fetched_data = get_host_data(verbose, service, hostnames)
|
||||
fetched_data.each do |hostname, host_data|
|
||||
case service.type
|
||||
when 'ABS'
|
||||
|
|
@ -116,13 +119,14 @@ class Utils
|
|||
|
||||
output_target.puts "- [JobID:#{host_data['request']['job']['id']}] <#{host_data['state']}>"
|
||||
host_data['allocated_resources'].each do |allocated_resources, _i|
|
||||
if (allocated_resources['engine'] == "vmpooler" || allocated_resources['engine'] == 'ondemand') && service.config["vmpooler_fallback"]
|
||||
if (allocated_resources['engine'] == 'vmpooler' || allocated_resources['engine'] == 'ondemand') && service.config['vmpooler_fallback']
|
||||
vmpooler_service = service.clone
|
||||
vmpooler_service.silent = true
|
||||
vmpooler_service.maybe_use_vmpooler
|
||||
self.pretty_print_hosts(verbose, vmpooler_service, allocated_resources['hostname'].split('.')[0], print_to_stderr, indent+2)
|
||||
pretty_print_hosts(verbose, vmpooler_service, allocated_resources['hostname'].split('.')[0],
|
||||
print_to_stderr, indent + 2)
|
||||
else
|
||||
#TODO we could add more specific metadata for the other services, nspooler and aws
|
||||
# TODO: we could add more specific metadata for the other services, nspooler and aws
|
||||
output_target.puts " - #{allocated_resources['hostname']} (#{allocated_resources['type']})"
|
||||
end
|
||||
end
|
||||
|
|
@ -132,7 +136,7 @@ class Utils
|
|||
duration = "#{host_data['running']}/#{host_data['lifetime']} hours"
|
||||
metadata = [host_data['state'], host_data['template'], duration, *tag_pairs]
|
||||
message = "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
|
||||
if host_data['state'] && host_data['state'] == "destroyed"
|
||||
if host_data['state'] && host_data['state'] == 'destroyed'
|
||||
output_target.puts message.colorize(:red)
|
||||
else
|
||||
output_target.puts message
|
||||
|
|
@ -153,30 +157,26 @@ class Utils
|
|||
result = {}
|
||||
hostnames = [hostnames] unless hostnames.is_a? Array
|
||||
hostnames.each do |hostname|
|
||||
begin
|
||||
response = service.query(verbose, hostname)
|
||||
host_data = response[hostname]
|
||||
if block_given?
|
||||
yield host_data result
|
||||
response = service.query(verbose, hostname)
|
||||
host_data = response[hostname]
|
||||
if block_given?
|
||||
yield host_data result
|
||||
else
|
||||
case service.type
|
||||
when 'ABS'
|
||||
# For ABS, 'hostname' variable is the jobID
|
||||
result[hostname] = host_data if host_data['state'] == 'allocated' || host_data['state'] == 'filled'
|
||||
when 'Pooler'
|
||||
result[hostname] = host_data
|
||||
when 'NonstandardPooler'
|
||||
result[hostname] = host_data
|
||||
else
|
||||
case service.type
|
||||
when 'ABS'
|
||||
# For ABS, 'hostname' variable is the jobID
|
||||
if host_data['state'] == 'allocated' || host_data['state'] == 'filled'
|
||||
result[hostname] = host_data
|
||||
end
|
||||
when 'Pooler'
|
||||
result[hostname] = host_data
|
||||
when 'NonstandardPooler'
|
||||
result[hostname] = host_data
|
||||
else
|
||||
raise "Invalid service type #{service.type}"
|
||||
end
|
||||
raise "Invalid service type #{service.type}"
|
||||
end
|
||||
rescue StandardError => e
|
||||
FloatyLogger.error("Something went wrong while trying to gather information on #{hostname}:")
|
||||
FloatyLogger.error(e)
|
||||
end
|
||||
rescue StandardError => e
|
||||
FloatyLogger.error("Something went wrong while trying to gather information on #{hostname}:")
|
||||
FloatyLogger.error(e)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
|
@ -192,16 +192,14 @@ class Utils
|
|||
|
||||
width = pools.keys.map(&:length).max
|
||||
pools.each do |name, pool|
|
||||
begin
|
||||
max = pool['max']
|
||||
ready = pool['ready']
|
||||
pending = pool['pending']
|
||||
missing = max - ready - pending
|
||||
char = 'o'
|
||||
puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
|
||||
rescue StandardError => e
|
||||
FloatyLogger.error "#{name.ljust(width)} #{e.red}"
|
||||
end
|
||||
max = pool['max']
|
||||
ready = pool['ready']
|
||||
pending = pool['pending']
|
||||
missing = max - ready - pending
|
||||
char = 'o'
|
||||
puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
|
||||
rescue StandardError => e
|
||||
FloatyLogger.error "#{name.ljust(width)} #{e.red}"
|
||||
end
|
||||
puts message.colorize(status_response['status']['ok'] ? :default : :red)
|
||||
when 'NonstandardPooler'
|
||||
|
|
@ -211,16 +209,14 @@ class Utils
|
|||
|
||||
width = pools.keys.map(&:length).max
|
||||
pools.each do |name, pool|
|
||||
begin
|
||||
max = pool['total_hosts']
|
||||
ready = pool['available_hosts']
|
||||
pending = pool['pending'] || 0 # not available for nspooler
|
||||
missing = max - ready - pending
|
||||
char = 'o'
|
||||
puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
|
||||
rescue StandardError => e
|
||||
FloatyLogger.error "#{name.ljust(width)} #{e.red}"
|
||||
end
|
||||
max = pool['total_hosts']
|
||||
ready = pool['available_hosts']
|
||||
pending = pool['pending'] || 0 # not available for nspooler
|
||||
missing = max - ready - pending
|
||||
char = 'o'
|
||||
puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
|
||||
rescue StandardError => e
|
||||
FloatyLogger.error "#{name.ljust(width)} #{e.red}"
|
||||
end
|
||||
when 'ABS'
|
||||
FloatyLogger.error 'ABS Not OK' unless status_response
|
||||
|
|
@ -256,11 +252,11 @@ class Utils
|
|||
def self.get_service_config(config, options)
|
||||
# The top-level url, user, and token values in the config file are treated as defaults
|
||||
service_config = {
|
||||
'url' => config['url'],
|
||||
'user' => config['user'],
|
||||
'url' => config['url'],
|
||||
'user' => config['user'],
|
||||
'token' => config['token'],
|
||||
'vmpooler_fallback' => config['vmpooler_fallback'],
|
||||
'type' => config['type'] || 'vmpooler',
|
||||
'type' => config['type'] || 'vmpooler'
|
||||
}
|
||||
|
||||
if config['services']
|
||||
|
|
@ -271,7 +267,10 @@ class Utils
|
|||
service_config.merge! values
|
||||
else
|
||||
# If the user provided a service name at the command line, use that service if posible, or fail
|
||||
raise ArgumentError, "Could not find a configured service named '#{options.service}' in ~/.vmfloaty.yml" unless config['services'][options.service]
|
||||
unless config['services'][options.service]
|
||||
raise ArgumentError,
|
||||
"Could not find a configured service named '#{options.service}' in ~/.vmfloaty.yml"
|
||||
end
|
||||
|
||||
# If the service is configured but some values are missing, use the top-level defaults to fill them in
|
||||
service_config.merge! config['services'][options.service]
|
||||
|
|
@ -295,22 +294,22 @@ class Utils
|
|||
config = Conf.read_config
|
||||
# The top-level url, user, and token values in the config file are treated as defaults
|
||||
service_config = {
|
||||
'url' => config['url'],
|
||||
'user' => config['user'],
|
||||
'token' => config['token'],
|
||||
'type' => 'vmpooler',
|
||||
'url' => config['url'],
|
||||
'user' => config['user'],
|
||||
'token' => config['token'],
|
||||
'type' => 'vmpooler'
|
||||
}
|
||||
|
||||
# at a minimum, the url needs to be configured
|
||||
if config['services'] && config['services'][vmpooler_fallback] && config['services'][vmpooler_fallback]['url']
|
||||
# If the service is configured but some values are missing, use the top-level defaults to fill them in
|
||||
service_config.merge! config['services'][vmpooler_fallback]
|
||||
elsif vmpooler_fallback.nil?
|
||||
raise ArgumentError,
|
||||
"The abs service should have a key named 'vmpooler_fallback' in ~/.vmfloaty.yml with a value that points to a vmpooler service name use this format:\nservices:\n myabs:\n url: 'http://abs.com'\n user: 'superman'\n token: 'kryptonite'\n vmpooler_fallback: 'myvmpooler'\n myvmpooler:\n url: 'http://vmpooler.com'\n user: 'superman'\n token: 'kryptonite'"
|
||||
else
|
||||
if vmpooler_fallback.nil?
|
||||
raise ArgumentError, "The abs service should have a key named 'vmpooler_fallback' in ~/.vmfloaty.yml with a value that points to a vmpooler service name use this format:\nservices:\n myabs:\n url: 'http://abs.com'\n user: 'superman'\n token: 'kryptonite'\n vmpooler_fallback: 'myvmpooler'\n myvmpooler:\n url: 'http://vmpooler.com'\n user: 'superman'\n token: 'kryptonite'"
|
||||
else
|
||||
raise ArgumentError, "Could not find a configured service named '#{vmpooler_fallback}' in ~/.vmfloaty.yml use this format:\nservices:\n #{vmpooler_fallback}:\n url: 'http://vmpooler.com'\n user: 'superman'\n token: 'kryptonite'"
|
||||
end
|
||||
raise ArgumentError,
|
||||
"Could not find a configured service named '#{vmpooler_fallback}' in ~/.vmfloaty.yml use this format:\nservices:\n #{vmpooler_fallback}:\n url: 'http://vmpooler.com'\n user: 'superman'\n token: 'kryptonite'"
|
||||
end
|
||||
|
||||
service_config
|
||||
|
|
|
|||
|
|
@ -3,4 +3,3 @@
|
|||
class Vmfloaty
|
||||
VERSION = '1.2.0'
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue