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