Skip to content

Commit 4ab02fa

Browse files
committed
Adding setup command for rmt-cli to update database and scc configs
Resolution for issue#987 : SUSE#987
1 parent dc30add commit 4ab02fa

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

MANUAL.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ You can install and run this wizard like this:
2222

2323
## USAGE
2424

25+
* `rmt-cli setup`:
26+
This command is responsibile for creating and updating the user configurations for RMT. If the user has defined configurations previously and wishes to update them, it interactively suggests these previously set values for user to review before updating them.
27+
2528
* `rmt-cli sync`:
2629
RMT comes with a preconfigured systemd timer to automatically get the latest product and repository data from the SUSE Customer Center over night.
2730
This command triggers the same synchronization instantly.

lib/rmt/cli/main.rb

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def sync
1414
desc 'products', _('List and modify products')
1515
subcommand 'products', RMT::CLI::Products
1616

17+
desc 'setup', _('Configure RMT')
18+
def setup
19+
RMT::CLI::Setup.start
20+
end
21+
1722
desc 'repos', _('List and modify repositories')
1823
subcommand 'repos', RMT::CLI::Repos
1924

lib/rmt/cli/setup.rb

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
class RMT::CLI::Setup < RMT::CLI::Base
2+
CONFIG_FILE = '/etc/rmt.conf'.freeze
3+
DEVELOPMENT_CONFIG_FILE = './config/rmt.yml'.freeze
4+
DB_CONFIG = 'database'.freeze
5+
SCC_CONFIG = 'scc'.freeze
6+
7+
desc 'setup', _('Update user configurations required for RMT setup')
8+
def setup
9+
puts _('-------- Starting RMT server setup ---------')
10+
copy_to_config_file unless File.exists?(CONFIG_FILE)
11+
update_config
12+
confirm_details_and_save
13+
end
14+
15+
private
16+
17+
#Copies the development config file to the main config file for RMT
18+
def copy_to_config_file
19+
puts "\n" + _("Could not find config at %{config_file}") % {config_file: CONFIG_FILE}
20+
puts _('Copying config from %{development_config_file} to %{config_file}') % {development_config_file: DEVELOPMENT_CONFIG_FILE , config_file: CONFIG_FILE }
21+
begin
22+
FileUtils.cp(DEVELOPMENT_CONFIG_FILE, CONFIG_FILE)
23+
puts _('Config copied successfully!')
24+
rescue StandardError => e
25+
puts _('Failed to copy configuration. Error : %{error_message}') % {error_message: e.message}
26+
puts _('Aborting.')
27+
exit(1)
28+
end
29+
end
30+
31+
#Prompts user to specify new configs or uses previously set config
32+
def update_config
33+
@config = YAML.load_file(CONFIG_FILE)
34+
35+
@new_config = {}
36+
puts "\n" + _('Please enter the following database configurations: ')
37+
@new_config[DB_CONFIG] = {}
38+
@new_config[DB_CONFIG]['database'] = input_prompt_generator('database', @config[DB_CONFIG]['database'])
39+
@new_config[DB_CONFIG]['username'] = input_prompt_generator('username', @config[DB_CONFIG]['username'])
40+
@new_config[DB_CONFIG]['password'] = input_prompt_generator('password', @config[DB_CONFIG]['password'], sensitive_data: true)
41+
42+
puts "\n\n" + _('Please enter your SCC credentials: ')
43+
puts _('NOTE: You can find them in https://scc.suse.com under the Proxies tab.')
44+
@new_config[SCC_CONFIG] ||= {}
45+
@new_config[SCC_CONFIG]['username'] = input_prompt_generator('username', @config[SCC_CONFIG]['username'])
46+
@new_config[SCC_CONFIG]['password'] = input_prompt_generator('password', @config[SCC_CONFIG]['password'], sensitive_data: true)
47+
end
48+
49+
def confirm_details_and_save
50+
loop do
51+
input = ask("\n\n" + _('Would you like to save the updated configuration? (y/n/exit) : ')).downcase
52+
53+
case input
54+
when 'y'
55+
#Merging updated configs with the older config for updation and saving result to CONFIG_FILE
56+
@config = @config.deep_merge(@new_config)
57+
begin
58+
File.open(CONFIG_FILE, 'w') {|f| f.write @config.to_yaml }
59+
rescue StandardError
60+
puts _('Failed to save config. Please retry.')
61+
end
62+
puts _('Successfully updated the configuration!')
63+
restart_rmt
64+
break
65+
when 'n'
66+
update_config
67+
when 'exit'
68+
confirmation = ask( _('You are attempting to exit before saving the configuration. Are you sure? (y/n) : ') ).downcase
69+
if confirmation == 'y'
70+
puts _('Aborted')
71+
break
72+
elsif confirmation == 'n'
73+
next
74+
else
75+
puts _('Invalid input. Please try again.')
76+
end
77+
else
78+
puts _('Invalid input. Please try again.')
79+
80+
end
81+
end
82+
83+
end
84+
85+
#Constructs the prompt message to be displayed to users to fetch config field data
86+
#Displays the existing default value to the user, if present
87+
#Example => username (Press enter for John ) :
88+
def input_prompt_generator(config_field, default_value, sensitive_data: false)
89+
prompt = "#{config_field}"
90+
91+
# Show default_value as "*****" for sensitive fields
92+
if !default_value.empty?
93+
prompt << " ("
94+
prompt << _('Press enter for ')
95+
prompt << "#{(sensitive_data ? "*****": default_value)} "
96+
prompt << ")"
97+
end
98+
prompt << " : "
99+
100+
input = ask(prompt, :echo => !sensitive_data)
101+
return input.empty? ? default_value : input
102+
end
103+
104+
#Restarts the RMT process
105+
def restart_rmt
106+
loop do
107+
puts "\n" + _('RMT must be restarted with the updated configuration.')
108+
input = ask( _('Restart RMT automatically? (y/n) : ') ).downcase
109+
case input
110+
when 'y'
111+
begin
112+
puts _('Restarting RMT server. Please wait.')
113+
`systemctl restart rmt-server`
114+
puts _('Successfully restarted RMT server.')
115+
rescue StandardError => e
116+
puts _('Failed to restart RMT server. Error : %{error_message}') % {error_message: e.message}
117+
end
118+
break
119+
when 'n'
120+
puts _('Please restart the RMT server manually.')
121+
break
122+
else
123+
puts _('Invalid input. Please try again.')
124+
end
125+
end
126+
end
127+
128+
end

0 commit comments

Comments
 (0)