building_manager.py 30.7 KB
Newer Older
1
#!/usr/bin/env python3
2
""" Python module to assist creating and maintaining docker openHab stacks."""
3
import crypt
4
import logging
5
import os
6
from shutil import copy2
7
8
9
10
from subprocess import PIPE, run

import bcrypt
import docker
11
from PyInquirer import prompt
12
13
14
15
16
from ruamel.yaml import YAML

# Configure YAML
yaml = YAML()
yaml.indent(mapping=4, sequence=4, offset=2)
17
18
19

# Log level during development is info
logging.basicConfig(level=logging.WARNING)
20

21
# Directories for config generation
22
23
CUSTOM_DIR = 'custom_configs'
TEMPLATE_DIR = 'template_configs'
24
25
26
COMPOSE_NAME = 'docker-stack.yml'
SKELETON_NAME = 'docker-skeleton.yml'
TEMPLATES_NAME = 'docker-templates.yml'
Dobli's avatar
Dobli committed
27
28
29
30
CONFIG_DIRS = ['mosquitto', 'nodered', 'ssh', 'traefik', 'volumerize']
TEMPLATE_FILES = [
    'mosquitto/mosquitto.conf', 'nodered/nodered_package.json',
    'nodered/nodered_settings.js', 'ssh/sshd_config', 'traefik/traefik.toml'
31
]
32
33
EDIT_FILES = {
    "mosquitto_passwords": "mosquitto/mosquitto_passwords",
34
    "sftp_users": "ssh/sftp_users.conf",
35
36
37
    "traefik_users": "traefik/traefik_users",
    "id_rsa": "ssh/id_rsa",
    "host_key": "ssh/ssh_host_ed25519_key",
38
39
40
    "known_hosts": "ssh/known_hosts",
    "backup_config": "volumerize/backup_config.json"
}
Dobli's avatar
Dobli committed
41
CONSTRAINTS = {"building": "node.labels.building"}
42
43
44
45
46
SERVICES = {
    "sftp": "sftp_X",
    "openhab": "openhab_X",
    "nodered": "nodered_X",
    "mqtt": "mqtt_X"
47
}
48

49
# Default Swarm port
dobli's avatar
dobli committed
50
SWARM_PORT = 2377
51
52
# UID for admin
UID = 9001
Dobli's avatar
Dobli committed
53
54
# Username for admin
ADMIN_USER = 'ohadmin'
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

# ******************************
# Compose file functions {{{
# ******************************


def generate_initial_compose(base_dir):
    """Creates the initial compose using the skeleton

    :base_dir: Folder to place configuration files into
    """
    base_path = base_dir + '/' + CUSTOM_DIR
    template_path = base_dir + '/' + TEMPLATE_DIR
    # compose file
    compose = base_path + '/' + COMPOSE_NAME
    # skeleton file
    skeleton = template_path + '/' + SKELETON_NAME

    with open(skeleton, 'r') as skeleton_f, open(compose, 'w+') as compose_f:
        init_content = yaml.load(skeleton_f)
        yaml.dump(init_content, compose_f)


def add_sftp_service(base_dir, hostname, number=0):
    """Generates an sftp entry and adds it to the compose file

    :base_dir: base directory for configuration files
    :hostname: names of host that the services is added to
    :number: increment of exposed port to prevent overlaps
    """
    base_path = base_dir + '/' + CUSTOM_DIR
    # compose file
    compose_path = base_path + '/' + COMPOSE_NAME
    # service name
    service_name = f'sftp_{hostname}'
90
91
92
93
94
95
    # template
    template = get_service_template(base_dir, SERVICES['sftp'])
    # only label contraint is building
    template['deploy']['placement']['constraints'][0] = (
        f"{CONSTRAINTS['building']} == {hostname}")
    template['ports'] = [f'{2222 + number}:22']
96

97
    add_or_update_compose_service(compose_path, service_name, template)
98
99
100
101
102
103
104
105
106
107
108
109
110


def add_openhab_service(base_dir, hostname):
    """Generates an openhab entry and adds it to the compose file

    :base_dir: base directory for configuration files
    :hostname: names of host that the services is added to
    """
    base_path = base_dir + '/' + CUSTOM_DIR
    # compose file
    compose_path = base_path + '/' + COMPOSE_NAME
    # service name
    service_name = f'openhab_{hostname}'
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    # template
    template = get_service_template(base_dir, SERVICES['openhab'])
    # only label contraint is building
    template['deploy']['placement']['constraints'][0] = (
        f"{CONSTRAINTS['building']} == {hostname}")
    # include in backups of this building
    template['deploy']['labels'].append(f'backup={hostname}')
    # traefik backend
    template['deploy']['labels'].append(f'traefik.backend={service_name}')
    # traefik frontend domain->openhab
    template['deploy']['labels'].extend(
        generate_traefik_host_labels(hostname, segment='main'))
    # traefik frontend subdomain openhab_hostname.* -> openhab
    template['deploy']['labels'].append(
        f'traefik.sub.frontend.rule=HostRegexp:'
        f'{service_name}.{{domain:[a-zA-z0-9-]+}}')
    template['deploy']['labels'].append('traefik.sub.frontend.priority=2')

    add_or_update_compose_service(compose_path, service_name, template)
130
131
132
133
134
135
136
137
138
139
140
141
142


def add_nodered_service(base_dir, hostname):
    """Generates an nodered entry and adds it to the compose file

    :base_dir: base directory for configuration files
    :hostname: names of host that the services is added to
    """
    base_path = base_dir + '/' + CUSTOM_DIR
    # compose file
    compose_path = base_path + '/' + COMPOSE_NAME
    # service name
    service_name = f'nodered_{hostname}'
143
144
145
146
147
148
149
150
151
    # template
    template = get_service_template(base_dir, SERVICES['nodered'])
    # only label contraint is building
    template['deploy']['placement']['constraints'][0] = (
        f"{CONSTRAINTS['building']} == {hostname}")
    template['deploy']['labels'].append(f'traefik.backend={service_name}')
    template['deploy']['labels'].append(f'backup={hostname}')
    template['deploy']['labels'].extend(
        generate_traefik_path_labels(service_name, segment='main'))
152

153
    add_or_update_compose_service(compose_path, service_name, template)
154
155
156
157
158
159
160
161
162
163
164
165
166
167


def add_mqtt_service(base_dir, hostname, number=0):
    """Generates an mqtt entry and adds it to the compose file

    :base_dir: base directory for configuration files
    :hostname: names of host that the services is added to
    :number: increment of exposed port to prevent overlaps
    """
    base_path = base_dir + '/' + CUSTOM_DIR
    # compose file
    compose_path = base_path + '/' + COMPOSE_NAME
    # service name
    service_name = f'mqtt_{hostname}'
168
169
170
171
172
173
174
    # template
    template = get_service_template(base_dir, SERVICES['mqtt'])
    # only label contraint is building
    template['deploy']['placement']['constraints'][0] = (
        f"{CONSTRAINTS['building']} == {hostname}")
    # ports incremented by number of services
    template['ports'] = [f'{1883 + number}:1883', f'{9001 + number}:9001']
175

176
    add_or_update_compose_service(compose_path, service_name, template)
177
178


179
# Helper functions
180
181
182
183
184
185
186
187
188
189
190
191
def get_service_template(base_dir, service_name):
    """Gets a service template entry from the template yaml

    :return: yaml entry of a service
    """
    template_path = base_dir + '/' + TEMPLATE_DIR
    templates = template_path + '/' + TEMPLATES_NAME

    with open(templates, 'r') as templates_file:
        template_content = yaml.load(templates_file)

    return template_content['services'][service_name]
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


def generate_traefik_host_labels(hostname, segment=None, priority=1):
    """Generates a traefik path url with necessary redirects

    :hostname: Hostname that gets assigned by the label
    :segment: Optional traefik segment when using multiple rules
    :priority: Priority of frontend rule
    :returns: list of labels for traefik
    """
    label_list = []
    # check segment
    segment = f'.{segment}' if segment is not None else ''
    # fill list
    label_list.append(
        f'traefik{segment}.frontend.rule=HostRegexp:{{domain:{hostname}}}')
    label_list.append(f'traefik{segment}.frontend.priority={priority}')
    return label_list


def generate_traefik_path_labels(url_path, segment=None, priority=2):
    """Generates a traefik path url with necessary redirects

    :url_path: path that should be used for the site
    :segment: Optional traefik segment when using multiple rules
    :priority: Priority of frontend rule
    :returns: list of labels for traefik
    """
    label_list = []
    # check segment
    segment = f'.{segment}' if segment is not None else ''
    # fill list
    label_list.append(f'traefik{segment}.frontend.priority={priority}')
    label_list.append(
        f'traefik{segment}.frontend.redirect.regex=^(.*)/{url_path}$$')
    label_list.append(
        f'traefik{segment}.frontend.redirect.replacement=$$1/{url_path}/')
    label_list.append(
        f'traefik{segment}.frontend.rule=PathPrefix:/{url_path};'
        f'ReplacePathRegex:^/{url_path}/(.*) /$$1')
    return label_list


def add_or_update_compose_service(compose_path, service_name, service_content):
    """Adds or replaces a service in a compose file

    :compose_path: path of the compose file to change
    :service_name: name of the service to add/replace
    :service_content: service definition to add
    """
    with open(compose_path, 'r+') as compose_f:
        # load compose file
        compose = yaml.load(compose_f)
        # add / update service with template
        compose['services'][service_name] = service_content
        # write content starting from first line
        compose_f.seek(0)
        # write new compose content
        yaml.dump(compose, compose_f)
        # reduce file to new size
        compose_f.truncate()
253
# }}}
dobli's avatar
dobli committed
254

255

256
# ******************************
dobli's avatar
dobli committed
257
# Config file functions {{{
258
# ******************************
259
def generate_config_folders(base_dir):
260
261
    """Generate folders for configuration files

262
    :base_dir: Path to add folders to
263
    """
264
265
266
267
268
269
    base_path = base_dir + '/' + CUSTOM_DIR
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)

    print(f'Initialize configuration in {base_path}')

270
    # generate empty config dirs
271
272
273
274
275
    for d in CONFIG_DIRS:
        new_dir = base_path + '/' + d
        if not os.path.exists(new_dir):
            os.makedirs(new_dir)

276
277
278
279
    # copy template configs
    for template_file in TEMPLATE_FILES:
        copy_template_config(base_dir, template_file)

280

281
282
283
284
285
286
def copy_template_config(base_dir, config_path):
    """Copies template configuration files into custom folder

    :base_dir: path that contains template and custom folders
    :config_path: relative path of config to copy from template
    """
Dobli's avatar
Dobli committed
287
288
289
290
291
    custom_path = base_dir + '/' + CUSTOM_DIR + "/" + config_path
    template_path = base_dir + '/' + TEMPLATE_DIR + "/" + config_path

    logging.info(f'Copy {config_path} from {custom_path} to {template_path}')
    copy2(template_path, custom_path)
292
293


294
295
296
297
298
299
def generate_mosquitto_user_line(username, password):
    """Generates a line for a mosquitto user with a crypt hashed password

    :username: username to use
    :password: password that will be hashed (SHA512)

300
    :returns: a line as expected by mosquitto
301
302
303
304
305
306
    """
    password_hash = crypt.crypt(password, crypt.mksalt(crypt.METHOD_SHA512))
    line = f"{username}:{password_hash}"
    return line


307
308
309
310
def generate_sftp_user_line(username, password, directories=None):
    """Generates a line for a sftp user with a hashed password

    :username: username to use
311
    :password: password that will be hashed (SHA512)
312
313
    :directories: list of directories which the user should have

314
    :returns: a line as expected by sshd
315
316
317
    """
    # generate user line with hashed password
    password_hash = crypt.crypt(password, crypt.mksalt(crypt.METHOD_SHA512))
318
    line = f"{username}:{password_hash}:e:{UID}:{UID}"
319
320
321
322
323
324
325
326
    # add directory entries when available
    if directories:
        # create comma separated string from list
        dir_line = ','.join(d for d in directories)
        line = f"{line}:{dir_line}"
    return line


327
328
329
330
331
332
333
334
335
336
337
338
339
def generate_traefik_user_line(username, password):
    """Generates a line for a traefik user with a bcrypt hashed password

    :username: username to use
    :password: password that will be hashed (bcrypt)

    :returns: a line as expected by traefik
    """
    password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
    line = f"{username}:{password_hash.decode()}"
    return line


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def generate_mosquitto_file(base_dir, username, password):
    """Generates a mosquitto password file using mosquitto_passwd system tool

    :base_dir: path that contains custom config folder
    :username: username to use
    :password: password that will be used
    """
    passwd_path = base_dir + '/' + CUSTOM_DIR + "/" + EDIT_FILES[
        'mosquitto_passwords']

    # ensure file exists
    if not os.path.exists(passwd_path):
        open(passwd_path, 'a').close()

    # execute mosquitto passwd
    mos_result = run(
        ['mosquitto_passwd', '-b', passwd_path, username, password],
357
        universal_newlines=True)
358
359
360
361
    return mos_result.returncode == 0


def generate_sftp_file(base_dir, username, password, direcories=None):
362
    """Generates a sftp password file
363
364
365
366
367
368
369
370
371
372
373
374

    :base_dir: path that contains custom config folder
    :username: username to use
    :password: password that will be used
    :directories: list of directories which the user should have
    """
    # generate line and save it into a file
    file_content = generate_sftp_user_line(username, password, direcories)
    create_or_replace_config_file(base_dir, EDIT_FILES['sftp_users'],
                                  file_content)


375
376
377
378
379
380
381
382
383
384
def generate_id_rsa_files(base_dir):
    """Generates id_rsa and id_rsa.pub private/public keys using ssh-keygen

    :base_dir: path that contains custom config folder
    """
    id_path = base_dir + '/' + CUSTOM_DIR + "/" + EDIT_FILES['id_rsa']

    # execute ssh-keygen
    id_result = run(
        ['ssh-keygen', '-t', 'rsa', '-b', '4096', '-f', id_path, '-N', ''],
385
        universal_newlines=True, stdout=PIPE)
386
387
388
389
390
391
392
393
394
395
396
    return id_result.returncode == 0


def generate_host_key_files(base_dir, hosts):
    """Generates ssh host keys and matching known_hosts using ssh-keygen

    :base_dir: path that contains custom config folder
    """
    key_path = base_dir + '/' + CUSTOM_DIR + "/" + EDIT_FILES['host_key']
    # ssh-keygen generates public key with .pub postfix
    pub_path = key_path + '.pub'
397
398
    # host_names with sftp_ postfix
    sftp_hosts = [f'sftp_{host}' for host in hosts]
399
400
401

    # execute ssh-keygen
    id_result = run(['ssh-keygen', '-t', 'ed25519', '-f', key_path, '-N', ''],
402
                    universal_newlines=True, stdout=PIPE)
403
404
405
406
407
408
409
410

    # read content of public key as known line
    known_line = ""
    with open(pub_path, 'r') as pub_file:
        pub_line = pub_file.readline()
        split_line = pub_line.split()
        # delete last list element
        del split_line[-1]
411
412
        # collect sftp hosts as comma separated string
        hosts_line = ','.join(h for h in sftp_hosts)
413
414
415
416
417
418
419
420
421
422
423
        split_line.insert(0, hosts_line)
        # collect parts as space separated string
        known_line = ' '.join(sp for sp in split_line)

    # write new known_line file
    create_or_replace_config_file(base_dir, EDIT_FILES['known_hosts'],
                                  known_line)

    return id_result.returncode == 0


424
425
426
427
428
429
430
431
432
433
434
435
436
def generate_traefik_file(base_dir, username, password):
    """Generates a traefik password file

    :base_dir: path that contains custom config folder
    :username: username to use
    :password: password that will be used
    """
    # generate line and save it into a file
    file_content = generate_traefik_user_line(username, password)
    create_or_replace_config_file(base_dir, EDIT_FILES['traefik_users'],
                                  file_content)


437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
def generate_volumerize_file(base_dir, hosts):
    """Generates config for volumerize backups

    :base_dir: path that contains custom config folder
    :hosts: names of backup hosts
    """
    configs = []

    for h in hosts:
        host_config = {
            'description': f'Backup Server on {h}',
            'url': f'sftp://ohadmin@sftp_{h}://home/ohadmin/backup_data/{h}'
        }
        configs.append(host_config)

    create_or_replace_config_file(
        base_dir, EDIT_FILES['backup_config'], configs, json=True)


def create_or_replace_config_file(base_dir, config_path, content, json=False):
457
458
459
460
461
462
463
464
    """Creates or replaces a config file with new content

    :base_dir: path that contains custom config folder
    :config_path: relative path of config
    :content: content of the file as a string
    """
    custom_path = base_dir + '/' + CUSTOM_DIR + "/" + config_path
    with open(custom_path, 'w+') as file:
465
466
467
468
469
        if json:
            import json
            json.dump(content, file, indent=2)
        else:
            file.write(content)
470
471


dobli's avatar
dobli committed
472
473
474
# }}}


475
# ******************************
dobli's avatar
dobli committed
476
# Docker machine functions {{{
477
# ******************************
478
479
480
481
482
483
def get_machine_list():
    """Get a list of docker machine names using the docker-machine system command

    :returns: a list of machine names managed by docker-machine
    """
    machine_result = run(['docker-machine', 'ls', '-q'],
484
485
                         universal_newlines=True,
                         stdout=PIPE)
486
487
488
489
490
491
492
    return machine_result.stdout.splitlines()


def check_machine_exists(machine_name):
    """Checks weather a docker machine exists and is available

    :machine_name: Name of the machine to check
dobli's avatar
dobli committed
493
    :returns: True when machine is available
494
495
496
497
498
499
500
501
502
503
504
505
506
    """
    machines = get_machine_list()

    return machine_name in machines


def get_machine_env(machine_name):
    """Gets dict of env settings from a machine

    :machine_name: Name of the machine to check
    :returns: Dict of env variables for this machine
    """
    env_result = run(['docker-machine', 'env', machine_name],
507
508
                     universal_newlines=True,
                     stdout=PIPE)
509
510
511
512
513
514
515
516
517
518
519
520

    machine_envs = {}

    lines = env_result.stdout.splitlines()
    for line in lines:
        if 'export' in line:
            assign = line.split('export ', 1)[1]
            env_entry = [a.strip('"') for a in assign.split('=', 1)]
            machine_envs[env_entry[0]] = env_entry[1]
    return machine_envs


dobli's avatar
dobli committed
521
522
523
524
525
526
def get_machine_ip(machine_name):
    """Asks for the ip of the docker machine

    :machine_name: Name of the machine to use for init
    """
    machine_result = run(['docker-machine', 'ip', machine_name],
527
528
                         universal_newlines=True,
                         stdout=PIPE)
529
    return machine_result.stdout.strip()
dobli's avatar
dobli committed
530
531
532
533
534
535


def init_swarm_machine(machine_name):
    """Creates a new swarm with the specified machine as leader

    :machine_name: Name of the machine to use for init
536
    :return: True if swarm init was successful
dobli's avatar
dobli committed
537
538
539
    """
    machine_ip = get_machine_ip(machine_name)
    init_command = 'docker swarm init --advertise-addr ' + machine_ip
540
    init_result = run(['docker-machine', 'ssh', machine_name, init_command],
541
                      universal_newlines=True)
542
    return init_result.returncode == 0
dobli's avatar
dobli committed
543
544
545
546
547
548
549


def join_swarm_machine(machine_name, leader_name):
    """Joins the swarm of the specified leader

    :machine_name: Name of the machine to join a swarm
    :leader_name: Name of the swarm leader machine
550
    :return: True if join to swarm was successful
dobli's avatar
dobli committed
551
552
553
    """
    token_command = 'docker swarm join-token manager -q'
    token_result = run(['docker-machine', 'ssh', leader_name, token_command],
554
555
                       universal_newlines=True,
                       stdout=PIPE)
556
    token = token_result.stdout.strip()
dobli's avatar
dobli committed
557
    leader_ip = get_machine_ip(leader_name)
558
    logging.info(f"Swarm leader with ip {leader_ip} uses token {token}")
dobli's avatar
dobli committed
559

560
561
562
    join_cmd = f'docker swarm join --token {token} {leader_ip}:{SWARM_PORT}'
    logging.info(f'Machine {machine_name} joins using command {join_cmd}')
    join_result = run(['docker-machine', 'ssh', machine_name, join_cmd],
563
                      universal_newlines=True)
dobli's avatar
dobli committed
564

565
    return join_result.returncode == 0
dobli's avatar
dobli committed
566
567


568
569
570
571
572
573
def generate_swarm(machines):
    """Generates a swarm, the first machine will be the initial leader

    :machines: list of machines in the swarm
    """
    leader = None
574
    for machine in machines:
575
576
577
578
579
580
        # init swarm with first machine
        if leader is None:
            leader = machine
            print(f'Create initial swarm with leader {leader}')
            if init_swarm_machine(leader):
                print('Swarm init successful\n')
581
582
                assign_label_to_node(leader, 'building',
                                     leader, manager=leader)
583
584
585
586
        else:
            print(f'Machine {machine} joins swarm of leader {leader}')
            if (join_swarm_machine(machine, leader)):
                print('Joining swarm successful\n')
587
588
                assign_label_to_node(machine, 'building',
                                     machine, manager=leader)
Dobli's avatar
Dobli committed
589
590


dobli's avatar
dobli committed
591
592
593
# }}}


594
# ******************************
dobli's avatar
dobli committed
595
# Docker client commands {{{
596
# ******************************
597
def assign_label_to_node(nodeid, label, value, manager=None):
598
599
600
601
602
    """Assigns a label to a node (e.g. building)

    :nodeid: Id or name of the node
    :label: Label you want to add
    :value: The value to assign to the label
603
    :manager: Dpcker machine to use for command, otherwise local
604
    """
605
606
607
608
609
    if manager:
        building_env = get_machine_env(manager)
        client = docker.from_env(environment=building_env)
    else:
        client = docker.from_env()
610
611
612
613
614

    node = client.nodes.get(nodeid)
    spec = node.attrs['Spec']
    spec['Labels'][label] = value
    node.update(spec)
615
    logging.info(f'Assign label {label} with value {value} to {nodeid}')
616
617
618
619

    client.close()


620
621
622
623
624
625
626
627
628
629
def run_command_in_service(service, command, building=None):
    """Runs a command in a service based on its name.
    When no matching container is found or the service name is ambigous
    an error will be displayed and the function exits

    :param service: Name of the service to execute command
    :param command: Command to execute
    :param building: Optional building, make service unambigous (Default: None)
    """

630
631
632
633
634
    if building:
        building_env = get_machine_env(building)
        client = docker.from_env(environment=building_env)
    else:
        client = docker.from_env()
635
636
637
638
639
640
641

    # Find containers matching name
    service_name_filter = {"name": service}
    containers = client.containers.list(filters=service_name_filter)

    # Ensure match is unambigous
    if (len(containers) > 1):
642
        print(f'Found multiple containers matching service name {service}, '
643
644
              'ensure service is unambigous')
    elif (len(containers) < 1):
645
        print(f'Found no matching container for service name {service}')
646
647
    else:
        service_container = containers[0]
648
649
        print(f'Executing {command} in container {service_container.name}'
              f'({service_container.id}) on building {building}')
650
        print(service_container.exec_run(command))
651
    client.close()
652
653


dobli's avatar
dobli committed
654
655
656
# }}}


657
# ******************************
dobli's avatar
dobli committed
658
# CLI base commands {{{
659
# ******************************
660
661
662
663
664
665
666
667
def init_config_dirs_command(args):
    """Initialize config directories

    :args: parsed commandline arguments
    """
    base_dir = args.base_dir

    if base_dir is None:
668
        base_dir = os.getcwd()
669

Dobli's avatar
Dobli committed
670
    # generate basic config folder
671
672
673
    generate_config_folders(base_dir)


674
675
676
677
678
679
680
681
def assign_building_command(args):
    """Assigns the role of a building to a node

    :args: parsed commandline arguments
    """
    node = args.node
    building = args.building

682
    print(f'Assign role of building {building} to node {node}')
683
684
685
686

    assign_label_to_node(node, 'building', building)


687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
def execute_command(args):
    """Top level function to manage command executions from CLI

    :args: parsed commandline arguments
    """
    service = args.service
    command = " ".join(str(x) for x in args.command)  # list to string
    building = args.building

    run_command_in_service(service, command, building)


def restore_command(args):
    """Top level function to manage command executions from CLI

    :args: parsed commandline arguments
    """
704
705
706
707
    building = args.building
    target = args.target

    if not check_machine_exists(target):
708
        print(f'Machine with name {target} not found')
709
710
        return

711
    print(f'Restoring building {building} on machine {target}')
712
713

    get_machine_env(target)
714
715


716
717
718
def interactive_command(args):
    """Top level function to start the interactive mode

719
    :args: parsed command line arguments
720
    """
Dobli's avatar
Dobli committed
721
    main_menu(args)
722
723


dobli's avatar
dobli committed
724
725
726
# }}}


727
# ******************************
dobli's avatar
dobli committed
728
# Interactive menu entries {{{
729
# ******************************
730
def main_menu(args):
731
732
    """ Display main menu
    """
733
734
735
736
737
738
739
    # Base directory for configs
    base_dir = args.base_dir

    if base_dir is None:
        base_dir = os.getcwd()

    # Main menu prompts
740
    questions = [{
741
742
743
744
        'type': 'list',
        'name': 'main',
        'message': 'Public Building Manager - Main Menu',
        'choices': load_main_entires(base_dir)
745
746
747
748
    }]
    answers = prompt(questions)

    if 'Create' in answers['main']:
749
        init_menu(args)
750
751
752
753

    return answers


754
def init_menu(args):
755
756
    """Menu entry for initial setup and file generation
    """
757
758
759
760
761
762
763
    # Base directory for configs
    base_dir = args.base_dir

    if base_dir is None:
        base_dir = os.getcwd()

    # Prompts
Dobli's avatar
Dobli committed
764
    questions = [
765
        {
Dobli's avatar
Dobli committed
766
767
768
769
            'type': 'input',
            'name': 'stack_name',
            'message': 'Choose a name for your setup'
        },
770
        {
Dobli's avatar
Dobli committed
771
772
773
774
775
776
            'type': 'checkbox',
            'name': 'machines',
            'message': 'What docker machines will be used?',
            'choices': generate_checkbox_choices(get_machine_list())
        }
    ]
777
778
    answers = prompt(questions)

779
780
781
782
    # Ensure passwords match
    password_match = False
    while not password_match:
        password_questions = [{
Dobli's avatar
Dobli committed
783
784
785
786
787
788
            'type':
            'password',
            'name':
            'password',
            'message':
            'Choose a password for the ohadmin user:',
789
790
        },
            {
Dobli's avatar
Dobli committed
791
792
793
794
795
796
            'type':
            'password',
            'name':
            'confirm',
            'message':
            'Repeat password for the ohadmin user',
797
798
799
800
        }]
        password_answers = prompt(password_questions)
        if password_answers['password'] == password_answers['confirm']:
            password_match = True
dobli's avatar
dobli committed
801
        else:
802
            print("Passwords did not match, try again")
803

804
805
    # Initialize custom configuration dirs and templates
    generate_config_folders(base_dir)
806
    generate_initial_compose(base_dir)
807
    # Generate config files based on input
Dobli's avatar
Dobli committed
808
    username = ADMIN_USER
809
810
811
812
813
    password = password_answers['password']
    hosts = answers['machines']
    generate_sftp_file(base_dir, username, password)
    generate_mosquitto_file(base_dir, username, password)
    generate_traefik_file(base_dir, username, password)
814
    generate_volumerize_file(base_dir, hosts)
815
    generate_id_rsa_files(base_dir)
816
817
    generate_host_key_files(base_dir, hosts)

818
    for i, host in enumerate(hosts):
819
        init_machine_menu(base_dir, host, i)
820

821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
    # print(answers)
    print(f"Configuration files generated in {base_dir}")

    # Check if changes shall be applied to docker environment
    generate_questions = [{
        'type': 'confirm',
        'name': 'generate',
        'message': 'Apply changes to docker environment?',
        'default': True
    }]
    generate_answers = prompt(generate_questions)

    if generate_answers['generate']:
        generate_swarm(answers['machines'])


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
def init_machine_menu(base_dir, host, increment):
    """Prompts to select server services

    :base_dir: Directory of config files
    :host: docker-machine host
    :increment: incrementing number to ensure ports are unique
    """
    # Prompt for services
    questions = [
        {
            'type': 'input',
            'name': 'buildingid',
            'message': f'Choose a name for building on server {host}'
        },
        {
            'type': 'checkbox',
            'name': 'services',
854
            'message': f'What services shall {host} provide?',
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
            'choices': generate_checkbox_choices(SERVICES.keys(), checked=True)
        }
    ]
    answers = prompt(questions)
    services = answers['services']
    if 'sftp' in services:
        add_sftp_service(base_dir, host, increment)
    if 'openhab' in services:
        add_openhab_service(base_dir, host)
    if 'nodered' in services:
        add_nodered_service(base_dir, host)
    if 'mqtt' in services:
        add_mqtt_service(base_dir, host, increment)
    print(answers)


871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
def load_main_entires(base_dir):
    """Loads entries for main menu depending on available files

    :base_dir: directory of configuration files
    :returns: entries of main menu
    """
    custom_path = base_dir + '/' + CUSTOM_DIR

    entries = []
    if not os.path.exists(custom_path):
        entries.append('Create initial structure')
    else:
        entries.append('Execute command')

    entries.append('Exit')
886

887
    return entries
888
889


890
def generate_checkbox_choices(list, checked=False):
dobli's avatar
dobli committed
891
892
    """Generates checkbox entries for lists of strings

893
894
    :list: pyhton list that shall be converted
    :checked: if true, selections will be checked by default
dobli's avatar
dobli committed
895
896
    :returns: A list of dicts with name keys
    """
897
    return [{'name': m, 'checked': checked} for m in list]
dobli's avatar
dobli committed
898
899
# }}}

900

901
# ******************************
902
# Script main ( entry) {{{
903
# ******************************
904
905
906
if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(
907
        prog='building_manager',
908
909
        description='Generate and manage multi'
        'building configurations of openHAB with docker swarm')
910
911
912
913
    parser.add_argument(
        '--base_dir',
        '-d',
        help='Directory to creat config folders in, default is current dir')
914
915
    subparsers = parser.add_subparsers()

916
917
918
919
920
921
    # Interactive mode
    parser_interactive = subparsers.add_parser(
        'interactive',
        help='Starts the interactive mode of the building manager')
    parser_interactive.set_defaults(func=interactive_command)

922
923
924
    # Restore command
    parser_restore = subparsers.add_parser('restore', help='Restore backups')
    parser_restore.add_argument(
925
        'building', help='Name (label) of the building that shall be restored')
926
927
928
929
    parser_restore.add_argument(
        'target', help='Name of the machine to restore to')
    parser_restore.set_defaults(func=restore_command)

930
931
932
933
934
935
936
937
938
    # Assign building command
    parser_assign_building = subparsers.add_parser(
        'assign_building', help='Assign the role of a building to a node')
    parser_assign_building.add_argument(
        'node', help='Name (or ID) of the node that gets the role assigned')
    parser_assign_building.add_argument(
        'building', help='Name of the building that will be assigned')
    parser_assign_building.set_defaults(func=assign_building_command)

939
940
941
942
943
944
945
946
947
948
949
950
951
952
    # Execute command
    parser_exec = subparsers.add_parser(
        'exec', help='Execute commands in a service container')
    parser_exec.add_argument(
        'service', help='Name of the service that will run the command')
    parser_exec.add_argument(
        'command', help='Command to be executed', nargs=argparse.REMAINDER)
    parser_exec.add_argument(
        '--building',
        '-b',
        help='Building name (label) of the service if '
        'service location is ambiguous')
    parser_exec.set_defaults(func=execute_command)

953
954
955
956
957
958
959
960
961
    # Config commands
    parser_config = subparsers.add_parser(
        'config', help='Manage configuration files')
    parser_config_subs = parser_config.add_subparsers()
    # - Config init
    parser_config_init = parser_config_subs.add_parser(
        'init', help='Initialize config file directories')
    parser_config_init.set_defaults(func=init_config_dirs_command)

962
    # Parse arguments into args dict
963
    args = parser.parse_args()
964
965
966
967
968
969

    # when no subcommand is defined show interactive menu
    try:
        args.func(args)
    except AttributeError:
        interactive_command(args)
dobli's avatar
dobli committed
970
971
972
973
# }}}

# --- vim settings ---
# vim:foldmethod=marker:foldlevel=0