forked from batrick/ceph-linode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linode-destroy.py
52 lines (38 loc) · 1.39 KB
/
linode-destroy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import binascii
import logging
import os
import errno
from multiprocessing.dummy import Pool as ThreadPool
from contextlib import closing
import linode.api as linapi
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
with open("LINODE_GROUP") as f:
GROUP = unicode(f.read().strip())
def main():
key = os.getenv("LINODE_API_KEY")
if key is None:
raise RuntimeError("please specify Linode API key")
client = linapi.Api(key = key, batching = False)
def destroy(linode):
client.linode_delete(LinodeID = linode[u'LINODEID'], skipChecks = 1)
linodes = client.linode_list()
logging.info("linodes: {}".format(linodes))
with closing(ThreadPool(5)) as pool:
group = filter(lambda linode: linode[u'LPM_DISPLAYGROUP'] == GROUP, linodes)
pool.map(destroy, group)
pool.close()
pool.join()
linodes = client.linode_list()
logging.info("linodes: {}".format(linodes))
# clear inventory file or else launch.sh won't create linodes
ansible_inv_file = os.getenv('ANSIBLE_INVENTORY')
if not ansible_inv_file:
ansible_inv_file = 'ansible_inventory'
try:
os.unlink(ansible_inv_file)
except OSError as e:
if e.errno != errno.ENOENT:
raise e
logging.info('removed ansible inventory file %s' % ansible_inv_file)
if __name__ == "__main__":
main()