Merge pull request #37 from sschneid/queryable_vm_metadata

Queryable VM metadata
This commit is contained in:
Colin 2014-11-03 13:25:34 -08:00
commit 258634e667
3 changed files with 59 additions and 13 deletions

View file

@ -143,6 +143,25 @@ $ curl -d --url vmpooler.company.com/vm/debian-7-i386+debian-7-i386+debian-7-x86
}
```
#### GET /vm/<hostname%gt;
Query a checked-out VM.
```
$ curl --url vmpooler.company.com/vm/pxpmtoonx7fiqg6
```
```json
{
"ok": true,
"pxpmtoonx7fiqg6": {
"template": "centos-6-x86_64",
"lifetime": 12,
"running": 3,
"domain": "company.com"
}
}
```
#### PUT /vm/<hostname>
Modify a checked-out VM.

View file

@ -22,6 +22,16 @@ module Vmpooler
set :environment, 'production'
helpers do
def hostname_shorten hostname
if ( $config[:config]['domain'] and hostname =~ /^\w+\.#{$config[:config]['domain']}$/ )
hostname = hostname[/[^\.]+/]
end
hostname
end
end
get '/' do
erb :dashboard, locals: {
site_name: $config[:config]['site_name'] || '<b>vmpooler</b>',
@ -228,16 +238,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 +296,32 @@ module Vmpooler
JSON.pretty_generate(result)
end
get '/vm/:hostname/?' do
content_type :json
result = {}
result['ok'] = false
params[:hostname] = hostname_shorten(params[:hostname])
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
@ -303,9 +329,7 @@ module Vmpooler
result['ok'] = false
if ( $config[:config]['domain'] and params[:hostname] =~ /^\w+\.#{$config[:config]['domain']}$/ )
params[:hostname] = params[:hostname][/[^\.]+/]
end
params[:hostname] = hostname_shorten(params[:hostname])
$config[:pools].each do |pool|
if $redis.sismember('vmpooler__running__'+pool['name'], params[:hostname])
@ -325,6 +349,8 @@ module Vmpooler
result['ok'] = false
params[:hostname] = hostname_shorten(params[:hostname])
if $redis.exists('vmpooler__vm__'+params[:hostname])
jdata = JSON.parse(request.body.read)

View file

@ -184,6 +184,7 @@ module Vmpooler
# Add VM to Redis inventory ('pending' pool)
$redis.sadd('vmpooler__pending__'+vm['template'], vm['hostname'])
$redis.hset('vmpooler__vm__'+vm['hostname'], 'clone', Time.now)
$redis.hset('vmpooler__vm__'+vm['hostname'], 'template', vm['template'])
# Annotate with creation time, origin template, etc.
configSpec = RbVmomi::VIM.VirtualMachineConfigSpec(