c02dcd573154b7fc3b437fc3d3a3c383fe293320
[releng.git] / jjb / dovetail / dovetail-run.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 #the noun INSTALLER is used in community, here is just the example to run.
11 #multi-platforms are supported.
12
13 set -e
14 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
15
16 DOVETAIL_HOME=${WORKSPACE}/cvp
17 if [ -d ${DOVETAIL_HOME} ]; then
18     sudo rm -rf ${DOVETAIL_HOME}/*
19 else
20     sudo mkdir -p ${DOVETAIL_HOME}
21 fi
22
23 DOVETAIL_CONFIG=${DOVETAIL_HOME}/pre_config
24 sudo mkdir -p ${DOVETAIL_CONFIG}
25
26 sshkey=""
27 # The path of openrc.sh is defined in fetch_os_creds.sh
28 OPENRC=${DOVETAIL_CONFIG}/env_config.sh
29 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
30     instack_mac=$(sudo virsh domiflist undercloud | grep default | \
31                   grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
32     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk {'print $1'})
33     sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
34     if [[ -n $(sudo iptables -L FORWARD |grep "REJECT"|grep "reject-with icmp-port-unreachable") ]]; then
35         #note: this happens only in opnfv-lf-pod1
36         sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
37         sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
38     fi
39 elif [[ ${INSTALLER_TYPE} == 'joid' ]]; then
40     # If production lab then creds may be retrieved dynamically
41     # creds are on the jumphost, always in the same folder
42     sudo cp $LAB_CONFIG/admin-openrc $OPENRC
43     # If dev lab, credentials may not be the default ones, just provide a path to put them into docker
44     # replace the default one by the customized one provided by jenkins config
45 fi
46
47 # Set iptables rule to allow forwarding return traffic for container
48 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
49     sudo iptables -I FORWARD -j RETURN
50 fi
51
52 releng_repo=${WORKSPACE}/releng
53 [ -d ${releng_repo} ] && sudo rm -rf ${releng_repo}
54 git clone https://gerrit.opnfv.org/gerrit/releng ${releng_repo} >/dev/null
55
56 if [[ ${INSTALLER_TYPE} != 'joid' ]]; then
57     ${releng_repo}/utils/fetch_os_creds.sh -d ${OPENRC} -i ${INSTALLER_TYPE} -a ${INSTALLER_IP} >${redirect}
58 fi
59
60 if [[ -f $OPENRC ]]; then
61     echo "INFO: openstack credentials path is $OPENRC"
62     cat $OPENRC
63 else
64     echo "ERROR: cannot find file $OPENRC. Please check if it is existing."
65     sudo ls -al ${DOVETAIL_CONFIG}
66     exit 1
67 fi
68
69 sudo pip install virtualenv
70
71 cd ${releng_repo}/modules
72 sudo virtualenv venv
73 source venv/bin/activate
74 sudo pip install -e ./ >/dev/null
75 sudo pip install netaddr
76
77 if [[ ${INSTALLER_TYPE} == compass ]]; then
78     options="-u root -p root"
79 elif [[ ${INSTALLER_TYPE} == fuel ]]; then
80     options="-u root -p r00tme"
81 else
82     echo "Don't support to generate pod.yaml on ${INSTALLER_TYPE} currently."
83     echo "HA test cases may not run properly."
84 fi
85
86 cmd="sudo python ${releng_repo}/utils/create_pod_file.py -t ${INSTALLER_TYPE} \
87      -i ${INSTALLER_IP} ${options} -f ${DOVETAIL_CONFIG}/pod.yaml"
88 echo ${cmd}
89 ${cmd}
90
91 deactivate
92
93 cd ${WORKSPACE}
94
95 if [ -f ${DOVETAIL_CONFIG}/pod.yaml ]; then
96     echo "file ${DOVETAIL_CONFIG}/pod.yaml:"
97     cat ${DOVETAIL_CONFIG}/pod.yaml
98 else
99     echo "Error: cannot find file ${DOVETAIL_CONFIG}/pod.yaml. Please check if it is existing."
100     sudo ls -al ${DOVETAIL_CONFIG}
101     echo "HA test cases may not run properly."
102 fi
103
104 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
105
106 if [ "$INSTALLER_TYPE" == "fuel" ]; then
107     echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
108     sshpass -p r00tme sudo scp $ssh_options root@${INSTALLER_IP}:~/.ssh/id_rsa ${DOVETAIL_CONFIG}/id_rsa
109 fi
110
111 # sdnvpn test case needs to download this image first before running
112 sudo wget -nc http://artifacts.opnfv.org/sdnvpn/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ${DOVETAIL_CONFIG}
113
114 opts="--privileged=true -id"
115
116 docker_volume="-v /var/run/docker.sock:/var/run/docker.sock"
117 dovetail_home_volume="-v ${DOVETAIL_HOME}:${DOVETAIL_HOME}"
118
119 # Pull the image with correct tag
120 echo "Dovetail: Pulling image opnfv/dovetail:${DOCKER_TAG}"
121 docker pull opnfv/dovetail:$DOCKER_TAG >$redirect
122
123 cmd="docker run ${opts} -e DOVETAIL_HOME=${DOVETAIL_HOME} ${docker_volume} ${dovetail_home_volume} \
124      ${sshkey} opnfv/dovetail:${DOCKER_TAG} /bin/bash"
125 echo "Dovetail: running docker run command: ${cmd}"
126 ${cmd} >${redirect}
127 sleep 5
128 container_id=$(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | awk '{print $1}' | head -1)
129 echo "Container ID=${container_id}"
130 if [ -z ${container_id} ]; then
131     echo "Cannot find opnfv/dovetail container ID ${container_id}. Please check if it is existing."
132     docker ps -a
133     exit 1
134 fi
135 echo "Container Start: docker start ${container_id}"
136 docker start ${container_id}
137 sleep 5
138 docker ps >${redirect}
139 if [ $(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | wc -l) == 0 ]; then
140     echo "The container opnfv/dovetail with ID=${container_id} has not been properly started. Exiting..."
141     exit 1
142 fi
143
144 list_cmd="dovetail list ${TESTSUITE}"
145 run_cmd="dovetail run --testsuite ${TESTSUITE} -d"
146 echo "Container exec command: ${list_cmd}"
147 docker exec $container_id ${list_cmd}
148 echo "Container exec command: ${run_cmd}"
149 docker exec $container_id ${run_cmd}
150
151 sudo cp -r ${DOVETAIL_HOME}/results ./
152 # To make sure the file owner is the current user, for the copied results files in the above line
153 # if not, there will be error when next time to wipe workspace
154 # CURRENT_USER=${SUDO_USER:-$USER}
155 # PRIMARY_GROUP=$(id -gn $CURRENT_USER)
156 # sudo chown -R ${CURRENT_USER}:${PRIMARY_GROUP} ${WORKSPACE}/results
157
158 echo "Dovetail: done!"
159