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