From 296f9abb1de2eba1ccb2f5b4c3c8a94cc28b471b Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sat, 5 Sep 2015 16:17:45 -0700 Subject: [PATCH 01/33] (#1) Update vmfloaty to use new vmpooler api This commit also uses commander for arg parsing. This commit is a WIP. --- Gemfile | 3 +- README.md | 30 +++++--- bin/floaty | 3 +- lib/vmfloaty.rb | 150 +++++++++++++++++++++++++++++++++++++--- lib/vmfloaty/auth.rb | 20 ++++++ lib/vmfloaty/cli.rb | 87 ----------------------- lib/vmfloaty/hosts.rb | 29 -------- lib/vmfloaty/http.rb | 13 ++++ lib/vmfloaty/pooler.rb | 66 ++++++++++++++++++ lib/vmfloaty/version.rb | 5 -- vmfloaty.gemspec | 5 +- 11 files changed, 266 insertions(+), 145 deletions(-) create mode 100644 lib/vmfloaty/auth.rb delete mode 100644 lib/vmfloaty/cli.rb delete mode 100644 lib/vmfloaty/hosts.rb create mode 100644 lib/vmfloaty/http.rb create mode 100644 lib/vmfloaty/pooler.rb delete mode 100644 lib/vmfloaty/version.rb diff --git a/Gemfile b/Gemfile index 6c10f3a..faec794 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,6 @@ source 'https://rubygems.org' -gem 'thor' +gem 'commander' +gem 'faraday' gemspec diff --git a/README.md b/README.md index 77d10a9..806a0f1 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,26 @@ gem install vmfloaty ## Usage -_note:_ subject to change +``` + delete Schedules the deletion of a host or hosts + get Gets a vm or vms based on the os flag + help Display global or [command] help documentation + list Shows a list of available vms from the pooler + modify + query + revert + snapshot + status Prints the status of vmpooler + summary -``` -Commands: - floaty get # Gets a VM - floaty help [COMMAND] # Describe available commands or one specific command - floaty list [PATTERN] # List all open VMs - floaty modify # (TODO STILL) Modify a VM - floaty release [--all] # Schedules a VM for deletion - floaty status # (TODO STILL) List status of all active VMs + GLOBAL OPTIONS: + + -h, --help + Display help documentation + + -v, --version + Display version information + + -t, --trace + Display backtrace when an error occurs ``` diff --git a/bin/floaty b/bin/floaty index 5c5a3f6..6fe5b10 100755 --- a/bin/floaty +++ b/bin/floaty @@ -1,6 +1,7 @@ #!/usr/bin/env ruby + $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__)) require 'vmfloaty' -Vmfloaty.new(ENV).start +Vmfloaty.new.run diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index da12900..4d48915 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -1,17 +1,149 @@ -require 'vmfloaty/cli' -require 'vmfloaty/hosts' +#!/usr/bin/env ruby + +require 'rubygems' +require 'commander' +require 'vmfloaty/auth' +require 'vmfloaty/http' +require 'vmfloaty/pooler' class Vmfloaty + include Commander::Methods - def initialize(env) - $vmpooler_url = env['VMPOOLER_URL'] + def run + program :version, '0.2.0' + program :description, 'A CLI helper tool for Puppet Labs vmpooler to help you stay afloat' - unless $vmpooler_url - $vmpooler_url = 'http://vcloud.delivery.puppetlabs.net' + command :get do |c| + c.syntax = 'floaty get [options]' + c.summary = 'Gets a vm or vms based on the os flag' + c.description = '' + c.example 'Gets 3 vms', 'floaty get --user brian --url http://vmpooler.example.com --os centos,centos,debian' + 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 '--os STRING', String, 'Operating systems to retrieve' + c.action do |args, options| + token = options.token + user = options.user + url = options.url + os_types = options.os + pass = password "Enter your password please:", '*' + + unless options.token + token = Auth.get_token(user, url, password) + end + + unless os_types.nil? + Pooler.retrieve(os_types, token, url) + else + puts 'You did not provide an OS to get' + end + end end - end - def start - CLI.start(ARGV) + command :list do |c| + c.syntax = 'floaty list [options]' + c.summary = 'Shows a list of available vms from the pooler' + c.description = '' + c.example 'Filter the list on centos', 'floaty list --filter centos --url http://vmpooler.example.com' + c.option '--filter STRING', String, 'A filter to apply to the list' + c.option '--url STRING', String, 'URL of vmpooler' + c.action do |args, options| + # Do something or c.when_called Floaty::Commands::Query + filter = options.filter + url = options.url + + unless filter.nil? + Pooler.list(url, filter) + else + Pooler.list(url) + end + end + end + + command :query do |c| + c.syntax = 'floaty query [options]' + c.summary = '' + c.description = '' + c.example 'description', 'command example' + c.option '--some-switch', 'Some switch that does something' + c.action do |args, options| + # Do something or c.when_called Floaty::Commands::Query + end + end + + command :modify do |c| + c.syntax = 'floaty modify [options]' + c.summary = '' + c.description = '' + c.example 'description', 'command example' + c.option '--some-switch', 'Some switch that does something' + c.action do |args, options| + # Do something or c.when_called Floaty::Commands::Modify + end + end + + command :delete do |c| + c.syntax = 'floaty delete [options]' + c.summary = 'Schedules the deletion of a host or hosts' + c.description = '' + c.example 'Schedules the deletion of a host or hosts', 'floaty delete --hosts myhost1,myhost2 --url http://vmpooler.example.com' + c.option '--hosts STRING', String, 'Hostname(s) to delete' + c.option '--url STRING', String, 'URL of vmpooler' + c.action do |args, options| + hosts = options.hosts + url = options.url + + Pool.delete(hosts, url) + end + end + + command :snapshot do |c| + c.syntax = 'floaty snapshot [options]' + c.summary = '' + c.description = '' + c.example 'description', 'command example' + c.option '--some-switch', 'Some switch that does something' + c.action do |args, options| + # Do something or c.when_called Floaty::Commands::Snapshot + end + end + + command :revert do |c| + c.syntax = 'floaty revert [options]' + c.summary = '' + c.description = '' + c.example 'description', 'command example' + c.option '--some-switch', 'Some switch that does something' + c.action do |args, options| + # Do something or c.when_called Floaty::Commands::Revert + end + end + + command :status do |c| + c.syntax = 'floaty status [options]' + c.summary = 'Prints the status of vmpooler' + c.description = '' + c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com' + c.option '--url STRING', String, 'URL of vmpooler' + c.action do |args, options| + url = options.url + + Pooler.status(url) + end + end + + command :summary do |c| + c.syntax = 'floaty summary [options]' + c.summary = '' + c.description = '' + c.example 'description', 'command example' + c.option '--some-switch', 'Some switch that does something' + c.action do |args, options| + # Do something or c.when_called Floaty::Commands::Summary + end + end + + run! end end diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb new file mode 100644 index 0000000..e2bdc74 --- /dev/null +++ b/lib/vmfloaty/auth.rb @@ -0,0 +1,20 @@ +require 'faraday' +require 'vmfloaty/http' + +class Auth + def self.get_token(user, url, password) + conn = Http.get_conn(url) + + #resp = conn.post do |req| + # req.url '/v1/token' + # req.headers['Content-Type'] = 'application/json' + # req.user = user + # end + # if ok: true, return token + puts 'Got token' + end + + def self.delete_token(user, token) + conn = Http.get_conn(url) + end +end diff --git a/lib/vmfloaty/cli.rb b/lib/vmfloaty/cli.rb deleted file mode 100644 index 5b41181..0000000 --- a/lib/vmfloaty/cli.rb +++ /dev/null @@ -1,87 +0,0 @@ -require 'thor' -require 'net/http' -require 'uri' -require 'json' - -class CLI < Thor - desc "get ", "Gets a VM" - def get(os_list) - # HTTP POST -d os_list vmpooler.company.com/vm - - uri = URI.parse("#{$vmpooler_url}/vm/#{os_list.gsub(",","+")}") - http = Net::HTTP.new(uri.host, uri.port) - request = Net::HTTP::Post.new(uri.request_uri) - response = http.request(request) - - if response.code.to_i == 200 - hosts = JSON.parse(response.body) - - # puts hosts - - save_hosts = {} - hosts.each do |k,v| - unless k == 'ok' || k == 'domain' - save_hosts[k] = v['hostname'] - end - end - - puts 'New Hosts:' - puts save_hosts - - #hosts.add_host save_hosts - end - - # parse host names/os's and save - end - - desc "modify ", "Modify a VM" - def modify(hostname) - say 'Modify a vm' - end - - desc "status", "List status of all active VMs" - def status - #$hosts.print_host_list - end - - desc "list [PATTERN]", "List all open VMs" - def list(pattern=nil) - # HTTP GET vmpooler.company.com/vm - uri = URI.parse("#{$vmpooler_url}/vm") - response = Net::HTTP.get_response(uri) - host_res = JSON.parse(response.body) - - if pattern - # Filtering VMs based on pattern - hosts = host_res.select { |i| i[/#{pattern}/] } - else - hosts = host_res - end - - puts hosts - end - - desc "release [--all]", "Schedules a VM for deletion" - option :all - def release(hostname_list=nil) - # HTTP DELETE vmpooler.company.com/vm/#{hostname} - # { "ok": true } - - if options[:all] - # release all hosts managed by vmfloaty - else - hostname_arr = hostname_list.split(',') - - hostname_arr.each do |hostname| - say "Releasing host #{hostname}..." - uri = URI.parse("#{$vmpooler_url}/vm/#{hostname}") - http = Net::HTTP.new(uri.host, uri.port) - request = Net::HTTP::Delete.new(uri.request_uri) - response = http.request(request) - res = JSON.parse(response.body) - - puts res - end - end - end -end diff --git a/lib/vmfloaty/hosts.rb b/lib/vmfloaty/hosts.rb deleted file mode 100644 index 5ca90a5..0000000 --- a/lib/vmfloaty/hosts.rb +++ /dev/null @@ -1,29 +0,0 @@ -# manage hosts used from vm pooler -class Hosts - def initialize - @host_list = {} - end - - def add_host(host_hash) - host_hash.each do |k,v| - if @host_list[k] - @host_list[k].push(v) - else - if v.is_a?(Array) - @host_list[k] = v - else - @host_list[k] = [v] - end - end - end - - puts @host_list - end - - def remove_host(host_id) - end - - def print_host_list - puts @host_list - end -end diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb new file mode 100644 index 0000000..086f0c0 --- /dev/null +++ b/lib/vmfloaty/http.rb @@ -0,0 +1,13 @@ +require 'faraday' + +class Http + def self.get_conn(url) + conn = Faraday.new(:url => url) do |faraday| + faraday.request :url_encoded + faraday.response :logger + faraday.adapter Faraday.default_adapter + end + + return conn + end +end diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb new file mode 100644 index 0000000..23a734c --- /dev/null +++ b/lib/vmfloaty/pooler.rb @@ -0,0 +1,66 @@ +require 'faraday' +require 'vmfloaty/http' +require 'json' + +class Pooler + def self.list(url, os_filter=nil) + conn = Http.get_conn(url) + + response = conn.get '/v1/vm' + response_body = JSON.parse(response.body) + + if os_filter + hosts = response_body.select { |i| i[/#{pattern}/] } + else + hosts = response_body + end + + puts hosts + end + + def self.retrieve(os_type, token, url) + os = os_type.split(',') + conn = Http.get_conn(url) + os_body = {} + + os.each do |os_type| + unless os_body.has_key?(os_type) + os_body[os_type] = 1 + else + os_body[os_type] = os_body[os_type] + 1 + end + end + + response = conn.post do |req| + req.url '/v1/vm' + req.headers['Content-Type'] = 'application/json' + req.body = os_body + end + + puts JSON.parse(response.body) + end + + def self.modify(hostname, token, url, lifetime, tags) + modify_body = {'lifetime'=>lifetime, 'tags'=>tags} + conn = Http.get_conn(url) + end + + def self.delete(hostnames, url) + hosts = hostnames.split(',') + conn = Http.get_conn(url) + + hosts.each do |host| + puts "Scheduling host #{host} for deletion" + response = conn.delete "/v1/#{host}" + res_body = JSON.parse(response.body) + puts res_body + end + end + + def self.status(url) + conn = Http.get_conn(url) + + response = conn.get '/v1/status' + res_body = JSON.parse(response.body) + end +end diff --git a/lib/vmfloaty/version.rb b/lib/vmfloaty/version.rb deleted file mode 100644 index 7da0994..0000000 --- a/lib/vmfloaty/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Vmfloaty - module CLI - VERSION = '0.1.0' - end -end diff --git a/vmfloaty.gemspec b/vmfloaty.gemspec index f20500b..c00e709 100644 --- a/vmfloaty.gemspec +++ b/vmfloaty.gemspec @@ -1,8 +1,6 @@ -require File.expand_path '../lib/vmfloaty/version', __FILE__ - Gem::Specification.new do |s| s.name = 'vmfloaty' - s.version = Vmfloaty::CLI::VERSION.dup + s.version = '0.2.0' s.authors = ['Brian Cain'] s.email = ['brian.cain@puppetlabs.com'] s.license = 'Apache' @@ -13,5 +11,4 @@ Gem::Specification.new do |s| s.files = Dir['LICENSE', 'README.md', 'lib/**/*'] s.test_files = Dir['spec/**/*'] s.require_path = 'lib' - s.add_dependency 'thor', '~> 0.19' end From 49b495632377c17dfb1d409760f4cd7f456de7af Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 6 Sep 2015 10:39:12 -0700 Subject: [PATCH 02/33] (maint) Remove filter check from commands class --- lib/vmfloaty.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 4d48915..4004d10 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -53,11 +53,7 @@ class Vmfloaty filter = options.filter url = options.url - unless filter.nil? - Pooler.list(url, filter) - else - Pooler.list(url) - end + Pooler.list(url, filter) end end From 4c249c0ce4186baddfc5999f2509e52ad51bcd45 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 6 Sep 2015 11:10:43 -0700 Subject: [PATCH 03/33] (#1) Use config file for defaults This commit allows vmfloaty to read from a dotfile for some simple configuration defaults --- README.md | 12 ++++++++++++ lib/vmfloaty.rb | 21 +++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 806a0f1..fb4ef6c 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,15 @@ gem install vmfloaty -t, --trace Display backtrace when an error occurs ``` + +### vmfloaty dotfile + +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: + +```yaml +#file at /Users/me/.vmpooler.yml +url: 'http://vmpooler.mycompany.net' +user: 'brian' +``` + +Now vmfloaty will use those config files if no flag was specified. diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 4004d10..73743a6 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -2,6 +2,7 @@ require 'rubygems' require 'commander' +require 'yaml' require 'vmfloaty/auth' require 'vmfloaty/http' require 'vmfloaty/pooler' @@ -13,7 +14,10 @@ class Vmfloaty program :version, '0.2.0' program :description, 'A CLI helper tool for Puppet Labs vmpooler to help you stay afloat' + config = read_config + command :get do |c| + puts config c.syntax = 'floaty get [options]' c.summary = 'Gets a vm or vms based on the os flag' c.description = '' @@ -24,13 +28,13 @@ class Vmfloaty c.option '--os STRING', String, 'Operating systems to retrieve' c.action do |args, options| token = options.token - user = options.user - url = options.url + user = options.user ||= config['user'] + url = options.url ||= config['url'] os_types = options.os pass = password "Enter your password please:", '*' unless options.token - token = Auth.get_token(user, url, password) + token = Auth.get_token(user, url, pass) end unless os_types.nil? @@ -51,7 +55,7 @@ class Vmfloaty c.action do |args, options| # Do something or c.when_called Floaty::Commands::Query filter = options.filter - url = options.url + url = options.url ||= config['url'] Pooler.list(url, filter) end @@ -88,7 +92,7 @@ class Vmfloaty c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| hosts = options.hosts - url = options.url + url = options.url ||= config['url'] Pool.delete(hosts, url) end @@ -123,7 +127,7 @@ class Vmfloaty c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| - url = options.url + url = options.url ||= config['url'] Pooler.status(url) end @@ -142,4 +146,9 @@ class Vmfloaty run! end + + def read_config + conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml") + conf + end end From 13f5a282c7ae06dbb2cca3446f9bda9721897633 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 6 Sep 2015 11:35:42 -0700 Subject: [PATCH 04/33] (#1) Add summary command Right now it does not support timespans, and just does current day. --- README.md | 2 +- lib/vmfloaty.rb | 11 ++++++----- lib/vmfloaty/pooler.rb | 9 +++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fb4ef6c..b0f66a7 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ gem install vmfloaty revert snapshot status Prints the status of vmpooler - summary + summary Prints the summary of vmpooler GLOBAL OPTIONS: diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 73743a6..621ba5b 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -17,7 +17,6 @@ class Vmfloaty config = read_config command :get do |c| - puts config c.syntax = 'floaty get [options]' c.summary = 'Gets a vm or vms based on the os flag' c.description = '' @@ -135,12 +134,14 @@ class Vmfloaty command :summary do |c| c.syntax = 'floaty summary [options]' - c.summary = '' + c.summary = 'Prints the summary of vmpooler' c.description = '' - c.example 'description', 'command example' - c.option '--some-switch', 'Some switch that does something' + c.example 'Gets the current day summary of vmpooler', 'floaty summary --url http://vmpooler.example.com' + c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| - # Do something or c.when_called Floaty::Commands::Summary + url = options.url ||= config['url'] + + Pooler.summary(url) end end diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 23a734c..b28f64f 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -62,5 +62,14 @@ class Pooler response = conn.get '/v1/status' res_body = JSON.parse(response.body) + puts res_body + end + + def self.summary(url) + conn = Http.get_conn(url) + + response = conn.get '/v1/summary' + res_body = JSON.parse(response.body) + puts res_body end end From f773e0a5a6e474dce21932fd97deade1748915e3 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 6 Sep 2015 11:36:47 -0700 Subject: [PATCH 05/33] Add note on vmpooler api --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b0f66a7..fd031a3 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,7 @@ user: 'brian' ``` Now vmfloaty will use those config files if no flag was specified. + +## vmpooler API + +This cli tool uses the [vmpooler API](https://github.com/puppetlabs/vmpooler/blob/master/API.md). From 02527b966506d1e63b55cb928168ab6e092667de Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 6 Sep 2015 12:16:08 -0700 Subject: [PATCH 06/33] (#1) Update query, snapshot, and revert --- README.md | 8 +++--- lib/vmfloaty.rb | 61 ++++++++++++++++++++++++++++++------------ lib/vmfloaty/auth.rb | 14 +++++----- lib/vmfloaty/pooler.rb | 37 +++++++++++++++++++++++-- 4 files changed, 91 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index fd031a3..583170b 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,10 @@ gem install vmfloaty get Gets a vm or vms based on the os flag help Display global or [command] help documentation list Shows a list of available vms from the pooler - modify - query - revert - snapshot + modify Modify a vms tags and TTL + query Get information about a given vm + revert Reverts a vm to a specified snapshot + snapshot Takes a snapshot of a given vm status Prints the status of vmpooler summary Prints the summary of vmpooler diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 621ba5b..47d0c7c 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -52,7 +52,6 @@ class Vmfloaty c.option '--filter STRING', String, 'A filter to apply to the list' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| - # Do something or c.when_called Floaty::Commands::Query filter = options.filter url = options.url ||= config['url'] @@ -62,23 +61,37 @@ class Vmfloaty command :query do |c| c.syntax = 'floaty query [options]' - c.summary = '' + c.summary = 'Get information about a given vm' c.description = '' - c.example 'description', 'command example' - c.option '--some-switch', 'Some switch that does something' + c.example 'Get information about a sample host', 'floaty query --url http://vmpooler.example.com --host myvmhost.example.com' + c.option '--url STRING', String, 'URL of vmpooler' + c.option '--host STRING', String, 'Hostname to query' c.action do |args, options| - # Do something or c.when_called Floaty::Commands::Query + url = options.url ||= config['url'] + hostname = options.hostname + + Pooler.query(url, hostname) end end command :modify do |c| c.syntax = 'floaty modify [options]' - c.summary = '' + c.summary = 'Modify a vms tags and TTL' c.description = '' c.example 'description', 'command example' - c.option '--some-switch', 'Some switch that does something' + c.option '--url STRING', String, 'URL of vmpooler' + c.option '--token STRING', String, 'Token for vmpooler' + c.option '--host STRING', String, 'Hostname to modify' + c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)' + c.option '--tags HASH', Hash, 'free-form VM tagging' c.action do |args, options| - # Do something or c.when_called Floaty::Commands::Modify + url = options.url ||= config['url'] + hostname = options.hostname + lifetime = options.lifetime + tags = options.tags + token = options.token + + Pooler.modify(url, hostname, token, lifetime, tags) end end @@ -93,29 +106,43 @@ class Vmfloaty hosts = options.hosts url = options.url ||= config['url'] - Pool.delete(hosts, url) + Pool.delete(url, hosts) end end command :snapshot do |c| c.syntax = 'floaty snapshot [options]' - c.summary = '' + c.summary = 'Takes a snapshot of a given vm' c.description = '' - c.example 'description', 'command example' - c.option '--some-switch', 'Some switch that does something' + c.example 'Takes a snapshot for a given host', 'floaty snapshot --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl' + c.option '--url STRING', String, 'URL of vmpooler' + c.option '--host STRING', String, 'Hostname to modify' + c.option '--token STRING', String, 'Token for vmpooler' c.action do |args, options| - # Do something or c.when_called Floaty::Commands::Snapshot + url = options.url ||= config['url'] + hostname = options.hostname + token = options.token + + Pooler.snapshot(url, hostname, token) end end command :revert do |c| c.syntax = 'floaty revert [options]' - c.summary = '' + c.summary = 'Reverts a vm to a specified snapshot' c.description = '' - c.example 'description', 'command example' - c.option '--some-switch', 'Some switch that does something' + c.example 'Reverts to a snapshot for a given host', 'floaty revert --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl --snapshot n4eb4kdtp7rwv4x158366vd9jhac8btq' + c.option '--url STRING', String, 'URL of vmpooler' + c.option '--host STRING', String, 'Hostname to modify' + c.option '--token STRING', String, 'Token for vmpooler' + c.option '--snapshot STRING', String, 'SHA of snapshot' c.action do |args, options| - # Do something or c.when_called Floaty::Commands::Revert + url = options.url ||= config['url'] + hostname = options.hostname + token = options.token + snapshot_sha = options.snapshot + + Pooler.revert(url, hostname, token, snapshot_sha) end end diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index e2bdc74..ce1c78d 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -1,17 +1,19 @@ require 'faraday' +require 'json' require 'vmfloaty/http' class Auth def self.get_token(user, url, password) conn = Http.get_conn(url) - #resp = conn.post do |req| - # req.url '/v1/token' - # req.headers['Content-Type'] = 'application/json' - # req.user = user - # end + resp = conn.post do |req| + req.url '/v1/token' + req.headers['Content-Type'] = 'application/json' + req.user = user + end # if ok: true, return token - puts 'Got token' + resp_body = JSON.parse(resp.body) + resp_body end def self.delete_token(user, token) diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index b28f64f..aed6880 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -40,12 +40,18 @@ class Pooler puts JSON.parse(response.body) end - def self.modify(hostname, token, url, lifetime, tags) + def self.modify(url, hostname, token, lifetime, tags) modify_body = {'lifetime'=>lifetime, 'tags'=>tags} conn = Http.get_conn(url) + + # need to use token + response = conn.put "/v1/#{hostname}" + res_body = JSON.parse(response.body) + + puts res_body end - def self.delete(hostnames, url) + def self.delete(url, hostname) hosts = hostnames.split(',') conn = Http.get_conn(url) @@ -72,4 +78,31 @@ class Pooler res_body = JSON.parse(response.body) puts res_body end + + def self.query(url, hostname) + conn = Http.get_conn(url) + + response = conn.get "/v1/vm/#{hostname}" + res_body = JSON.parse(response.body) + + puts res_body + end + + def self.snapshot(url, hostname, token) + conn = Http.get_conn(url) + + # need to use token + response = conn.post "/v1/#{hostname}/snapshot" + res_body = JSON.parse(response.body) + puts res_body + end + + def self.revert(url, hostname, token, snapshot_sha) + conn = Http.get_conn(url) + + # need to use token + response = conn.post "/v1/#{hostname}/snapshot/#{snapshot}" + res_body = JSON.parse(response.body) + puts res_body + end end From d3b1af4a06cfd3e2ff00e45e66ec97df12dd99e2 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 7 Sep 2015 14:11:05 -0700 Subject: [PATCH 07/33] (#1) Add token status and delete methods --- lib/vmfloaty.rb | 30 +++++++++++++++++++++++++++++- lib/vmfloaty/auth.rb | 29 +++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 47d0c7c..ad311f6 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -33,7 +33,7 @@ class Vmfloaty pass = password "Enter your password please:", '*' unless options.token - token = Auth.get_token(user, url, pass) + token = Auth.get_token(url, user, pass) end unless os_types.nil? @@ -172,6 +172,34 @@ class Vmfloaty end end + command :token do |c| + c.syntax = 'floaty token [get | delete | status]' + c.summary = 'Retrieves or deletes a token' + c.description = '' + c.example '', '' + c.option '--url STRING', String, 'URL of vmpooler' + c.option '--user STRING', String, 'User to authenticate with' + c.option '--token STRING', String, 'Token for vmpooler' + c.action do |args, options| + action = args.first + url = options.url ||= config['url'] + token = options.token + user = options.user ||= config['user'] + pass = password "Enter your password please:", '*' + + case action + when "get" + puts Auth.get_token(url, user, pass) + when "delete" + Auth.delete_token(url, user, pass, token) + when "status" + Auth.token_status(url, user, pass, token) + else + puts "Unknown action: #{action}" + end + end + end + run! end diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index ce1c78d..d799153 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -3,20 +3,41 @@ require 'json' require 'vmfloaty/http' class Auth - def self.get_token(user, url, password) + def self.get_token(url, user, password) conn = Http.get_conn(url) resp = conn.post do |req| req.url '/v1/token' req.headers['Content-Type'] = 'application/json' - req.user = user end - # if ok: true, return token + resp_body = JSON.parse(resp.body) resp_body end - def self.delete_token(user, token) + def self.delete_token(url, user, pass, token) + if token.nil? + puts 'You did not provide a token' + return + end + conn = Http.get_conn(url) + + response = conn.delete "/v1/token/#{token}" + res_body = JSON.parse(response) + puts res_body + end + + def self.token_status(url, user, pass, token) + if token.nil? + puts 'You did not provide a token' + return + end + + conn = Http.get_conn(url) + + response = conn.get "/v1/token/#{token}" + res_body = JSON.parse(response.body) + puts res_body end end From 02f56d325699949b124851a74eba7c6f6dc410f2 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 7 Sep 2015 14:12:55 -0700 Subject: [PATCH 08/33] Update readme with token commant --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 583170b..e9b5bf6 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ gem install vmfloaty snapshot Takes a snapshot of a given vm status Prints the status of vmpooler summary Prints the summary of vmpooler + token Retrieves or deletes a token GLOBAL OPTIONS: From b64d9ba3913ce4a1613c0dcf0db44a6ff4403d28 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 7 Sep 2015 14:14:42 -0700 Subject: [PATCH 09/33] Add example dotfile config --- vmfloaty.yml.example | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 vmfloaty.yml.example diff --git a/vmfloaty.yml.example b/vmfloaty.yml.example new file mode 100644 index 0000000..7728537 --- /dev/null +++ b/vmfloaty.yml.example @@ -0,0 +1,2 @@ +url: 'http://vmpooler.example.com' +user: 'brian' From 28180c17fe7f1fa13f524bd3d9de5cc52c26ffd7 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 7 Sep 2015 14:23:27 -0700 Subject: [PATCH 10/33] Add floaty image in readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e9b5bf6..71e1032 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ vmfloaty A CLI helper tool for Puppet Labs vmpooler to help you stay afloat. + + ## Install Grab the latest from ruby gems... From bda732fc17acb6ec358fe4d46cb7f16057dfc961 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 7 Sep 2015 14:27:15 -0700 Subject: [PATCH 11/33] Add vmpooler link in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 71e1032..c60aaba 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ vmfloaty ======== -A CLI helper tool for Puppet Labs vmpooler to help you stay afloat. +A CLI helper tool for [Puppet Labs vmpooler](https://github.com/puppetlabs/vmpooler) to help you stay afloat. From 4c1add6060f4d4dec2a3d907fcb26e943c40cd81 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 14:40:34 -0700 Subject: [PATCH 12/33] Handle exception if not config file exists --- lib/vmfloaty.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index ad311f6..8eb1a37 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -204,7 +204,12 @@ class Vmfloaty end def read_config - conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml") + conf = {} + begin + conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml") + rescue + STDERR.puts "There was no config file at #{Dir.home}/.vmfloaty.yml" + end conf end end From 2e32472ac27e88181b04a123949be9fab6228baa Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 14:50:35 -0700 Subject: [PATCH 13/33] Remove /v1 from urls in pooler library --- lib/vmfloaty/pooler.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index aed6880..ca312b7 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -6,7 +6,7 @@ class Pooler def self.list(url, os_filter=nil) conn = Http.get_conn(url) - response = conn.get '/v1/vm' + response = conn.get '/vm' response_body = JSON.parse(response.body) if os_filter @@ -32,7 +32,7 @@ class Pooler end response = conn.post do |req| - req.url '/v1/vm' + req.url '/vm' req.headers['Content-Type'] = 'application/json' req.body = os_body end @@ -45,7 +45,7 @@ class Pooler conn = Http.get_conn(url) # need to use token - response = conn.put "/v1/#{hostname}" + response = conn.put "/#{hostname}" res_body = JSON.parse(response.body) puts res_body @@ -57,7 +57,7 @@ class Pooler hosts.each do |host| puts "Scheduling host #{host} for deletion" - response = conn.delete "/v1/#{host}" + response = conn.delete "/#{host}" res_body = JSON.parse(response.body) puts res_body end @@ -66,7 +66,7 @@ class Pooler def self.status(url) conn = Http.get_conn(url) - response = conn.get '/v1/status' + response = conn.get '/status' res_body = JSON.parse(response.body) puts res_body end @@ -74,7 +74,7 @@ class Pooler def self.summary(url) conn = Http.get_conn(url) - response = conn.get '/v1/summary' + response = conn.get '/summary' res_body = JSON.parse(response.body) puts res_body end @@ -82,7 +82,7 @@ class Pooler def self.query(url, hostname) conn = Http.get_conn(url) - response = conn.get "/v1/vm/#{hostname}" + response = conn.get "/vm/#{hostname}" res_body = JSON.parse(response.body) puts res_body @@ -92,7 +92,7 @@ class Pooler conn = Http.get_conn(url) # need to use token - response = conn.post "/v1/#{hostname}/snapshot" + response = conn.post "/#{hostname}/snapshot" res_body = JSON.parse(response.body) puts res_body end @@ -101,7 +101,7 @@ class Pooler conn = Http.get_conn(url) # need to use token - response = conn.post "/v1/#{hostname}/snapshot/#{snapshot}" + response = conn.post "/#{hostname}/snapshot/#{snapshot}" res_body = JSON.parse(response.body) puts res_body end From 31de937e4fe69a0289c37323177aa18093535ca0 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 16:51:48 -0700 Subject: [PATCH 14/33] Fix filter unknown variable --- lib/vmfloaty/pooler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index ca312b7..a2f56ee 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -10,7 +10,7 @@ class Pooler response_body = JSON.parse(response.body) if os_filter - hosts = response_body.select { |i| i[/#{pattern}/] } + hosts = response_body.select { |i| i[/#{os_filter}/] } else hosts = response_body end From 73827f4ee2f6d4481f158e30c4fa758967132eea Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 16:51:49 -0700 Subject: [PATCH 15/33] Add verbose flag for future use --- lib/vmfloaty.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 8eb1a37..11208b0 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -21,6 +21,7 @@ class Vmfloaty c.summary = 'Gets a vm or vms based on the os flag' c.description = '' c.example 'Gets 3 vms', 'floaty get --user brian --url http://vmpooler.example.com --os centos,centos,debian' + c.option '--verbose', 'Enables verbose output' 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' @@ -49,6 +50,7 @@ class Vmfloaty c.summary = 'Shows a list of available vms from the pooler' c.description = '' c.example 'Filter the list on centos', 'floaty list --filter centos --url http://vmpooler.example.com' + c.option '--verbose', 'Enables verbose output' c.option '--filter STRING', String, 'A filter to apply to the list' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| @@ -64,6 +66,7 @@ class Vmfloaty c.summary = 'Get information about a given vm' c.description = '' c.example 'Get information about a sample host', 'floaty query --url http://vmpooler.example.com --host myvmhost.example.com' + c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--host STRING', String, 'Hostname to query' c.action do |args, options| @@ -79,6 +82,7 @@ class Vmfloaty c.summary = 'Modify a vms tags and TTL' c.description = '' c.example 'description', 'command example' + c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--token STRING', String, 'Token for vmpooler' c.option '--host STRING', String, 'Hostname to modify' @@ -100,6 +104,7 @@ class Vmfloaty c.summary = 'Schedules the deletion of a host or hosts' c.description = '' c.example 'Schedules the deletion of a host or hosts', 'floaty delete --hosts myhost1,myhost2 --url http://vmpooler.example.com' + c.option '--verbose', 'Enables verbose output' c.option '--hosts STRING', String, 'Hostname(s) to delete' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| @@ -115,6 +120,7 @@ class Vmfloaty c.summary = 'Takes a snapshot of a given vm' c.description = '' c.example 'Takes a snapshot for a given host', 'floaty snapshot --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl' + c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--host STRING', String, 'Hostname to modify' c.option '--token STRING', String, 'Token for vmpooler' @@ -132,6 +138,7 @@ class Vmfloaty c.summary = 'Reverts a vm to a specified snapshot' c.description = '' c.example 'Reverts to a snapshot for a given host', 'floaty revert --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl --snapshot n4eb4kdtp7rwv4x158366vd9jhac8btq' + c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--host STRING', String, 'Hostname to modify' c.option '--token STRING', String, 'Token for vmpooler' @@ -151,6 +158,7 @@ class Vmfloaty c.summary = 'Prints the status of vmpooler' c.description = '' c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com' + c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| url = options.url ||= config['url'] @@ -164,6 +172,7 @@ class Vmfloaty c.summary = 'Prints the summary of vmpooler' c.description = '' c.example 'Gets the current day summary of vmpooler', 'floaty summary --url http://vmpooler.example.com' + c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| url = options.url ||= config['url'] @@ -177,6 +186,7 @@ class Vmfloaty c.summary = 'Retrieves or deletes a token' c.description = '' c.example '', '' + c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--user STRING', String, 'User to authenticate with' c.option '--token STRING', String, 'Token for vmpooler' From d99e401bb0b0773a5f2e172e892d2bc984b0f734 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 16:53:14 -0700 Subject: [PATCH 16/33] Add verbose variable in command handling --- lib/vmfloaty.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 11208b0..ebdcb42 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -27,6 +27,7 @@ class Vmfloaty c.option '--token STRING', String, 'Token for vmpooler' c.option '--os STRING', String, 'Operating systems to retrieve' c.action do |args, options| + verbose = options.verbose || config['verbose'] token = options.token user = options.user ||= config['user'] url = options.url ||= config['url'] @@ -54,6 +55,7 @@ class Vmfloaty c.option '--filter STRING', String, 'A filter to apply to the list' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| + verbose = options.verbose || config['verbose'] filter = options.filter url = options.url ||= config['url'] @@ -70,6 +72,7 @@ class Vmfloaty c.option '--url STRING', String, 'URL of vmpooler' c.option '--host STRING', String, 'Hostname to query' c.action do |args, options| + verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] hostname = options.hostname @@ -89,6 +92,7 @@ class Vmfloaty c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)' c.option '--tags HASH', Hash, 'free-form VM tagging' c.action do |args, options| + verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] hostname = options.hostname lifetime = options.lifetime @@ -108,6 +112,7 @@ class Vmfloaty c.option '--hosts STRING', String, 'Hostname(s) to delete' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| + verbose = options.verbose || config['verbose'] hosts = options.hosts url = options.url ||= config['url'] @@ -125,6 +130,7 @@ class Vmfloaty c.option '--host STRING', String, 'Hostname to modify' c.option '--token STRING', String, 'Token for vmpooler' c.action do |args, options| + verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] hostname = options.hostname token = options.token @@ -144,6 +150,7 @@ class Vmfloaty c.option '--token STRING', String, 'Token for vmpooler' c.option '--snapshot STRING', String, 'SHA of snapshot' c.action do |args, options| + verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] hostname = options.hostname token = options.token @@ -161,6 +168,7 @@ class Vmfloaty c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| + verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] Pooler.status(url) @@ -175,6 +183,7 @@ class Vmfloaty c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| + verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] Pooler.summary(url) @@ -191,6 +200,7 @@ class Vmfloaty c.option '--user STRING', String, 'User to authenticate with' c.option '--token STRING', String, 'Token for vmpooler' c.action do |args, options| + verbose = options.verbose || config['verbose'] action = args.first url = options.url ||= config['url'] token = options.token From 44c5315bfd536fe1018618f20765216ef84ed4da Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 21:37:12 -0700 Subject: [PATCH 17/33] Move printing to command class instead of pooler class This commit makes the pooler class more of a library rather than a helper class that prints the response body of api requests --- lib/vmfloaty.rb | 24 ++++++++++++++++-------- lib/vmfloaty/pooler.rb | 17 +++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index ebdcb42..39049ea 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -39,7 +39,8 @@ class Vmfloaty end unless os_types.nil? - Pooler.retrieve(os_types, token, url) + response = Pooler.retrieve(os_types, token, url) + puts response else puts 'You did not provide an OS to get' end @@ -59,7 +60,8 @@ class Vmfloaty filter = options.filter url = options.url ||= config['url'] - Pooler.list(url, filter) + os_list = Pooler.list(url, filter) + puts os_list end end @@ -76,7 +78,8 @@ class Vmfloaty url = options.url ||= config['url'] hostname = options.hostname - Pooler.query(url, hostname) + query = Pooler.query(url, hostname) + puts query end end @@ -99,7 +102,8 @@ class Vmfloaty tags = options.tags token = options.token - Pooler.modify(url, hostname, token, lifetime, tags) + res_body = Pooler.modify(url, hostname, token, lifetime, tags) + puts res_body end end @@ -135,7 +139,8 @@ class Vmfloaty hostname = options.hostname token = options.token - Pooler.snapshot(url, hostname, token) + res_body = Pooler.snapshot(url, hostname, token) + puts res_body end end @@ -156,7 +161,8 @@ class Vmfloaty token = options.token snapshot_sha = options.snapshot - Pooler.revert(url, hostname, token, snapshot_sha) + res_body = Pooler.revert(url, hostname, token, snapshot_sha) + puts res_body end end @@ -171,7 +177,8 @@ class Vmfloaty verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] - Pooler.status(url) + status = Pooler.status(url) + puts status end end @@ -186,7 +193,8 @@ class Vmfloaty verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] - Pooler.summary(url) + summary = Pooler.summary(url) + puts summary end end diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index a2f56ee..68c5988 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -15,7 +15,7 @@ class Pooler hosts = response_body end - puts hosts + hosts end def self.retrieve(os_type, token, url) @@ -37,7 +37,8 @@ class Pooler req.body = os_body end - puts JSON.parse(response.body) + res_body = JSON.parse(response.body) + res_body end def self.modify(url, hostname, token, lifetime, tags) @@ -48,7 +49,7 @@ class Pooler response = conn.put "/#{hostname}" res_body = JSON.parse(response.body) - puts res_body + res_body end def self.delete(url, hostname) @@ -68,7 +69,7 @@ class Pooler response = conn.get '/status' res_body = JSON.parse(response.body) - puts res_body + res_body end def self.summary(url) @@ -76,7 +77,7 @@ class Pooler response = conn.get '/summary' res_body = JSON.parse(response.body) - puts res_body + res_body end def self.query(url, hostname) @@ -85,7 +86,7 @@ class Pooler response = conn.get "/vm/#{hostname}" res_body = JSON.parse(response.body) - puts res_body + res_body end def self.snapshot(url, hostname, token) @@ -94,7 +95,7 @@ class Pooler # need to use token response = conn.post "/#{hostname}/snapshot" res_body = JSON.parse(response.body) - puts res_body + res_body end def self.revert(url, hostname, token, snapshot_sha) @@ -103,6 +104,6 @@ class Pooler # need to use token response = conn.post "/#{hostname}/snapshot/#{snapshot}" res_body = JSON.parse(response.body) - puts res_body + res_body end end From 201f59c37624e9acc2de53b2ae397efeaa2715ba Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 21:45:33 -0700 Subject: [PATCH 18/33] Add verbose to methods --- lib/vmfloaty.rb | 26 +++++++++++++------------- lib/vmfloaty/auth.rb | 12 ++++++------ lib/vmfloaty/http.rb | 4 ++-- lib/vmfloaty/pooler.rb | 36 ++++++++++++++++++------------------ 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 39049ea..deab8ec 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -35,11 +35,11 @@ class Vmfloaty pass = password "Enter your password please:", '*' unless options.token - token = Auth.get_token(url, user, pass) + token = Auth.get_token(verbose, url, user, pass) end unless os_types.nil? - response = Pooler.retrieve(os_types, token, url) + response = Pooler.retrieve(verbose, os_types, token, url) puts response else puts 'You did not provide an OS to get' @@ -60,7 +60,7 @@ class Vmfloaty filter = options.filter url = options.url ||= config['url'] - os_list = Pooler.list(url, filter) + os_list = Pooler.list(verbose, url, filter) puts os_list end end @@ -78,7 +78,7 @@ class Vmfloaty url = options.url ||= config['url'] hostname = options.hostname - query = Pooler.query(url, hostname) + query = Pooler.query(verbose, url, hostname) puts query end end @@ -102,7 +102,7 @@ class Vmfloaty tags = options.tags token = options.token - res_body = Pooler.modify(url, hostname, token, lifetime, tags) + res_body = Pooler.modify(verbose, url, hostname, token, lifetime, tags) puts res_body end end @@ -120,7 +120,7 @@ class Vmfloaty hosts = options.hosts url = options.url ||= config['url'] - Pool.delete(url, hosts) + Pool.delete(verbose, url, hosts) end end @@ -139,7 +139,7 @@ class Vmfloaty hostname = options.hostname token = options.token - res_body = Pooler.snapshot(url, hostname, token) + res_body = Pooler.snapshot(verbose, url, hostname, token) puts res_body end end @@ -161,7 +161,7 @@ class Vmfloaty token = options.token snapshot_sha = options.snapshot - res_body = Pooler.revert(url, hostname, token, snapshot_sha) + res_body = Pooler.revert(verbose, url, hostname, token, snapshot_sha) puts res_body end end @@ -177,7 +177,7 @@ class Vmfloaty verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] - status = Pooler.status(url) + status = Pooler.status(verbose, url) puts status end end @@ -193,7 +193,7 @@ class Vmfloaty verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] - summary = Pooler.summary(url) + summary = Pooler.summary(verbose, url) puts summary end end @@ -217,11 +217,11 @@ class Vmfloaty case action when "get" - puts Auth.get_token(url, user, pass) + puts Auth.get_token(verbose, url, user, pass) when "delete" - Auth.delete_token(url, user, pass, token) + Auth.delete_token(verbose, url, user, pass, token) when "status" - Auth.token_status(url, user, pass, token) + Auth.token_status(verbose, url, user, pass, token) else puts "Unknown action: #{action}" end diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index d799153..29ecaa4 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -3,8 +3,8 @@ require 'json' require 'vmfloaty/http' class Auth - def self.get_token(url, user, password) - conn = Http.get_conn(url) + def self.get_token(verbose, url, user, password) + conn = Http.get_conn(verbose, url) resp = conn.post do |req| req.url '/v1/token' @@ -15,26 +15,26 @@ class Auth resp_body end - def self.delete_token(url, user, pass, token) + def self.delete_token(verbose, url, user, pass, token) if token.nil? puts 'You did not provide a token' return end - conn = Http.get_conn(url) + conn = Http.get_conn(verbose, url) response = conn.delete "/v1/token/#{token}" res_body = JSON.parse(response) puts res_body end - def self.token_status(url, user, pass, token) + def self.token_status(verbose, url, user, pass, token) if token.nil? puts 'You did not provide a token' return end - conn = Http.get_conn(url) + conn = Http.get_conn(verbose, url) response = conn.get "/v1/token/#{token}" res_body = JSON.parse(response.body) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 086f0c0..2750cbb 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -1,10 +1,10 @@ require 'faraday' class Http - def self.get_conn(url) + def self.get_conn(verbose, url) conn = Faraday.new(:url => url) do |faraday| faraday.request :url_encoded - faraday.response :logger + faraday.response :logger if verbose faraday.adapter Faraday.default_adapter end diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 68c5988..bdcf37c 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -3,8 +3,8 @@ require 'vmfloaty/http' require 'json' class Pooler - def self.list(url, os_filter=nil) - conn = Http.get_conn(url) + def self.list(verbose, url, os_filter=nil) + conn = Http.get_conn(verbose, url) response = conn.get '/vm' response_body = JSON.parse(response.body) @@ -18,9 +18,9 @@ class Pooler hosts end - def self.retrieve(os_type, token, url) + def self.retrieve(verbose, os_type, token, url) os = os_type.split(',') - conn = Http.get_conn(url) + conn = Http.get_conn(verbose, url) os_body = {} os.each do |os_type| @@ -41,9 +41,9 @@ class Pooler res_body end - def self.modify(url, hostname, token, lifetime, tags) + def self.modify(verbose, url, hostname, token, lifetime, tags) modify_body = {'lifetime'=>lifetime, 'tags'=>tags} - conn = Http.get_conn(url) + conn = Http.get_conn(verbose, url) # need to use token response = conn.put "/#{hostname}" @@ -52,9 +52,9 @@ class Pooler res_body end - def self.delete(url, hostname) + def self.delete(verbose, url, hostname) hosts = hostnames.split(',') - conn = Http.get_conn(url) + conn = Http.get_conn(verbose, url) hosts.each do |host| puts "Scheduling host #{host} for deletion" @@ -64,24 +64,24 @@ class Pooler end end - def self.status(url) - conn = Http.get_conn(url) + def self.status(verbose, url) + conn = Http.get_conn(verbose, url) response = conn.get '/status' res_body = JSON.parse(response.body) res_body end - def self.summary(url) - conn = Http.get_conn(url) + def self.summary(verbose, url) + conn = Http.get_conn(verbose, url) response = conn.get '/summary' res_body = JSON.parse(response.body) res_body end - def self.query(url, hostname) - conn = Http.get_conn(url) + def self.query(verbose, url, hostname) + conn = Http.get_conn(verbose, url) response = conn.get "/vm/#{hostname}" res_body = JSON.parse(response.body) @@ -89,8 +89,8 @@ class Pooler res_body end - def self.snapshot(url, hostname, token) - conn = Http.get_conn(url) + def self.snapshot(verbose, url, hostname, token) + conn = Http.get_conn(verbose, url) # need to use token response = conn.post "/#{hostname}/snapshot" @@ -98,8 +98,8 @@ class Pooler res_body end - def self.revert(url, hostname, token, snapshot_sha) - conn = Http.get_conn(url) + def self.revert(verbose, url, hostname, token, snapshot_sha) + conn = Http.get_conn(verbose, url) # need to use token response = conn.post "/#{hostname}/snapshot/#{snapshot}" From 832169a0667732ad85f9b2577ab2c2432a9b517d Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 21:53:40 -0700 Subject: [PATCH 19/33] Check if url is nil before making connection --- lib/vmfloaty/http.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 2750cbb..22fb874 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -2,6 +2,11 @@ require 'faraday' class Http def self.get_conn(verbose, url) + if url.nil? + STDERR.puts "The url you provided was empty" + exit 1 + end + conn = Faraday.new(:url => url) do |faraday| faraday.request :url_encoded faraday.response :logger if verbose From 4198321127608be2d15bce4653e943552f57fcb7 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:01:47 -0700 Subject: [PATCH 20/33] Remove /v1 from auth class --- lib/vmfloaty/auth.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 29ecaa4..3e7ebde 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -7,7 +7,7 @@ class Auth conn = Http.get_conn(verbose, url) resp = conn.post do |req| - req.url '/v1/token' + req.url '/token' req.headers['Content-Type'] = 'application/json' end @@ -23,7 +23,7 @@ class Auth conn = Http.get_conn(verbose, url) - response = conn.delete "/v1/token/#{token}" + response = conn.delete "/token/#{token}" res_body = JSON.parse(response) puts res_body end @@ -36,7 +36,7 @@ class Auth conn = Http.get_conn(verbose, url) - response = conn.get "/v1/token/#{token}" + response = conn.get "/token/#{token}" res_body = JSON.parse(response.body) puts res_body end From 607a679a81e4407e3492a4c0d6284270f0472acf Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:15:01 -0700 Subject: [PATCH 21/33] Add http request method that takes user/password combo --- lib/vmfloaty/auth.rb | 10 +++++----- lib/vmfloaty/http.rb | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 3e7ebde..3bbc94c 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -4,7 +4,7 @@ require 'vmfloaty/http' class Auth def self.get_token(verbose, url, user, password) - conn = Http.get_conn(verbose, url) + conn = Http.get_conn(verbose, url, user, password) resp = conn.post do |req| req.url '/token' @@ -15,26 +15,26 @@ class Auth resp_body end - def self.delete_token(verbose, url, user, pass, token) + def self.delete_token(verbose, url, user, password, token) if token.nil? puts 'You did not provide a token' return end - conn = Http.get_conn(verbose, url) + conn = Http.get_conn(verbose, url, user, password) response = conn.delete "/token/#{token}" res_body = JSON.parse(response) puts res_body end - def self.token_status(verbose, url, user, pass, token) + def self.token_status(verbose, url, user, password, token) if token.nil? puts 'You did not provide a token' return end - conn = Http.get_conn(verbose, url) + conn = Http.get_conn(verbose, url, user, password) response = conn.get "/token/#{token}" res_body = JSON.parse(response.body) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 22fb874..47f892a 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -15,4 +15,25 @@ class Http return conn end + + def self.get_conn(verbose, url, user, password) + if url.nil? + STDERR.puts "The url you provided was empty" + exit 1 + end + + if user.nil? + STDERR.puts "You did not provide a user to authenticate with" + exit 1 + end + + conn = Faraday.new(:url => url) do |faraday| + faraday.request :url_encoded + faraday.request :basic_auth, user, password + faraday.response :logger if verbose + faraday.adapter Faraday.default_adapter + end + + return conn + end end From ad003d474b6cdc49efb84f828ad04732d15eac60 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:21:06 -0700 Subject: [PATCH 22/33] Move error messages to STDERR and exit 1 --- lib/vmfloaty/auth.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 3bbc94c..eff9d8e 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -17,8 +17,8 @@ class Auth def self.delete_token(verbose, url, user, password, token) if token.nil? - puts 'You did not provide a token' - return + STDERR.puts 'You did not provide a token' + exit 1 end conn = Http.get_conn(verbose, url, user, password) @@ -30,8 +30,8 @@ class Auth def self.token_status(verbose, url, user, password, token) if token.nil? - puts 'You did not provide a token' - return + STDERR.puts 'You did not provide a token' + exit 1 end conn = Http.get_conn(verbose, url, user, password) From 8616397c8ece394343474347cc10cf9fdfd0913c Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:23:55 -0700 Subject: [PATCH 23/33] Properly handle token requests --- lib/vmfloaty/auth.rb | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index eff9d8e..ac262e5 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -12,7 +12,13 @@ class Auth end resp_body = JSON.parse(resp.body) - resp_body + if resp_body["ok"] + return resp_body["token"] + else + STDERR.puts "There was a problem with your request:" + puts resp_body + exit 1 + end end def self.delete_token(verbose, url, user, password, token) @@ -25,7 +31,13 @@ class Auth response = conn.delete "/token/#{token}" res_body = JSON.parse(response) - puts res_body + if resp_body["ok"] + puts resp_body + else + STDERR.puts "There was a problem with your request:" + puts resp_body + exit 1 + end end def self.token_status(verbose, url, user, password, token) @@ -38,6 +50,13 @@ class Auth response = conn.get "/token/#{token}" res_body = JSON.parse(response.body) - puts res_body + + if resp_body["ok"] + puts resp_body + else + STDERR.puts "There was a problem with your request:" + puts resp_body + exit 1 + end end end From e194f04ea9b95adbb590380b95895555e6dc4073 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:26:27 -0700 Subject: [PATCH 24/33] Simplify operating system hash for get method --- lib/vmfloaty/pooler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index bdcf37c..2308712 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -27,7 +27,7 @@ class Pooler unless os_body.has_key?(os_type) os_body[os_type] = 1 else - os_body[os_type] = os_body[os_type] + 1 + os_body[os_type]++ end end From eee7aab7600f352a4c13b0c38db0b40907dbe599 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:50:01 -0700 Subject: [PATCH 25/33] Add http class for api token requests --- lib/vmfloaty/http.rb | 21 +++++++++++++++++++++ lib/vmfloaty/pooler.rb | 24 +++++++++++------------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 47f892a..336097f 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -36,4 +36,25 @@ class Http return conn end + + def self.get_conn_with_token(verbose, url, token) + if url.nil? + STDERR.puts "The url you provided was empty" + exit 1 + end + + if token.nil? + STDERR.puts "The token you provided was empty" + exit 1 + end + + conn = Faraday.new(:url => url) do |faraday| + faraday.request :url_encoded + faraday.request :token_auth, token + faraday.response :logger if verbose + faraday.adapter Faraday.default_adapter + end + + return conn + end end diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 2308712..5eda873 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -20,20 +20,19 @@ class Pooler def self.retrieve(verbose, os_type, token, url) os = os_type.split(',') - conn = Http.get_conn(verbose, url) + conn = Http.get_conn_with_token(verbose, url, token) os_body = {} os.each do |os_type| unless os_body.has_key?(os_type) os_body[os_type] = 1 else - os_body[os_type]++ + os_body[os_type] += 1 end end response = conn.post do |req| req.url '/vm' - req.headers['Content-Type'] = 'application/json' req.body = os_body end @@ -43,10 +42,11 @@ class Pooler def self.modify(verbose, url, hostname, token, lifetime, tags) modify_body = {'lifetime'=>lifetime, 'tags'=>tags} - conn = Http.get_conn(verbose, url) + conn = Http.get_conn_with_token(verbose, url, token) - # need to use token - response = conn.put "/#{hostname}" + response = conn.put do |req| + req.url "/vm/#{hostname}" + end res_body = JSON.parse(response.body) res_body @@ -58,7 +58,7 @@ class Pooler hosts.each do |host| puts "Scheduling host #{host} for deletion" - response = conn.delete "/#{host}" + response = conn.delete "/vm/#{host}" res_body = JSON.parse(response.body) puts res_body end @@ -90,19 +90,17 @@ class Pooler end def self.snapshot(verbose, url, hostname, token) - conn = Http.get_conn(verbose, url) + conn = Http.get_conn_with_token(verbose, url, token) - # need to use token - response = conn.post "/#{hostname}/snapshot" + response = conn.post "/vm/#{hostname}/snapshot" res_body = JSON.parse(response.body) res_body end def self.revert(verbose, url, hostname, token, snapshot_sha) - conn = Http.get_conn(verbose, url) + conn = Http.get_conn_with_token(verbose, url, token) - # need to use token - response = conn.post "/#{hostname}/snapshot/#{snapshot}" + response = conn.post "/vm/#{hostname}/snapshot/#{snapshot}" res_body = JSON.parse(response.body) res_body end From 992a37cabfea801708a879200edd839c4fb93039 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:53:39 -0700 Subject: [PATCH 26/33] Update http method name that takes user/password --- lib/vmfloaty.rb | 1 - lib/vmfloaty/auth.rb | 6 +++--- lib/vmfloaty/http.rb | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index deab8ec..adb4e49 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -4,7 +4,6 @@ require 'rubygems' require 'commander' require 'yaml' require 'vmfloaty/auth' -require 'vmfloaty/http' require 'vmfloaty/pooler' class Vmfloaty diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index ac262e5..fef56b6 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -4,7 +4,7 @@ require 'vmfloaty/http' class Auth def self.get_token(verbose, url, user, password) - conn = Http.get_conn(verbose, url, user, password) + conn = Http.get_conn_with_auth(verbose, url, user, password) resp = conn.post do |req| req.url '/token' @@ -27,7 +27,7 @@ class Auth exit 1 end - conn = Http.get_conn(verbose, url, user, password) + conn = Http.get_conn_with_auth(verbose, url, user, password) response = conn.delete "/token/#{token}" res_body = JSON.parse(response) @@ -46,7 +46,7 @@ class Auth exit 1 end - conn = Http.get_conn(verbose, url, user, password) + conn = Http.get_conn_with_auth(verbose, url, user, password) response = conn.get "/token/#{token}" res_body = JSON.parse(response.body) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 336097f..fc352bd 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -16,7 +16,7 @@ class Http return conn end - def self.get_conn(verbose, url, user, password) + def self.get_conn_with_auth(verbose, url, user, password) if url.nil? STDERR.puts "The url you provided was empty" exit 1 From 9c69e752e76751e64cab51c5023f2e1a49eebe79 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:56:17 -0700 Subject: [PATCH 27/33] Add more error handling around token command --- lib/vmfloaty.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index adb4e49..bd94992 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -221,8 +221,10 @@ class Vmfloaty Auth.delete_token(verbose, url, user, pass, token) when "status" Auth.token_status(verbose, url, user, pass, token) + when nil + STDERR.puts "No action provided" else - puts "Unknown action: #{action}" + STDERR.puts "Unknown action: #{action}" end end end From c9dd50716ef19fd0f23f5a0130cb985fa87659b9 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 15 Sep 2015 10:26:54 -0700 Subject: [PATCH 28/33] Do not verify ssl certificates --- lib/vmfloaty/http.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index fc352bd..ecc6b2f 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -7,7 +7,7 @@ class Http exit 1 end - conn = Faraday.new(:url => url) do |faraday| + conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| faraday.request :url_encoded faraday.response :logger if verbose faraday.adapter Faraday.default_adapter @@ -27,7 +27,7 @@ class Http exit 1 end - conn = Faraday.new(:url => url) do |faraday| + conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| faraday.request :url_encoded faraday.request :basic_auth, user, password faraday.response :logger if verbose @@ -48,7 +48,7 @@ class Http exit 1 end - conn = Faraday.new(:url => url) do |faraday| + conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| faraday.request :url_encoded faraday.request :token_auth, token faraday.response :logger if verbose From 562b81186771e8dbc900b217e280a7ac1b6a7cee Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 15 Sep 2015 10:40:36 -0700 Subject: [PATCH 29/33] Simplify token requests --- lib/vmfloaty/auth.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index fef56b6..4d6c70b 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -6,10 +6,7 @@ class Auth def self.get_token(verbose, url, user, password) conn = Http.get_conn_with_auth(verbose, url, user, password) - resp = conn.post do |req| - req.url '/token' - req.headers['Content-Type'] = 'application/json' - end + resp = conn.post "/token" resp_body = JSON.parse(resp.body) if resp_body["ok"] From 9e4e7b3dabbdb17b3f4b153297f3b727f3f59dcc Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 15 Sep 2015 10:44:51 -0700 Subject: [PATCH 30/33] Do not ask for password if token is provided --- lib/vmfloaty.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index bd94992..a8fc29f 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -27,13 +27,13 @@ class Vmfloaty c.option '--os STRING', String, 'Operating systems to retrieve' c.action do |args, options| verbose = options.verbose || config['verbose'] - token = options.token + token = options.token || config['token'] user = options.user ||= config['user'] url = options.url ||= config['url'] os_types = options.os - pass = password "Enter your password please:", '*' unless options.token + pass = password "Enter your password please:", '*' token = Auth.get_token(verbose, url, user, pass) end From 2ea08f95a59e2acfabeb485f25ad85f3b7987b5a Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 15 Sep 2015 11:24:17 -0700 Subject: [PATCH 31/33] Fix res_body variable typo in Auth class --- lib/vmfloaty/auth.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 4d6c70b..54224b3 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -8,12 +8,12 @@ class Auth resp = conn.post "/token" - resp_body = JSON.parse(resp.body) - if resp_body["ok"] - return resp_body["token"] + res_body = JSON.parse(resp.body) + if res_body["ok"] + return res_body["token"] else STDERR.puts "There was a problem with your request:" - puts resp_body + puts res_body exit 1 end end @@ -28,11 +28,11 @@ class Auth response = conn.delete "/token/#{token}" res_body = JSON.parse(response) - if resp_body["ok"] - puts resp_body + if res_body["ok"] + puts res_body else STDERR.puts "There was a problem with your request:" - puts resp_body + puts res_body exit 1 end end @@ -48,11 +48,11 @@ class Auth response = conn.get "/token/#{token}" res_body = JSON.parse(response.body) - if resp_body["ok"] - puts resp_body + if res_body["ok"] + puts res_body else STDERR.puts "There was a problem with your request:" - puts resp_body + puts res_body exit 1 end end From 7fb07a9a6983d4a48da2bd2c2e456707b420efe8 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 15 Sep 2015 11:29:47 -0700 Subject: [PATCH 32/33] Properly parse response body in token delete --- lib/vmfloaty/auth.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 54224b3..4816e04 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -27,7 +27,7 @@ class Auth conn = Http.get_conn_with_auth(verbose, url, user, password) response = conn.delete "/token/#{token}" - res_body = JSON.parse(response) + res_body = JSON.parse(response.body) if res_body["ok"] puts res_body else From 9fc9820120c73eb756982c33157b043395fcb02e Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 15 Sep 2015 13:05:57 -0700 Subject: [PATCH 33/33] Simplify how floaty gets vms Instead of building a json hash for each requested vm, sub the host strings commas with + and append it to the request url. --- lib/vmfloaty.rb | 8 ++++++++ lib/vmfloaty/pooler.rb | 21 +++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index a8fc29f..b7e104c 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -25,12 +25,20 @@ class Vmfloaty c.option '--url STRING', String, 'URL of vmpooler' c.option '--token STRING', String, 'Token for vmpooler' c.option '--os STRING', String, 'Operating systems to retrieve' + c.option '--notoken', 'Makes a request without a token' 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'] os_types = options.os + no_token = options.notoken + + unless no_token.nil? + response = Pooler.retrieve(verbose, os_types, token, url) + puts response + return + end unless options.token pass = password "Enter your password please:", '*' diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 5eda873..25b79c7 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -19,22 +19,15 @@ class Pooler end def self.retrieve(verbose, os_type, token, url) - os = os_type.split(',') - conn = Http.get_conn_with_token(verbose, url, token) - os_body = {} - - os.each do |os_type| - unless os_body.has_key?(os_type) - os_body[os_type] = 1 - else - os_body[os_type] += 1 - end + os = os_type.gsub(',','+') + if token.nil? + conn = Http.get_conn(verbose, url) + else + conn = Http.get_conn_with_token(verbose, url, token) + conn.headers['X-AUTH-TOKEN'] end - response = conn.post do |req| - req.url '/vm' - req.body = os_body - end + response = conn.post "/vm/#{os}" res_body = JSON.parse(response.body) res_body