-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldns
executable file
·126 lines (92 loc) · 2.46 KB
/
ldns
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import os
import sys
import warnings
warnings.filterwarnings(
action='ignore',
category=RuntimeWarning,
module='linode.api',
lineno=59
)
from linode.api import Api
API_KEY = os.environ.get('LINODE_API_KEY')
A = 'A'
AAAA = 'AAAA'
def list_domains(l, args):
for domain in l.domain_list():
print domain['DOMAIN']
def _domain_id_by_name(l, domain_name):
for domain in l.domain_list():
if domain['DOMAIN'] == domain_name:
return domain['DOMAINID']
def _records_for_domain(l, domain_id, record_type=None):
all_records = l.domain_resource_list(DomainID=domain_id)
for record in all_records:
if record_type and record_type != record['TYPE'].upper():
continue
yield record
def _add_record(l, domain_id, record_type, **kwargs):
l.domain_resource_create(
DomainID=domain_id,
Type=record_type,
**kwargs
)
def _a_record_add(l, domain_id, record_type, args):
if len(args) != 2:
print "usage"
sys.exit(1)
name, ip = args
_add_record(
l,
domain_id,
record_type,
Name=name,
Target=ip
)
_a_actions = {
'add': _a_record_add
}
def _a_record(l, args, record_type):
if len(args) == 0:
print "usage"
sys.exit(1)
domain_name = args[0]
args = args[1:]
domain_id = _domain_id_by_name(l, domain_name)
if len(args) == 0:
# list all A records
names_and_ips = [
(record['NAME'], record['TARGET'])
for record in _records_for_domain(l, domain_id, record_type)
]
max_name_width = min(8, max(len(name) for name, ip in names_and_ips))
for name, ip in names_and_ips:
print name + ' ' * (max_name_width - len(name) + 4) + ip
return
action = args[0]
args = args[1:]
if action not in _a_actions:
print "usage"
sys.exit(1)
_a_actions[action](l, domain_id, record_type, args)
def a_record(l, args):
return _a_record(l, args, A)
def aaaa_record(l, args):
return _a_record(l, args, AAAA)
_actions = {
'ls': list_domains,
'a': a_record,
'aaaa': aaaa_record
}
if __name__ == '__main__':
args = sys.argv[1:]
if len(args) == 0:
print "usage"
sys.exit(0)
l = Api(API_KEY)
action = args[0]
args = args[1:]
if action not in _actions:
print "blah"
sys.exit(1)
_actions[action](l, args)