aboutsummaryrefslogtreecommitdiffstats
path: root/deploy/compass_conf/templates/ansible_installer/kubernetes/inventories/ansible-kubernetes.tmpl
blob: 4c363a61751c1dbf5a072c9652ff31111b78ba6f (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
#set inventory_json = $getVar('inventory_json', [])
#!/usr/bin/env python

import os
import sys
import copy
import argparse

try:
    import json
except ImportError:
    import simplejson as json

local_inventory='$inventory_json'

def _byteify(data, ignore_dicts = False):
    if isinstance(data, unicode):
        return data.encode('utf-8')
    if isinstance(data, list):
        return [ _byteify(item, ignore_dicts=True) for item in data ]
    if isinstance(data, dict) and not ignore_dicts:
        return {
            _byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
            for key, value in data.iteritems()
        }
    return data

def merge_dict(ldict, rdict, overwrite=True):
    if not (ldict and rdict):
        return

    if not isinstance(ldict, dict):
        raise TypeError('ldict type is %s not dict' % type(ldict))

    if not isinstance(rdict, dict):
        raise TypeError('rdict type is %s not dict' % type(rdict))

    for key, value in rdict.items():
        if isinstance(value, dict) and key in ldict and isinstance(ldict[key],
                                                                 dict):
            merge_dict(ldict[key], value, overwrite)
        else:
            if overwrite or key not in ldict:
                ldict[key] = copy.deepcopy(value)

def load_inventory(inventory):
    if not os.path.exists(inventory):
        raise RuntimeError('file: %s not exist' % inventory)
    with open(inventory, 'r') as fd:
        return json.load(fd, object_hook=_byteify)

def dump_inventory(inventory, data):
    with open(inventory, 'w') as fd:
        json.dump(data, fd, indent=4)

def merge_inventory(linv, rinv):
    ldata = load_inventory(linv)
    rdata = load_inventory(rinv)
    merge_dict(ldata, rdata, overwrite=True)
    dump_inventory(linv, ldata)

def read_cli_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--list', action = 'store_true')
    parser.add_argument('--merge', action = 'store')
    return parser.parse_args()

if __name__ == '__main__':
    get_args = read_cli_args()
    new_inventory = get_args.merge
    if get_args.list:
        print load_inventory(local_inventory)
    elif new_inventory:
        merge_inventory(local_inventory, new_inventory)