11# frozen_string_literal: true
22
3- require 'byebug'
43require 'uffizzi'
54require 'uffizzi/config_file'
65
@@ -11,7 +10,7 @@ class Cli::Install < Thor
1110 CHART_NAME = 'uffizzi-app'
1211 VALUES_FILE_NAME = 'helm_values.yaml'
1312 DEFAULT_ISSUER = 'letsencrypt'
14- DEFAULT_NAMESPACE = 'default '
13+ DEFAULT_NAMESPACE = 'uffizzi '
1514 DEFAULT_APP_PREFIX = 'uffizzi'
1615
1716 desc 'application' , 'Install uffizzi to cluster'
@@ -20,7 +19,6 @@ class Cli::Install < Thor
2019 method_option :'user-email' , type : :string
2120 method_option :'acme-email' , type : :string
2221 method_option :'user-password' , type : :string
23- method_option :'controller-password' , type : :string
2422 method_option :issuer , type : :string , enum : [ 'letsencrypt' , 'zerossl' ]
2523 method_option :'wildcard-cert-path' , type : :string
2624 method_option :'wildcard-key-path' , type : :string
@@ -42,10 +40,11 @@ def application
4240 method_option :cert , type : :string
4341 method_option :key , type : :string
4442 method_option :namespace , type : :string
43+ method_option :repo , type : :string
4544 def wildcard_tls
4645 kubectl_exists?
4746
48- params = if options . present? && wildcard_tls_options_valid?
47+ params = if options . except ( :repo ) . present? && wildcard_tls_options_valid?
4948 {
5049 namespace : options [ :namespace ] || DEFAULT_NAMESPACE ,
5150 domain : options [ :domain ] ,
@@ -55,18 +54,25 @@ def wildcard_tls
5554 else
5655 namespace = Uffizzi . prompt . ask ( 'Namespace: ' , required : true , default : DEFAULT_NAMESPACE )
5756 domain = Uffizzi . prompt . ask ( 'Domain: ' , required : true , default : 'example.com' )
58- wildcard_cert_paths = ask_wildcard_cert ( has_user_wildcard_cert : true )
57+ wildcard_cert_paths = ask_wildcard_cert ( has_user_wildcard_cert : true , domain : domain )
5958
6059 { namespace : namespace , domain : domain } . merge ( wildcard_cert_paths )
6160 end
6261
6362 kubectl_add_wildcard_tls ( params )
63+ helm_values = helm_get_values ( namespace , namespace )
64+ helm_values [ 'uffizzi-controller' ] [ 'tlsPerDeploymentEnabled' ] = false . to_s
65+ create_helm_values_file ( helm_values )
66+ helm_set_repo unless options [ :repo ]
67+ helm_install ( release_name : namespace , namespace : namespace , repo : options [ :repo ] )
6468 end
6569
70+ default_task :application
71+
6672 private
6773
6874 def wildcard_tls_options_valid?
69- required_options = [ :domain , :cert , :key ]
75+ required_options = [ :namespace , : domain, :cert , :key ]
7076 missing_options = required_options - options . symbolize_keys . keys
7177
7278 return true if missing_options . empty?
@@ -85,9 +91,12 @@ def run_installation
8591
8692 create_helm_values_file ( helm_values )
8793 helm_set_repo unless options [ :repo ]
88- helm_set_release ( params . fetch ( :namespace ) )
94+ helm_install ( release_name : params [ :namespace ] , namespace : params [ :namespace ] , repo : options [ :repo ] )
8995 kubectl_add_wildcard_tls ( params ) if params [ :wildcard_cert_path ] && params [ :wildcard_key_path ]
9096 delete_helm_values_file
97+
98+ Uffizzi . ui . say ( 'Helm release is deployed' )
99+ Uffizzi . ui . say ( "The uffizzi application url is https://#{ DEFAULT_APP_PREFIX } .#{ params [ :domain ] } " )
91100 end
92101
93102 def kubectl_exists?
@@ -107,16 +116,6 @@ def helm_set_repo
107116 helm_repo_add
108117 end
109118
110- def helm_set_release ( namespace )
111- releases = helm_release_list ( namespace )
112- release = releases . detect { |r | r [ 'name' ] == namespace }
113- if release . present?
114- Uffizzi . ui . say_error_and_exit ( "The release #{ release [ 'name' ] } already exists with status #{ release [ 'status' ] } " )
115- end
116-
117- helm_install ( namespace )
118- end
119-
120119 def helm_repo_add
121120 cmd = "helm repo add #{ HELM_REPO_NAME } https://uffizzicloud.github.io/uffizzi"
122121 execute_command ( cmd )
@@ -130,32 +129,31 @@ def helm_repo_search
130129 end
131130 end
132131
133- def helm_release_list ( namespace )
134- cmd = "helm list -n #{ namespace } -o json"
135- result = execute_command ( cmd , say : false )
136-
137- JSON . parse ( result )
138- end
139-
140- def helm_install ( namespace )
132+ def helm_install ( release_name :, namespace :, repo :)
141133 Uffizzi . ui . say ( 'Start helm release installation' )
142134
143- release_name = namespace
144- repo = options [ :repo ] || "#{ HELM_REPO_NAME } /#{ CHART_NAME } "
145- cmd = "helm install #{ release_name } #{ repo } " \
135+ repo = repo || "#{ HELM_REPO_NAME } /#{ CHART_NAME } "
136+ cmd = "helm upgrade #{ release_name } #{ repo } " \
146137 " --values #{ helm_values_file_path } " \
147138 " --namespace #{ namespace } " \
148139 ' --create-namespace' \
140+ ' --install' \
149141 ' --output json'
150142
151143 res = execute_command ( cmd , say : false )
152144 info = JSON . parse ( res ) [ 'info' ]
153145
154- return Uffizzi . ui . say ( 'Helm release is deployed' ) if info [ 'status' ] == HELM_DEPLOYED_STATUS
146+ return if info [ 'status' ] == HELM_DEPLOYED_STATUS
155147
156148 Uffizzi . ui . say_error_and_exit ( info )
157149 end
158150
151+ def helm_get_values ( release_name , namespace )
152+ cmd = "helm get values #{ release_name } -n #{ namespace } -o json"
153+ res = execute_command ( cmd , say : false )
154+ JSON . parse ( res )
155+ end
156+
159157 def kubectl_add_wildcard_tls ( params )
160158 cmd = "kubectl create secret tls wildcard.#{ params . fetch ( :domain ) } " \
161159 " --cert=#{ params . fetch ( :wildcard_cert_path ) } " \
@@ -165,7 +163,7 @@ def kubectl_add_wildcard_tls(params)
165163 execute_command ( cmd )
166164 end
167165
168- def ask_wildcard_cert ( has_user_wildcard_cert : nil )
166+ def ask_wildcard_cert ( has_user_wildcard_cert : nil , domain : nil )
169167 has_user_wildcard_cert ||= Uffizzi . prompt . yes? ( 'Uffizzi use a wildcard tls certificate. Do you have it?' )
170168
171169 if has_user_wildcard_cert
@@ -177,13 +175,12 @@ def ask_wildcard_cert(has_user_wildcard_cert: nil)
177175
178176 Uffizzi . ui . say ( 'Uffizzi does not work properly without a wildcard certificate.' )
179177 Uffizzi . ui . say ( 'You can add wildcard cert later with command:' )
180- Uffizzi . ui . say ( ' uffizzi install wildcard-tls --domain your. domain.com --cert /path/to/cert --key /path/to/key' )
178+ Uffizzi . ui . say ( " uffizzi install wildcard-tls --domain #{ domain } --cert /path/to/cert --key /path/to/key" )
181179
182180 { }
183181 end
184182
185183 def ask_installation_params
186- wildcard_cert_paths = ask_wildcard_cert
187184 namespace = Uffizzi . prompt . ask ( 'Namespace: ' , required : true , default : DEFAULT_NAMESPACE )
188185 domain = Uffizzi . prompt . ask ( 'Domain: ' , required : true , default : 'example.com' )
189186 user_email = Uffizzi . prompt . ask ( 'User email: ' , required : true , default : "admin@#{ domain } " )
@@ -194,6 +191,7 @@ def ask_installation_params
194191 { name : 'ZeroSSL' , value : 'zerossl' } ,
195192 ]
196193 cluster_issuer = Uffizzi . prompt . select ( 'Cluster issuer' , cluster_issuers )
194+ wildcard_cert_paths = ask_wildcard_cert ( domain : domain )
197195
198196 {
199197 namespace : namespace ,
@@ -226,7 +224,7 @@ def build_installation_options
226224 domain : options [ :domain ] ,
227225 user_email : options [ :'user-email' ] || "admin@#{ options [ :domain ] } " ,
228226 user_password : options [ :'user-password' ] || generate_password ,
229- controller_password : options [ :'controller-password' ] || generate_password ,
227+ controller_password : generate_password ,
230228 cert_email : options [ :'acme-email' ] || options [ :'user-email' ] ,
231229 cluster_issuer : options [ :issuer ] || DEFAULT_ISSUER ,
232230 wildcard_cert_path : options [ :'wildcard-cert-path' ] ,
@@ -237,6 +235,7 @@ def build_installation_options
237235 def build_helm_values ( params )
238236 domain = params . fetch ( :domain )
239237 namespace = params . fetch ( :namespace )
238+ tls_per_deployment_enabled = params . slice ( :wildcard_cert_pathm , :wildcard_key_path ) . compact . empty?
240239 app_host = [ DEFAULT_APP_PREFIX , domain ] . join ( '.' )
241240
242241 {
@@ -260,6 +259,7 @@ def build_helm_values(params)
260259 disabled : true ,
261260 } ,
262261 clusterIssuer : params . fetch ( :cluster_issuer ) ,
262+ tlsPerDeploymentEnabled : tls_per_deployment_enabled . to_s ,
263263 certEmail : params . fetch ( :cert_email ) ,
264264 'ingress-nginx' => {
265265 controller : {
0 commit comments