aboutsummaryrefslogtreecommitdiffstats
path: root/deploy/config_parse.py
blob: 1575ca37b86fccf1de6d8faddc3247306b4e1cfa (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
##############################################################################
# Copyright (c) 2016 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import os
import netaddr
import yaml
import sys
from Cheetah.Template import Template

PXE_INTF = "eth0"


def init(file):
    with open(file) as fd:
        return yaml.safe_load(fd)


def export_env_dict(env_dict, ofile, direct=False):
    if not os.path.exists(ofile):
        raise IOError("output file: %s not exist" % ofile)
    if direct:
        for k, v in env_dict.items():
            os.system("echo 'export %s=\"%s\"' >> %s" % (k, v, ofile))
    else:
        for k, v in env_dict.items():
            os.system("echo 'export %s=${%s:-%s}' >> %s" % (k, k, v, ofile))


def decorator(func):
    def wrapter(s, seq):
        host_list = s.get('hosts', [])
        result = []
        for host in host_list:
            s = func(s, seq, host)
            if not s:
                continue
            result.append(s)
        if len(result) == 0:
            return ""
        else:
            return "\"" + seq.join(result) + "\""
    return wrapter


@decorator
def hostnames(s, seq, host=None):
    return host.get('name', '')


@decorator
def hostroles(s, seq, host=None):
    return "%s=%s" % (host.get('name', ''), ','.join(host.get('roles', [])))


@decorator
def hostmacs(s, seq, host=None):
    return host.get('mac', '')


def export_network_file(dha, network, ofile):
    env = {}

    mgmt_net = [item for item in network['ip_settings']
                if item['name'] == 'mgmt'][0]
    mgmt_gw = mgmt_net['gw']
    mgmt_cidr = mgmt_net['cidr']
    prefix = int(mgmt_cidr.split('/')[1])
    mgmt_netmask = '.'.join([str((0xffffffff << (32 - prefix) >> i) & 0xff)
                             for i in [24, 16, 8, 0]])
    dhcp_ip_range = ' '.join(mgmt_net['dhcp_ranges'][0])
    env.update({'INSTALL_GW': mgmt_gw})
    env.update({'INSTALL_CIDR': mgmt_cidr})
    env.update({'INSTALL_NETMASK': mgmt_netmask})
    env.update({'INSTALL_IP_RANGE': dhcp_ip_range})
    export_env_dict(env, ofile)

    host_ip_range = mgmt_net['ip_ranges'][0]
    host_ips = netaddr.iter_iprange(host_ip_range[0], host_ip_range[1])
    host_networks = []
    for host in dha['hosts']:
        host_name = host['name']
        host_ip = str(host_ips.next())
        host_networks.append(
            "{0}:{1}={2}|is_mgmt".format(host_name, PXE_INTF, host_ip))
    host_network_env = {"HOST_NETWORKS": ';'.join(host_networks)}
    export_env_dict(host_network_env, ofile, True)


def export_dha_file(dha, dha_file, ofile):
    env = {}
    env.update(dha)
    if env.get('hosts', []):
        env.pop('hosts')
    if 'plugins' in env:
        plugin_list = []
        for item in env.get('plugins'):
            plugin_str = ':'.join([item.keys()[0], item.values()[0]])
            plugin_list.append(plugin_str)
        env.update({'plugins': ','.join(plugin_list)})

    env.update({'CLUSTER_NAME': dha.get('NAME', "opnfv")})
    env.update({'TYPE': dha.get('TYPE', "virtual")})
    env.update({'FLAVOR': dha.get('FLAVOR', "cluster")})
    env.update({'HOSTNAMES': hostnames(dha, ',')})
    env.update({'HOST_ROLES': hostroles(dha, ';')})
    env.update({'DHA': dha_file})

    value = hostmacs(dha, ',')
    if len(value) > 0:
        env.update({'HOST_MACS': value})

    export_env_dict(env, ofile)


def export_reset_file(dha, tmpl_dir, output_dir, ofile):
    tmpl_file_name = dha.get('POWER_TOOL', '')
    if not tmpl_file_name:
        return

    tmpl = Template(
        file=os.path.join(
            tmpl_dir,
            'power',
            tmpl_file_name +
            '.tmpl'),
        searchList=dha)

    reset_file_name = os.path.join(output_dir, tmpl_file_name + '.sh')
    with open(reset_file_name, 'w') as f:
        f.write(tmpl.respond())

    power_manage_env = {'POWER_MANAGE': reset_file_name}
    export_env_dict(power_manage_env, ofile, True)

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("parameter wrong%d %s" % (len(sys.argv), sys.argv))
        sys.exit(1)

    _, dha_file, network_file, tmpl_dir, output_dir, output_file = sys.argv

    if not os.path.exists(dha_file):
        print("%s is not exist" % dha_file)
        sys.exit(1)

    ofile = os.path.join(output_dir, output_file)
    os.system("touch %s" % ofile)
    os.system("echo \#config file deployment parameter > %s" % ofile)

    dha_data = init(dha_file)
    network_data = init(network_file)

    export_dha_file(dha_data, dha_file, ofile)
    export_network_file(dha_data, network_data, ofile)
    export_reset_file(dha_data, tmpl_dir, output_dir, ofile)

    sys.exit(0)