diff --git a/bin/rhc b/bin/rhc index b9a185019..c146e17f2 100755 --- a/bin/rhc +++ b/bin/rhc @@ -60,31 +60,22 @@ begin run_setup_wizard_if_needed system("rhc-port-forward #{get_args} 2>&1") retcode = $?.exitstatus - when "server" - run_setup_wizard_if_needed - begin - require 'rhc/cli' - RHC::CLI.start(ARGV) - retcode = 0 - rescue SystemExit => e - retcode = e.status - end - when "setup" - if ARGV.include?('--help') or ARGV.include?('-h') or ARGV.include?('help') - puts "Usage: rhc setup" - puts "Runs the setup wizard to configure your account" - exit 0 - end - - w = RHC::RerunWizard.new(RHC::Config.local_config_path) - success = w.run - retcode = 0 - retcode = 1 unless success when "-h", "--help", "help", nil p_usage 0 else - puts "Invalid rhc command: #{ARGV[0]}" - p_usage + if ["server", "setup"].include?(ARGV[0]) + run_setup_wizard_if_needed if ARGV[0] != "setup" + begin + require 'rhc/cli' + RHC::CLI.start(ARGV) + retcode = 0 + rescue SystemExit => e + retcode = e.status + end + else + puts "Invalid rhc command: #{ARGV[0]}" + p_usage + end end if retcode == nil diff --git a/lib/rhc/commands/base.rb b/lib/rhc/commands/base.rb index 3cc249fbc..5be58fa48 100644 --- a/lib/rhc/commands/base.rb +++ b/lib/rhc/commands/base.rb @@ -36,6 +36,8 @@ def client def config @config ||= begin RHC::Config.set_opts_config(options.config) if options.config + RHC::Config.password = options.password if options.password + RHC::Config.opts_login = options.rhlogin if options.rhlogin RHC::Config end end diff --git a/lib/rhc/commands/setup.rb b/lib/rhc/commands/setup.rb new file mode 100644 index 000000000..f54b4fc47 --- /dev/null +++ b/lib/rhc/commands/setup.rb @@ -0,0 +1,20 @@ +require 'rhc/commands/base' +require 'rhc/wizard' +require 'rhc/config' + +module RHC::Commands + class Setup < Base + + summary "Configure your OpenShift account" + description "Runs the setup wizard to configure your OpenShift account." + def run + # TODO: pass in config object to wizard instead of it using RHC::Config directly + w = RHC::RerunWizard.new(config.config_path) + s = w.run + + # exit 0 on success 1 otherwise + 0 if s + 1 + end + end +end diff --git a/lib/rhc/config.rb b/lib/rhc/config.rb index e2dd55ea5..fec8080bd 100644 --- a/lib/rhc/config.rb +++ b/lib/rhc/config.rb @@ -23,7 +23,8 @@ def self.set_defaults @@defaults = RHC::Vendor::ParseConfig.new() @@global_config = nil @@local_config = nil - @@opts_config = nil + @@opts_config = nil # config file passed in the options + @@opts = RHC::Vendor::ParseConfig.new() # option switches that override config file @@default_proxy = nil @@env_config = RHC::Vendor::ParseConfig.new() @@ -36,6 +37,17 @@ def self.set_defaults @@home_dir = File.expand_path("~") @@home_conf_path = File.join(@@home_dir, '.openshift') @@local_config_path = File.join(@@home_conf_path, @@conf_name) + + # config path passed in on the command line + @@opts_config_path = nil + + # authoritive config path + # this can be @@local_config_path or @@opts_config_path + # @@opts_config_path trumps + # this is used to determine where config options should be written to + # when a script modifies the config such as in rhc setup + @@config_path = @@local_config_path + @@ssh_priv_key_file_path = "#{@@home_dir}/.ssh/id_rsa" @@ssh_pub_key_file_path = "#{@@home_dir}/.ssh/id_rsa.pub" @@ -66,8 +78,10 @@ def self.home_dir=(home_dir) end def self.[](key) + raise KeyError("Please use RHC::Config.password to access the password config") if key == "password" + # evaluate in cascading order - configs = [@@opts_config, @@env_config, @@local_config, @@global_config, @@defaults] + configs = [@@opts, @@opts_config, @@env_config, @@local_config, @@global_config, @@defaults] result = nil configs.each do |conf| result = conf[key] if !conf.nil? @@ -86,9 +100,24 @@ def self.config_user(username) @@defaults.add('default_rhlogin', username) end + def self.opts_login=(username) + @@opts['default_rhlogin'] = username + end + + # password is not allowed in config files and can only be passed on comman line + def self.password=(password) + @@opts['password'] = password + end + + def self.password + @@opts['password'] + end + def self.set_local_config(confpath) begin - @@local_config = RHC::Vendor::ParseConfig.new(File.expand_path(confpath)) + @@local_config_path = File.expand_path(confpath) + @@config_path = @@local_config_path if @@opts_config_path.nil? + @@local_config = RHC::Vendor::ParseConfig.new(@@local_config_path) rescue Errno::EACCES => e say "Could not open config file: #{e.message}" exit 253 @@ -97,7 +126,9 @@ def self.set_local_config(confpath) def self.set_opts_config(confpath) begin - @@opts_config = RHC::Vendor::ParseConfig.new(File.expand_path(confpath)) + @@opts_config_path = File.expand_path(confpath) + @@config_path = @@opts_config_path + @@opts_config = RHC::Vendor::ParseConfig.new(@@opts_config_path) if File.exists?(@@opts_config_path) rescue Errno::EACCES => e say "Could not open config file: #{e.message}" exit 253 @@ -137,6 +168,16 @@ def self.should_run_ssh_wizard? not File.exists? @@ssh_priv_key_file_path end + ## + # config_path + # + # authoritive configuration path + # this is used to determine where config options should be written to + # when a script modifies the config such as in rhc setup + def self.config_path + @@config_path + end + def self.local_config_path @@local_config_path end diff --git a/lib/rhc/helpers.rb b/lib/rhc/helpers.rb index e98e0113f..2d24b7c9a 100644 --- a/lib/rhc/helpers.rb +++ b/lib/rhc/helpers.rb @@ -49,6 +49,9 @@ def config raise "Operations requiring configuration must define a config accessor" end + global_option '-l', '--rhlogin login', "Red Hat login (RHN or OpenShift login with OpenShift access)" + global_option '-p', '--password password', "RHLogin password" + def openshift_server config.get_value('libra_server') end diff --git a/lib/rhc/wizard.rb b/lib/rhc/wizard.rb index a3cf0e656..bf4c16716 100644 --- a/lib/rhc/wizard.rb +++ b/lib/rhc/wizard.rb @@ -79,7 +79,9 @@ def login_stage @username = ask("To connect to #{@libra_server} enter your OpenShift login (email or Red Hat login id): ") do |q| q.default = RHC::Config.default_rhlogin end - @password = RHC::get_password + + @password = RHC::Config.password + @password = RHC::get_password if @password.nil? end # Confirm username / password works: