3
3
import json
4
4
import os
5
5
import subprocess
6
+ import re
7
+ import sys
6
8
7
9
import yaml
8
10
@@ -96,7 +98,44 @@ def setup_auth_gcloud(release, cluster):
96
98
97
99
def setup_helm (release ):
98
100
"""ensure helm is up to date"""
99
- subprocess .check_call (['helm' , 'init' , '--upgrade' ])
101
+ # First check the helm client and server versions
102
+ client_helm_cmd = ["helm" , "version" , "-c" , "--short" ]
103
+ client_version = subprocess .check_output (client_helm_cmd
104
+ ).decode ('utf-8' ).split (":" )[1 ].split ("+" )[0 ].strip ()
105
+
106
+ server_helm_cmd = ["helm" , "version" , "-s" , "--short" ]
107
+ server_version = subprocess .check_output (server_helm_cmd
108
+ ).decode ('utf-8' ).split (":" )[1 ].split ("+" )[0 ].strip ()
109
+
110
+ print (BOLD + GREEN +
111
+ f"Client version: { client_version } , Server version: { server_version } " +
112
+ NC ,
113
+ flush = True
114
+ )
115
+
116
+ # Now check if the version of helm matches v2.11.0 which travis is expecting
117
+ if client_version != "v2.11.0" :
118
+ # The local helm version is not v2.11.0 - user needs to change the installation
119
+ raise Exception (
120
+ "You are not running helm v2.11.0 which is the version our continuous deployment system uses.\n " +
121
+ "Please change your installation and try again.\n "
122
+ )
123
+ elif (client_version == "v2.11.0" ) and (client_version != server_version ):
124
+ # The correct local version of helm is installed, but the server side
125
+ # has previously accidentally been upgraded. Perform a force-upgrade
126
+ # to bring the server side back to v2.11.0
127
+ raise Exception (
128
+ "Helm client and server versions do not match. Performing a force upgrade often resolves this issue." +
129
+ "Please run the following command and re-execute this script.\n \n \t " +
130
+ "helm init --upgrade --force-upgrade"
131
+ )
132
+ elif (client_version == "v2.11.0" ) and (client_version == server_version ):
133
+ # All is good! Perform normal helm init command.
134
+ # We use the --client-only flag so that the Tiller installation is not affected.
135
+ subprocess .check_call (['helm' , 'init' , '--client-only' ])
136
+ else :
137
+ # This is a catch-all exception. Hopefully this doesn't execute!
138
+ raise Exception ("Please check your helm installation." )
100
139
101
140
deployment = json .loads (subprocess .check_output ([
102
141
'kubectl' ,
@@ -202,6 +241,11 @@ def deploy(release):
202
241
203
242
204
243
def main ():
244
+
245
+ # Get current working directory
246
+ cwd = os .getcwd ()
247
+
248
+ # parse command line args
205
249
argparser = argparse .ArgumentParser ()
206
250
argparser .add_argument (
207
251
'release' ,
@@ -212,17 +256,52 @@ def main():
212
256
'cluster' ,
213
257
help = 'Cluster to do the deployment in'
214
258
)
259
+ argparser .add_argument (
260
+ '--local' ,
261
+ action = 'store_true' ,
262
+ help = "If the script is running locally, skip auth and helm steps."
263
+ )
215
264
216
265
args = argparser .parse_args ()
217
266
218
- if args .cluster == 'binder-ovh' :
219
- setup_auth_ovh (args .release , args .cluster )
220
- elif args .cluster == 'turing' :
221
- setup_auth_turing (args .cluster )
222
- else :
223
- setup_auth_gcloud (args .release , args .cluster )
267
+ # Check if the local flag is set
268
+ if not args .local :
269
+ # Check if the script is being run on travis
270
+ if not (cwd .startswith ('/home/travis' )):
271
+ # Catch the case where the script is running locally but the --local flag
272
+ # has not been set. Check that the user is sure that they want to do this!
273
+ print (
274
+ "You do not seem to be running on Travis but have not set the --local flag."
275
+ )
276
+
277
+ # Use regex to match user input
278
+ regex_no = re .compile ("^[n|N][o|O]$" )
279
+ regex_yes = re .compile ("^[y|Y][e|E][s|S]$" )
280
+ response = input ("Are you sure you want to execute this script? [yes/no]: " )
281
+
282
+ if regex_no .match (response ):
283
+ # User isn't sure - exit script
284
+ print ("Exiting script." )
285
+ sys .exit ()
286
+ elif regex_yes .match (response ):
287
+ # User is sure - proceed
288
+ pass
289
+ else :
290
+ # User wrote something that wasn't "yes" or "no"
291
+ raise ValueError (
292
+ "Unrecognised input. Expecting either yes or no."
293
+ )
294
+
295
+ # script is running on travis, proceed with auth and helm setup
296
+ if args .cluster == 'binder-ovh' :
297
+ setup_auth_ovh (args .release , args .cluster )
298
+ elif args .cluster == 'turing' :
299
+ setup_auth_turing (args .cluster )
300
+ else :
301
+ setup_auth_gcloud (args .release , args .cluster )
302
+
303
+ setup_helm (args .release )
224
304
225
- setup_helm (args .release )
226
305
deploy (args .release )
227
306
228
307
0 commit comments