11# frozen_string_literal: true
22
3+ require 'byebug'
34require 'uffizzi'
45require 'uffizzi/config_file'
56
@@ -9,15 +10,14 @@ class Cli::Install < Thor
910 HELM_DEPLOYED_STATUS = 'deployed'
1011 CHART_NAME = 'uffizzi-app'
1112 VALUES_FILE_NAME = 'helm_values.yaml'
12- DEFAULT_ISSUER = 'letsencrypt'
1313 DEFAULT_NAMESPACE = 'uffizzi'
1414 DEFAULT_APP_PREFIX = 'uffizzi'
15+ DEFAULT_CLUSTER_ISSUER = 'letsencrypt'
1516
1617 desc 'application' , 'Install uffizzi to cluster'
1718 method_option :namespace , type : :string
1819 method_option :domain , type : :string
1920 method_option :'user-email' , type : :string
20- method_option :'acme-email' , type : :string
2121 method_option :'user-password' , type : :string
2222 method_option :issuer , type : :string , enum : [ 'letsencrypt' , 'zerossl' ]
2323 method_option :'wildcard-cert-path' , type : :string
@@ -53,7 +53,7 @@ def wildcard_tls
5353 }
5454 else
5555 namespace = Uffizzi . prompt . ask ( 'Namespace: ' , required : true , default : DEFAULT_NAMESPACE )
56- domain = Uffizzi . prompt . ask ( 'Domain: ' , required : true , default : 'example.com' )
56+ domain = Uffizzi . prompt . ask ( 'Root Domain: ' , required : true , default : 'example.com' )
5757 wildcard_cert_paths = ask_wildcard_cert ( has_user_wildcard_cert : true , domain : domain )
5858
5959 { namespace : namespace , domain : domain } . merge ( wildcard_cert_paths )
@@ -89,14 +89,36 @@ def run_installation
8989 helm_values = build_helm_values ( params )
9090 return Uffizzi . ui . say ( helm_values . to_yaml ) if options [ :'print-values' ]
9191
92+ namespace = params [ :namespace ]
93+ release_name = params [ :namespace ]
94+
9295 create_helm_values_file ( helm_values )
9396 helm_set_repo unless options [ :repo ]
94- helm_install ( release_name : params [ :namespace ] , namespace : params [ : namespace] , repo : options [ :repo ] )
97+ helm_install ( release_name : release_name , namespace : namespace , repo : options [ :repo ] )
9598 kubectl_add_wildcard_tls ( params ) if params [ :wildcard_cert_path ] && params [ :wildcard_key_path ]
9699 delete_helm_values_file
97100
101+ ingress_ip = get_web_ingress_ip_address ( release_name , namespace )
102+
98103 Uffizzi . ui . say ( 'Helm release is deployed' )
99- Uffizzi . ui . say ( "The uffizzi application url is https://#{ DEFAULT_APP_PREFIX } .#{ params [ :domain ] } " )
104+ Uffizzi . ui . say ( "The uffizzi application url is 'https://#{ DEFAULT_APP_PREFIX } .#{ params [ :domain ] } '" )
105+ Uffizzi . ui . say ( "Create a DNS A record for domain '*.#{ params [ :domain ] } ' with value '#{ ingress_ip } '" )
106+ end
107+
108+ def get_web_ingress_ip_address ( release_name , namespace )
109+ Uffizzi . ui . say ( 'Getting an ingress ip address...' )
110+
111+ 10 . times do
112+ web_ingress = kubectl_get_web_ingress ( release_name , namespace )
113+ ingresses = web_ingress . dig ( 'status' , 'loadBalancer' , 'ingress' ) || [ ]
114+ ip_address = ingresses . first &.fetch ( 'ip' , nil )
115+
116+ return ip_address if ip_address . present?
117+
118+ sleep ( 1 )
119+ end
120+
121+ Uffizzi . ui . say_error_and_exit ( 'We can`t get the uffizzi ingress ip address' )
100122 end
101123
102124 def kubectl_exists?
@@ -163,34 +185,38 @@ def kubectl_add_wildcard_tls(params)
163185 execute_command ( cmd )
164186 end
165187
188+ def kubectl_get_web_ingress ( release_name , namespace )
189+ cmd = "kubectl get ingress/#{ release_name } -web-ingress -n #{ namespace } -o json"
190+
191+ res = execute_command ( cmd , say : false )
192+ JSON . parse ( res )
193+ end
194+
166195 def ask_wildcard_cert ( has_user_wildcard_cert : nil , domain : nil )
167196 has_user_wildcard_cert ||= Uffizzi . prompt . yes? ( 'Uffizzi use a wildcard tls certificate. Do you have it?' )
168197
169- if has_user_wildcard_cert
170- cert_path = Uffizzi . prompt . ask ( 'Path to cert: ' , required : true )
171- key_path = Uffizzi . prompt . ask ( 'Path to key: ' , required : true )
198+ if !has_user_wildcard_cert
199+ Uffizzi . ui . say ( 'Uffizzi does not work properly without a wildcard certificate.' )
200+ Uffizzi . ui . say ( 'You can add wildcard cert later with command:' )
201+ Uffizzi . ui . say ( "uffizzi install wildcard-tls --domain #{ domain } --cert /path/to/cert --key /path/to/key" )
172202
173- return { wildcard_cert_path : cert_path , wildcard_key_path : key_path }
203+ return { }
174204 end
175205
176- Uffizzi . ui . say ( 'Uffizzi does not work properly without a wildcard certificate.' )
177- Uffizzi . ui . say ( 'You can add wildcard cert later with command:' )
178- Uffizzi . ui . say ( "uffizzi install wildcard-tls --domain #{ domain } --cert /path/to/cert --key /path/to/key" )
206+ cert_path = Uffizzi . prompt . ask ( 'Path to cert: ' , required : true )
207+ Uffizzi . ui . say_error_and_exit ( "File '#{ cert_path } ' does not exists" ) unless File . exist? ( cert_path )
208+
209+ key_path = Uffizzi . prompt . ask ( 'Path to key: ' , required : true )
210+ Uffizzi . ui . say_error_and_exit ( "File '#{ key_path } ' does not exists" ) unless File . exist? ( key_path )
179211
180- { }
212+ { wildcard_cert_path : cert_path , wildcard_key_path : key_path }
181213 end
182214
183215 def ask_installation_params
184216 namespace = Uffizzi . prompt . ask ( 'Namespace: ' , required : true , default : DEFAULT_NAMESPACE )
185- domain = Uffizzi . prompt . ask ( 'Domain: ' , required : true , default : 'example.com' )
186- user_email = Uffizzi . prompt . ask ( 'User email: ' , required : true , default : "admin@#{ domain } " )
187- user_password = Uffizzi . prompt . ask ( 'User password: ' , required : true , default : generate_password )
188- cert_email = Uffizzi . prompt . ask ( 'Email address for ACME registration: ' , required : true , default : user_email )
189- cluster_issuers = [
190- { name : 'Letsencrypt' , value : 'letsencrypt' } ,
191- { name : 'ZeroSSL' , value : 'zerossl' } ,
192- ]
193- cluster_issuer = Uffizzi . prompt . select ( 'Cluster issuer' , cluster_issuers )
217+ domain = Uffizzi . prompt . ask ( 'Root domain: ' , required : true , default : 'example.com' )
218+ user_email = Uffizzi . prompt . ask ( 'First user email: ' , required : true , default : "admin@#{ domain } " )
219+ user_password = Uffizzi . prompt . ask ( 'First user password: ' , required : true , default : generate_password )
194220 wildcard_cert_paths = ask_wildcard_cert ( domain : domain )
195221
196222 {
@@ -199,8 +225,8 @@ def ask_installation_params
199225 user_email : user_email ,
200226 user_password : user_password ,
201227 controller_password : generate_password ,
202- cert_email : cert_email ,
203- cluster_issuer : cluster_issuer ,
228+ cert_email : user_email ,
229+ cluster_issuer : DEFAULT_CLUSTER_ISSUER ,
204230 } . merge ( wildcard_cert_paths )
205231 end
206232
@@ -225,8 +251,8 @@ def build_installation_options
225251 user_email : options [ :'user-email' ] || "admin@#{ options [ :domain ] } " ,
226252 user_password : options [ :'user-password' ] || generate_password ,
227253 controller_password : generate_password ,
228- cert_email : options [ :'acme-email' ] || options [ :' user-email'] ,
229- cluster_issuer : options [ :issuer ] || DEFAULT_ISSUER ,
254+ cert_email : options [ :'user-email' ] ,
255+ cluster_issuer : options [ :issuer ] || DEFAULT_CLUSTER_ISSUER ,
230256 wildcard_cert_path : options [ :'wildcard-cert-path' ] ,
231257 wildcard_key_path : options [ :'wildcard-key-path' ] ,
232258 }
0 commit comments