mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
This commit adds options for setting redis port and password. Without this change it is not possible to specify the redis port or password.
110 lines
3.2 KiB
Ruby
110 lines
3.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'
|
|
require 'set'
|
|
|
|
%w[api graphite logger pool_manager statsd dummy_statsd generic_connection_pool providers].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')
|
|
# Take the config either from an ENV config variable or from a config file
|
|
if ENV['VMPOOLER_CONFIG']
|
|
config_string = ENV['VMPOOLER_CONFIG']
|
|
# Parse the YAML config into a Hash
|
|
# Whitelist the Symbol class
|
|
parsed_config = YAML.safe_load(config_string, [Symbol])
|
|
else
|
|
# Take the name of the config file either from an ENV variable or from the filepath argument
|
|
config_file = ENV['VMPOOLER_CONFIG_FILE'] || filepath
|
|
parsed_config = YAML.load_file(config_file)
|
|
end
|
|
|
|
exit unless parsed_config
|
|
|
|
# Bail out if someone attempts to start vmpooler with dummy authentication
|
|
# without enbaling debug mode.
|
|
if parsed_config[:auth]['provider'] == 'dummy'
|
|
unless ENV['VMPOOLER_DEBUG']
|
|
warning = [
|
|
'Dummy authentication should not be used outside of debug mode',
|
|
'please set environment variable VMPOOLER_DEBUG to \'true\' if you want to use dummy authentication'
|
|
]
|
|
|
|
raise warning.join(";\s")
|
|
end
|
|
end
|
|
|
|
# Set some configuration 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
|
|
parsed_config[:config]['prefix'] ||= ''
|
|
|
|
# Create an index of pool aliases
|
|
parsed_config[:pool_names] = Set.new
|
|
parsed_config[:pools].each do |pool|
|
|
parsed_config[:pool_names] << pool['name']
|
|
if pool['alias']
|
|
if pool['alias'].is_a?(Array)
|
|
pool['alias'].each do |a|
|
|
parsed_config[:alias] ||= {}
|
|
parsed_config[:alias][a] = pool['name']
|
|
parsed_config[:pool_names] << a
|
|
end
|
|
elsif pool['alias'].is_a?(String)
|
|
parsed_config[:alias][pool['alias']] = pool['name']
|
|
parsed_config[:pool_names] << pool['alias']
|
|
end
|
|
end
|
|
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', port = nil, password = nil)
|
|
Redis.new(host: host, port: port, password: password)
|
|
end
|
|
|
|
def self.new_logger(logfile)
|
|
Vmpooler::Logger.new logfile
|
|
end
|
|
|
|
def self.new_metrics(params)
|
|
if params[:statsd]
|
|
Vmpooler::Statsd.new(params[:statsd])
|
|
elsif params[:graphite]
|
|
Vmpooler::Graphite.new(params[:graphite])
|
|
else
|
|
Vmpooler::DummyStatsd.new
|
|
end
|
|
end
|
|
|
|
def self.pools(conf)
|
|
conf[:pools]
|
|
end
|
|
end
|