b7d745a79d735bd0686eed575e363c66f4527cc0
[releng.git] / jjb / functest / set-functest-env.sh
1 #!/bin/bash
2
3 set -e
4 [[ $CI_DEBUG == true ]] && redirect="/dev/stdout" || redirect="/dev/null"
5 # labconfig is used only for joid
6 labconfig=""
7 if [[ ${INSTALLER_TYPE} == 'apex' ]]; then
8     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
9     if sudo virsh list | grep instack; then
10         instack_mac=$(sudo virsh domiflist instack | grep default | \
11                       grep -Eo "[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+")
12     elif sudo virsh list | grep undercloud; 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     else
16         echo "No available installer VM exists...exiting"
17         exit 1
18     fi
19     INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk {'print $1'})
20     sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa"
21     sudo scp $ssh_options root@${INSTALLER_IP}:/home/stack/stackrc ${HOME}/stackrc
22     stackrc="-v ${HOME}/stackrc:/home/opnfv/functest/conf/stackrc"
23
24     if sudo iptables -C FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable 2> ${redirect}; then
25         sudo iptables -D FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
26     fi
27     if sudo iptables -C FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable 2> ${redirect}; then
28         sudo iptables -D FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
29     fi
30
31 elif [[ ${INSTALLER_TYPE} == 'joid' ]]; then
32     # If production lab then creds may be retrieved dynamically
33     # creds are on the jumphost, always in the same folder
34     labconfig="-v $LAB_CONFIG/admin-openrc:/home/opnfv/functest/conf/openstack.creds"
35     # If dev lab, credentials may not be the default ones, just provide a path to put them into docker
36     # replace the default one by the customized one provided by jenkins config
37 fi
38
39 # Set iptables rule to allow forwarding return traffic for container
40 if ! sudo iptables -C FORWARD -j RETURN 2> ${redirect} || ! sudo iptables -L FORWARD | awk 'NR==3' | grep RETURN 2> ${redirect}; then
41     sudo iptables -I FORWARD -j RETURN
42 fi
43
44 DEPLOY_TYPE=baremetal
45 [[ $BUILD_TAG =~ "virtual" ]] && DEPLOY_TYPE=virt
46
47 echo "Functest: Start Docker and prepare environment"
48 envs="-e INSTALLER_TYPE=${INSTALLER_TYPE} -e INSTALLER_IP=${INSTALLER_IP} \
49     -e NODE_NAME=${NODE_NAME} -e DEPLOY_SCENARIO=${DEPLOY_SCENARIO} \
50     -e BUILD_TAG=${BUILD_TAG} -e CI_DEBUG=${CI_DEBUG} -e DEPLOY_TYPE=${DEPLOY_TYPE}"
51 branch=${GIT_BRANCH##*/}
52 dir_result="${HOME}/opnfv/functest/results/${branch}"
53 mkdir -p ${dir_result}
54 sudo rm -rf ${dir_result}/*
55 res_volume="-v ${dir_result}:/home/opnfv/functest/results"
56 custom_params=
57 test -f ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG} && custom_params=$(cat ${HOME}/opnfv/functest/custom/params_${DOCKER_TAG})
58
59 echo "Functest: Pulling image opnfv/functest:${DOCKER_TAG}"
60 docker pull opnfv/functest:$DOCKER_TAG >/dev/null
61
62 cmd="sudo docker run --privileged=true -id ${envs} ${labconfig} ${sshkey} ${res_volume} ${custom_params} ${stackrc} opnfv/functest:${DOCKER_TAG} /bin/bash"
63 echo "Functest: Running docker run command: ${cmd}"
64 ${cmd} >${redirect}
65 sleep 5
66 container_id=$(docker ps | grep "opnfv/functest:${DOCKER_TAG}" | awk '{print $1}' | head -1)
67 echo "Container ID=${container_id}"
68 if [ -z ${container_id} ]; then
69     echo "Cannot find opnfv/functest container ID ${container_id}. Please check if it is existing."
70     docker ps -a
71     exit 1
72 fi
73 echo "Starting the container: docker start ${container_id}"
74 docker start ${container_id}
75 sleep 5
76 docker ps >${redirect}
77 if [ $(docker ps | grep "opnfv/functest:${DOCKER_TAG}" | wc -l) == 0 ]; then
78     echo "The container opnfv/functest with ID=${container_id} has not been properly started. Exiting..."
79     exit 1
80 fi
81 if [[ ${branch} == *"brahmaputra"* ]]; then
82     cmd="${FUNCTEST_REPO_DIR}/docker/prepare_env.sh"
83 else
84     cmd="python ${FUNCTEST_REPO_DIR}/ci/prepare_env.py start"
85 fi
86 echo "Executing command inside the docker: ${cmd}"
87 docker exec ${container_id} ${cmd}