summaryrefslogtreecommitdiffstats
path: root/deploy/get_conf.py
blob: d2b505c8586bb8f32c6626002a0279030bff11ad (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
#!/usr/bin/python
##############################################################################
# Copyright (c) 2016 ZTE Coreporation and others.
# hu.zhijiang@zte.com.cn
# lu.yao135@zte.com.cn
# 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 yaml


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


def networkdecorator(func):
    def wrapter(s, seq):
        network_list = s.get('networks', [])
        result = {}
        for network in network_list:
            s = func(s, seq, network)
            if not s:
                continue
            result.update(s)
        if len(result) == 0:
            return ""
        else:
            return result
    return wrapter


def hostdecorator(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.update(s)
        if len(result) == 0:
            return ""
        else:
            return result
    return wrapter


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 result
    return wrapter


@networkdecorator
def network(s, seq, network=None):
    net_plane = network.get('name', '')
    network.pop('name')
    map = {}
    map[net_plane] = network
    return map


@hostdecorator
def interface(s, seq, host=None):
    hostname = host.get('name', '')
    interface = host.get('interface', '')
    map = {}
    map[hostname] = interface
    return map


@hostdecorator
def role(s, seq, host=None):
    hostname = host.get('name', '')
    role = host.get('roles', '')
    map = {}
    map[hostname] = role
    return map


@decorator
def host(s, seq, host=None):
    hostip = host.get('ip', [])
    passwd = host.get('password', [])
    map = {}
    map = {'ip': hostip, 'passwd': passwd}
    return map


def network_config_parse(s, dha_file):
    network_map = network(s, ',')
    vip = s.get('internal_vip')
    return network_map, vip


def dha_config_parse(s, dha_file):
    host_interface_map = interface(s, ',')
    host_role_map = role(s, ',')
    host_ip_passwd_map = host(s, ',')
    return host_interface_map, host_role_map, host_ip_passwd_map


def config(dha_file, network_file):
    data = init(dha_file)
    host_interface_map, host_role_map, host_ip_passwd_map = \
        dha_config_parse(data, dha_file)
    data = init(network_file)
    network_map, vip = network_config_parse(data, network_file)
    return host_interface_map, host_role_map, \
        host_ip_passwd_map, network_map, vip