extra loopback cleanup
[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
17 if ! grep -q "Defaults env_keep += \"YARD_IMG_ARCH\"" "/etc/sudoers"; then
18     echo "Defaults env_keep += \"YARD_IMG_ARCH YARDSTICK_REPO_DIR\"" | sudo tee -a /etc/sudoers
19 fi
20
21 # Look for a compute node, that is online, and check if it is aarch64
22 if [ "${INSTALLER_TYPE}" == 'fuel' ]; then
23     ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
24     COMPUTE_ARCH=$(ssh -l ubuntu ${INSTALLER_IP} -i ${SSH_KEY} ${ssh_options} \
25         "sudo salt 'cmp*' grains.get cpuarch --out yaml | awk '{print \$2; exit}'")
26     if [ "${COMPUTE_ARCH}" == 'aarch64' ]; then
27         YARD_IMG_ARCH=arm64
28     fi
29 fi
30 export YARD_IMG_ARCH
31
32 HW_FW_TYPE=""
33 if [ "${YARD_IMG_ARCH}" == "arm64" ]; then
34     HW_FW_TYPE=uefi
35 fi
36 export HW_FW_TYPE
37
38 UCA_HOST="cloud-images.ubuntu.com"
39 if [ "${YARD_IMG_ARCH}" == "arm64" ]; then
40     export CLOUD_IMG_URL="http://${UCA_HOST}/${release}/current/${release}-server-cloudimg-${YARD_IMG_ARCH}.tar.gz"
41     if ! grep -q "Defaults env_keep += \"CLOUD_IMG_URL\"" "/etc/sudoers"; then
42         echo "Defaults env_keep += \"CLOUD_IMG_URL\"" | sudo tee -a /etc/sudoers
43     fi
44 fi
45
46 cleanup_loopbacks() {
47     # try again to cleanup loopbacks in case of error
48     losetup -a
49     losetup -O NAME,BACK-FILE | awk '/yardstick/ { print $1 }' | xargs -l1 losetup -v -d || true
50 }
51
52 build_yardstick_image()
53 {
54     echo
55     echo "========== Build yardstick cloud image =========="
56
57     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
58         if [ ! -f "${RAW_IMAGE}" ];then
59             local cmd
60             cmd="sudo $(which yardstick-img-lxd-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
61
62             # Build the image. Retry once if the build fails
63             $cmd || $cmd
64
65             cleanup_loopbacks
66             if [ ! -f "${RAW_IMAGE}" ]; then
67                 echo "Failed building RAW image"
68                 exit 1
69             fi
70         fi
71     else
72         if [ ! -f "${QCOW_IMAGE}" ];then
73             ANSIBLE_SCRIPTS="${0%/*}/../../ansible"
74             cd ${ANSIBLE_SCRIPTS} &&\
75             ansible-playbook \
76                      -e img_property="normal" \
77                      -e YARD_IMG_ARCH=${YARD_IMG_ARCH} \
78                      -vvv -i inventory.ini build_yardstick_image.yml
79
80             cleanup_loopbacks
81             if [ ! -f "${QCOW_IMAGE}" ]; then
82                 echo "Failed building QCOW image"
83                 exit 1
84             fi
85         fi
86         if [[ $DEPLOY_SCENARIO == *[_-]ovs[_-]* ]]; then
87             ansible-playbook \
88                      -e img_property="nsb" \
89                      -e YARD_IMG_ARCH=${YARD_IMG_ARCH} \
90                      -vvv -i inventory.ini build_yardstick_image.yml
91             cleanup_loopbacks
92             if [ ! -f "${QCOW_NSB_IMAGE}" ]; then
93                 echo "Failed building QCOW NSB image"
94                 exit 1
95             fi
96         fi
97     fi
98 }
99
100 load_yardstick_image()
101 {
102     echo
103     echo "========== Loading yardstick cloud image =========="
104     EXTRA_PARAMS=""
105     if [[ "${YARD_IMG_ARCH}" == "arm64" ]]; then
106         EXTRA_PARAMS="--property hw_video_model=vga"
107     fi
108
109     # VPP requires guest memory to be backed by large pages
110     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
111         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
112     fi
113
114     if [[ -n "${HW_FW_TYPE}" ]]; then
115         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_firmware_type=${HW_FW_TYPE}"
116     fi
117
118     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
119         output=$(eval openstack ${SECURE} image create \
120             --public \
121             --disk-format raw \
122             --container-format bare \
123             ${EXTRA_PARAMS} \
124             --file ${RAW_IMAGE} \
125             yardstick-image)
126     else
127         output=$(eval openstack ${SECURE} image create \
128             --public \
129             --disk-format qcow2 \
130             --container-format bare \
131             ${EXTRA_PARAMS} \
132             --file ${QCOW_IMAGE} \
133             yardstick-image)
134         if [[ $DEPLOY_SCENARIO == *[_-]ovs[_-]* ]]; then
135             nsb_output=$(eval openstack ${SECURE} image create \
136                 --public \
137                 --disk-format qcow2 \
138                 --container-format bare \
139                 ${EXTRA_PARAMS} \
140                 --file ${QCOW_NSB_IMAGE} \
141                 yardstick-samplevnfs)
142             echo "$nsb_output"
143         fi
144     fi
145
146     echo "$output"
147
148     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
149
150     if [ -z "$GLANCE_IMAGE_ID" ]; then
151         echo 'Failed uploading image to cloud'.
152         exit 1
153     fi
154
155     echo "Glance image id: $GLANCE_IMAGE_ID"
156 }
157
158 load_cirros_image()
159 {
160     EXTRA_PARAMS=""
161     if [[ "${YARD_IMG_ARCH}" == "arm64" ]]; then
162         CIRROS_IMAGE_VERSION="cirros-d161201"
163         CIRROS_IMAGE_PATH="/home/opnfv/images/cirros-d161201-aarch64-disk.img"
164         EXTRA_PARAMS="--property hw_video_model=vga --property short_id=ubuntu16.04"
165     else
166         CIRROS_IMAGE_VERSION="cirros-0.3.5"
167         CIRROS_IMAGE_PATH="/home/opnfv/images/cirros-0.3.5-x86_64-disk.img"
168     fi
169
170     if [[ -n $(openstack ${SECURE} image list | grep -e "${CIRROS_IMAGE_VERSION}") ]]; then
171         echo "${CIRROS_IMAGE_VERSION} image already exist, skip loading cirros image"
172     else
173         echo
174         echo "========== Loading cirros cloud image =========="
175
176         local image_file="${CIRROS_IMAGE_PATH}"
177
178         # VPP requires guest memory to be backed by large pages
179         if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
180             EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
181         fi
182
183         if [[ -n "${HW_FW_TYPE}" ]]; then
184             EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_firmware_type=${HW_FW_TYPE}"
185         fi
186
187         output=$(openstack ${SECURE} image create \
188             --disk-format qcow2 \
189             --container-format bare \
190             ${EXTRA_PARAMS} \
191             --file ${image_file} \
192             cirros-0.3.5)
193         echo "$output"
194
195         CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
196         if [ -z "$CIRROS_IMAGE_ID" ]; then
197             echo 'Failed uploading cirros image to cloud'.
198             exit 1
199         fi
200
201         echo "Cirros image id: $CIRROS_IMAGE_ID"
202     fi
203 }
204
205 load_ubuntu_image()
206 {
207     echo
208     echo "========== Loading ubuntu cloud image =========="
209
210     local ubuntu_image_file=/home/opnfv/images/xenial-server-cloudimg-amd64-disk1.img
211
212     EXTRA_PARAMS=""
213     # VPP requires guest memory to be backed by large pages
214     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
215         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
216     fi
217
218     output=$(openstack ${SECURE} image create \
219         --disk-format qcow2 \
220         --container-format bare \
221         $EXTRA_PARAMS \
222         --file $ubuntu_image_file \
223         Ubuntu-16.04)
224     echo "$output"
225
226     UBUNTU_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
227
228     if [ -z "$UBUNTU_IMAGE_ID" ]; then
229         echo 'Failed uploading UBUNTU image to cloud'.
230         exit 1
231     fi
232
233     echo "Ubuntu image id: $UBUNTU_IMAGE_ID"
234 }
235
236 create_nova_flavor()
237 {
238     if ! openstack ${SECURE} flavor list | grep -q yardstick-flavor; then
239         echo
240         echo "========== Creating yardstick-flavor =========="
241         # Create the nova flavor used by some sample test cases
242         openstack ${SECURE} flavor create --id 100 --ram 1024 --disk 10 --vcpus 1 yardstick-flavor
243         # DPDK-enabled OVS requires guest memory to be backed by large pages
244         if [[ $DEPLOY_SCENARIO == *[_-]ovs[_-]* ]]; then
245             openstack ${SECURE} flavor set --property hw:mem_page_size=large yardstick-flavor
246         fi
247         # VPP requires guest memory to be backed by large pages
248         if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
249             openstack ${SECURE} flavor set --property hw:mem_page_size=large yardstick-flavor
250         fi
251     fi
252
253     if ! openstack ${SECURE} flavor list | grep -q storperf; then
254         echo
255         echo "========== Creating storperf flavor =========="
256         # Create the nova flavor used by storperf test case
257         openstack ${SECURE} flavor create --id auto --ram 8192 --disk 4 --vcpus 2 storperf
258     fi
259 }
260
261 main()
262 {
263     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-image.img"
264     QCOW_NSB_IMAGE="/tmp/workspace/yardstick/yardstick-nsb-image.img"
265     RAW_IMAGE="/tmp/workspace/yardstick/yardstick-image.tar.gz"
266
267     if [ -f /home/opnfv/images/yardstick-image.img ];then
268         QCOW_IMAGE='/home/opnfv/images/yardstick-image.img'
269     fi
270     if [ -f /home/opnfv/images/yardstick-image.tar.gz ];then
271         RAW_IMAGE='/home/opnfv/images/yardstick-image.tar.gz'
272     fi
273
274     if [ $OS_INSECURE ] && [ "$(echo $OS_INSECURE | tr '[:upper:]' '[:lower:]')" = "true" ]; then
275         SECURE="--insecure"
276     else
277         SECURE=""
278     fi
279
280     build_yardstick_image
281     load_yardstick_image
282     if [ "${YARD_IMG_ARCH}" == "arm64" ]; then
283         #We have overlapping IP with the real network
284         for filename in ${YARDSTICK_REPO_DIR}/tests/opnfv/test_cases/*; do
285             sed -i "s/cidr: '10.0.1.0\/24'/cidr: '10.3.1.0\/24'/g" "${filename}"
286         done
287     else
288         load_ubuntu_image
289     fi
290     load_cirros_image
291     create_nova_flavor
292 }
293
294 main