#!/usr/bin/env ruby # Copyright (C) 2011 RightScale, Inc, All Rights Reserved Worldwide. # # THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO RIGHTSCALE # AND CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use, # reproduction, modification, or disclosure of this program is # strictly prohibited. Any use of this program by an authorized # licensee is strictly subject to the terms and conditions, # including confidentiality obligations, set forth in the applicable # License Agreement between RightScale.com, Inc. and # the licensee $stdout.sync = true require File.join(File.dirname(__FILE__), '..', 'lib', 'rconf') module RightConf class Configurer include ProgressReporter def self.run opts = Trollop::options do version "rconf #{VERSION} (c) 2011 RightScale" banner <<-EOS #{DESCRIPTION} Usage: rconf [options] where [options] are: EOS opt :configurators, 'Show available configurators' opt :config, 'Set path to configuration file', :type => :string opt :output, 'Output file (output to STDOUT by default)', :type => :string opt :verbose, 'Print debug output' end if opts[:config].nil? && !opts[:configurators] opts[:config] = Dir['./*.rc'] if opts[:config].empty? Trollop::die :config, "not used and could not find a '.rc' file in the working directory" else opts[:config] = opts[:config].first end end if opts[:output] begin FileUtils.mkdir_p(File.dirname(opts[:output])) rescue Exception => e Trollop::die :output, "Failed to initialize output file: #{e.message}" end end new.configure(opts) end # Actually configure environment # # === Parameters # options[:config](String):: Configuration file # options[:output](String):: Output file, optional # # === Return # true:: Always return true def configure(options) if options[:configurators] puts "The following configurators are registered:\n\n" ConfiguratorRegistry.each do |key, configurator| puts "== #{key} ==".bold puts configurator.desc puts 'Settings:' max_size = configurator.all_settings.keys.map(&:to_s).map(&:size).max configurator.all_settings.each do |name, desc| num_spaces = max_size - name.to_s.size + 1 print " - #{name.to_s.blue}:#{' ' * num_spaces}#{desc}" required_settings = configurator.required_settings || [] if required_settings.include?(name) puts ' [required]'.green else puts end end puts end exit 0 end ProgressReporter.report_to_stdout ProgressReporter.report_to_file(options[:output]) if options[:output] Command.set_verbose if options[:verbose] begin lang = Language.load(options[:config]) report_fatal("Validation of configuration file failed:\n -#{lang.validation_errors.join("\n -").map(&:red)}") unless lang.validation_errors.empty? Dir.chdir(File.dirname(options[:config])) { lang.configurators.each(&:run) } report("Successfully configured #{File.basename(options[:config], '.rc').blue} for #{Platform.family.to_s.blue}") rescue Exception => e raise if e.is_a?(SystemExit) report_fatal("Execution failed with exception '#{e.message.red}'\n#{e.backtrace.join("\n").grey}") ensure lang.configurators.each { |c| report("\n!!NOTE: #{c.post_note}\n".green) if c.post_note } end end end end # Yeeeehaaaa! RightConf::Configurer.run