Bugfix: load_images cleanup, shellcheck fixes
[yardstick.git] / tests / ci / load_images.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 # Set up the environment to run yardstick test suites.
12
13 set -e
14
15 YARD_IMG_ARCH=amd64
16 export YARD_IMG_ARCH
17
18 HW_FW_TYPE=""
19 if [ "${YARD_IMG_ARCH}" == "arm64" ]; then
20     HW_FW_TYPE=uefi
21 fi
22 export HW_FW_TYPE
23
24 if ! grep -q "Defaults env_keep += \"YARD_IMG_ARCH\"" "/etc/sudoers"; then
25     echo "Defaults env_keep += \"YARD_IMG_ARCH YARDSTICK_REPO_DIR\"" | sudo tee -a /etc/sudoers
26 fi
27
28 ARCH_SCRIPT="test -f /etc/fuel_openstack_arch && grep -q arm64 /etc/fuel_openstack_arch"
29 if [ "$INSTALLER_TYPE" == "fuel" ]; then
30     sshpass -p r00tme ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -l root "${INSTALLER_IP}" "${ARCH_SCRIPT}" && YARD_IMG_ARCH=arm64
31 fi
32
33 UCA_HOST="cloud-images.ubuntu.com"
34 if [ "${YARD_IMG_ARCH}"= "arm64" ]; then
35     export CLOUD_IMG_URL="http://${UCA_HOST}/${release}/current/${release}-server-cloudimg-${YARD_IMG_ARCH}.tar.gz"
36     if ! grep -q "Defaults env_keep += \"CLOUD_IMG_URL\"" "/etc/sudoers"; then
37         echo "Defaults env_keep += \"CLOUD_IMG_URL\"" | sudo tee -a /etc/sudoers
38     fi
39 fi
40
41 build_yardstick_image()
42 {
43     echo
44     echo "========== Build yardstick cloud image =========="
45
46     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
47         local cmd
48         cmd="sudo $(which yardstick-img-lxd-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
49
50         # Build the image. Retry once if the build fails
51         $cmd || $cmd
52
53         if [ ! -f "${RAW_IMAGE}" ]; then
54             echo "Failed building RAW image"
55             exit 1
56         fi
57     else
58         local cmd
59         cmd="sudo $(which yardstick-img-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
60
61         # Build the image. Retry once if the build fails
62         $cmd || $cmd
63
64         if [ ! -f "${QCOW_IMAGE}" ]; then
65             echo "Failed building QCOW image"
66             exit 1
67         fi
68     fi
69 }
70
71 load_yardstick_image()
72 {
73     echo
74     echo "========== Loading yardstick cloud image =========="
75     EXTRA_PARAMS=""
76     if [[ "${YARD_IMG_ARCH}" == "arm64" && "${YARD_IMG_AKI}" == "true" ]]; then
77         CLOUD_IMAGE="/tmp/${release}-server-cloudimg-${YARD_IMG_ARCH}.tar.gz"
78         CLOUD_KERNEL="/tmp/${release}-server-cloudimg-${YARD_IMG_ARCH}-vmlinuz-generic"
79         cd /tmp
80         if [ ! -f "${CLOUD_IMAGE}" ]; then
81             wget "${CLOUD_IMG_URL}"
82         fi
83         if [ ! -f "${CLOUD_KERNEL}" ]; then
84             tar xf "${CLOUD_IMAGE}" "${CLOUD_KERNEL##**/}"
85         fi
86         create_kernel=$(openstack image create \
87                 --public \
88                 --disk-format qcow2 \
89                 --container-format bare \
90                 --file ${CLOUD_KERNEL} \
91                 yardstick-${release}-kernel)
92
93         GLANCE_KERNEL_ID=$(echo "$create_kernel" | awk '/ id / {print $(NF-1)}')
94         if [ -z "$GLANCE_KERNEL_ID" ]; then
95             echo 'Failed uploading kernel to cloud'.
96             exit 1
97         fi
98
99         command_line="root=/dev/vdb1 console=tty0 console=ttyS0 console=ttyAMA0 rw"
100
101         EXTRA_PARAMS="--property kernel_id=$GLANCE_KERNEL_ID --property os_command_line=\"$command_line\""
102
103         rm -f -- "${CLOUD_KERNEL}" "${CLOUD_IMAGE}"
104         cd "${YARDSTICK_REPO_DIR}"
105     fi
106
107     # VPP requires guest memory to be backed by large pages
108     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
109         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
110     fi
111
112     if [[ -n "${HW_FW_TYPE}" ]]; then
113         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_firmware_type=${HW_FW_TYPE}"
114     fi
115
116     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
117         output=$(eval openstack image create \
118             --public \
119             --disk-format root-tar \
120             --container-format bare \
121             ${EXTRA_PARAMS} \
122             --file ${RAW_IMAGE} \
123             yardstick-image)
124     else
125         output=$(eval openstack image create \
126             --public \
127             --disk-format qcow2 \
128             --container-format bare \
129             ${EXTRA_PARAMS} \
130             --file ${QCOW_IMAGE} \
131             yardstick-image)
132     fi
133
134     echo "$output"
135
136     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
137
138     if [ -z "$GLANCE_IMAGE_ID" ]; then
139         echo 'Failed uploading image to cloud'.
140         exit 1
141     fi
142
143     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
144         sudo rm -f -- "${RAW_IMAGE}"
145     else
146         sudo rm -f -- "${QCOW_IMAGE}"
147     fi
148
149     echo "Glance image id: $GLANCE_IMAGE_ID"
150 }
151
152 load_cirros_image()
153 {
154     echo
155     echo "========== Loading cirros cloud image =========="
156
157     local image_file=/home/opnfv/images/cirros-0.3.3-x86_64-disk.img
158
159     EXTRA_PARAMS=""
160     # VPP requires guest memory to be backed by large pages
161     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
162         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
163     fi
164
165     output=$(openstack image create \
166         --disk-format qcow2 \
167         --container-format bare \
168         ${EXTRA_PARAMS} \
169         --file ${image_file} \
170         cirros-0.3.3)
171     echo "$output"
172
173     CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
174     if [ -z "$CIRROS_IMAGE_ID" ]; then
175         echo 'Failed uploading cirros image to cloud'.
176         exit 1
177     fi
178
179     echo "Cirros image id: $CIRROS_IMAGE_ID"
180 }
181
182 load_ubuntu_image()
183 {
184     echo
185     echo "========== Loading ubuntu cloud image =========="
186
187     local ubuntu_image_file=/home/opnfv/images/trusty-server-cloudimg-amd64-disk1.img
188
189     EXTRA_PARAMS=""
190     # VPP requires guest memory to be backed by large pages
191     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
192         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
193     fi
194
195     output=$(openstack image create \
196         --disk-format qcow2 \
197         --container-format bare \
198         $EXTRA_PARAMS \
199         --file $ubuntu_image_file \
200         Ubuntu-14.04)
201     echo "$output"
202
203     UBUNTU_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
204
205     if [ -z "$UBUNTU_IMAGE_ID" ]; then
206         echo 'Failed uploading UBUNTU image to cloud'.
207         exit 1
208     fi
209
210     echo "Ubuntu image id: $UBUNTU_IMAGE_ID"
211 }
212
213 create_nova_flavor()
214 {
215     if ! openstack flavor list | grep -q yardstick-flavor; then
216         echo
217         echo "========== Create nova flavor =========="
218         # Create the nova flavor used by some sample test cases
219         openstack flavor create --id 100 --ram 512 --disk 3 --vcpus 1 yardstick-flavor
220         # DPDK-enabled OVS requires guest memory to be backed by large pages
221         if [[ $DEPLOY_SCENARIO == *[_-]ovs[_-]* ]]; then
222             openstack flavor set --property hw:mem_page_size=large yardstick-flavor
223         fi
224         # VPP requires guest memory to be backed by large pages
225         if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
226             openstack flavor set --property hw:mem_page_size=large yardstick-flavor
227         fi
228     fi
229 }
230
231 main()
232 {
233     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-image.img"
234     RAW_IMAGE="/tmp/workspace/yardstick/yardstick-image.tar.gz"
235
236     build_yardstick_image
237     load_yardstick_image
238     if [ "${YARD_IMG_ARCH}" == "arm64" ]; then
239         sed -i 's/image: cirros-0.3.3/image: TestVM/g' tests/opnfv/test_cases/opnfv_yardstick_tc002.yaml \
240         samples/ping.yaml
241         #We have overlapping IP with the real network
242         for filename in tests/opnfv/test_cases/*; do
243             sed -i "s/cidr: '10.0.1.0\/24'/cidr: '10.3.1.0\/24'/g" "${filename}"
244         done
245     else
246         load_cirros_image
247         load_ubuntu_image
248     fi
249     create_nova_flavor
250 }
251
252 main