Skip to content
Snippets Groups Projects
Commit eccb9efd authored by Ana Aviles's avatar Ana Aviles Committed by Varac
Browse files

Resolve "Remove stale CI droplets after a given time"

parent 44215045
No related branches found
No related tags found
No related merge requests found
......@@ -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):
......
......@@ -12,3 +12,4 @@ tabulate>=0.8.3
testinfra>=2.0.0
setuptools>=40.6.2
wheel>=0.33.1
pytz>=2019.1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment