From a8f195af1d99c0217ef7ee529854526ddc6b1a8c Mon Sep 17 00:00:00 2001 From: Maarten de Waard <maarten@greenhost.nl> Date: Thu, 5 Dec 2019 11:42:14 +0100 Subject: [PATCH] speedup terminate-droplets command --- greenhost_cloud/cosmos.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/greenhost_cloud/cosmos.py b/greenhost_cloud/cosmos.py index 50bcd4c..1707c22 100755 --- a/greenhost_cloud/cosmos.py +++ b/greenhost_cloud/cosmos.py @@ -298,14 +298,29 @@ def terminate_droplets_by_name(name_regex: str, ndays: int = 0, if 'NO_TERMINATE_DROPLETS' in os.environ: noterminate_droplets = os.environ['NO_TERMINATE_DROPLETS'].split(',') + # List of droplets that will be terminated + terminate_droplets = [] + for droplet in all_droplets: if droplet['name'] not in noterminate_droplets: if re.match(name_regex, droplet['name']): if droplet['created_at'] < threshold_time: - delete_domain_records_by_name( - domain, r'^\*.'+droplet['name']) - delete_domain_records_by_name(domain, '^'+droplet['name']) - terminate_droplet(droplet['id']) + terminate_droplets.append(droplet) + + # First stop all droplets + for droplet in terminate_droplets: + shutdown_droplet(droplet['id']) + + # Then delete all domain names associated with the droplets + for droplet in terminate_droplets: + delete_domain_records_by_name( + domain, r'^\*.'+droplet['name']) + delete_domain_records_by_name(domain, '^'+droplet['name']) + + # Lastly delete all droplets: + for droplet in terminate_droplets: + wait_for_state(droplet['id'], 'stopped') + delete_droplet(droplet['id']) def wait_for_ssh(droplet_ip: str): -- GitLab