summaryrefslogtreecommitdiff
path: root/hetznertool
blob: ed1fe5c910c8269abb3c8ffcd6adc4e088a72f6a (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3

import argparse
import copy
import os
import subprocess

import yaml


CONFIG_DIR = os.path.expanduser('~/.config/hetznertool')
DEFAULT_PROFILE = 'hetznertool'


default_config = {
    'ssh-key': None,
    'ns1': 'root@ns1.qvarnlabs.net',
}


def main():
    profile = os.environ.get('HETZNERTOOL_PROFILE', DEFAULT_PROFILE)
    config = read_config(profile)
    parser = create_parser(config)
    args = vars(parser.parse_args())
    func = args['func']
    func(config, args)


def hcloud(*args):
    argv = ['hcloud'] + list(args)
    output = subprocess.check_output(argv)
    return output.decode('UTF-8').splitlines()


def use_context(context):
    hcloud('context', 'use', context)


def list_contexts():
    lines = hcloud('context', 'list')
    assert len(lines) > 0
    assert lines[0] == "NAME"
    del lines[0]  # This is just the title line, says "NAME"
    return lines


def list_servers():
    lines = hcloud('server', 'list')
    assert len(lines) > 0
    del lines[0]  # This is just the title line
    return [parse_server_line(line) for line in lines]


def parse_server_line(line):
    words = line.split()
    return {
        'name': words[1],
        'ipv4': words[3],
    }


def dns_name(context, name, domain):
    return '{}-{}.{}'.format(context, name, domain)


class ServerSpecification(dict):

    def from_dict(self, as_dict):
        self.clear()
        self.update(copy.deepcopy(as_dict))

    def from_yaml(self, stream):
        self.from_dict(yaml.safe_load(stream))

    def from_file(self, filename):
        with open(filename) as f:
            self.from_yaml(f)

    def _hosts(self):
        return self.get('hosts', [])

    def _defaults(self):
        return copy.deepcopy(self.get('defaults', {}))

    def _get(self, name):
        hosts = self._hosts()
        for host in hosts:
            if host.get('name') == name:
                return copy.deepcopy(host)

    def get_servers(self):
        return [host['name'] for host in self._hosts()]

    def get_server(self, name):
        server = self._defaults()
        server.update(self._get(name))
        return server
        


def create_func(config, args):
    spec = ServerSpecification()
    spec.from_file(args['specfile'])

    context = args['context']
    use_context(context)

    for name in spec.get_servers():
        server = spec.get_server(name)
        print('creating {} in {}'.format(name, context))
        if args['act']:
            hcloud(
                'server', 'create',
                '--name', name,
                '--image', server['image'],
                '--type', server['type'],
                '--ssh-key', args['ssh_key'],
            )
    if args['act']:
        zonedir = get_zonedir_for_context(config, context)
        zonefile = get_zonefile_for_context(config, context)
        kick = get_kick_for_context(config, context)
        domain = get_domain_for_context(config, context)
        style = get_style_for_context(config, context)
        update_zone_file(style, domain, kick, args, zonedir, zonefile)
        write_inventory_files(config, context)


def list_func(config, args):
    contexts = list_contexts()
    for context in contexts:
        use_context(context)
        domain = get_domain_for_context(config, context)
        assert domain is not None
        for info in list_servers():
            name = dns_name(context, info['name'], domain)
            print(name, info['ipv4'])


def get_config_for_context(config, context):
    return config.get('contexts', {}).get(context, {})


def get_domain_for_context(config, context):
    cc = get_config_for_context(config, context)
    return cc.get('domain')


def get_zonedir_for_context(config, context):
    cc = get_config_for_context(config, context)
    return cc.get('dnszone-dir')


def get_zonefile_for_context(config, context):
    cc = get_config_for_context(config, context)
    return cc.get('dnszone-file')


def get_inventorydir_for_context(config, context):
    cc = get_config_for_context(config, context)
    return cc.get('ansible-inventory-dir')


def get_kick_for_context(config, context):
    cc = get_config_for_context(config, context)
    return cc.get('kick_bind9')


def get_style_for_context(config, context):
    cc = get_config_for_context(config, context)
    return cc.get('style')


def get_inventory_style_for_context(config, context):
    print('context:', repr(context))
    cc = get_config_for_context(config, context)
    return cc.get('inventory-style')


def delete_func(config, args):
    context = args['context']
    use_context(context)
    domain = get_domain_for_context(config, context)
    for info in list_servers():
        name = dns_name(args['context'], info['name'], domain)
        print(
            'deleting {} ({} in {})'.format(
                name, info['name'], args['context']))
        if args['act']:
            hcloud('server', 'delete', info['name'])

    if args['act']:
        zonedir = get_zonedir_for_context(config, context)
        zonefile = get_zonefile_for_context(config, context)
        kick = get_kick_for_context(config, context)
        style = get_style_for_context(config, context)
        update_zone_file(style, domain, kick, args, zonedir, zonefile)
        write_inventory_files(config, context)


def update_zone_file(style, domain, kick, args, dirname, basename):
    filename = os.path.join(dirname, basename)
    serial_name = os.path.join(dirname, 'serial')

    print('Updating zone file {}'.format(filename))

    subprocess.check_call(['git', 'pull'], cwd=dirname)

    filenames = [basename]

    if style == 'qvarnlabs':
        serial = int(open(serial_name).readline().strip())
        serial += 1
        open(serial_name, 'w').write('{}\n'.format(serial))
        with open(filename, 'w') as f:
            write_qvarnlabs_zone(f, serial)
        filenames.append(serial_name)
    elif style == 'dns-api':
        with open(filename, 'w') as f:
            write_dnsapi_zone(f, domain)
    else:
        assert 0

    subprocess.call(
        ['git', 'commit', '-m', 'automatic zone update'] + filenames,
        cwd=dirname)

    subprocess.check_call(['git', 'push'], cwd=dirname)

    if kick:
        kick_bind9(args['ns1'], filename, basename)


def write_qvarnlabs_zone(stream, serial):
    stream.write('''
$TTL 30
$ORIGIN h.qvarnlabs.eu.

@ IN SOA ns1.qvarnlabs.net. ops.qvarnlabs.com (
 {} 30 30 8640000 15 )

@ IN NS ns1.qvarnlabs.net.
@ IN NS ns2.qvarnlabs.net.

'''.format(serial))

    for context in list_contexts():
        use_context(context)
        for info in list_servers():
            stream.write(
                '{}-{} IN A {}\n'.format(context, info['name'], info['ipv4']))


def write_dnsapi_zone(stream, domain):
    for context in list_contexts():
        use_context(context)
        for info in list_servers():
            stream.write(
                '+{}-{}.{}:{}:60\n'.format(
                    context, info['name'], domain, info['ipv4']))


def write_inventory_files(config, context):
    style = get_inventory_style_for_context(config, context)
    if style == 'ipv4':
        write_inventory_files_with_ipv4(config)
    elif style == 'dns':
        write_inventory_files_with_dns_names(config, context)
    else:
        assert 0


def write_inventory_files_with_ipv4(config):
    for context in list_contexts():
        use_context(context)
        dirname = get_inventorydir_for_context(config, context)
        filename = os.path.join(dirname, 'hosts.{}'.format(context))
        print('Writing Ansible inventory file {}'.format(filename))
        with open(filename, 'w') as f:
            for info in list_servers():
                f.write(
                    '{} ansible_ssh_host={}\n'.format(
                        info['name'], info['ipv4']))


def write_inventory_files_with_dns_names(config, context):
    domain = get_domain_for_context(config, context)
    for context in list_contexts():
        use_context(context)
        dirname = get_inventorydir_for_context(config, context)
        filename = os.path.join(dirname, 'hosts.{}'.format(context))
        print('Writing Ansible inventory file {}'.format(filename))
        with open(filename, 'w') as f:
            for info in list_servers():
                name = dns_name(context, info['name'], domain)
                f.write(
                    '{} ansible_ssh_host={}\n'.format(info['name'], name))


def kick_bind9(ssh_target, filename, basename):
    target = '{}:/etc/bind/{}'.format(ssh_target, basename)
    subprocess.check_call(['scp', filename, target])
    subprocess.check_call(['ssh', ssh_target, 'systemctl', 'reload', 'bind9'])
    


def read_config(profile):
    config = copy.deepcopy(default_config)
    filename = os.path.join(CONFIG_DIR, '{}.yaml'.format(profile))
    if os.path.exists(filename):
        with open(filename) as f:
            config.update(yaml.safe_load(f))
    return config


def create_parser(config):
    parser = argparse.ArgumentParser(
        description='Manage VMs in Hetzner Cloud for QvarnLabs.')

    factory = parser.add_subparsers()

    create = factory.add_parser('create')
    create.add_argument(
        '-n', '--no-act', dest='act', default=True, action='store_false')
    create.add_argument(
        'context', help='hcloud context to create VMs in')
    create.add_argument(
        'specfile', help='file to read VM specifications from')
    create.add_argument(
        '--ssh-key',
        metavar='KEYNAME',
        required='ssh-key' not in config,
        default=config['ssh-key'],
        help='create VM so it allow login via ssh key uploaded as KEYNAME')
    create.add_argument(
        '--ns1', default=config['ns1'],
        required='ns1' not in config,
        metavar='USER@ADDRESS',
        help='copy zone file to primary DNS server via ssh')

    servers = factory.add_parser('list')

    delete = factory.add_parser('delete')
    delete.add_argument(
        '-n', '--no-act', dest='act', default=True, action='store_false')
    delete.add_argument(
        'context', help='hcloud context to delete all VMs from')
    delete.add_argument(
        '--ns1', default=config['ns1'],
        required='ns1' not in config,
        metavar='USER@ADDRESS',
        help='copy zone file to primary DNS server via ssh')

    create.set_defaults(func=create_func)
    servers.set_defaults(func=list_func)
    delete.set_defaults(func=delete_func)

    return parser


if __name__ == '__main__':
    main()