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