lib/capstrap/cli.rb in capstrap-0.2.2 vs lib/capstrap/cli.rb in capstrap-0.3.0

- old
+ new

@@ -11,163 +11,128 @@ def version puts "capstrap v#{Capstrap::VERSION}" end desc "ruby HOST", "Install an RVM ruby on remote SSH host HOST" - method_option "ruby", :type => :string, :desc => - "Version of ruby to install.", :default => "ree-1.8.7" method_option "default", :type => :boolean, :desc => "Set this ruby to be RVM default." def ruby(ssh_host) @ssh_host = ssh_host - abort ">> HOST must be set" unless @ssh_host - setup_config options - - config.find_and_execute_task "rvm:install:#{options[:ruby]}" - if options[:default] - config.find_and_execute_task "rvm:default:#{options[:ruby]}" - end + exec_ruby end desc "chef HOST", "Install chef gem on remote SSH host HOST" - method_option "ruby", :type => :string, :desc => - "Version of ruby to install.", :default => "ree-1.8.7" def chef(ssh_host) @ssh_host = ssh_host - abort ">> HOST must be set" unless @ssh_host - setup_config options - - invoke :ruby, [ssh_host], :ruby => options[:ruby], :default => true - config.find_and_execute_task "chef:install:lib" + exec_chef end desc "solo HOST", "Install chef cookbooks & config on remote SSH host HOST" - method_option "ruby", :type => :string, - :desc => "Version of ruby to install.", - :default => "ree-1.8.7", - :aliases => "-r" - method_option "cookbooks-repo", :type => :string, - :desc => "Chef cookbooks git repository URL.", - :aliases => "-c" - method_option "cookbooks-path", :type => :string, - :desc => "Install path to chef cookbooks git repository.", - :default => "/var/chef-solo", - :aliases => "-p" - method_option "cookbooks-rake-update", :type => :boolean, :desc => - "Run rake update vs. git submodule init/update when updating cookbooks repo", - :default => false, - :aliases => "-u" - method_option "config-repo", :type => :string, - :desc => "Chef configuration git repository URL.", - :aliases => "-C" - method_option "config-path", :type => :string, - :desc => "Install path to chef configuration git repository.", - :default => "/etc/chef", - :aliases => "-P" - method_option "config-rake-update", :type => :boolean, :desc => - "Run rake update vs. git submodule init/update when updating config repo", - :default => false, - :aliases => "-U" def solo(ssh_host) @ssh_host = ssh_host - abort ">> HOST must be set" unless @ssh_host - setup_config options - - unless options["cookbooks-repo"] - abort ">> --cookbooks-repo=<git_url> must be set" - end - unless options["config-repo"] - abort ">> --config-repo=<git_url> must be set" - end - - invoke :chef, [ssh_host], :ruby => options[:ruby], :default => true - config.find_and_execute_task "chef:install:cookbooks" - config.find_and_execute_task "chef:install:config" + assert_repos_set + exec_solo end desc "execute HOST", "Executes chef solo config on remote SSH host HOST" - method_option "ruby", :type => :string, - :desc => "Version of ruby to install.", - :default => "ree-1.8.7", - :aliases => "-r" - method_option "cookbooks-repo", :type => :string, - :desc => "Chef cookbooks git repository URL.", - :aliases => "-c" - method_option "cookbooks-path", :type => :string, - :desc => "Install path to chef cookbooks git repository.", - :default => "/var/chef-solo", - :aliases => "-p" - method_option "cookbooks-rake-update", :type => :boolean, :desc => - "Run rake update vs. git submodule init/update when updating cookbooks repo", - :default => false, - :aliases => "-u" - method_option "config-repo", :type => :string, - :desc => "Chef configuration git repository URL.", - :aliases => "-C" - method_option "config-path", :type => :string, - :desc => "Install path to chef configuration git repository.", - :default => "/etc/chef", - :aliases => "-P" - method_option "config-rake-update", :type => :boolean, :desc => - "Run rake update vs. git submodule init/update when updating config repo", - :default => false, - :aliases => "-U" def execute(ssh_host) @ssh_host = ssh_host - abort ">> HOST must be set" unless @ssh_host + setup_config options + assert_repos_set + exec_execute + end + desc "update HOST", "Updates and executes chef solo on remote SSH host HOST" + def update(ssh_host) + @ssh_host = ssh_host setup_config options + exec_update + end - unless options["cookbooks-repo"] + [:ruby, :chef, :solo, :execute, :update].each do |task| + method_option "config", :for => task, :type => :string, + :desc => "Read from alternative configuration.", + :default => File.join(ENV['HOME'], ".capstraprc"), + :aliases => "-f" + + method_option "ruby", :for => task, :type => :string, + :desc => "Version of ruby to install.", + :default => "ree-1.8.7" + end + + [:chef, :solo, :execute, :update].each do |task| + method_option "cookbooks-path", :for => task, :type => :string, + :desc => "Install path to chef cookbooks git repository.", + :default => "/var/chef-solo", + :aliases => "-p" + + method_option "cookbooks-rake-update", :for => task, :type => :boolean, + :desc => "Run rake update vs. git submodule init/update when updating cookbooks repo", + :default => false, + :aliases => "-u" + + method_option "config-path", :for => task, :type => :string, + :desc => "Install path to chef configuration git repository.", + :default => "/etc/chef", + :aliases => "-P" + + method_option "config-rake-update", :for => task, :type => :boolean, + :desc => "Run rake update vs. git submodule init/update when updating config repo", + :default => false, + :aliases => "-U" + end + + [:chef, :solo, :execute].each do |task| + method_option "cookbooks-repo", :for => task, :type => :string, + :desc => "Chef cookbooks git repository URL.", + :aliases => "-c" + + method_option "config-repo", :for => task, :type => :string, + :desc => "Chef configuration git repository URL.", + :aliases => "-C" + end + + private + + def assert_repos_set + unless config.fetch(:cookbooks_repo) abort ">> --cookbooks-repo=<git_url> must be set" end - unless options["config-repo"] + unless config.fetch(:config_repo) abort ">> --config-repo=<git_url> must be set" end + end - invoke :solo, [ssh_host], :ruby => options[:ruby], :default => true, - :"cookbooks-repo" => options["cookbooks-repo"], - :"cookbooks-path" => options["cookbooks-path"], - :"config-repo" => options["config-repo"], - :"config-path" => options["config-path"] - config.find_and_execute_task "chef:execute:solo" + def exec_ruby + config.find_and_execute_task "rvm:install:#{config.fetch(:ruby)}" + if options[:default] + config.find_and_execute_task "rvm:default:#{config.fetch(:ruby)}" + end end - desc "update HOST", "Updates and executes chef solo on remote SSH host HOST" - method_option "ruby", :type => :string, - :desc => "Version of ruby to install.", - :default => "ree-1.8.7", - :aliases => "-r" - method_option "cookbooks-path", :type => :string, - :desc => "Install path to chef cookbooks git repository.", - :default => "/var/chef-solo", - :aliases => "-p" - method_option "cookbooks-rake-update", :type => :boolean, :desc => - "Run rake update vs. git submodule init/update when updating cookbooks repo", - :default => false, - :aliases => "-u" - method_option "config-path", :type => :string, - :desc => "Install path to chef configuration git repository.", - :default => "/etc/chef", - :aliases => "-P" - method_option "config-rake-update", :type => :boolean, :desc => - "Run rake update vs. git submodule init/update when updating config repo", - :default => false, - :aliases => "-U" - def update(ssh_host) - @ssh_host = ssh_host - abort ">> HOST must be set" unless @ssh_host + def exec_chef + exec_ruby + config.find_and_execute_task "chef:install:lib" + end - setup_config options + def exec_solo + exec_chef + config.find_and_execute_task "chef:install:cookbooks" + config.find_and_execute_task "chef:install:config" + end - config.find_and_execute_task "chef:execute:update" + def exec_execute + exec_solo + config.find_and_execute_task "chef:execute:solo" end - private + def exec_update + config.find_and_execute_task "chef:execute:update" + end def config @config ||= prep_config end @@ -182,10 +147,18 @@ Capstrap::Chef.load_into(config) config end - def setup_config(options) + def setup_config(cli_options) + abort ">> HOST must be set" unless @ssh_host + + options = Hash.new + options.merge!(cli_options) + if File.exists?(options["config"]) + options.merge!(YAML::load_file(options["config"])) + end + [ {:sym => :ruby, :opt => "ruby"}, {:sym => :cookbooks_repo, :opt => "cookbooks-repo"}, {:sym => :cookbooks_path, :opt => "cookbooks-path"}, {:sym => :config_repo, :opt => "config-repo"},