From 0d643bc668a33a45a8df63a50a71683bf232cf34 Mon Sep 17 00:00:00 2001 From: Harry Huang Date: Mon, 28 Nov 2016 21:13:05 +0800 Subject: Add ip and mac valid check functionality Change-Id: I5b560b2ed75036e72e51dfe1ee79c4ff6123e24e Signed-off-by: Harry Huang --- deploy/deploy_parameter.sh | 8 +++ deploy/launch.sh | 1 + util/check_valid.py | 155 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 util/check_valid.py diff --git a/deploy/deploy_parameter.sh b/deploy/deploy_parameter.sh index d8696ce8..9c0d5dc0 100755 --- a/deploy/deploy_parameter.sh +++ b/deploy/deploy_parameter.sh @@ -111,3 +111,11 @@ 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 +} diff --git a/deploy/launch.sh b/deploy/launch.sh index 488e0fd7..489c06d8 100755 --- a/deploy/launch.sh +++ b/deploy/launch.sh @@ -19,6 +19,7 @@ prepare_python_env source ${COMPASS_DIR}/util/log.sh source ${COMPASS_DIR}/deploy/deploy_parameter.sh 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 index 00000000..87cd57ed --- /dev/null +++ b/util/check_valid.py @@ -0,0 +1,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]))?$", + 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])) + 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) -- cgit 1.2.3-korg