aboutsummaryrefslogtreecommitdiffstats
path: root/util/check_valid.py
blob: 39109b371de9f73e9d618a3347da4aa088eace08 (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
import re
import os
import yaml
import sys
import traceback


def load_file(file):
    with open(file) as fd:
        try:
            return yaml.load(fd)
        except:
            traceback.print_exc()
            return None


def err_print(info):
    print '\033[0;31m%s\033[0m' % info


def is_valid_ip(ip):
    """return True if the given string is a well-formed IP address
       currently only support IPv4
    """
    if not ip:
        return False
    res = re.search(
        "^(0?\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}(\/(\d|[1-2]\d|3[0-2]))?$",  # noqa: E501
        ip) is not None
    return res


def is_valid_mac(mac):
    """return True if the given string is a well-formed MAC address
    """
    if not mac:
        return False
    res = re.search("^([a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}$", mac) is not None
    return res


def check_network_file(network):
    invalid = False
    for i in network['ip_settings']:
        if not is_valid_ip(i['cidr']):
            err_print('''invalid address:
                ip_settings:
                  - name: %s
                    cidr: %s''' % (i['name'], i['cidr']))
            invalid = True
        if not is_valid_ip(i['ip_ranges'][0][0]):
            err_print('''invalid address:
                ip_settings:
                  - name: %s
                    ip_ranges:
                    - - %s''' % (i['name'], i['ip_ranges'][0][0]))
            invalid = True
        if not is_valid_ip(i['ip_ranges'][0][1]):
            err_print('''invalid address:
                ip_settings:
                  - name: %s
                    ip_ranges:
                    - %s''' % (i['name'], i['ip_ranges'][0][1]))
            invalid = True
        if i['name'] == 'external' and not is_valid_ip(i['gw']):
            err_print(i['gw'])
            err_print('''invalid address:
                ip_settings:
                  - name: %s
                    gw: %s''' % (i['name'], i['gw']))
            invalid = True

    for i in network['public_net_info'].keys():
        if i in ('external_gw', 'floating_ip_cidr',
                 'floating_ip_start', 'floating_ip_end'):
            if not is_valid_ip(network['public_net_info'][i]):
                err_print('''invalid address:
                public_net_info:
                  %s: %s''' % (i, network['public_net_info'][i]))
                invalid = True

    if not invalid:
        return True
    else:
        return False


def check_dha_file(dha):
    invalid = False
    if dha['TYPE'] == 'baremetal':
        for i in dha['hosts']:
            if not is_valid_mac(i['mac']):
                err_print('''invalid address:
                hosts:
                 - name: %s
                   mac: %s''' % (i['name'], i['mac']))
                invalid = True
            for j in i['interfaces']:
                if not is_valid_mac(j.values()[0]):
                    err_print('''invalid address:
                    hosts:
                        - name: %s
                          interfaces:
                            - %s: %s''' % (i['name'], j.keys()[0], j.values()[0]))  # noqa: E501
                    invalid = True
            if not is_valid_ip(i['ipmiIp']):
                err_print('''invalid address:
                hosts:
                 - name: %s
                   ipmiIp: %s''' % (i['name'], i['ipmiIp']))
                invalid = True

    if not invalid:
        return True
    else:
        return False

if __name__ == "__main__":

    has_invalid = False

    if len(sys.argv) != 3:
        err_print('input file error')
        sys.exit(1)

    _, dha_file, network_file = sys.argv

    if not os.path.exists(dha_file):
        err_print("DHA file doesn't exit")
        sys.exit(1)
    else:
        dha = load_file(dha_file)
        if not dha:
            err_print('format error in DHA: %s' % dha_file)
            has_invalid = True
        else:
            if not check_dha_file(dha):
                err_print('in DHA: %s' % dha_file)
                has_invalid = True

    if not os.path.exists(network_file):
        err_print("NETWORK file doesn't exit")
        sys.exit(1)
    else:
        network = load_file(network_file)
        if not network:
            err_print('format error in NETWORK: %s' % network_file)
            has_invalid = True
        else:
            if not check_network_file(network):
                err_print('in NETWORK: %s' % network_file)
                has_invalid = True

    if has_invalid:
        sys.exit(1)