From 6c077560e1cd2293fb9c6fac9065f11e555b1bbb Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Thu, 16 Mar 2017 15:16:12 +0800 Subject: bugfix hardcoded network configuration Change-Id: I1fb6036a2805ccb9bdbe23622514ccd9d997c1a5 Signed-off-by: SerenaFeng --- deploy/config/network.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ deploy/post.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ deploy/post/execute.py | 44 ++++++++++++++++++++++++++++---------------- 3 files changed, 122 insertions(+), 16 deletions(-) create mode 100755 deploy/post.sh (limited to 'deploy') diff --git a/deploy/config/network.py b/deploy/config/network.py index ed14f95b..a386b5b5 100644 --- a/deploy/config/network.py +++ b/deploy/config/network.py @@ -4,9 +4,24 @@ from deploy.common import query class NetworkConfig(object): + type2name = { + 'EXTERNAL': 'ext', + 'MANAGEMENT': 'man', + 'STORAGE': 'stor', + 'PUBLICAPI': 'pub', + 'TENANT': 'tenant', + } + def __init__(self, network_file): + self._parsers = { + 'network-config-metadata': self._parse_metadata, + 'networks': self._parse_networks, + 'interfaces': self._parse_interfaces + } + self._file = network_file self._get_config() + self._parse() def _get_config(self): self.config = yaml.safe_load(file(self._file)) @@ -15,6 +30,37 @@ class NetworkConfig(object): return query.find(lambda item: item['name'] == name, self.config['networks']) + def _parse(self): + for conf_k, conf_v in self.config.iteritems(): + self._parsers.get(conf_k, + lambda x: setattr(self, conf_k, conf_v))(conf_v) + + def _parse_metadata(self, metadatas): + for meta_k, meta_v in metadatas.iteritems(): + setattr(self, meta_k, meta_v) + + def _parse_networks(self, networks): + for network in networks: + name = network['name'] + self._setattr(name, '', network) + for network_k, network_v in network.iteritems(): + self._setattr(name, network_k, network_v) + + def _parse_interfaces(self, interfaces): + for interface in interfaces: + self._setattr(interface['name'], + 'iterface', + interface['interface']) + + def _setattr(self, network_type, field, value): + prefix = self.type2name[network_type] + name = '{}_{}'.format(prefix, field) if field else prefix + setattr(self, name, value) + @property def external_network(self): return self._get_network('EXTERNAL') + + @property + def external_name(self): + return self.external_network['network_name'] diff --git a/deploy/post.sh b/deploy/post.sh new file mode 100755 index 00000000..df3c280c --- /dev/null +++ b/deploy/post.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +SCRIPT_PATH=$(readlink -f $(dirname $0)) + +export PYTHONPATH=$SCRIPT_PATH/.. + +usage () +{ +cat << EOF +xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +`basename $0`: post process after OpenStack is deployed + +usage: `basename $0` -n network_config_file + +OPTIONS: + -n network configuration path, necessary + -h Print this message and exit + +Description: + post process after OpenStack is deployed + +Examples: +sudo `basename $0` -n /home/daisy/labs/zte/virtual1/daisy/config/network.yml +xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +EOF +} + +NETWORK='' + +while getopts "n:h" OPTION +do + case $OPTION in + n) + NETWORK=${OPTARG} + ;; + h) + usage + exit 0 + ;; + *) + echo "${OPTION} is not a valid argument" + usage + exit 0 + ;; + esac +done + +python $PYTHONPATH/deploy/post/execute.py -nw $NETWORK diff --git a/deploy/post/execute.py b/deploy/post/execute.py index b9665e10..159f3b67 100644 --- a/deploy/post/execute.py +++ b/deploy/post/execute.py @@ -9,15 +9,17 @@ import os import glance +import argparse + import neutron import nova +from deploy.config.network import NetworkConfig -def _config_admin_external_network(): - name = 'admin_external' +def _config_external_network(ext_name): body = { 'network': { - 'name': name, + 'name': ext_name, 'admin_state_up': True, 'shared': False, 'provider:network_type': 'flat', @@ -26,28 +28,34 @@ def _config_admin_external_network(): } } - return name, body + return body -def _config_admin_external_subnet(nid): +def _config_external_subnet(ext_id, network_conf): return { 'subnets': [ { - 'name': 'admin_external_subnet', - 'cidr': '172.10.101.0/24', + 'name': '{}_subnet'.format(network_conf.ext_network_name), + 'cidr': network_conf.ext_cidr, 'ip_version': 4, - 'network_id': nid, - 'gateway_ip': '172.10.101.1', - 'allocation_pools': [{ - 'start': '172.10.101.2', - 'end': '172.10.101.12' - }], + 'network_id': ext_id, + 'gateway_ip': network_conf.ext_gateway, + 'allocation_pools': network_conf.ext_ip_ranges, 'enable_dhcp': False } ] } +def _create_external_network(network_file): + network_conf = NetworkConfig(network_file=network_file) + ext_name = network_conf.ext_network_name + neutronclient = neutron.Neutron() + ext_id = neutronclient.create_network(ext_name, + _config_external_network(ext_name)) + neutronclient.create_subnet(_config_external_subnet(ext_id, network_conf)) + + def _create_flavor_m1_micro(): name = 'm1.micro' novaclient = nova.Nova() @@ -96,9 +104,13 @@ def _create_image_TestVM(): def main(): - neutronclient = neutron.Neutron() - nid = neutronclient.create_network(*(_config_admin_external_network())) - neutronclient.create_subnet(_config_admin_external_subnet(nid)) + parser = argparse.ArgumentParser() + parser.add_argument('-nw', '--network-file', + type=str, + required=True, + help='network configuration file') + args = parser.parse_args() + _create_external_network(args.network_file) _create_flavor_m1_micro() _create_image_TestVM() -- cgit 1.2.3-korg