mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
(#1) Update vmfloaty to use new vmpooler api
This commit also uses commander for arg parsing. This commit is a WIP.
This commit is contained in:
parent
fcb43ac379
commit
296f9abb1d
11 changed files with 266 additions and 145 deletions
3
Gemfile
3
Gemfile
|
|
@ -1,5 +1,6 @@
|
|||
source 'https://rubygems.org'
|
||||
|
||||
gem 'thor'
|
||||
gem 'commander'
|
||||
gem 'faraday'
|
||||
|
||||
gemspec
|
||||
|
|
|
|||
30
README.md
30
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 <OPERATING SYSTEM,...> # Gets a VM
|
||||
floaty help [COMMAND] # Describe available commands or one specific command
|
||||
floaty list [PATTERN] # List all open VMs
|
||||
floaty modify <HOSTNAME> # (TODO STILL) Modify a VM
|
||||
floaty release <HOSTNAME,...> [--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
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
150
lib/vmfloaty.rb
150
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
|
||||
|
|
|
|||
20
lib/vmfloaty/auth.rb
Normal file
20
lib/vmfloaty/auth.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
require 'thor'
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
require 'json'
|
||||
|
||||
class CLI < Thor
|
||||
desc "get <OPERATING SYSTEM,...>", "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 <HOSTNAME>", "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 <HOSTNAME,...> [--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
|
||||
|
|
@ -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
|
||||
13
lib/vmfloaty/http.rb
Normal file
13
lib/vmfloaty/http.rb
Normal file
|
|
@ -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
|
||||
66
lib/vmfloaty/pooler.rb
Normal file
66
lib/vmfloaty/pooler.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
module Vmfloaty
|
||||
module CLI
|
||||
VERSION = '0.1.0'
|
||||
end
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue