Merge "Cleanup Ansible common unit tests"
[yardstick.git] / tests / ci / prepare_env.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB, Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 # Perepare the environment to run yardstick ci
12
13 : ${DEPLOY_TYPE:='bm'} # Can be any of 'bm' (Bare Metal) or 'virt' (Virtual)
14 : ${INSTALLER_TYPE:='unknown'}
15 : ${NODE_NAME:='unknown'}
16 : ${EXTERNAL_NETWORK:='admin_floating_net'}
17 : ${USER_NAME:='ubuntu'}
18 : ${SSH_KEY:='/root/.ssh/id_rsa'}
19 : ${DEPLOY_SCENARIO:='unknown'}
20
21 # Extract network name from EXTERNAL_NETWORK
22 #  e.g. EXTERNAL_NETWORK='ext-net;flat;192.168.0.2;192.168.0.253;192.168.0.1;192.168.0.0/24'
23 EXTERNAL_NETWORK=$(echo ${EXTERNAL_NETWORK} | cut -f1 -d \;)
24 export EXTERNAL_NETWORK
25
26 # Create openstack credentials
27 echo "INFO: Creating openstack credentials .."
28 mkdir -p /etc/yardstick
29 OPENRC=/etc/yardstick/openstack.creds
30 INSTALLERS=(apex compass fuel joid)
31
32 RC_VAR_EXIST=false
33 if [[ "${OS_AUTH_URL}" && "${OS_USERNAME}" && "${OS_PASSWORD}" && "${EXTERNAL_NETWORK}" ]];then
34     RC_VAR_EXIST=true
35 fi
36
37 if [[ "${RC_VAR_EXIST}" = false && -f ${OPENRC} ]]; then
38     . ${OPENRC}
39 fi
40
41 export EXTERNAL_NETWORK INSTALLER_TYPE DEPLOY_TYPE NODE_NAME
42
43 # Prepare a admin-rc file for StorPerf integration
44 "${YARDSTICK_REPO_DIR}"/tests/ci/prepare_storperf_admin-rc.sh
45
46 # copy Storperf related files to the deployment location
47 if [ "$INSTALLER_TYPE" == "compass" ]; then
48     . "${YARDSTICK_REPO_DIR}"/tests/ci/scp_storperf_files.sh
49 fi
50
51 # Fetching id_rsa file from jump_server..."
52 verify_connectivity() {
53     local ip=$1
54     echo "Verifying connectivity to $ip..."
55     for i in $(seq 0 10); do
56         if ping -c 1 -W 1 "${ip}" > /dev/null; then
57             echo "$ip is reachable!"
58             return 0
59         fi
60         sleep 1
61     done
62     error "Can not talk to $ip."
63 }
64
65 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
66 if [ "$INSTALLER_TYPE" == "apex" ]; then
67
68     # check the connection
69     verify_connectivity "${INSTALLER_IP}"
70
71     pod_yaml="$YARDSTICK_REPO_DIR/etc/yardstick/nodes/apex_baremetal/pod.yaml"
72
73     # update "ip" according to the CI env
74     ssh -l root "${INSTALLER_IP}" -i ${SSH_KEY} ${ssh_options} \
75          "source /home/stack/stackrc && openstack server list -f yaml" > node_info
76
77     controller_ips=($(awk '/control/{getline; {print $2}}' < node_info | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'))
78     compute_ips=($(awk '/compute/{getline; {print $2}}' < node_info | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'))
79     odl_ip=""
80     # Get ODL's cluster default module-shard Leader IP in HA scenario
81     if [[ ${DEPLOY_SCENARIO} == os-odl-*-ha ]]; then
82         for ip in "${controller_ips[@]}";
83         do
84            if [[ "$odl_ip" ]]; then
85                break
86            fi
87            for ((i=0; i<${#controller_ips[@]}; i++));
88            do
89                ODL_STATE=$(curl -s -u admin:admin -H "Accept: application/json" -H "Content-Type: application/json" \
90                            "http://"${ip}":8081/jolokia/read/org.opendaylight.controller:Category=Shards,name=member-"${i}"-shard-default-operational,type=DistributedOperationalDatastore" \
91                            | grep -o \"RaftState\"\:\"Leader\" | tr ":" "\n" | sed -n '2p' | sed 's/\"//g');
92
93                if [[ ${ODL_STATE} == "Leader" ]]; then
94                     odl_ip=${ip}
95                     break
96                fi
97            done;
98         done
99
100         if [[ -z "$odl_ip" ]]; then
101           echo "ERROR: Opendaylight Leader IP is emtpy"
102           exit 1
103         fi
104
105     elif [[ ${DEPLOY_SCENARIO} == *"odl"* ]]; then
106         odl_ip=${controller_ips[0]}
107     fi
108
109     if [[ ${controller_ips[0]} ]]; then
110         sed -i "s|ip1|${controller_ips[0]}|" "${pod_yaml}"
111     fi
112     if [[ ${controller_ips[1]} ]]; then
113         sed -i "s|ip2|${controller_ips[1]}|" "${pod_yaml}"
114     fi
115     if [[ ${controller_ips[2]} ]]; then
116         sed -i "s|ip3|${controller_ips[2]}|" "${pod_yaml}"
117     fi
118     if [[ ${compute_ips[0]} ]]; then
119         sed -i "s|ip4|${compute_ips[0]}|" "${pod_yaml}"
120     fi
121     if [[ ${compute_ips[1]} ]]; then
122         sed -i "s|ip5|${compute_ips[1]}|" "${pod_yaml}"
123     fi
124     if [[ ${odl_ip} ]]; then
125         sed -i "s|ip6|${odl_ip}|" "${pod_yaml}"
126     fi
127
128
129     # update 'key_filename' according to the CI env
130     sed -i "s|node_keyfile|${SSH_KEY}|" "${pod_yaml}"
131
132 fi
133 if [ "$INSTALLER_TYPE" == "fuel" ]; then
134
135     # check the connection
136     verify_connectivity "${INSTALLER_IP}"
137
138     pod_yaml="$YARDSTICK_REPO_DIR/etc/yardstick/nodes/fuel_baremetal/pod.yaml"
139
140     # update "ip" according to the CI env
141     ssh -l ubuntu "${INSTALLER_IP}" -i ${SSH_KEY} ${ssh_options} \
142          "sudo salt -C 'ctl* or cmp* or odl01* or gtw*' grains.get fqdn_ip4  --out yaml">node_info
143
144     controller_ips=($(awk '/ctl/{getline; print $2}' < node_info))
145     compute_ips=($(awk '/cmp/{getline; print $2}' < node_info))
146     odl_ip=($(awk '/odl01/{getline; print $2}' < node_info))
147     gateway_ip=($(awk '/gtw/{getline; print $2}' < node_info))
148
149     if [[ ${controller_ips[0]} ]]; then
150         sed -i "s|ip1|${controller_ips[0]}|" "${pod_yaml}"
151     fi
152     if [[ ${controller_ips[1]} ]]; then
153         sed -i "s|ip2|${controller_ips[1]}|" "${pod_yaml}"
154     fi
155     if [[ ${controller_ips[2]} ]]; then
156         sed -i "s|ip3|${controller_ips[2]}|" "${pod_yaml}"
157     fi
158     if [[ ${compute_ips[0]} ]]; then
159         sed -i "s|ip4|${compute_ips[0]}|" "${pod_yaml}"
160     fi
161     if [[ ${compute_ips[1]} ]]; then
162         sed -i "s|ip5|${compute_ips[1]}|" "${pod_yaml}"
163     fi
164     if [[ ${odl_ip[0]} ]]; then
165         sed -i "s|ip6|${odl_ip[0]}|" "${pod_yaml}"
166     fi
167     if [[ ${gateway_ip[0]} ]]; then
168         sed -i "s|ip7|${gateway_ip[0]}|" "${pod_yaml}"
169     fi
170
171     # update 'user' and 'key_filename' according to the CI env
172     sed -i "s|node_username|${USER_NAME}|;s|node_keyfile|${SSH_KEY}|" "${pod_yaml}"
173
174 fi