Skip to content

Commit abcf8e2

Browse files
authored
Merge pull request #1317 from jupyterhub/check-helm-version
[RVW] Add a version check before helm upgrade happens
2 parents 29f6c20 + 31c4149 commit abcf8e2

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

deploy.py

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import json
44
import os
55
import subprocess
6+
import re
7+
import sys
68

79
import yaml
810

@@ -96,7 +98,44 @@ def setup_auth_gcloud(release, cluster):
9698

9799
def setup_helm(release):
98100
"""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.")
100139

101140
deployment = json.loads(subprocess.check_output([
102141
'kubectl',
@@ -202,6 +241,11 @@ def deploy(release):
202241

203242

204243
def main():
244+
245+
# Get current working directory
246+
cwd = os.getcwd()
247+
248+
# parse command line args
205249
argparser = argparse.ArgumentParser()
206250
argparser.add_argument(
207251
'release',
@@ -212,17 +256,52 @@ def main():
212256
'cluster',
213257
help='Cluster to do the deployment in'
214258
)
259+
argparser.add_argument(
260+
'--local',
261+
action='store_true',
262+
help="If the script is running locally, skip auth and helm steps."
263+
)
215264

216265
args = argparser.parse_args()
217266

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)
224304

225-
setup_helm(args.release)
226305
deploy(args.release)
227306

228307

0 commit comments

Comments
 (0)