-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroy.py
55 lines (42 loc) · 1.5 KB
/
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
53
54
55
#!/usr/bin/env python
import argparse
import digitalocean
import helper
import sys
from configuration import Configuration
def destroy(config):
"""Destroy a droplet based on the configuration given"""
# Create manager
manager = digitalocean.Manager(token=config.token)
# Get droplet so we can destroy it
droplet = helper.get_droplet_by_name(manager, config.droplet_name)
if not droplet:
print 'Droplet does not exist: {}'.format(config.droplet_name)
return False
droplet.destroy()
# Should probably only see one?
actions = droplet.get_actions()
for action in actions:
action.wait()
print 'Action {} is {}'.format(action.type, action.status)
# Now get and upate the DNS record -- assumes a single A record, which we
# delete
domains = manager.get_all_domains()
for domain in domains:
if domain.name == config.domain_name:
records = domain.get_records()
for record in records:
if record.type == 'A':
record.destroy()
print 'Destroy of {} complete!'.format(config.domain_name)
return True
if __name__ == '__main__':
# Use argparse, even though just getting on arg for now
parser = argparse.ArgumentParser()
parser.add_argument('config_file', help='configuration file to use')
args = parser.parse_args()
# Read config file
config = Configuration()
if not config.read_config(args.config_file):
sys.exit(1)
destroy(config)