diff --git a/Tools/dea_tools/dask.py b/Tools/dea_tools/dask.py index 226b26d1c..534a4c675 100644 --- a/Tools/dea_tools/dask.py +++ b/Tools/dea_tools/dask.py @@ -21,7 +21,7 @@ create_local_dask_cluster create_dask_gateway_cluster -Last modified: March 2020 +Last modified: June 2022 ''' @@ -111,9 +111,22 @@ def create_dask_gateway_cluster(profile='r5_L', workers=2): """ try: gateway = Gateway() + + # Close any existing clusters + cluster_names = gateway.list_clusters() + if len(cluster_names) > 0: + print("Cluster(s) still running:", cluster_names) + for n in cluster_names: + cluster = gateway.connect(n.name) + cluster.shutdown() + options = gateway.cluster_options() options['profile'] = profile - options['jupyterhub_user'] = os.getenv('JUPYTERHUB_USER') + + # limit username to alphanumeric characters + # kubernetes pods won't launch if labels contain anything other than [a-Z, -, _] + options['jupyterhub_user'] = ''.join(c if c.isalnum() else '-' for c in os.getenv('JUPYTERHUB_USER')) + cluster = gateway.new_cluster(options) cluster.scale(workers) return cluster