Bugfix: Failed to build local DB on Centos 47/46547/4
authorxudan <xudan16@huawei.com>
Thu, 2 Nov 2017 06:17:10 +0000 (02:17 -0400)
committerLeo wang <grakiss.wanglei@huawei.com>
Mon, 6 Nov 2017 01:28:29 +0000 (01:28 +0000)
1. The scripts for building local DB and testapi service just work on Ubuntu.
2. For Centos, they fails to do that.
3. This is mainly because of that Centos can't use the IP of the JumpHost
   to connect the DB and testapi. It needs to use the internal IPs of the
   DB container and the testapi container.
4. Change the IPs in the scripts to be the containers' internal IPs.

JIRA: DOVETAIL-548

Change-Id: I71aed82cb620b58bcd43ace61497db65c5bd7ecc
Signed-off-by: xudan <xudan16@huawei.com>
dovetail/utils/local_db/init_db.py
dovetail/utils/local_db/launch_db.sh

index bea3eac..365927b 100644 (file)
 import requests
 import json
 import sys
+import os
 
 db_host_ip = sys.argv[1]
 testapi_port = sys.argv[2]
 
 target_url = 'http://{}:{}/api/v1'.format(db_host_ip, testapi_port)
 print(target_url)
+dir_path = os.path.dirname(os.path.realpath(__file__))
 
 
 def get(url):
@@ -31,7 +33,7 @@ def post(url, data):
 def pod():
     target = '{}/pods'.format(target_url)
 
-    with open('pods.json', 'r') as f:
+    with open(os.path.join(dir_path, 'pods.json'), 'r') as f:
         pods = json.load(f)
     for p in pods:
         post(target, p)
@@ -39,14 +41,14 @@ def pod():
 
 def project():
     target = '{}/projects'.format(target_url)
-    with open('projects.json', 'r') as f:
+    with open(os.path.join(dir_path, 'projects.json'), 'r') as f:
         projects = json.load(f)
     for p in projects:
         post(target, p)
 
 
 def cases():
-    with open('cases.json', 'r') as f:
+    with open(os.path.join(dir_path, 'cases.json'), 'r') as f:
         for line in f:
             try:
                 cases = json.loads(line)
index dbc0d00..cad0f36 100755 (executable)
@@ -8,20 +8,8 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
-if [ "$#" -ne 1 ]; then
-    echo "Error: missing parameter! try again like this:"
-    echo ""
-    echo "./launch_db.sh 192.168.115.2"
-    echo ""
-    echo "parameters:"
-    echo "  host_ip: your localhost ip address "
-    echo ""
-    exit 1
-fi
-
 export mongodb_port=${mongodb_port:-"27017"}
 export testapi_port=${testapi_port:-"8000"}
-export db_host_ip=${db_host_ip:-"$1"}
 
 set -e
 
@@ -53,6 +41,11 @@ ${cmd}
 
 echo "Successfully create mongo DB."
 
+echo "Step4: get the internal IP of ${container_name} container..."
+get_ip_cmd="ip a | grep eth0 | grep inet | awk '{print \$2}' | cut -d'/' -f 1"
+mongo_ip=$(sudo docker exec ${container_name} /bin/bash -c "${get_ip_cmd}")
+echo "The internal IP of container ${container_name} is ${mongo_ip}"
+
 
 echo "=========================="
 echo "Create the testapi service."
@@ -75,10 +68,15 @@ fi
 
 # run testapi container
 echo "Step3: run ${container_name} container."
-cmd="sudo docker run -itd -p ${testapi_port}:8000 --name ${container_name} -v ${DOVETAIL_HOME}/testapi/logs:/home/testapi/logs -e mongodb_url=mongodb://${db_host_ip}:${mongodb_port}/ ${testapi_img}"
+cmd="sudo docker run -itd -p ${testapi_port}:8000 --name ${container_name} -v ${DOVETAIL_HOME}/testapi/logs:/home/testapi/logs -e mongodb_url=mongodb://${mongo_ip}:27017/ ${testapi_img}"
 echo $cmd
 ${cmd}
 
+echo "Step4: get the internal IP of ${container_name} container..."
+get_ip_cmd="ip a | grep eth0 | grep inet | awk '{print \$2}' | cut -d'/' -f 1"
+testapi_ip=$(sudo docker exec ${container_name} /bin/bash -c "${get_ip_cmd}")
+echo "The internal IP of container ${container_name} is ${testapi_ip}"
+
 echo "Wait for testapi to work..."
 sleep 10
 
@@ -87,7 +85,7 @@ echo "Upload default project info to DB"
 echo "================================="
 
 echo "Init DB info..."
-cmd="python ./init_db.py ${db_host_ip} ${testapi_port}"
+cmd="python /home/opnfv/dovetail/dovetail/utils/local_db/init_db.py ${testapi_ip} 8000"
 echo $cmd
 ${cmd}