Bugfix: HA failed on Compass master because of lacking pod.yaml
[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 [ -d ${DOVETAIL_HOME} ] && sudo rm -rf ${DOVETAIL_HOME}
18
19 mkdir -p ${DOVETAIL_HOME}
20
21 DOVETAIL_CONFIG=${DOVETAIL_HOME}/pre_config
22 mkdir -p ${DOVETAIL_CONFIG}
23
24 sshkey=""
25 # The path of openrc.sh is defined in fetch_os_creds.sh
26 OPENRC=${DOVETAIL_CONFIG}/env_config.sh
27 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
28     instack_mac=$(sudo virsh domiflist undercloud | grep default | \
29                   grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
30     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk {'print $1'})
31     sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
32     if [[ -n $(sudo iptables -L FORWARD |grep "REJECT"|grep "reject-with icmp-port-unreachable") ]]; then
33         #note: this happens only in opnfv-lf-pod1
34         sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
35         sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
36     fi
37 elif [[ ${INSTALLER_TYPE} == 'joid' ]]; then
38     # If production lab then creds may be retrieved dynamically
39     # creds are on the jumphost, always in the same folder
40     sudo cp $LAB_CONFIG/admin-openrc $OPENRC
41     # If dev lab, credentials may not be the default ones, just provide a path to put them into docker
42     # replace the default one by the customized one provided by jenkins config
43 fi
44
45 # Set iptables rule to allow forwarding return traffic for container
46 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
47     sudo iptables -I FORWARD -j RETURN
48 fi
49
50 releng_repo=${WORKSPACE}/releng
51 [ -d ${releng_repo} ] && sudo rm -rf ${releng_repo}
52 git clone https://gerrit.opnfv.org/gerrit/releng ${releng_repo} >/dev/null
53
54 if [[ ${INSTALLER_TYPE} != 'joid' ]]; then
55     echo "SUT branch is $SUT_BRANCH"
56     echo "dovetail branch is $BRANCH"
57     BRANCH_BACKUP=$BRANCH
58     export BRANCH=$SUT_BRANCH
59     ${releng_repo}/utils/fetch_os_creds.sh -d ${OPENRC} -i ${INSTALLER_TYPE} -a ${INSTALLER_IP} >${redirect}
60     export BRANCH=$BRANCH_BACKUP
61 fi
62
63 if [[ -f $OPENRC ]]; then
64     echo "INFO: openstack credentials path is $OPENRC"
65     cat $OPENRC
66 else
67     echo "ERROR: cannot find file $OPENRC. Please check if it is existing."
68     sudo ls -al ${DOVETAIL_CONFIG}
69     exit 1
70 fi
71
72 if [[ ! "${SUT_BRANCH}" =~ "danube" && ${INSTALLER_TYPE} == "compass" ]]; then
73     cat << EOF >${DOVETAIL_CONFIG}/pod.yaml
74 nodes:
75 - {ip: 10.1.0.52, name: node1, password: root, role: controller, user: root}
76 - {ip: 10.1.0.51, name: node2, password: root, role: controller, user: root}
77 - {ip: 10.1.0.50, name: node3, password: root, role: controller, user: root}
78 - {ip: 10.1.0.54, name: node4, password: root, role: compute, user: root}
79 - {ip: 10.1.0.53, name: node5, password: root, role: compute, user: root}
80
81 EOF
82 fi
83
84 if [[ ! -f ${DOVETAIL_CONFIG}/pod.yaml ]]; then
85     set +e
86
87     sudo pip install virtualenv
88
89     cd ${releng_repo}/modules
90     sudo virtualenv venv
91     source venv/bin/activate
92     sudo pip install -e ./ >/dev/null
93     sudo pip install netaddr
94
95     if [[ ${INSTALLER_TYPE} == compass ]]; then
96         options="-u root -p root"
97     elif [[ ${INSTALLER_TYPE} == fuel ]]; then
98         options="-u root -p r00tme"
99     elif [[ ${INSTALLER_TYPE} == apex ]]; then
100         options="-u stack -k /root/.ssh/id_rsa"
101     else
102         echo "Don't support to generate pod.yaml on ${INSTALLER_TYPE} currently."
103         echo "HA test cases may not run properly."
104     fi
105
106     cmd="sudo python ${releng_repo}/utils/create_pod_file.py -t ${INSTALLER_TYPE} \
107          -i ${INSTALLER_IP} ${options} -f ${DOVETAIL_CONFIG}/pod.yaml"
108     echo ${cmd}
109     ${cmd}
110
111     deactivate
112
113     set -e
114
115     cd ${WORKSPACE}
116 fi
117
118 if [ -f ${DOVETAIL_CONFIG}/pod.yaml ]; then
119     echo "file ${DOVETAIL_CONFIG}/pod.yaml:"
120     cat ${DOVETAIL_CONFIG}/pod.yaml
121 else
122     echo "Error: cannot find file ${DOVETAIL_CONFIG}/pod.yaml. Please check if it is existing."
123     sudo ls -al ${DOVETAIL_CONFIG}
124     echo "HA test cases may not run properly."
125 fi
126
127 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
128
129 if [ "$INSTALLER_TYPE" == "fuel" ]; then
130     echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
131     sshpass -p r00tme sudo scp $ssh_options root@${INSTALLER_IP}:~/.ssh/id_rsa ${DOVETAIL_CONFIG}/id_rsa
132 fi
133
134 if [ "$INSTALLER_TYPE" == "apex" ]; then
135     echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
136     sudo scp $ssh_options stack@${INSTALLER_IP}:~/.ssh/id_rsa ${DOVETAIL_CONFIG}/id_rsa
137 fi
138
139 image_path=${HOME}/opnfv/dovetail/images
140 if [[ ! -d ${image_path} ]]; then
141     mkdir -p ${image_path}
142 fi
143 # sdnvpn test case needs to download this image first before running
144 ubuntu_image=${image_path}/ubuntu-16.04-server-cloudimg-amd64-disk1.img
145 if [[ ! -f ${ubuntu_image} ]]; then
146     echo "Download image ubuntu-16.04-server-cloudimg-amd64-disk1.img ..."
147     wget -q -nc http://artifacts.opnfv.org/sdnvpn/ubuntu-16.04-server-cloudimg-amd64-disk1.img -P ${image_path}
148 fi
149 sudo cp ${ubuntu_image} ${DOVETAIL_CONFIG}
150
151 # functest needs to download this image first before running
152 cirros_image=${image_path}/cirros-0.3.5-x86_64-disk.img
153 if [[ ! -f ${cirros_image} ]]; then
154     echo "Download image cirros-0.3.5-x86_64-disk.img ..."
155     wget -q -nc http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img -P ${image_path}
156 fi
157 sudo cp ${cirros_image} ${DOVETAIL_CONFIG}
158
159
160 opts="--privileged=true -id"
161
162 docker_volume="-v /var/run/docker.sock:/var/run/docker.sock"
163 dovetail_home_volume="-v ${DOVETAIL_HOME}:${DOVETAIL_HOME}"
164
165 # Pull the image with correct tag
166 echo "Dovetail: Pulling image opnfv/dovetail:${DOCKER_TAG}"
167 docker pull opnfv/dovetail:$DOCKER_TAG >$redirect
168
169 env4bgpvpn="-e INSTALLER_TYPE=${INSTALLER_TYPE} -e INSTALLER_IP=${INSTALLER_IP}"
170
171 cmd="docker run ${opts} -e DOVETAIL_HOME=${DOVETAIL_HOME} ${docker_volume} ${dovetail_home_volume} \
172      ${sshkey} ${env4bgpvpn} opnfv/dovetail:${DOCKER_TAG} /bin/bash"
173 echo "Dovetail: running docker run command: ${cmd}"
174 ${cmd} >${redirect}
175 sleep 5
176 container_id=$(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | awk '{print $1}' | head -1)
177 echo "Container ID=${container_id}"
178 if [ -z ${container_id} ]; then
179     echo "Cannot find opnfv/dovetail container ID ${container_id}. Please check if it is existing."
180     docker ps -a
181     exit 1
182 fi
183 echo "Container Start: docker start ${container_id}"
184 docker start ${container_id}
185 sleep 5
186 docker ps >${redirect}
187 if [ $(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | wc -l) == 0 ]; then
188     echo "The container opnfv/dovetail with ID=${container_id} has not been properly started. Exiting..."
189     exit 1
190 fi
191
192 # Modify tempest_conf.yaml file
193 tempest_conf_file=${DOVETAIL_CONFIG}/tempest_conf.yaml
194 if [[ ${INSTALLER_TYPE} == 'compass' || ${INSTALLER_TYPE} == 'apex' ]]; then
195     volume_device='vdb'
196 else
197     volume_device='vdc'
198 fi
199
200 cat << EOF >$tempest_conf_file
201
202 compute:
203     min_compute_nodes: 2
204     volume_device_name: ${volume_device}
205
206 EOF
207
208 echo "${tempest_conf_file}..."
209 cat ${tempest_conf_file}
210
211 cp_tempest_cmd="docker cp ${DOVETAIL_CONFIG}/tempest_conf.yaml $container_id:/home/opnfv/dovetail/dovetail/userconfig"
212 echo "exec command: ${cp_tempest_cmd}"
213 $cp_tempest_cmd
214
215 list_cmd="dovetail list ${TESTSUITE}"
216 run_cmd="dovetail run --testsuite ${TESTSUITE} -d"
217 echo "Container exec command: ${list_cmd}"
218 docker exec $container_id ${list_cmd}
219 echo "Container exec command: ${run_cmd}"
220 docker exec $container_id ${run_cmd}
221
222 sudo cp -r ${DOVETAIL_HOME}/results ./
223 # To make sure the file owner is the current user, for the copied results files in the above line
224 # if not, there will be error when next time to wipe workspace
225 # CURRENT_USER=${SUDO_USER:-$USER}
226 # PRIMARY_GROUP=$(id -gn $CURRENT_USER)
227 # sudo chown -R ${CURRENT_USER}:${PRIMARY_GROUP} ${WORKSPACE}/results
228
229 #remove useless workspace from yardstick to save disk space
230 sudo rm -rf ./results/workspace
231
232 echo "Dovetail: done!"
233