Deploy kill_ec2_nodes cronjob.
This commit is contained in:
parent
9a37206911
commit
0c241c4478
3 changed files with 89 additions and 0 deletions
2
roles/fedimg/files/cron/kill_ec2_nodes.cron
Normal file
2
roles/fedimg/files/cron/kill_ec2_nodes.cron
Normal file
|
@ -0,0 +1,2 @@
|
|||
MAILTO=sysadmin-fedimg-members@fedoraproject.org
|
||||
*/2 * * * * fedmsg /usr/local/bin/kill_ec2_nodes.py
|
39
roles/fedimg/files/cron/kill_ec2_nodes.py
Normal file
39
roles/fedimg/files/cron/kill_ec2_nodes.py
Normal file
|
@ -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)
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue