(#1) Update query, snapshot, and revert

This commit is contained in:
Brian Cain 2015-09-06 12:16:08 -07:00
parent f773e0a5a6
commit 02527b9665
4 changed files with 91 additions and 29 deletions

View file

@ -18,10 +18,10 @@ gem install vmfloaty
get Gets a vm or vms based on the os flag get Gets a vm or vms based on the os flag
help Display global or [command] help documentation help Display global or [command] help documentation
list Shows a list of available vms from the pooler list Shows a list of available vms from the pooler
modify modify Modify a vms tags and TTL
query query Get information about a given vm
revert revert Reverts a vm to a specified snapshot
snapshot snapshot Takes a snapshot of a given vm
status Prints the status of vmpooler status Prints the status of vmpooler
summary Prints the summary of vmpooler summary Prints the summary of vmpooler

View file

@ -52,7 +52,6 @@ class Vmfloaty
c.option '--filter STRING', String, 'A filter to apply to the list' c.option '--filter STRING', String, 'A filter to apply to the list'
c.option '--url STRING', String, 'URL of vmpooler' c.option '--url STRING', String, 'URL of vmpooler'
c.action do |args, options| c.action do |args, options|
# Do something or c.when_called Floaty::Commands::Query
filter = options.filter filter = options.filter
url = options.url ||= config['url'] url = options.url ||= config['url']
@ -62,23 +61,37 @@ class Vmfloaty
command :query do |c| command :query do |c|
c.syntax = 'floaty query [options]' c.syntax = 'floaty query [options]'
c.summary = '' c.summary = 'Get information about a given vm'
c.description = '' c.description = ''
c.example 'description', 'command example' c.example 'Get information about a sample host', 'floaty query --url http://vmpooler.example.com --host myvmhost.example.com'
c.option '--some-switch', 'Some switch that does something' c.option '--url STRING', String, 'URL of vmpooler'
c.option '--host STRING', String, 'Hostname to query'
c.action do |args, options| 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
end end
command :modify do |c| command :modify do |c|
c.syntax = 'floaty modify [options]' c.syntax = 'floaty modify [options]'
c.summary = '' c.summary = 'Modify a vms tags and TTL'
c.description = '' c.description = ''
c.example 'description', 'command example' 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| 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
end end
@ -93,29 +106,43 @@ class Vmfloaty
hosts = options.hosts hosts = options.hosts
url = options.url ||= config['url'] url = options.url ||= config['url']
Pool.delete(hosts, url) Pool.delete(url, hosts)
end end
end end
command :snapshot do |c| command :snapshot do |c|
c.syntax = 'floaty snapshot [options]' c.syntax = 'floaty snapshot [options]'
c.summary = '' c.summary = 'Takes a snapshot of a given vm'
c.description = '' c.description = ''
c.example 'description', 'command example' c.example 'Takes a snapshot for a given host', 'floaty snapshot --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
c.option '--some-switch', 'Some switch that does something' 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| 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
end end
command :revert do |c| command :revert do |c|
c.syntax = 'floaty revert [options]' c.syntax = 'floaty revert [options]'
c.summary = '' c.summary = 'Reverts a vm to a specified snapshot'
c.description = '' c.description = ''
c.example 'description', 'command example' 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 '--some-switch', 'Some switch that does something' 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| 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
end end

View file

@ -1,17 +1,19 @@
require 'faraday' require 'faraday'
require 'json'
require 'vmfloaty/http' require 'vmfloaty/http'
class Auth class Auth
def self.get_token(user, url, password) def self.get_token(user, url, password)
conn = Http.get_conn(url) conn = Http.get_conn(url)
#resp = conn.post do |req| resp = conn.post do |req|
# req.url '/v1/token' req.url '/v1/token'
# req.headers['Content-Type'] = 'application/json' req.headers['Content-Type'] = 'application/json'
# req.user = user req.user = user
# end end
# if ok: true, return token # if ok: true, return token
puts 'Got token' resp_body = JSON.parse(resp.body)
resp_body
end end
def self.delete_token(user, token) def self.delete_token(user, token)

View file

@ -40,12 +40,18 @@ class Pooler
puts JSON.parse(response.body) puts JSON.parse(response.body)
end end
def self.modify(hostname, token, url, lifetime, tags) def self.modify(url, hostname, token, lifetime, tags)
modify_body = {'lifetime'=>lifetime, 'tags'=>tags} modify_body = {'lifetime'=>lifetime, 'tags'=>tags}
conn = Http.get_conn(url) conn = Http.get_conn(url)
# need to use token
response = conn.put "/v1/#{hostname}"
res_body = JSON.parse(response.body)
puts res_body
end end
def self.delete(hostnames, url) def self.delete(url, hostname)
hosts = hostnames.split(',') hosts = hostnames.split(',')
conn = Http.get_conn(url) conn = Http.get_conn(url)
@ -72,4 +78,31 @@ class Pooler
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
puts res_body puts res_body
end 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 end