From 5d4c8b659478a5507996b10ddebd1a8a170fb8e7 Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Thu, 16 Jan 2020 17:28:50 +0000 Subject: [PATCH 01/12] Add a version check before helm --- deploy.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/deploy.py b/deploy.py index 422ea24a6..3b831608e 100755 --- a/deploy.py +++ b/deploy.py @@ -96,7 +96,36 @@ def setup_auth_gcloud(release, cluster): def setup_helm(release): """ensure helm is up to date""" - subprocess.check_call(['helm', 'init', '--upgrade']) + # First check the helm client and server versions + client_helm_cmd = ["helm", "version", "-c", "--short"] + client_version = subprocess.check_output(client_helm_cmd + ).decode('utf-8').split(":")[1].split("+")[0].strip() + + server_helm_cmd = ["helm", "version", "-s", "--short"] + server_version = subprocess.check_output(server_helm_cmd + ).decode('utf-8').split(":")[1].split("+")[0].strip() + + print( + "Client version: {}, Server version: {}".format( + client_version, + server_version, + ) + ) + + if client_version != "v2.11.0": + raise Exception( + "You are not running helm v2.11.0 which is the version our continuous deployment system uses.\n" + + "Please change your installation and try again.\n" + + ) + elif (client_version == "v2.11.0") and (client_version != server_version): + print( + "Helm client and server versions do not match. Performing a force upgrade." + ) + subprocess.check_call(["helm", "init", "--upgrade", "--force-upgrade"]) + elif (client_version == "v2.11.0") and (client_version == server_version): + subprocess.check_call(['helm', 'init', '--upgrade']) + else: + raise Exception("Please check your helm installation.") deployment = json.loads(subprocess.check_output([ 'kubectl', From a7a741c81bbba0e173442b5288a5391af07322a3 Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Thu, 16 Jan 2020 17:35:33 +0000 Subject: [PATCH 02/12] Add comments --- deploy.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/deploy.py b/deploy.py index 3b831608e..fa26ec977 100755 --- a/deploy.py +++ b/deploy.py @@ -112,19 +112,26 @@ def setup_helm(release): ) ) + # Now check if the version of helm matches v2.11.0 which travis is expecting if client_version != "v2.11.0": + # The local helm version is not v2.11.0 - user needs to change the installation raise Exception( "You are not running helm v2.11.0 which is the version our continuous deployment system uses.\n" + "Please change your installation and try again.\n" + ) elif (client_version == "v2.11.0") and (client_version != server_version): + # The correct local version of helm is installed, but the server side + # has previously accidentally been upgraded. Perform a force-upgrade + # to bring the server side back to v2.11.0 print( "Helm client and server versions do not match. Performing a force upgrade." ) subprocess.check_call(["helm", "init", "--upgrade", "--force-upgrade"]) elif (client_version == "v2.11.0") and (client_version == server_version): + # All is good! Perform normal helm init command. subprocess.check_call(['helm', 'init', '--upgrade']) else: + # This is a catch-all exception. Hopefully this doesn't execute! raise Exception("Please check your helm installation.") deployment = json.loads(subprocess.check_output([ From 2d9d5f6c596eb851b304c271b70b73a90288bd97 Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 11:26:11 +0000 Subject: [PATCH 03/12] Raise an exception to suggest running a force upgrade --- deploy.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/deploy.py b/deploy.py index fa26ec977..eb24460b4 100755 --- a/deploy.py +++ b/deploy.py @@ -123,10 +123,11 @@ def setup_helm(release): # The correct local version of helm is installed, but the server side # has previously accidentally been upgraded. Perform a force-upgrade # to bring the server side back to v2.11.0 - print( - "Helm client and server versions do not match. Performing a force upgrade." + raise Exception( + "Helm client and server versions do not match. Performing a force upgrade often resolves this issue." + + "Please run the following command and re-execute this script.\n\n" + + "\thelm init --upgrade --force-upgrade" ) - subprocess.check_call(["helm", "init", "--upgrade", "--force-upgrade"]) elif (client_version == "v2.11.0") and (client_version == server_version): # All is good! Perform normal helm init command. subprocess.check_call(['helm', 'init', '--upgrade']) From 2c0d89753ea7d9b00b0435e3c1e058c00ee451d3 Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 11:29:36 +0000 Subject: [PATCH 04/12] Run helm init with client-only flag instead of upgrade --- deploy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.py b/deploy.py index eb24460b4..8e7f74d0f 100755 --- a/deploy.py +++ b/deploy.py @@ -130,7 +130,7 @@ def setup_helm(release): ) elif (client_version == "v2.11.0") and (client_version == server_version): # All is good! Perform normal helm init command. - subprocess.check_call(['helm', 'init', '--upgrade']) + subprocess.check_call(['helm', 'init', '--client-only']) else: # This is a catch-all exception. Hopefully this doesn't execute! raise Exception("Please check your helm installation.") From 6b2f28be79969ba9676c5d217bf3dd4f513fa92d Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 11:30:45 +0000 Subject: [PATCH 05/12] Add comment about client only flag --- deploy.py | 1 + 1 file changed, 1 insertion(+) diff --git a/deploy.py b/deploy.py index 8e7f74d0f..a14431638 100755 --- a/deploy.py +++ b/deploy.py @@ -130,6 +130,7 @@ def setup_helm(release): ) elif (client_version == "v2.11.0") and (client_version == server_version): # All is good! Perform normal helm init command. + # We use the --client-only flag so that the Tiller installation is not affected. subprocess.check_call(['helm', 'init', '--client-only']) else: # This is a catch-all exception. Hopefully this doesn't execute! From e110a14dfd231ac5d03dad347d4bc15f2367b070 Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 11:42:38 +0000 Subject: [PATCH 06/12] Add a local flag which turns off auth and helm steps --- deploy.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/deploy.py b/deploy.py index a14431638..0c878f266 100755 --- a/deploy.py +++ b/deploy.py @@ -117,7 +117,7 @@ def setup_helm(release): # The local helm version is not v2.11.0 - user needs to change the installation raise Exception( "You are not running helm v2.11.0 which is the version our continuous deployment system uses.\n" + - "Please change your installation and try again.\n" + + "Please change your installation and try again.\n" ) elif (client_version == "v2.11.0") and (client_version != server_version): # The correct local version of helm is installed, but the server side @@ -250,17 +250,24 @@ def main(): 'cluster', help='Cluster to do the deployment in' ) + argparser.add_argument( + '--local', + action='store_true', + help="If the script is running locally, skip auth and helm steps." + ) args = argparser.parse_args() - if args.cluster == 'binder-ovh': - setup_auth_ovh(args.release, args.cluster) - elif args.cluster == 'turing': - setup_auth_turing(args.cluster) - else: - setup_auth_gcloud(args.release, args.cluster) + if not args.local: + if args.cluster == 'binder-ovh': + setup_auth_ovh(args.release, args.cluster) + elif args.cluster == 'turing': + setup_auth_turing(args.cluster) + else: + setup_auth_gcloud(args.release, args.cluster) + + setup_helm(args.release) - setup_helm(args.release) deploy(args.release) From 92897048d261e9d6dd7bceb8daee766bbe346b5a Mon Sep 17 00:00:00 2001 From: Sarah Gibson <44771837+sgibson91@users.noreply.github.com> Date: Tue, 21 Jan 2020 15:58:06 +0000 Subject: [PATCH 07/12] Update deploy.py --- deploy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.py b/deploy.py index 0c878f266..63885cb39 100755 --- a/deploy.py +++ b/deploy.py @@ -126,7 +126,7 @@ def setup_helm(release): raise Exception( "Helm client and server versions do not match. Performing a force upgrade often resolves this issue." + "Please run the following command and re-execute this script.\n\n" + - "\thelm init --upgrade --force-upgrade" + "helm init --upgrade --force-upgrade" ) elif (client_version == "v2.11.0") and (client_version == server_version): # All is good! Perform normal helm init command. From 257e76c93b152a503f40777626e9aad5d1e536d8 Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 16:04:08 +0000 Subject: [PATCH 08/12] Ask user if they're sure they want to execute locally --- deploy.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/deploy.py b/deploy.py index 0c878f266..645ce24ec 100755 --- a/deploy.py +++ b/deploy.py @@ -3,6 +3,8 @@ import json import os import subprocess +import re +import sys import yaml @@ -240,6 +242,10 @@ def deploy(release): def main(): + + # Get current working directory + cwd = os.getcwd() + argparser = argparse.ArgumentParser() argparser.add_argument( 'release', @@ -258,7 +264,7 @@ def main(): args = argparser.parse_args() - if not args.local: + if (cwd.startswith('/home/travis')) and (not args.local): if args.cluster == 'binder-ovh': setup_auth_ovh(args.release, args.cluster) elif args.cluster == 'turing': @@ -267,6 +273,24 @@ def main(): setup_auth_gcloud(args.release, args.cluster) setup_helm(args.release) + elif (not cwd.startswith('/home/travis')) and (not args.local): + print( + "You do not seem to be running on Travis but have not set the --local flag." + ) + + regex_no = re.compile("^[n|N][o|O]$") + regex_yes = re.compile("^[y|Y][e|E][s|S]$") + response = input("Are you sure you want to execute this script? [yes/no]: ") + + if regex_no.match(response): + print("Exiting script.") + sys.exit() + elif regex_yes.match(response): + pass + else: + raise ValueError( + "Unrecognised input. Expecting either yes or no." + ) deploy(args.release) From 5b1469f9518a7f01fac96f9fac90ab636f37a79d Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 16:08:17 +0000 Subject: [PATCH 09/12] Add comments --- deploy.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/deploy.py b/deploy.py index fab2d82c8..c1f6dcdd4 100755 --- a/deploy.py +++ b/deploy.py @@ -246,6 +246,7 @@ def main(): # Get current working directory cwd = os.getcwd() + # parse command line args argparser = argparse.ArgumentParser() argparser.add_argument( 'release', @@ -264,7 +265,9 @@ def main(): args = argparser.parse_args() + # Check if the script is being run on travis or hand the --local flag set if (cwd.startswith('/home/travis')) and (not args.local): + # script is running on travis, proceed with auth and helm setup if args.cluster == 'binder-ovh': setup_auth_ovh(args.release, args.cluster) elif args.cluster == 'turing': @@ -273,21 +276,28 @@ def main(): setup_auth_gcloud(args.release, args.cluster) setup_helm(args.release) + elif (not cwd.startswith('/home/travis')) and (not args.local): + # Catch the case where the script is running locally but the --local flag + # has not been set. Check that the user is sure that they want to do this! print( "You do not seem to be running on Travis but have not set the --local flag." ) + # Use regex to match user input regex_no = re.compile("^[n|N][o|O]$") regex_yes = re.compile("^[y|Y][e|E][s|S]$") response = input("Are you sure you want to execute this script? [yes/no]: ") if regex_no.match(response): + # User isn't sure - exit script print("Exiting script.") sys.exit() elif regex_yes.match(response): + # User is sure - proceed pass else: + # User wrote something that wasn't "yes" or "no" raise ValueError( "Unrecognised input. Expecting either yes or no." ) From 4f5c514d23a82df245996781689cf722e930280a Mon Sep 17 00:00:00 2001 From: Sarah Gibson <44771837+sgibson91@users.noreply.github.com> Date: Tue, 21 Jan 2020 16:09:09 +0000 Subject: [PATCH 10/12] Update deploy.py --- deploy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.py b/deploy.py index c1f6dcdd4..1b353eac1 100755 --- a/deploy.py +++ b/deploy.py @@ -127,7 +127,7 @@ def setup_helm(release): # to bring the server side back to v2.11.0 raise Exception( "Helm client and server versions do not match. Performing a force upgrade often resolves this issue." + - "Please run the following command and re-execute this script.\n\n" + + "Please run the following command and re-execute this script.\n\n\t" + "helm init --upgrade --force-upgrade" ) elif (client_version == "v2.11.0") and (client_version == server_version): From d1ffbe017a46d44b9eb4c91e277ba0999ba9f904 Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 16:25:41 +0000 Subject: [PATCH 11/12] Refactor to reduce repeated or unexecuted code --- deploy.py | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/deploy.py b/deploy.py index 1b353eac1..f5a5f6cbb 100755 --- a/deploy.py +++ b/deploy.py @@ -265,8 +265,34 @@ def main(): args = argparser.parse_args() - # Check if the script is being run on travis or hand the --local flag set - if (cwd.startswith('/home/travis')) and (not args.local): + # Check if the local flag is set + if not args.local: + # Check if the script is being run on travis + if not (cwd.startswith('/home/travis')): + # Catch the case where the script is running locally but the --local flag + # has not been set. Check that the user is sure that they want to do this! + print( + "You do not seem to be running on Travis but have not set the --local flag." + ) + + # Use regex to match user input + regex_no = re.compile("^[n|N][o|O]$") + regex_yes = re.compile("^[y|Y][e|E][s|S]$") + response = input("Are you sure you want to execute this script? [yes/no]: ") + + if regex_no.match(response): + # User isn't sure - exit script + print("Exiting script.") + sys.exit() + elif regex_yes.match(response): + # User is sure - proceed + pass + else: + # User wrote something that wasn't "yes" or "no" + raise ValueError( + "Unrecognised input. Expecting either yes or no." + ) + # script is running on travis, proceed with auth and helm setup if args.cluster == 'binder-ovh': setup_auth_ovh(args.release, args.cluster) @@ -277,31 +303,6 @@ def main(): setup_helm(args.release) - elif (not cwd.startswith('/home/travis')) and (not args.local): - # Catch the case where the script is running locally but the --local flag - # has not been set. Check that the user is sure that they want to do this! - print( - "You do not seem to be running on Travis but have not set the --local flag." - ) - - # Use regex to match user input - regex_no = re.compile("^[n|N][o|O]$") - regex_yes = re.compile("^[y|Y][e|E][s|S]$") - response = input("Are you sure you want to execute this script? [yes/no]: ") - - if regex_no.match(response): - # User isn't sure - exit script - print("Exiting script.") - sys.exit() - elif regex_yes.match(response): - # User is sure - proceed - pass - else: - # User wrote something that wasn't "yes" or "no" - raise ValueError( - "Unrecognised input. Expecting either yes or no." - ) - deploy(args.release) From 31c41499078d8745303c36c819130c1bb2fcb1ed Mon Sep 17 00:00:00 2001 From: Sarah Gibson Date: Tue, 21 Jan 2020 16:28:52 +0000 Subject: [PATCH 12/12] Use f-strings when printing helm version --- deploy.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/deploy.py b/deploy.py index f5a5f6cbb..17484fc9b 100755 --- a/deploy.py +++ b/deploy.py @@ -107,11 +107,10 @@ def setup_helm(release): server_version = subprocess.check_output(server_helm_cmd ).decode('utf-8').split(":")[1].split("+")[0].strip() - print( - "Client version: {}, Server version: {}".format( - client_version, - server_version, - ) + print(BOLD + GREEN + + f"Client version: {client_version}, Server version: {server_version}" + + NC, + flush=True ) # Now check if the version of helm matches v2.11.0 which travis is expecting