Bugfix: Permission denied when pip install virtualenv
[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 releng_repo=${WORKSPACE}/releng
36 [ -d ${releng_repo} ] && sudo rm -rf ${releng_repo}
37 git clone https://gerrit.opnfv.org/gerrit/releng ${releng_repo} >/dev/null
38
39 if [[ ${INSTALLER_TYPE} != 'joid' ]]; then
40     ${releng_repo}/utils/fetch_os_creds.sh -d ${OPENRC} -i ${INSTALLER_TYPE} -a ${INSTALLER_IP} >${redirect}
41 fi
42
43 if [[ -f $OPENRC ]]; then
44     echo "INFO: openstack credentials path is $OPENRC"
45     cat $OPENRC
46 else
47     echo "ERROR: file $OPENRC does not exist."
48     exit 1
49 fi
50
51 sudo pip install virtualenv
52
53 cd ${releng_repo}/modules
54 sudo virtualenv venv
55 source venv/bin/activate
56 sudo pip install -e ./ >/dev/null
57
58 if [[ ${INSTALLER_TYPE} == compass ]]; then
59     options="-u root -p root"
60 elif [[ ${INSTALLER_TYPE} == fuel ]]; then
61     options="-u root -p r00tme"
62 else
63     echo "Don't support to generate pod.yaml on ${INSTALLER_TYPE} currently."
64     echo "HA test cases may not run properly."
65 fi
66
67 pod_file_dir="/home/opnfv/dovetail/userconfig"
68 cmd="sudo python ${releng_repo}/utils/create_pod_file.py -t ${INSTALLER_TYPE} -i ${INSTALLER_IP} ${options} -f ${pod_file_dir}/pod.yaml"
69 echo ${cmd}
70 ${cmd}
71
72 deactivate
73
74 if [ -f ${pod_file_dir}/pod.yaml ]; then
75     echo "file ${pod_file_dir}/pod.yaml:"
76     cat ${pod_file_dir}/pod.yaml
77 else
78     echo "Error: There doesn't exist file ${pod_file_dir}/pod.yaml."
79     echo "HA test cases may not run properly."
80 fi
81
82 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
83
84 if [ "$INSTALLER_TYPE" == "fuel" ]; then
85     echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
86     sshpass -p r00tme sudo scp $ssh_options root@${INSTALLER_IP}:~/.ssh/id_rsa ${pod_file_dir}/id_rsa
87 fi
88
89 opts="--privileged=true -id"
90 results_envs="-v /var/run/docker.sock:/var/run/docker.sock \
91               -v /home/opnfv/dovetail/results:/home/opnfv/dovetail/results"
92 openrc_volume="-v ${OPENRC}:${OPENRC}"
93 userconfig_volume="-v ${pod_file_dir}:${pod_file_dir}"
94
95 # Pull the image with correct tag
96 echo "Dovetail: Pulling image opnfv/dovetail:${DOCKER_TAG}"
97 docker pull opnfv/dovetail:$DOCKER_TAG >$redirect
98
99 cmd="docker run ${opts} ${results_envs} ${openrc_volume} ${userconfig_volume} \
100      ${sshkey} opnfv/dovetail:${DOCKER_TAG} /bin/bash"
101 echo "Dovetail: running docker run command: ${cmd}"
102 ${cmd} >${redirect}
103 sleep 5
104 container_id=$(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | awk '{print $1}' | head -1)
105 echo "Container ID=${container_id}"
106 if [ -z ${container_id} ]; then
107     echo "Cannot find opnfv/dovetail container ID ${container_id}. Please check if it is existing."
108     docker ps -a
109     exit 1
110 fi
111 echo "Container Start: docker start ${container_id}"
112 docker start ${container_id}
113 sleep 5
114 docker ps >${redirect}
115 if [ $(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | wc -l) == 0 ]; then
116     echo "The container opnfv/dovetail with ID=${container_id} has not been properly started. Exiting..."
117     exit 1
118 fi
119
120 list_cmd="dovetail list ${TESTSUITE}"
121 run_cmd="dovetail run --openrc ${OPENRC} --testsuite ${TESTSUITE} -d"
122 echo "Container exec command: ${list_cmd}"
123 docker exec $container_id ${list_cmd}
124 echo "Container exec command: ${run_cmd}"
125 docker exec $container_id ${run_cmd}
126
127 sudo cp -r ${DOVETAIL_REPO_DIR}/results ./
128 # To make sure the file owner is the current user, for the copied results files in the above line
129 # if not, there will be error when next time to wipe workspace
130 # CURRENT_USER=${SUDO_USER:-$USER}
131 # PRIMARY_GROUP=$(id -gn $CURRENT_USER)
132 # sudo chown -R ${CURRENT_USER}:${PRIMARY_GROUP} ${WORKSPACE}/results
133
134 echo "Dovetail: done!"
135