diff --git a/test/cosmos.py b/test/cosmos.py index 02c7856951850a381bcf56c3da7efc046cee7d07..eabfd1510f4a41479f501e326a12f32ff13e1beb 100755 --- a/test/cosmos.py +++ b/test/cosmos.py @@ -9,7 +9,9 @@ import requests import socket from tabulate import tabulate from time import sleep - +from datetime import datetime +from datetime import timedelta +from pytz import timezone # Helper functions def request_api(resource: str, request_type: str = 'GET', @@ -265,18 +267,23 @@ def terminate_droplet(id: int): delete_droplet(id) -def terminate_droplets_by_name(name_regex: str): +def terminate_droplets_by_name(name_regex: str, ndays: int = 0, domain: str = 'openappstack.net'): r""" - Terminate droplets matching a regex in their names. + Terminate droplets matching a regex and for x days older than current day. Example how to terminate all CI instances: - terminate_droplets_by_name(name_regex='^ci\d+') - will match i.e. 'ci1234', 'ci1', etc + terminate_old_droplets(name_regex='^ci\d+', ndays=5) + will match i.e 'ci1234' , 'ci1', with a creation time older than 5 days """ + + threshold_time = (datetime.now(tz=timezone('Europe/Stockholm')) - timedelta(days=ndays)).strftime("%Y-%m-%dT%H:%M:%S+00:00") all = get_droplets() for droplet in all: if re.match(name_regex, droplet['name']): - terminate_droplet(droplet['id']) + if droplet['created_at'] < threshold_time: + delete_domain_records_by_name(domain, '^\*.'+droplet['name']) + delete_domain_records_by_name(domain, '^'+droplet['name']) + terminate_droplet(droplet['id']) def wait_for_ssh(ip: str): diff --git a/test/requirements.txt b/test/requirements.txt index e3927fc39ef8d29c205599df8c313a04aa2caa9c..ad875e9dd6b4417d90741e993796859d14379d9e 100644 --- a/test/requirements.txt +++ b/test/requirements.txt @@ -12,3 +12,4 @@ tabulate>=0.8.3 testinfra>=2.0.0 setuptools>=40.6.2 wheel>=0.33.1 +pytz>=2019.1