Add ip and mac valid check functionality 53/20853/8
authorHerry Huang <huangxiangyu5@huawei.com>
Mon, 12 Sep 2016 11:03:40 +0000 (19:03 +0800)
committerHerry Huang <huangxiangyu5@huawei.com>
Fri, 23 Sep 2016 11:45:46 +0000 (19:45 +0800)
Add function check_input_para in /deploy/launch.sh to check
whether there is invalid address in DHA and NETWORK file after
function process_input_para is called.

Check_input_para is defined in /deploy/deploy_parameter.sh and
calls check_valid.py in /util. Exit installation when invalid
address or format error is found.

JIRA:COMPASS-480

Change-Id: Ib01c368db6b48cbded0045a2819d895c980c2b1b
Signed-off-by: Herry Huang <huangxiangyu5@huawei.com>
deploy/deploy_parameter.sh
deploy/launch.sh
util/check_valid.py [new file with mode: 0644]

index d8696ce..92e3467 100755 (executable)
@@ -111,3 +111,12 @@ function process_input_para()
 
     echo $input_file
 }
+
+function check_input_para()
+{
+    python ${COMPASS_DIR}/util/check_valid.py "$DHA" "$NETWORK"
+    if [ $? -ne 0 ];then
+        exit 1
+    fi
+}
+
index 488e0fd..f73b400 100755 (executable)
@@ -18,7 +18,8 @@ source ${COMPASS_DIR}/deploy/prepare.sh
 prepare_python_env
 source ${COMPASS_DIR}/util/log.sh
 source ${COMPASS_DIR}/deploy/deploy_parameter.sh
-source $(process_input_para $*) || exit 1
+source $(process_input_para $* ) || exit 1
+check_input_para
 source $(process_default_para $*) || exit 1
 source ${COMPASS_DIR}/deploy/conf/${FLAVOR}.conf
 source ${COMPASS_DIR}/deploy/conf/${TYPE}.conf
diff --git a/util/check_valid.py b/util/check_valid.py
new file mode 100644 (file)
index 0000000..13b5602
--- /dev/null
@@ -0,0 +1,96 @@
+import re
+import os
+import yaml
+import sys
+import traceback
+
+def init(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 check_ip(ip):
+    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]))?$",ip)!=None
+    return res
+
+def check_mac(mac):
+    if not mac:
+        return False
+    res=re.search("^([a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}$",mac)!=None
+    return res
+
+def check_network(network):
+    for i in network.get('ip_settings'):
+        if not (check_ip(i['cidr']) and check_ip(i['ip_ranges'][0][0]) and check_ip(i['ip_ranges'][0][1])):
+            return False
+        if i['name'] == 'external' and not check_ip(i['gw']):
+            return False
+
+    if not check_ip(network['internal_vip']['ip']):
+        return False
+
+    if not check_ip(network['public_vip']['ip']):
+        return False
+
+    if not check_ip(network['public_net_info']['external_gw']):
+        return False
+
+    if not check_ip(network['public_net_info']['floating_ip_cidr']):
+        return False
+
+    if not check_ip(network['public_net_info']['floating_ip_start']):
+        return False
+
+    if not check_ip(network['public_net_info']['floating_ip_end']):
+        return False
+
+    return True
+
+def check_dha(dha):
+    if dha['TYPE'] == 'baremetal':
+        for i in dha['hosts']:
+            if not (check_mac(i['mac']) and check_mac(i['interface'][0]['eth1']) and check_ip(i['impiIp'])):
+                return False
+    return True
+
+if __name__ == "__main__":
+    flag = 0
+
+    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):
+        sys.exit(1)
+    else:
+        dha = init(dha_file)
+        if not dha:
+            err_print('format error in DHA')
+        else:
+            if not check_dha(dha):
+                err_print('invalid address in DHA')
+                flag = 1
+
+    if not os.path.exists(network_file):
+        sys.exit(1)
+    else:
+        network = init(network_file)
+        if not network:
+            err_print('format error in NETWORK')
+        else:
+            if not check_network(network):
+                err_print('invalid address in NETWORK')
+                flag = 1
+
+    if flag == 1:
+        sys.exit(1)