aboutsummaryrefslogtreecommitdiff
path: root/scripts/resolve-profile.py.in
blob: 67f9d244faaf5715d0fcbdda474a3bc0f83eba41 (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
@PYTHON@

import json, os, shutil, sys, time
from pyhocon import ConfigFactory

script_dir = os.path.dirname(os.path.realpath(__file__))

profile = sys.argv[1]

# path to the profile config file
profile_conf = os.path.join(script_dir, '..', 'profiles', profile + '.conf')

# where we store profile's builds
profile_dir = os.path.join(script_dir, 'profile')

builds = ConfigFactory.parse_file(profile_conf)['builds']

# clean out any old builds
if os.path.exists(profile_dir):
    shutil.rmtree(profile_dir)
os.makedirs(profile_dir)

# func to fold dict down to scalar
def fold(dict, fmt):
    a = []
    for k, v in dict.items():
        if v == True:
            a.append(k)
        elif not (v == None or v == False):
            a.append(fmt.format(k, v))
    return ','.join(str(x) for x in a)

# fold these dicts vars down to scalar, based on item values
fold_dicts = {
    'ami_access': '{0}',
    'ami_regions': '{0}',
    'repos': '@{1} {0}',
    'pkgs': '{0}@{1}'
}

# parse/resolve HOCON profile config, we need just the 'builds' portion
# for each build
for bk, b in builds.items():

    # make profile build directory
    build_dir = os.path.join(profile_dir, bk)
    os.makedirs(build_dir)
    vars_file = os.path.join(build_dir, 'vars.json')

    # populate profile build vars
    b['profile'] = profile
    b['profile_build'] = bk

    # edge-related temporal substitutions
    if b['end_of_life'] == '@TODAY@':
        b['end_of_life'] = time.strftime('%Y-%m-%d', time.gmtime())
    if b['revision'] == '@NOW@':
        b['revision'] = time.strftime('%Y%m%d%H%M%S', time.gmtime())

    # fold dict vars to scalars
    for dk, fmt in fold_dicts.items():
        b[dk] = fold(b[dk], fmt)

    # fold 'svcs' dict to scalar
    svcs = {}
    for svc, v in b['svcs'].items():
        if v == True:
            # service in default runlevel
            svcs['default'].append(svc)
        elif not (v == None or v == False):
            # service in specified runlevel (skip svc when false/null)
            if v not in svcs.keys():
                svcs[v] = []
            svcs[v].append(svc)
    b['svcs'] = ':'.join(str(l) + '=' + ','.join(str(s) for s in ss) for l, ss in svcs.items())

    # write build def json file
    with open(vars_file, 'w') as out:
        json.dump(b, out, indent=4, separators=(',', ': '))