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