Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
make wizard run from Commander and setup default command line args
Browse files Browse the repository at this point in the history
  • Loading branch information
John (J5) Palmieri committed Jul 11, 2012
1 parent 88cf3e1 commit 68f0e83
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 27 deletions.
35 changes: 13 additions & 22 deletions bin/rhc
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/rhc/commands/base.rb
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions 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
49 changes: 45 additions & 4 deletions lib/rhc/config.rb
Expand Up @@ -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()

Expand All @@ -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"

Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/rhc/helpers.rb
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/rhc/wizard.rb
Expand Up @@ -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:
Expand Down

0 comments on commit 68f0e83

Please sign in to comment.