This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zonemanager.py
executable file
·220 lines (175 loc) · 7.8 KB
/
zonemanager.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#! /usr/bin/env python2.7
import argparse
from os import path
import ldns
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from zonedb.api import ApiApplication
from zonedb.models import AuthToken, Base, Environment, Record, Zone
from zonedb import master
def get_config():
"""Produces the configuration."""
parser = argparse.ArgumentParser(description='The LIGHTest zone manager.')
# Global options.
parser.add_argument("--database", "-d", action="store",
help="URL for then zones database",
default="sqlite:///zones.db")
subparsers = parser.add_subparsers(help="sub-command help")
Init.args(subparsers)
AddEnvironment.args(subparsers)
Resign.args(subparsers)
AddZone.args(subparsers)
AddRecord.args(subparsers)
AddToken.args(subparsers)
Server.args(subparsers)
return parser.parse_args()
class Init:
"""Initializes the database."""
@classmethod
def args(cls, subparsers):
sub = subparsers.add_parser("init", help=Init.__doc__)
sub.add_argument("--force", "-f", action="store_true",
help="recreate all tables")
sub.set_defaults(func=cls.run)
@classmethod
def run(cls, config):
if config.force:
print("Dropping existing database tables in '%s'" % config.database)
Base.metadata.drop_all(config.engine)
print("Creating database tables in `%s'" % config.database)
Base.metadata.create_all(config.engine)
class AddEnvironment:
"""Adds a new server config."""
@classmethod
def args(cls, subparsers):
sub = subparsers.add_parser("add-environment", help=cls.__doc__)
sub.add_argument("--environment", "-e", action="store", required=True,
help="name of the new environment")
sub.add_argument("--nsd-name", "-n", action="store", required=True,
help="host name of the primary name server")
sub.add_argument("--nsd-conf", "-c", action="store", required=True,
help="path to the NSD config to be created")
sub.add_argument("--nsd-reload", "-r", action="store", required=True,
help="command to reload NSD")
sub.add_argument("--key-file", "-k", action="store", required=True,
help="path to a file for temporary key storage.")
sub.set_defaults(func=cls.run)
@classmethod
def run(cls, config):
env = Environment(name=config.environment, nsd_name=config.nsd_name,
nsd_conf=path.abspath(config.nsd_conf),
nsd_reload=config.nsd_reload,
key_file=path.abspath(config.key_file))
config.session.add(env)
config.session.commit()
master.refresh_master(config.session)
class Resign:
"""Resigns all zones in all environments."""
@classmethod
def args(cls, subparsers):
sub = subparsers.add_parser("resign", help=cls.__doc__)
sub.set_defaults(func=cls.run)
@classmethod
def run(cls, config):
master.refresh_master(config.session)
class AddZone:
"""Adds a new zone."""
@classmethod
def args(cls, subparsers):
sub = subparsers.add_parser("add-zone", help=cls.__doc__)
sub.add_argument("--environment", "-e", action="store", required=True,
help="name of the environment to use")
sub.add_argument("--apex", "-a", action="store", required=True,
help="apex domain name for the zone")
sub.add_argument("--pattern", "-p", action="store", required=False,
help="the NSD pattern for the zone")
sub.set_defaults(func=cls.run)
@classmethod
def run(cls, config):
env = config.session.query(Environment) \
.filter_by(name=config.environment).one()
zone = Zone.defaults(config.apex, env)
if config.pattern:
zone.pattern = config.pattern
config.session.add(zone)
zone.create_keys(config.session)
config.session.commit()
master.refresh_master(config.session)
zone = config.session.query(Zone) \
.filter_by(environment=env, apex=config.apex).one()
print(master.get_ds(config.session, env, zone))
class AddRecord:
"""Adds one or more records to a zone."""
@classmethod
def args(cls, subparsers):
sub = subparsers.add_parser("add-record", help=cls.__doc__)
sub.add_argument("--environment", "-e", action="store", required=True,
help="name of the environment to use")
sub.add_argument("--apex", "-a", action="store", required=True,
help="apex domain name for the zone")
sub.add_argument("--ttl", "-t", action="store", type=int, default=3600,
help="the TTL of the records")
sub.add_argument("name", action="store",
help="the domain name of the records to be added")
sub.add_argument("rtype", action="store",
help="the record type of the records to be added")
sub.add_argument("data", action="store", nargs="+",
help="the data of the record (quoted if with space")
sub.set_defaults(func=cls.run)
@classmethod
def run(cls, config):
env = config.session.query(Environment) \
.filter_by(name=config.environment).one()
zone = config.session.query(Zone) \
.filter_by(environment=env, apex=config.apex).one()
for data in config.data:
config.session.add(
Record(zone=zone, name=config.name, rtype=config.rtype,
ttl=config.ttl, rdata=data)
)
config.session.commit()
master.refresh_master(config.session)
class AddToken:
"""Creates a new authentication token."""
@classmethod
def args(cls, subparsers):
sub = subparsers.add_parser("create-token", help=cls.__doc__)
sub.add_argument("--environment", "-e", action="store", required=True,
help="name of the environment to use")
sub.add_argument("name", action="store",
help="a name for the token to refer to it later")
sub.add_argument("zone", action="store",
help="zone to allow access to")
sub.set_defaults(func=cls.run)
@classmethod
def run(cls, config):
env = config.session.query(Environment) \
.filter_by(name=config.environment).one()
zone = config.session.query(Zone) \
.filter_by(environment=env, apex=config.zone).one()
token = AuthToken.create(config.name, zone)
config.session.add(token)
config.session.commit()
print(token.token)
class Server:
"""Runs the HTTP Server."""
@classmethod
def args(cls, subparsers):
sub = subparsers.add_parser("server", help=cls.__doc__)
sub.add_argument("--environment", "-e", action="store", required=True,
help="name of the environment to use")
sub.add_argument("bind", action="store", default="127.0.0.1:8088",
nargs="?",
help="address:port to bind to")
sub.set_defaults(func=cls.run)
@classmethod
def run(cls, config):
config.environment = config.session.query(Environment) \
.filter_by(name=config.environment).one()
ApiApplication(config).run()
if __name__ == "__main__":
config = get_config()
config.engine = create_engine(config.database)
Session = sessionmaker(bind=config.engine)
config.session = Session()
config.func(config)