From 02708db0054bcabf4db151d4b364d70ecc44ef18 Mon Sep 17 00:00:00 2001 From: Harry Huang Date: Tue, 25 Jul 2017 17:01:04 +0800 Subject: Adapt network.yml to OpenStack Ansible JIRA:COMPASS-557 1. modify sys_intf_mappings and ip_settings in network.yml to synchronize with OSA network. networks compass build will be mgmt, external, tenant, storage. 2. modify compass_conf to support new netwok.yml 3. fix some hard coding when transfer ip parameters 4. support heterogeneous network Change-Id: Ib4a8af1f96a40e6456242e3dc2b456e0c8c17c2e Signed-off-by: Harry Huang --- deploy/config_parse.py | 100 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 33 deletions(-) (limited to 'deploy/config_parse.py') diff --git a/deploy/config_parse.py b/deploy/config_parse.py index 8a1ac54b..3d8dedc5 100644 --- a/deploy/config_parse.py +++ b/deploy/config_parse.py @@ -8,16 +8,30 @@ ############################################################################## 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', []) @@ -49,9 +63,38 @@ def hostmacs(s, seq, host=None): return host.get('mac', '') -def export_dha_file(s, dha_file, conf_dir, ofile): +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(s) + env.update(dha) if env.get('hosts', []): env.pop('hosts') if 'plugins' in env: @@ -61,23 +104,21 @@ def export_dha_file(s, dha_file, conf_dir, ofile): plugin_list.append(plugin_str) env.update({'plugins': ','.join(plugin_list)}) - env.update({'TYPE': s.get('TYPE', "virtual")}) - env.update({'FLAVOR': s.get('FLAVOR', "cluster")}) - env.update({'HOSTNAMES': hostnames(s, ',')}) - env.update({'HOST_ROLES': hostroles(s, ';')}) + 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(s, ',') + value = hostmacs(dha, ',') if len(value) > 0: env.update({'HOST_MACS': value}) - os.system("echo \#config file deployment parameter > %s" % ofile) - for k, v in env.items(): - os.system("echo 'export %s=${%s:-%s}' >> %s" % (k, k, v, ofile)) + export_env_dict(env, ofile) -def export_reset_file(s, tmpl_dir, output_dir, output_file): - tmpl_file_name = s.get('POWER_TOOL', '') +def export_reset_file(dha, tmpl_dir, output_dir, ofile): + tmpl_file_name = dha.get('POWER_TOOL', '') if not tmpl_file_name: return @@ -87,42 +128,35 @@ def export_reset_file(s, tmpl_dir, output_dir, output_file): 'power', tmpl_file_name + '.tmpl'), - searchList=s) + 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()) - os.system( - "echo 'export POWER_MANAGE=%s' >> %s" % - (reset_file_name, output_file)) + 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, conf_dir, tmpl_dir, output_dir, output_file = sys.argv + _, 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) - data = init(dha_file) - - export_dha_file( - data, - dha_file, - conf_dir, - os.path.join( - output_dir, - output_file)) - export_reset_file( - data, - tmpl_dir, - output_dir, - os.path.join( - output_dir, - output_file)) + 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) -- cgit 1.2.3-korg