Allow configuration of multiple services

This commit is contained in:
Casey Williams 2017-09-17 20:23:11 -07:00
parent ed82c44af5
commit e78bcc6216
4 changed files with 206 additions and 64 deletions

View file

@ -17,26 +17,28 @@ class Vmfloaty
def run
program :version, Vmfloaty::VERSION
program :description, 'A CLI helper tool for Puppet Labs vmpooler to help you stay afloat'
program :description, 'A CLI helper tool for Puppet Labs VM poolers to help you stay afloat'
config = Conf.read_config
command :get do |c|
c.syntax = 'floaty get os_type0 os_type1=x ox_type2=y [options]'
c.summary = 'Gets a vm or vms based on the os argument'
c.description = 'A command to retrieve vms from vmpooler. Can either be a single vm, or multiple with the `=` syntax.'
c.description = 'A command to retrieve vms from a pooler service. Can either be a single vm, or multiple with the `=` syntax.'
c.example 'Gets a few vms', 'floaty get centos=3 debian --user brian --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--user STRING', String, 'User to authenticate with'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--notoken', 'Makes a request without a token'
c.option '--force', 'Forces vmfloaty to get requested vms'
c.action do |args, options|
verbose = options.verbose || config['verbose']
token = options.token || config['token']
user = options.user ||= config['user']
url = options.url ||= config['url']
service_config = Utils.get_service_from_config(config, options.service)
token = options.token || service_config['token'] || config['token']
user = options.user ||= service_config['user'] || config['user']
url = options.url ||= service_config['url'] || config['url']
no_token = options.notoken
force = options.force
@ -72,10 +74,10 @@ class Vmfloaty
unless token
puts "No token found. Retrieving a token..."
if !user
STDERR.puts "You did not provide a user to authenticate to vmpooler with"
STDERR.puts "You did not provide a user to authenticate to the pooler service with"
exit 1
end
pass = password "Enter your vmpooler password please:", '*'
pass = password "Enter your pooler service password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue TokenError => e
@ -109,17 +111,19 @@ class Vmfloaty
command :list do |c|
c.syntax = 'floaty list [options]'
c.summary = 'Shows a list of available vms from the pooler or vms obtained with a token'
c.description = 'List will either show all vm templates available in vmpooler, or with the --active flag it will list vms obtained with a vmpooler token.'
c.description = 'List will either show all vm templates available in pooler service, or with the --active flag it will list vms obtained with a pooler service token.'
c.example 'Filter the list on centos', 'floaty list centos --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--active', 'Prints information about active vms for a given token'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
filter = args[0]
url = options.url ||= config['url']
token = options.token || config['token']
url = options.url ||= service_config['url'] ||=config['url']
token = options.token || service_config['token'] || config['token']
active = options.active
if active
@ -148,13 +152,15 @@ class Vmfloaty
command :query do |c|
c.syntax = 'floaty query hostname [options]'
c.summary = 'Get information about a given vm'
c.description = 'Given a hostname from vmpooler, vmfloaty with query vmpooler to get various details about the vm.'
c.description = 'Given a hostname from the pooler service, vmfloaty with query the service to get various details about the vm.'
c.example 'Get information about a sample host', 'floaty query hostname --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
hostname = args[0]
query_req = Pooler.query(verbose, url, hostname)
@ -165,23 +171,25 @@ class Vmfloaty
command :modify do |c|
c.syntax = 'floaty modify hostname [options]'
c.summary = 'Modify a vms tags, time to live, and disk space'
c.description = 'This command makes modifications to the virtual machines state in vmpooler. You can either append tags to the vm, increase how long it stays active for, or increase the amount of disk space.'
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.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)'
c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb)'
c.option '--tags STRING', String, 'free-form VM tagging (json)'
c.option '--all', 'Modifies all vms acquired by a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
hostname = args[0]
lifetime = options.lifetime
disk = options.disk
tags = JSON.parse(options.tags) if options.tags
token = options.token || config['token']
token = options.token || service_config['token'] || config['token']
modify_all = options.all
running_vms = nil
@ -289,18 +297,20 @@ class Vmfloaty
command :delete do |c|
c.syntax = 'floaty delete hostname,hostname2 [options]'
c.summary = 'Schedules the deletion of a host or hosts'
c.description = 'Given a comma separated list of hostnames, or --all for all vms, vmfloaty makes a request to vmpooler to schedule the deletion of those vms.'
c.description = 'Given a comma separated list of hostnames, or --all for all vms, vmfloaty makes a request to the pooler service to schedule the deletion of those vms.'
c.example 'Schedules the deletion of a host or hosts', 'floaty delete myhost1,myhost2 --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--all', 'Deletes all vms acquired by a token'
c.option '-f', 'Does not prompt user when deleting all vms'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
hostnames = args[0]
token = options.token || config['token']
url = options.url ||= config['url']
token = options.token || service_config['token'] || config['token']
url = options.url ||= service_config['url'] ||= config['url']
delete_all = options.all
force = options.f
@ -355,7 +365,7 @@ class Vmfloaty
exit 1
end
puts "Schedulered vmpooler to delete vms #{hosts}."
puts "Scheduled pooler service to delete vms #{hosts}."
exit 0
end
end
@ -364,16 +374,18 @@ class Vmfloaty
command :snapshot do |c|
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 vmpooler. This command is known to take a while depending on how much load is on vmpooler.'
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.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
hostname = args[0]
token = options.token ||= config['token']
token = options.token ||= service_config['token'] ||= config['token']
begin
snapshot_req = Pooler.snapshot(verbose, url, hostname, token)
@ -390,17 +402,19 @@ class Vmfloaty
command :revert do |c|
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 vmpooler to go back to a previous 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.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--snapshot STRING', String, 'SHA of snapshot'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
hostname = args[0]
token = options.token || config['token']
token = options.token || service_config['token'] || config['token']
snapshot_sha = args[1] || options.snapshot
if args[1] && options.snapshot
@ -420,15 +434,17 @@ class Vmfloaty
command :status do |c|
c.syntax = 'floaty status [options]'
c.summary = 'Prints the status of pools in vmpooler'
c.description = 'Makes a request to vmpooler to request the information about vm pools and how many are ready to be used, what pools are empty, etc.'
c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com'
c.summary = 'Prints the status of pools in the pooler service'
c.description = 'Makes a request to the pooler service to request the information about vm pools and how many are ready to be used, what pools are empty, etc.'
c.example 'Gets the current pooler service status', 'floaty status --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--json', 'Prints status in JSON format'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
status = Pooler.status(verbose, url)
message = status['status']['message']
@ -446,14 +462,16 @@ class Vmfloaty
command :summary do |c|
c.syntax = 'floaty summary [options]'
c.summary = 'Prints a summary of vmpooler'
c.description = 'Gives a very detailed summary of information related to vmpooler.'
c.example 'Gets the current day summary of vmpooler', 'floaty summary --url http://vmpooler.example.com'
c.summary = 'Prints a summary of a pooler service'
c.description = 'Gives a very detailed summary of information related to the pooler service.'
c.example 'Gets the current day summary of the pooler service', 'floaty summary --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
summary = Pooler.summary(verbose, url)
pp summary
@ -464,22 +482,24 @@ class Vmfloaty
command :token do |c|
c.syntax = 'floaty token <get delete status> [options]'
c.summary = 'Retrieves or deletes a token or checks token status'
c.description = 'This command is used to manage your vmpooler token. Through the various options, you are able to get a new token, delete an existing token, and request a tokens status.'
c.description = 'This command is used to manage your pooler service token. Through the various options, you are able to get a new token, delete an existing token, and request a tokens status.'
c.example 'Gets a token from the pooler', 'floaty token get'
c.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--user STRING', String, 'User to authenticate with'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--token STRING', String, 'Token for pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
action = args.first
url = options.url ||= config['url']
token = args[1] ||= options.token ||= config['token']
user = options.user ||= config['user']
url = options.url ||= service_config['url'] ||= config['url']
token = args[1] ||= options.token ||= service_config['token'] ||= config['token']
user = options.user ||= service_config['user'] ||= config['user']
case action
when "get"
pass = password "Enter your vmpooler password please:", '*'
pass = password "Enter your pooler service password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue TokenError => e
@ -489,7 +509,7 @@ class Vmfloaty
puts token
exit 0
when "delete"
pass = password "Enter your vmpooler password please:", '*'
pass = password "Enter your pooler service password please:", '*'
begin
result = Auth.delete_token(verbose, url, user, pass, token)
rescue TokenError => e
@ -521,15 +541,17 @@ class Vmfloaty
c.description = 'This command simply will grab a vm template that was requested, and then ssh the user into the machine all at once.'
c.example 'SSHs into a centos vm', 'floaty ssh centos7 --url https://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--user STRING', String, 'User to authenticate with'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--notoken', 'Makes a request without a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
token = options.token ||= config['token']
user = options.user ||= config['user']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
token = options.token ||= service_config['token'] ||= config['token']
user = options.user ||= service_config['user'] ||= config['user']
no_token = options.notoken
if args.empty?
@ -542,10 +564,10 @@ class Vmfloaty
if !no_token && !token
puts "No token found. Retrieving a token..."
if !user
STDERR.puts "You did not provide a user to authenticate to vmpooler with"
STDERR.puts "You did not provide a user to authenticate to the pooler service with"
exit 1
end
pass = password "Enter your vmpooler password please:", '*'
pass = password "Enter your pooler service password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue TokenError => e