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