diff --git a/README.md b/README.md index 5b7eb47..849cb86 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,10 @@ floaty get centos-7-x86_64=2 debian-7-x86_64 windows-10=3 --token mytokenstring If you do not wish to continuely specify various config options with the cli, you can have a dotfile in your home directory for some defaults. For example: +#### Basic configuration + ```yaml -#file at /Users/me/.vmfloaty.yml +# file at /Users/me/.vmfloaty.yml url: 'https://vmpooler.mycompany.net/api/v1' user: 'brian' token: 'tokenstring' @@ -75,6 +77,40 @@ token: 'tokenstring' Now vmfloaty will use those config files if no flag was specified. +#### Configuring multiple services + +Most commands allow you to specify a `--service ` option to allow the use of multiple vmpooler instances. This can be useful when you'd rather not specify a `--url` or `--token` by hand for alternate services. + +To configure multiple services, you can set up your `~/.vmfloaty.yml` config file like this: + +```yaml +# file at /Users/me/.vmfloaty.yml +user: 'brian' +services: + main: + url: 'https://vmpooler.mycompany.net/api/v1' + token: 'tokenstring' + alternate: + url: 'https://vmpooler.alternate.net/api/v1' + token: 'alternate-tokenstring' +``` + +vmfloaty will now use the top-level keys (just "user" above) as default values, and you will be able to specify a service name with `--service` when you run floaty. If you don't specify a service name, vmfloaty will first try to use the default, top-level values. If there is no default URL or token specified in the config file, vmfloaty will then use the first configured service as the default. + +Examples using the above configuration: + +List available vm types from our main vmpooler instance: +```sh +floaty list --service main --active +# or, since the first configured service is the default: +floaty list --active +``` + +List available vm types from our alternate vmpooler instance: +```sh +floaty list --service alternate --active +``` + #### Valid config keys Here are the keys that vmfloaty currently supports: @@ -87,6 +123,8 @@ Here are the keys that vmfloaty currently supports: + String - url + String +- services + + Map ### Tab Completion diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index e0fbce4..c4e985a 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -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 [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 diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index 31c6fd0..93ad7e0 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -121,4 +121,35 @@ class Utils str.gsub(/^[ \t]{#{min_indent_size}}/, '') end + + def self.get_service_from_config(config, service_name = '') + # The top-level url, user, and token values are treated as defaults + service = { + 'url' => config['url'], + 'user' => config['user'], + 'token' => config['token'] + } + + # If no named services have been configured, use the default values + return service unless config['services'] and config['services'].length + + if not service_name.empty? + if config['services'][service_name] + # If the user specified a configured service name, use that service + # If values are missing, use the top-level defaults + service.merge!(config['services'][service_name]) { |key, default, value| value } + else + STDERR.puts "WARNING: Could not find a configured service matching the name #{service_name} at #{Dir.home}/.vmfloaty.yml" + return {} + end + else + # Otherwise, use the first service configured under the 'services' key + # If values are missing, use the top-level defaults + name, config_hash = config['services'].first + service.merge!(config_hash) { |key, default, value| value } + end + + service + end + end diff --git a/spec/vmfloaty/utils_spec.rb b/spec/vmfloaty/utils_spec.rb index c30d378..ed8b0b2 100644 --- a/spec/vmfloaty/utils_spec.rb +++ b/spec/vmfloaty/utils_spec.rb @@ -16,6 +16,57 @@ describe Utils do end end + describe "#get_service_from_config" do + before :each do + @default_config = { + "url" => "http://default.url", + "user" => "first.last.default", + "token" => "default-token", + } + @services_config = { + "services" => { + "vm" => { + "url" => "http://vmpooler.url", + "user" => "first.last.vmpooler", + "token" => "vmpooler-token" + }, + "ns" => { + "url" => "http://nspooler.url", + "user" => "first.last.nspooler", + "token" => "nspooler-token" + } + } + } + end + + it "returns the first service configured under 'services' as the default if available" do + config = @default_config.merge @services_config + expect(Utils.get_service_from_config(config)).to include @services_config['services']['vm'] + end + + it "uses top-level service config values as defaults when service values are missing" do + config = {"services" => { "vm" => {}}} + config.merge! @default_config + expect(Utils.get_service_from_config(config, 'vm')).to include @default_config + end + + it "allows selection by configured service key" do + config = @default_config.merge @services_config + expect(Utils.get_service_from_config(config, 'ns')).to include @services_config['services']['ns'] + end + + it "fills in missing values in configured services with the defaults" do + config = @default_config.merge @services_config + config["services"]['vm'].delete 'url' + expect(Utils.get_service_from_config(config, 'vm')['url']).to eq 'http://default.url' + end + + it "returns an empty hash if passed a service name that hasn't been configured" do + config = @default_config.merge @services_config + expect(Utils.get_service_from_config(config, 'nil')).to eq({}) + end + end + describe "#generate_os_hash" do before :each do @host_hash = {"centos"=>1, "debian"=>5, "windows"=>1}