Use PDF(POD descriptor file) and correct the mapping of nodes and roles 57/38657/2
authorAlex Yang <yangyang1@zte.com.cn>
Thu, 3 Aug 2017 03:48:13 +0000 (11:48 +0800)
committerAlex Yang <yangyang1@zte.com.cn>
Thu, 3 Aug 2017 11:28:52 +0000 (19:28 +0800)
JIRA: DAISY-42
JIRA: DAISY-56

In bare metal deployment, we can use PDF to get MAC addresses of nodes
https://gerrit.opnfv.org/gerrit/#/c/38387/.
Then we can use the MACs to help to distinguish the discovered nodes
and assign roles to them, like virtual deployment in the link
https://gerrit.opnfv.org/gerrit/#/c/38381/.

Change-Id: Ib0f1a60b8935f528a828f716ccc916b767cfa6f9
Signed-off-by: Alex Yang <yangyang1@zte.com.cn>
ci/deploy/deploy.sh
deploy/deploy.py
requirements.txt
test-requirements.txt

index bfe0f6f..a4f1e7a 100755 (executable)
@@ -172,6 +172,40 @@ done
 BMDEPLOY_DAISY_SERVER_NET=$WORKSPACE/templates/physical_environment/networks/daisy.xml
 BMDEPLOY_DAISY_SERVER_VM=$WORKSPACE/templates/physical_environment/vms/daisy.xml
 
+function update_dha_by_pdf()
+{
+    local pdf_yaml=securedlab/labs/$LAB_NAME/${POD_NAME}.yaml
+    local jinja2_template=securedlab/installers/daisy/pod_config.yaml.j2
+    local generate_config=securedlab/utils/generate_config.py
+    if [ ! -f ${generate_config} ] || [ ! -f ${pdf_yaml} ] || [ ! -f ${jinja2_template} ]; then
+        return
+    fi
+
+    local tmpfile=$(mktemp XXXXXXXX.yml)
+    python ${generate_config} -j ${jinja2_template} -y ${pdf_yaml} > ${tmpfile}
+    if [ $? -ne 0 ]; then
+        echo "Cannot generate config from POD Descriptor File, use original deploy.yml !"
+        return
+    fi
+    if [ -z $(awk "BEGIN{}(/daisy_ip/){print NR}" $tmpfile) ]; then
+        line=$(awk "BEGIN{}(/daisy_gateway/){print NR}" $tmpfile)
+        sed -i "${line}b\daisy_ip: $INSTALLER_IP" $tmpfile
+    fi
+    if [ $? -ne 0 ]; then
+        echo "Cannot write INSTALLER_IP to config file, use original deploy.yml !"
+        return
+    fi
+    cp ${tmpfile} ${DHA_CONF}
+    echo "====== Update deploy.yml from POD Descriptor File ======"
+    rm -f $tmpfile
+}
+
+if [[ ! -z $INSTALLER_IP ]]; then
+    pushd ${WORKSPACE}
+    update_dha_by_pdf
+    popd
+fi
+
 PARAS_FROM_DEPLOY=`python $WORKSPACE/deploy/get_conf.py --dha $DHA_CONF`
 TARGET_HOSTS_NUM=`echo $PARAS_FROM_DEPLOY | cut -d " " -f 1`
 DAISY_IP=`echo $PARAS_FROM_DEPLOY | cut -d " " -f 2`
index 47549b4..e8c9434 100644 (file)
 ##############################################################################
 
 import argparse
-import yaml
-import time
+from jinja2 import Template
 import os
+import tempfile
+import time
+import yaml
 
 
 from config.schemas import (
@@ -32,6 +34,7 @@ from utils import (
     WORKSPACE,
     save_log_to_file,
     LI,
+    LW,
     LE,
     err_exit,
     log_bar,
@@ -57,7 +60,7 @@ class DaisyDeployment(object):
 
         self.src_deploy_file = deploy_file
         self.scenario = scenario
-        self.deploy_struct = self._construct_final_deploy_conf(deploy_file, scenario)
+        self.deploy_struct = self._construct_final_deploy_conf(scenario)
         self.deploy_file, self.deploy_file_name = self._construct_final_deploy_file(self.deploy_struct, work_dir)
 
         if not cleanup_only:
@@ -123,7 +126,43 @@ class DaisyDeployment(object):
                 'password': password,
                 'disk_size': disk_size}
 
-    def _construct_final_deploy_conf(self, deploy_file, scenario):
+    def _use_pod_descriptor_file(self):
+        # INSTALLER_IP is provided by Jenkins on an OPNFV CI POD (bare metal)
+        installer_ip = os.environ.get('INSTALLER_IP', '')
+        if not installer_ip:
+            LW('INSTALLER_IP is not provided. Use deploy.yml in POD configuration directory !')
+            return None
+
+        pdf_yaml = path_join(WORKSPACE, 'labs', self.lab_name, self.pod_name + '.yaml')
+        template_file = path_join(WORKSPACE, 'securedlab/installers/daisy/pod_config.yaml.j2')
+        if not os.access(pdf_yaml, os.R_OK) or not os.access(template_file, os.R_OK):
+            LI('There is not a POD Descriptor File or an installer template file for this deployment.')
+            LI('Use deploy.yml in POD configuration directory !')
+            return None
+
+        try:
+            template = Template(open(template_file).read())
+            output = template.render(conf=yaml.safe_load(open(pdf_yaml)))
+            deploy_struct = yaml.safe_load(output)
+        except Exception as e:
+            LE('Fail to use POD Descriptor File: %s' % e)
+            return None
+
+        if not deploy_struct.get('daisy_ip', ''):
+            deploy_struct['daisy_ip'] = installer_ip
+
+        dummy, deploy_file = tempfile.mkstemp(prefix='daisy_', suffix='.yml')
+        fh = open(deploy_file, 'w')
+        fh.write(yaml.safe_dump(deploy_struct))
+        fh.close()
+        LI('Use %s generated by PDO Descriptor File as deployment configuration.' % deploy_file)
+        return deploy_file
+
+    def _construct_final_deploy_conf(self, scenario):
+        deploy_file = self._use_pod_descriptor_file()
+        if not deploy_file:
+            deploy_file = self.src_deploy_file
+        check_file_exists(deploy_file)
         with open(deploy_file) as yaml_file:
             deploy_struct = yaml.safe_load(yaml_file)
         scenario_file = path_join(WORKSPACE, 'deploy/scenario/scenario.yaml')
@@ -229,7 +268,6 @@ def parse_arguments():
     deploy_file = path_join(conf_base_dir, 'daisy/config/deploy.yml')
     net_file = path_join(conf_base_dir, 'daisy/config/network.yml')
 
-    check_file_exists(deploy_file)
     if not args.cleanup_only:
         check_file_exists(net_file)
     make_file_executable(args.bin_file)
index 7a329b1..2bdce8b 100644 (file)
@@ -1,4 +1,5 @@
 configobj
+jinja2
 jsonschema
 paramiko
 pyyaml
index 1def47c..7179616 100644 (file)
@@ -1,4 +1,5 @@
 configobj
+jinja2
 jsonschema
 paramiko
 pytest