Skip to content
Snippets Groups Projects
Commit 3fd3b64a authored by Varac's avatar Varac
Browse files

Merge branch '28-remove-stale-ci-droplets-after-a-given-time' into 'master'

Resolve "Remove stale CI droplets after a given time"

Closes #28

See merge request openappstack/bootstrap!98
parents 484eac3f df992dbc
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,10 @@ Used by CI to bootstrap a new cluster and run tests.
Env vars needed:
- COSMOS_API_TOKEN
Env vars optional:
- NO_TERMINATE_DROPLETS: list of droplet ids that should not be
removed when deleting droplets.
Install requirements:
- Alpine using `requirements.txt`:
......@@ -38,7 +42,6 @@ import subprocess
import sys
import traceback
import yaml
import cosmos
SETTINGS_FILE = './group_vars/all/settings.yml'
......@@ -231,9 +234,6 @@ def main(): # pylint: disable=too-many-statements,too-many-branches
if args.terminate:
cosmos.terminate_droplets_by_name(droplet_name)
cosmos.delete_domain_records_by_name('openappstack.net', droplet_name)
cosmos.delete_domain_records_by_name('openappstack.net',
'\*.' + droplet_name)
def run_ansible(playbook, ansible_params):
......
......@@ -269,7 +269,8 @@ def terminate_droplet(id: int):
def terminate_droplets_by_name(name_regex: str, ndays: int = 0, domain: str = 'openappstack.net'):
r"""
Terminate droplets matching a regex and for x days older than current day.
Terminate droplets matching a regex and for x days older than current day.
Droplets defined on the env variable NO_TERMINATE_DROPLETS will not be delated
Example how to terminate all CI instances:
terminate_old_droplets(name_regex='^ci\d+', ndays=5)
......@@ -278,13 +279,18 @@ def terminate_droplets_by_name(name_regex: str, ndays: int = 0, domain: str = 'o
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']):
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'])
noterminate_droplets = []
if 'NO_TERMINATE_DROPLETS' in os.environ:
noterminate_droplets = os.environ['NO_TERMINATE_DROPLETS'].split(',')
for droplet in all:
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, '^\*.'+droplet['name'])
delete_domain_records_by_name(domain, '^'+droplet['name'])
terminate_droplet(droplet['id'])
def wait_for_ssh(ip: str):
"""Wait for ssh to be reachable on port 22."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment