bugfix hardcoded network configuration 53/30653/4
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Thu, 16 Mar 2017 07:16:12 +0000 (15:16 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Tue, 21 Mar 2017 10:25:21 +0000 (18:25 +0800)
Change-Id: I1fb6036a2805ccb9bdbe23622514ccd9d997c1a5
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
ci/deploy/deploy.sh
deploy/config/network.py
deploy/post.sh [new file with mode: 0755]
deploy/post/execute.py

index 0317d0b..e0ee6a4 100755 (executable)
@@ -347,9 +347,11 @@ fi
 
 if [ $IS_BARE == 0 ];then
     echo "============post deploy====================="
-    ssh $SSH_PARAS $DAISY_IP "python ${REMOTE_SPACE}/deploy/post/execute.py"
+    ssh $SSH_PARAS $DAISY_IP "bash $REMOTE_SPACE/deploy/post.sh -n $NETWORK"
 fi
 
+echo "============deploy success==================="
+
 exit 0
 
 #
index ed14f95..a386b5b 100644 (file)
@@ -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 (executable)
index 0000000..df3c280
--- /dev/null
@@ -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
index b9665e1..159f3b6 100644 (file)
@@ -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()