dovetail: fix wipe workspace error
[releng.git] / jjb / dovetail / dovetail-run.sh
1 #!/bin/bash
2
3 #the noun INSTALLER is used in community, here is just the example to run.
4 #multi-platforms are supported.
5
6 set -e
7 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
8
9 sshkey=""
10 # The path of openrc.sh is defined in fetch_os_creds.sh
11 OPENRC=$WORKSPACE/opnfv-openrc.sh
12 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
13     instack_mac=$(sudo virsh domiflist undercloud | grep default | \
14                   grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
15     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk {'print $1'})
16     sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
17     if [[ -n $(sudo iptables -L FORWARD |grep "REJECT"|grep "reject-with icmp-port-unreachable") ]]; then
18         #note: this happens only in opnfv-lf-pod1
19         sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
20         sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
21     fi
22 elif [[ ${INSTALLER_TYPE} == 'joid' ]]; then
23     # If production lab then creds may be retrieved dynamically
24     # creds are on the jumphost, always in the same folder
25     sudo cp $LAB_CONFIG/admin-openrc $OPENRC
26     # If dev lab, credentials may not be the default ones, just provide a path to put them into docker
27     # replace the default one by the customized one provided by jenkins config
28 fi
29
30 # Set iptables rule to allow forwarding return traffic for container
31 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
32     sudo iptables -I FORWARD -j RETURN
33 fi
34
35 if [[ ${INSTALLER_TYPE} != 'joid' ]]; then
36     releng_repo=${WORKSPACE}/releng
37     [ -d ${releng_repo} ] && sudo rm -rf ${releng_repo}
38     git clone https://gerrit.opnfv.org/gerrit/releng ${releng_repo} >/dev/null
39     ${releng_repo}/utils/fetch_os_creds.sh -d ${OPENRC} -i ${INSTALLER_TYPE} -a ${INSTALLER_IP} >${redirect}
40 fi
41
42 if [[ -f $OPENRC ]]; then
43     echo "INFO: openstack credentials path is $OPENRC"
44     cat $OPENRC
45 else
46     echo "ERROR: file $OPENRC does not exist."
47     exit 1
48 fi
49
50 opts="--privileged=true -id"
51 results_envs="-v /var/run/docker.sock:/var/run/docker.sock \
52               -v /home/opnfv/dovetail/results:/home/opnfv/dovetail/results"
53 openrc_volume="-v ${OPENRC}:${OPENRC}"
54
55 # Pull the image with correct tag
56 echo "Dovetail: Pulling image opnfv/dovetail:${DOCKER_TAG}"
57 docker pull opnfv/dovetail:$DOCKER_TAG >$redirect
58
59 cmd="docker run ${opts} ${results_envs} ${openrc_volume} \
60      ${sshkey} opnfv/dovetail:${DOCKER_TAG} /bin/bash"
61 echo "Dovetail: running docker run command: ${cmd}"
62 ${cmd} >${redirect}
63 sleep 5
64 container_id=$(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | awk '{print $1}' | head -1)
65 echo "Container ID=${container_id}"
66 if [ -z ${container_id} ]; then
67     echo "Cannot find opnfv/dovetail container ID ${container_id}. Please check if it is existing."
68     docker ps -a
69     exit 1
70 fi
71 echo "Container Start: docker start ${container_id}"
72 docker start ${container_id}
73 sleep 5
74 docker ps >${redirect}
75 if [ $(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | wc -l) == 0 ]; then
76     echo "The container opnfv/dovetail with ID=${container_id} has not been properly started. Exiting..."
77     exit 1
78 fi
79
80 list_cmd="dovetail list ${TESTSUITE}"
81 run_cmd="dovetail run --openrc ${OPENRC} --testsuite ${TESTSUITE} -d"
82 echo "Container exec command: ${list_cmd}"
83 docker exec $container_id ${list_cmd}
84 echo "Container exec command: ${run_cmd}"
85 docker exec $container_id ${run_cmd}
86
87 sudo cp -r ${DOVETAIL_REPO_DIR}/results ./
88 # To make sure the file owner is the current user, for the copied results files in the above line
89 # if not, there will be error when next time to wipe workspace
90 # CURRENT_USER=${SUDO_USER:-$USER}
91 # PRIMARY_GROUP=$(id -gn $CURRENT_USER)
92 # sudo chown -R ${CURRENT_USER}:${PRIMARY_GROUP} ${WORKSPACE}/results
93
94 echo "Dovetail: done!"
95