module Merb module Virtuozzo class << self ## # Returns the expected location of a deployment configuration file. def config_file() Merb.root / "config" / "virtuozzo.yml" end ## # Returnes the expected location of a sample configuration file. def sample_dest() Merb.root / "config" / "virtuozzo.yml.sample" end ## # Returnes the location of the template for a sample configuration file. def sample_source() File.dirname(__FILE__) / "virtuozzo.yml.sample" end ## # Copies the template for the sample configuration file into the # application's config folder. def copy_sample_config FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest) end ## # Parses the configuration file converting keys to symbols and stores # values to Merb::Plugins.config[:virtuozzo]. def config @config ||= begin # Convert string keys to symbols full_config = Erubis.load_yaml_file(config_file) config = (Merb::Plugins.config[:virtuozzo] = {}) (full_config[Merb.environment.to_sym] || full_config[Merb.environment] || full_config[:development]).each do |key, value| config[key.to_sym] = value end config end end ## # Extract and merge default values from configuration options. def config_options(config = {}) options = {} options[:host] = (config[:host] || "https://localhost:4646") options[:username] = (config[:username] || config[:user] || "") options[:password] = config[:password] || "" options end ## # Singleton method for accessing an established connection session. def connection @connection ||= connect end ## # Establishes connection or logs errors returning the connection on # success. def connect if File.exists?(config_file) Merb.logger.info!("Connecting to the Virtuozzo Agent at '#{config[:host]}' ...") o = config_options(config) connection_options = [ o[:host], o[:username], o[:password], o.except(:host, :username, :password) ] @connection = ::Virtuozzo::SOAP::Connection.new(*connection_options) Merb.logger.error!("Connection Error: #{e}") unless @connection connection else copy_sample_config Merb.logger.set_log(STDERR) Merb.logger.error! "No virtuozzo.yml file found in #{Merb.root}/config." Merb.logger.error! "A sample file was created called config/virtuozzo.yml.sample for you to copy and edit." exit(1) end end end end end