mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
The following pool configuration would allow a pool to be aliased in POST
requests as 'centos-6-x86_64', 'centos-6-amd64', or 'centos-6-64':
````yaml
- name: 'centos-6-x86_64'
alias: [ 'centos-6-amd64', 'centos-6-64' ]
template: 'templates/centos-6-x86_64'
folder: 'vmpooler/centos-6-x86_64'
datastore: 'instance1'
size: 5
````
The 'alias' configuration can be either a string or an array.
Note that even when requesting an alias, the pool's 'name' is returned in
the JSON response:
````
$ curl -d '{"centos-6-64":"1"}' --url vmpooler/api/v1/vm
````
````json
{
"ok": true,
"centos-6-x86_64": {
"hostname": "cuna2qeahwlzji7"
},
"domain": "company.com"
}
````
84 lines
2 KiB
Ruby
84 lines
2 KiB
Ruby
require 'rubygems' unless defined?(Gem)
|
|
|
|
module Vmpooler
|
|
require 'date'
|
|
require 'json'
|
|
require 'open-uri'
|
|
require 'rbvmomi'
|
|
require 'redis'
|
|
require 'sinatra/base'
|
|
require 'time'
|
|
require 'timeout'
|
|
require 'yaml'
|
|
|
|
%w( api graphite logger pool_manager vsphere_helper ).each do |lib|
|
|
begin
|
|
require "vmpooler/#{lib}"
|
|
rescue LoadError
|
|
require File.expand_path(File.join(File.dirname(__FILE__), 'vmpooler', lib))
|
|
end
|
|
end
|
|
|
|
def self.config(filepath='vmpooler.yaml')
|
|
# Load the configuration file
|
|
config_file = File.expand_path(filepath)
|
|
parsed_config = YAML.load_file(config_file)
|
|
|
|
# Set some defaults
|
|
parsed_config[:redis] ||= {}
|
|
parsed_config[:redis]['server'] ||= 'localhost'
|
|
parsed_config[:redis]['data_ttl'] ||= 168
|
|
|
|
parsed_config[:config]['task_limit'] ||= 10
|
|
parsed_config[:config]['vm_checktime'] ||= 15
|
|
parsed_config[:config]['vm_lifetime'] ||= 24
|
|
|
|
# Create an index of pool aliases
|
|
parsed_config[:pools].each do |pool|
|
|
if pool['alias']
|
|
if pool['alias'].kind_of?(Array)
|
|
pool['alias'].each do |a|
|
|
parsed_config[:alias] ||= {}
|
|
parsed_config[:alias][a] = pool['name']
|
|
end
|
|
elsif pool['alias'].kind_of?(String)
|
|
parsed_config[:alias][pool['alias']] = pool['name']
|
|
end
|
|
end
|
|
end
|
|
|
|
if parsed_config[:graphite]['server']
|
|
parsed_config[:graphite]['prefix'] ||= 'vmpooler'
|
|
end
|
|
|
|
if parsed_config[:tagfilter]
|
|
parsed_config[:tagfilter].keys.each do |tag|
|
|
parsed_config[:tagfilter][tag] = Regexp.new(parsed_config[:tagfilter][tag])
|
|
end
|
|
end
|
|
|
|
parsed_config[:uptime] = Time.now
|
|
|
|
parsed_config
|
|
end
|
|
|
|
def self.new_redis(host='localhost')
|
|
Redis.new(host: host)
|
|
end
|
|
|
|
def self.new_logger(logfile)
|
|
Vmpooler::Logger.new logfile
|
|
end
|
|
|
|
def self.new_graphite(server)
|
|
if server.nil? or server.empty? or server.length == 0
|
|
nil
|
|
else
|
|
Vmpooler::Graphite.new server
|
|
end
|
|
end
|
|
|
|
def self.pools(conf)
|
|
conf[:pools]
|
|
end
|
|
end
|