Merge "yardstick offline support"
[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     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
149         sudo rm -f -- "${RAW_IMAGE}"
150     else
151         sudo rm -f -- "${QCOW_IMAGE}"
152     fi
153
154     echo "Glance image id: $GLANCE_IMAGE_ID"
155 }
156
157 load_cirros_image()
158 {
159     if [[ -n $(openstack image list | grep -e Cirros-0.3.5) ]]; then
160         echo "Cirros-0.3.5 image already exist, skip loading cirros image"
161     else
162         echo
163         echo "========== Loading cirros cloud image =========="
164
165         local image_file=/home/opnfv/images/cirros-0.3.5-x86_64-disk.img
166
167         EXTRA_PARAMS=""
168         # VPP requires guest memory to be backed by large pages
169         if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
170             EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
171         fi
172
173         output=$(openstack image create \
174             --disk-format qcow2 \
175             --container-format bare \
176             ${EXTRA_PARAMS} \
177             --file ${image_file} \
178             cirros-0.3.5)
179         echo "$output"
180
181         CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
182         if [ -z "$CIRROS_IMAGE_ID" ]; then
183             echo 'Failed uploading cirros image to cloud'.
184             exit 1
185         fi
186
187         echo "Cirros image id: $CIRROS_IMAGE_ID"
188     fi
189 }
190
191 load_ubuntu_image()
192 {
193     echo
194     echo "========== Loading ubuntu cloud image =========="
195
196     local ubuntu_image_file=/home/opnfv/images/trusty-server-cloudimg-amd64-disk1.img
197
198     EXTRA_PARAMS=""
199     # VPP requires guest memory to be backed by large pages
200     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
201         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
202     fi
203
204     output=$(openstack image create \
205         --disk-format qcow2 \
206         --container-format bare \
207         $EXTRA_PARAMS \
208         --file $ubuntu_image_file \
209         Ubuntu-14.04)
210     echo "$output"
211
212     UBUNTU_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
213
214     if [ -z "$UBUNTU_IMAGE_ID" ]; then
215         echo 'Failed uploading UBUNTU image to cloud'.
216         exit 1
217     fi
218
219     echo "Ubuntu image id: $UBUNTU_IMAGE_ID"
220 }
221
222 create_nova_flavor()
223 {
224     if ! openstack flavor list | grep -q yardstick-flavor; then
225         echo
226         echo "========== Creating yardstick-flavor =========="
227         # Create the nova flavor used by some sample test cases
228         openstack flavor create --id 100 --ram 512 --disk 3 --vcpus 1 yardstick-flavor
229         # DPDK-enabled OVS requires guest memory to be backed by large pages
230         if [[ $DEPLOY_SCENARIO == *[_-]ovs[_-]* ]]; then
231             openstack flavor set --property hw:mem_page_size=large yardstick-flavor
232         fi
233         # VPP requires guest memory to be backed by large pages
234         if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
235             openstack flavor set --property hw:mem_page_size=large yardstick-flavor
236         fi
237     fi
238
239     if ! openstack flavor list | grep -q storperf; then
240         echo
241         echo "========== Creating storperf flavor =========="
242         # Create the nova flavor used by storperf test case
243         openstack flavor create --id auto --ram 8192 --disk 4 --vcpus 2 storperf
244     fi
245 }
246
247 main()
248 {
249     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-image.img"
250     RAW_IMAGE="/tmp/workspace/yardstick/yardstick-image.tar.gz"
251
252     if [ -f /home/opnfv/images/yardstick-image.img ];then
253         QCOW_IMAGE='/home/opnfv/images/yardstick-image.img'
254     fi
255     if [ -f /home/opnfv/images/yardstick-image.tar.gz ];then
256         RAW_IMAGE='/home/opnfv/images/yardstick-image.tar.gz'
257     fi
258
259     build_yardstick_image
260     load_yardstick_image
261     if [ "${YARD_IMG_ARCH}" == "arm64" ]; then
262         sed -i 's/image: {{image}}/image: TestVM/g' tests/opnfv/test_cases/opnfv_yardstick_tc002.yaml
263         sed -i 's/image: cirros-0.3.5/image: TestVM/g' samples/ping.yaml
264         #We have overlapping IP with the real network
265         for filename in tests/opnfv/test_cases/*; do
266             sed -i "s/cidr: '10.0.1.0\/24'/cidr: '10.3.1.0\/24'/g" "${filename}"
267         done
268         # Update file changes
269         pip install -U .
270     else
271         load_cirros_image
272         load_ubuntu_image
273     fi
274     create_nova_flavor
275 }
276
277 main