summaryrefslogtreecommitdiff
path: root/parse-deployment.py
blob: 036efa689608c71a49d79fca88415a01d215fbed (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
#!/usr/bin/env python3
# Copyright (c) 2019 PHYTEC Messtechnik GmbH
# SPDX-License-Identifier: Apache-2.0

import argparse
import json
import os
import sys

def main(args):
    with open(args.env, 'r') as envFile:
        envContent = envFile.read()
        env = json.loads(envContent)

        with open(args.template, 'r') as templateFile:
            templateContent = templateFile.read()
            for envKey in env:
                templateContent = templateContent.replace(envKey, env[envKey])

            for module in os.listdir('modules'):
                if not os.path.isfile('modules/' + module + '/create-options.json'):
                    continue

                with open('modules/' + module + '/create-options.json', 'r') \
                        as createOptionsFile:
                    createOptions = createOptionsFile.read().replace('"', '\\"')
                    templateContent = templateContent.replace(
                            '${MODULE_' + module.upper() + '_CREATE_OPTIONS}',
                            createOptions.replace(' ', '').replace('\n', ''))

        with open(args.out, 'w') as output:
            output.write(templateContent)
    return 0

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Parse a deployment template')
    parser.add_argument('-e', '--env', type=str, action='store',
            default='env.json',
            help='specify a custom file containing the variables')
    parser.add_argument('-o', '--out', type=str, action='store',
            default='deployment.json',
            help='the output deployment file')
    parser.add_argument('template', type=str, action='store',
            help='the input deployment template file')
    args = parser.parse_args()
    sys.exit(main(args))