diff --git a/roles/fedimg/files/cron/kill_ec2_nodes.cron b/roles/fedimg/files/cron/kill_ec2_nodes.cron new file mode 100644 index 0000000000..40cfb9b746 --- /dev/null +++ b/roles/fedimg/files/cron/kill_ec2_nodes.cron @@ -0,0 +1,2 @@ +MAILTO=sysadmin-fedimg-members@fedoraproject.org +*/2 * * * * fedmsg /usr/local/bin/kill_ec2_nodes.py diff --git a/roles/fedimg/files/cron/kill_ec2_nodes.py b/roles/fedimg/files/cron/kill_ec2_nodes.py new file mode 100644 index 0000000000..7e654f7b04 --- /dev/null +++ b/roles/fedimg/files/cron/kill_ec2_nodes.py @@ -0,0 +1,39 @@ +#!/bin/env python +# -*- coding: utf8 -*- + +# NOTE this is taken from the github repo +# https://github.com/fedora-infra/fedimg/blob/develop/bin/kill_ec2_nodes.py + + +from libcloud.compute.types import Provider +from libcloud.compute.providers import get_driver +import datetime +import fedimg + +EC2_ACCESS_ID = fedimg.AWS_ACCESS_ID +EC2_SECRET_KEY = fedimg.AWS_SECRET_KEY + +def kill_all_instances(region): + """ + Kills all the instances which are running for 2 hours or more. + + :param region: AWS region + """ + cls = get_driver(region) + driver = cls(EC2_ACCESS_ID, EC2_SECRET_KEY) + nodes = driver.list_nodes() + for n in nodes: + d1 = datetime.datetime.strptime(n.extra['launch_time'], '%Y-%m-%dT%H:%M:%S.000Z') + d2 = datetime.datetime.utcnow() + delta = d2 - d1 + if delta.total_seconds() > 7200: # If more than 2 hours of up time. + n.destroy() + + +if __name__ == '__main__': + regions = [Provider.EC2_AP_NORTHEAST, Provider.EC2_AP_SOUTHEAST, + Provider.EC2_AP_SOUTHEAST2, Provider.EC2_EU_WEST, + Provider.EC2_SA_EAST, Provider.EC2_US_EAST, + Provider.EC2_US_WEST, Provider.EC2_US_WEST_OREGON] + for region in regions: + kill_all_instances(region) diff --git a/roles/fedimg/tasks/main.yml b/roles/fedimg/tasks/main.yml index cfbfd6446b..41c160c1fd 100644 --- a/roles/fedimg/tasks/main.yml +++ b/roles/fedimg/tasks/main.yml @@ -80,3 +80,51 @@ when: env != "staging" tags: - fedimg + +- name: ensure the fedmsg user has a homedir for cron to work in + file: > + state=directory + path=/usr/share/fedmsg + mode=700 + owner=fedmsg + group=fedmsg + tags: + - cron + - fedimg + +- name: ensure fedimg cron directories exist + file: > + state=directory + path={{ item }} + mode=755 + owner=root + with_items: + - /usr/share/fedimg/cronjobs/ + - /etc/cron.d/ + tags: + - cron + - fedimg + +- name: fedimg cron scripts + copy: > + src=cron/{{ item }} + dest=/usr/share/fedimg/cronjobs/{{ item }} + owner=fedmsg + mode=744 + with_items: + - kill_ec2_nodes.py + tags: + - cron + - fedimg + +- name: fedimg cron definitions + copy: > + src=cron/{{ item }} + dest=/etc/cron.d/{{ item }} + owner=root + mode=644 + with_items: + - kill_ec2_nodes.cron + tags: + - cron + - fedimg