From 296f9abb1de2eba1ccb2f5b4c3c8a94cc28b471b Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sat, 5 Sep 2015 16:17:45 -0700 Subject: [PATCH] (#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