Bugfix: Error when create pod.yaml file
[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 if [ -d ${pod_file_dir} ]; then
69     sudo rm -r ${pod_file_dir}/*
70 else
71     sudo mkdir -p ${pod_file_dir}
72 fi
73 cmd="sudo python ${releng_repo}/utils/create_pod_file.py -t ${INSTALLER_TYPE} -i ${INSTALLER_IP} ${options} -f ${pod_file_dir}/pod.yaml"
74 echo ${cmd}
75 ${cmd}
76
77 deactivate
78
79 cd ${WORKSPACE}
80
81 if [ -f ${pod_file_dir}/pod.yaml ]; then
82     echo "file ${pod_file_dir}/pod.yaml:"
83     cat ${pod_file_dir}/pod.yaml
84 else
85     echo "Error: There doesn't exist file ${pod_file_dir}/pod.yaml."
86     echo "HA test cases may not run properly."
87 fi
88
89 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
90
91 if [ "$INSTALLER_TYPE" == "fuel" ]; then
92     echo "Fetching id_rsa file from jump_server $INSTALLER_IP..."
93     sshpass -p r00tme sudo scp $ssh_options root@${INSTALLER_IP}:~/.ssh/id_rsa ${pod_file_dir}/id_rsa
94 fi
95
96 opts="--privileged=true -id"
97 results_envs="-v /var/run/docker.sock:/var/run/docker.sock \
98               -v /home/opnfv/dovetail/results:/home/opnfv/dovetail/results"
99 openrc_volume="-v ${OPENRC}:${OPENRC}"
100 userconfig_volume="-v ${pod_file_dir}:${pod_file_dir}"
101
102 # Pull the image with correct tag
103 echo "Dovetail: Pulling image opnfv/dovetail:${DOCKER_TAG}"
104 docker pull opnfv/dovetail:$DOCKER_TAG >$redirect
105
106 cmd="docker run ${opts} ${results_envs} ${openrc_volume} ${userconfig_volume} \
107      ${sshkey} opnfv/dovetail:${DOCKER_TAG} /bin/bash"
108 echo "Dovetail: running docker run command: ${cmd}"
109 ${cmd} >${redirect}
110 sleep 5
111 container_id=$(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | awk '{print $1}' | head -1)
112 echo "Container ID=${container_id}"
113 if [ -z ${container_id} ]; then
114     echo "Cannot find opnfv/dovetail container ID ${container_id}. Please check if it is existing."
115     docker ps -a
116     exit 1
117 fi
118 echo "Container Start: docker start ${container_id}"
119 docker start ${container_id}
120 sleep 5
121 docker ps >${redirect}
122 if [ $(docker ps | grep "opnfv/dovetail:${DOCKER_TAG}" | wc -l) == 0 ]; then
123     echo "The container opnfv/dovetail with ID=${container_id} has not been properly started. Exiting..."
124     exit 1
125 fi
126
127 list_cmd="dovetail list ${TESTSUITE}"
128 run_cmd="dovetail run --openrc ${OPENRC} --testsuite ${TESTSUITE} -d"
129 echo "Container exec command: ${list_cmd}"
130 docker exec $container_id ${list_cmd}
131 echo "Container exec command: ${run_cmd}"
132 docker exec $container_id ${run_cmd}
133
134 sudo cp -r ${DOVETAIL_REPO_DIR}/results ./
135 # To make sure the file owner is the current user, for the copied results files in the above line
136 # if not, there will be error when next time to wipe workspace
137 # CURRENT_USER=${SUDO_USER:-$USER}
138 # PRIMARY_GROUP=$(id -gn $CURRENT_USER)
139 # sudo chown -R ${CURRENT_USER}:${PRIMARY_GROUP} ${WORKSPACE}/results
140
141 echo "Dovetail: done!"
142