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