Allow VM metadata to be queryable via 'GET'

This commit is contained in:
Scott Schneider 2014-11-03 09:28:40 -08:00
parent 4363d8e067
commit 4035114152

View file

@ -228,16 +228,6 @@ module Vmpooler
JSON.pretty_generate(result)
end
get '/vm/:template/?' do
content_type :json
result = {}
result[params[:template]] = {}
result[params[:template]]['hosts'] = $redis.smembers('vmpooler__ready__'+params[:template])
JSON.pretty_generate(result)
end
post '/vm/:template/?' do
content_type :json
@ -296,6 +286,34 @@ module Vmpooler
JSON.pretty_generate(result)
end
get '/vm/:hostname/?' do
content_type :json
result = {}
result['ok'] = false
if ( $config[:config]['domain'] and params[:hostname] =~ /^\w+\.#{$config[:config]['domain']}$/ )
params[:hostname] = params[:hostname][/[^\.]+/]
end
if $redis.exists('vmpooler__vm__'+params[:hostname])
result['ok'] = true
result[params[:hostname]] = {}
result[params[:hostname]]['template'] = $redis.hget('vmpooler__vm__'+params[:hostname], 'template')
result[params[:hostname]]['lifetime'] = $redis.hget('vmpooler__vm__'+params[:hostname], 'lifetime') || $config[:config]['vm_lifetime']
result[params[:hostname]]['running'] = ((Time.now - Time.parse($redis.hget('vmpooler__active__'+result[params[:hostname]]['template'], params[:hostname])))/60/60).round(2)
if ( $config[:config]['domain'] )
result[params[:hostname]]['domain'] = $config[:config]['domain']
end
end
JSON.pretty_generate(result)
end
delete '/vm/:hostname/?' do
content_type :json
@ -325,6 +343,10 @@ module Vmpooler
result['ok'] = false
if ( $config[:config]['domain'] and params[:hostname] =~ /^\w+\.#{$config[:config]['domain']}$/ )
params[:hostname] = params[:hostname][/[^\.]+/]
end
if $redis.exists('vmpooler__vm__'+params[:hostname])
jdata = JSON.parse(request.body.read)