From 4035114152b3ab4931055b5fe93bcaf4b9b51397 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Mon, 3 Nov 2014 09:28:40 -0800 Subject: [PATCH] Allow VM metadata to be queryable via 'GET' --- lib/vmpooler/api.rb | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/vmpooler/api.rb b/lib/vmpooler/api.rb index 7ab4a08..4ae70e4 100644 --- a/lib/vmpooler/api.rb +++ b/lib/vmpooler/api.rb @@ -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)