Skip to content

Commit d6201cb

Browse files
committed
[1110_uffizzi_platform] refactor
1 parent a180400 commit d6201cb

2 files changed

Lines changed: 76 additions & 46 deletions

File tree

lib/uffizzi/cli/install.rb

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@ class Cli::Install < Thor
1212
VALUES_FILE_NAME = 'helm_values.yaml'
1313
DEFAULT_ISSUER = 'letsencrypt'
1414
DEFAULT_NAMESPACE = 'default'
15+
DEFAULT_APP_PREFIX = 'uffizzi'
1516

1617
desc 'application', 'Install uffizzi to cluster'
17-
method_option :namespace, required: false, type: :string
18-
method_option :domain, required: false, type: :string
19-
method_option :'user-email', required: false, type: :string
20-
method_option :'acme-email', required: false, type: :string
21-
method_option :'user-password', required: false, type: :string
22-
method_option :'controller-password', required: false, type: :string
18+
method_option :namespace, type: :string
19+
method_option :domain, type: :string
20+
method_option :'user-email', type: :string
21+
method_option :'acme-email', type: :string
22+
method_option :'user-password', type: :string
23+
method_option :'controller-password', type: :string
2324
method_option :issuer, type: :string, enum: ['letsencrypt', 'zerossl']
24-
method_option :'wildcard-cert-path', required: false, type: :string
25-
method_option :'wildcard-key-path', required: false, type: :string
26-
method_option :'without-wildcard-tls', required: false, type: :boolean
25+
method_option :'wildcard-cert-path', type: :string
26+
method_option :'wildcard-key-path', type: :string
27+
method_option :'without-wildcard-tls', type: :boolean
28+
method_option :repo, type: :string
29+
method_option :'print-values', type: :boolean
2730
def application
2831
run_installation do
29-
if options.present?
32+
if options.except(:repo, :'print-values').present?
3033
validate_installation_options
3134
else
3235
ask_installation_params
@@ -35,34 +38,56 @@ def application
3538
end
3639

3740
desc 'wildcard-tls', 'Add wildcard tls from files'
38-
method_option :domain, required: true, type: :string
39-
method_option :cert, required: true, type: :string
40-
method_option :key, required: true, type: :string
41-
method_option :namespace, required: false, type: :string
42-
def add_wildcard_tls
41+
method_option :domain, type: :string
42+
method_option :cert, type: :string
43+
method_option :key, type: :string
44+
method_option :namespace, type: :string
45+
def wildcard_tls
4346
kubectl_exists?
4447

45-
params = {
46-
namespace: options[:namespace],
47-
domain: options[:domain],
48-
wildcard_cert_path: options[:cert],
49-
wildcard_key_path: options[:key],
50-
}
48+
params = if options.present? && wildcard_tls_options_valid?
49+
{
50+
namespace: options[:namespace] || DEFAULT_NAMESPACE,
51+
domain: options[:domain],
52+
wildcard_cert_path: options[:cert],
53+
wildcard_key_path: options[:key],
54+
}
55+
else
56+
namespace = Uffizzi.prompt.ask('Namespace: ', required: true, default: DEFAULT_NAMESPACE)
57+
domain = Uffizzi.prompt.ask('Domain: ', required: true, default: 'example.com')
58+
wildcard_cert_paths = ask_wildcard_cert(has_user_wildcard_cert: true)
59+
60+
{ namespace: namespace, domain: domain }.merge(wildcard_cert_paths)
61+
end
5162

5263
kubectl_add_wildcard_tls(params)
5364
end
5465

5566
private
5667

68+
def wildcard_tls_options_valid?
69+
required_options = [:domain, :cert, :key]
70+
missing_options = required_options - options.symbolize_keys.keys
71+
72+
return true if missing_options.empty?
73+
74+
rendered_missing_options = missing_options.map { |o| "'--#{o}'" }.join(', ')
75+
76+
Uffizzi.ui.say_error_and_exit("No value provided for required options #{rendered_missing_options}")
77+
end
78+
5779
def run_installation
5880
kubectl_exists?
5981
helm_exists?
6082
params = yield
6183
helm_values = build_helm_values(params)
84+
return Uffizzi.ui.say(helm_values.to_yaml) if options[:'print-values']
85+
6286
create_helm_values_file(helm_values)
63-
helm_set_repo
87+
helm_set_repo unless options[:repo]
6488
helm_set_release(params.fetch(:namespace))
6589
kubectl_add_wildcard_tls(params) if params[:wildcard_cert_path] && params[:wildcard_key_path]
90+
delete_helm_values_file
6691
end
6792

6893
def kubectl_exists?
@@ -116,7 +141,8 @@ def helm_install(namespace)
116141
Uffizzi.ui.say('Start helm release installation')
117142

118143
release_name = namespace
119-
cmd = "helm install #{release_name} #{HELM_REPO_NAME}/#{CHART_NAME}" \
144+
repo = options[:repo] || "#{HELM_REPO_NAME}/#{CHART_NAME}"
145+
cmd = "helm install #{release_name} #{repo}" \
120146
" --values #{helm_values_file_path}" \
121147
" --namespace #{namespace}" \
122148
' --create-namespace' \
@@ -139,8 +165,8 @@ def kubectl_add_wildcard_tls(params)
139165
execute_command(cmd)
140166
end
141167

142-
def ask_wildcard_cert
143-
has_user_wildcard_cert = Uffizzi.prompt.yes?('Uffizzi use a wildcard tls certificate. Do you have it?')
168+
def ask_wildcard_cert(has_user_wildcard_cert: nil)
169+
has_user_wildcard_cert ||= Uffizzi.prompt.yes?('Uffizzi use a wildcard tls certificate. Do you have it?')
144170

145171
if has_user_wildcard_cert
146172
cert_path = Uffizzi.prompt.ask('Path to cert: ', required: true)
@@ -153,7 +179,7 @@ def ask_wildcard_cert
153179
Uffizzi.ui.say('You can add wildcard cert later with command:')
154180
Uffizzi.ui.say('uffizzi install wildcard-tls --domain your.domain.com --cert /path/to/cert --key /path/to/key')
155181

156-
{ wildcard_cert_path: nil, wildcard_key_path: nil }
182+
{}
157183
end
158184

159185
def ask_installation_params
@@ -181,38 +207,37 @@ def ask_installation_params
181207
end
182208

183209
def validate_installation_options
184-
base_params = {
185-
namespace: options[:namespace] || DEFAULT_NAMESPACE,
186-
domain: options[:domain],
187-
user_email: options[:'user-email'] || "admin@#{options[:domain]}",
188-
user_password: options[:'user-password'] || generate_password,
189-
controller_password: options[:'controller-password'] || generate_password,
190-
cert_email: options[:'acme-email'] || options[:'user-email'],
191-
cluster_issuer: options[:issuer] || DEFAULT_ISSUER,
192-
wildcard_cert_path: nil,
193-
wildcard_key_path: nil,
194-
}
210+
installation_options = build_installation_options
195211

196-
return base_params if options[:'without-wildcard-tls']
212+
if options[:'without-wildcard-tls']
213+
return installation_options.except(:wildcard_cert_path, :wildcard_key_path)
214+
end
197215

198216
empty_key = [:'wildcard-cert-path', :'wildcard-key-path'].detect { |k| options[k].nil? }
199217

200218
if empty_key.present?
201-
return Uffizzi.ui.say_error_and_exit("#{empty_key} is required or use the flag --without-wildcard-tls")
219+
Uffizzi.ui.say_error_and_exit("#{empty_key} is required or use the flag --without-wildcard-tls")
202220
end
221+
end
203222

204-
wildcard_params = {
223+
def build_installation_options
224+
{
225+
namespace: options[:namespace] || DEFAULT_NAMESPACE,
226+
domain: options[:domain],
227+
user_email: options[:'user-email'] || "admin@#{options[:domain]}",
228+
user_password: options[:'user-password'] || generate_password,
229+
controller_password: options[:'controller-password'] || generate_password,
230+
cert_email: options[:'acme-email'] || options[:'user-email'],
231+
cluster_issuer: options[:issuer] || DEFAULT_ISSUER,
205232
wildcard_cert_path: options[:'wildcard-cert-path'],
206233
wildcard_key_path: options[:'wildcard-key-path'],
207234
}
208-
209-
base_params.merge(wildcard_params)
210235
end
211236

212237
def build_helm_values(params)
213238
domain = params.fetch(:domain)
214239
namespace = params.fetch(:namespace)
215-
app_host = ['app', domain].join('.')
240+
app_host = [DEFAULT_APP_PREFIX, domain].join('.')
216241

217242
{
218243
app_url: "https://#{app_host}",
@@ -232,7 +257,7 @@ def build_helm_values(params)
232257
},
233258
'uffizzi-controller' => {
234259
ingress: {
235-
hostname: "controller.#{domain}",
260+
disabled: true,
236261
},
237262
clusterIssuer: params.fetch(:cluster_issuer),
238263
certEmail: params.fetch(:cert_email),
@@ -264,6 +289,10 @@ def create_helm_values_file(values)
264289
File.write(helm_values_file_path, values.to_yaml)
265290
end
266291

292+
def delete_helm_values_file
293+
File.delete(helm_values_file_path) if File.exist?(helm_values_file_path)
294+
end
295+
267296
def helm_values_file_path
268297
File.join(helm_values_dir_path, VALUES_FILE_NAME)
269298
end

test/uffizzi/cli/install_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def setup
1616
def test_install_by_wizard
1717
@mock_prompt.promise_question_answer('Uffizzi use a wildcard tls certificate. Do you have it?', 'n')
1818
@mock_prompt.promise_question_answer('Do you want to add wildcard certificate later?', 'y')
19+
@mock_prompt.promise_question_answer('Namespace: ', 'uffizzi')
1920
@mock_prompt.promise_question_answer('Domain: ', 'my-domain.com')
2021
@mock_prompt.promise_question_answer('User email: ', 'admin@my-domain.com')
2122
@mock_prompt.promise_question_answer('User password: ', 'password')
@@ -30,7 +31,7 @@ def test_install_by_wizard
3031
@mock_shell.promise_execute(/helm list/, stdout: [].to_json)
3132
@mock_shell.promise_execute(/helm install/, stdout: { info: { status: 'deployed' } }.to_json)
3233

33-
@install.by_wizard('uffizzi')
34+
@install.application
3435

3536
last_message = Uffizzi.ui.last_message
3637
assert_match('deployed', last_message)
@@ -45,7 +46,7 @@ def test_install_by_options
4546
@mock_shell.promise_execute(/helm install/, stdout: { info: { status: 'deployed' } }.to_json)
4647

4748
@install.options = command_options(domain: 'my-domain.com', 'without-wildcard-tls' => true)
48-
@install.by_options('uffizzi')
49+
@install.application
4950

5051
last_message = Uffizzi.ui.last_message
5152
assert_match('deployed', last_message)

0 commit comments

Comments
 (0)