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
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue