Merge "Move test scripts to tools/"
[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 build_yardstick_image()
47 {
48     echo
49     echo "========== Build yardstick cloud image =========="
50
51     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
52         if [ ! -f "${RAW_IMAGE}" ];then
53             local cmd
54             cmd="sudo $(which yardstick-img-lxd-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
55
56             # Build the image. Retry once if the build fails
57             $cmd || $cmd
58
59             if [ ! -f "${RAW_IMAGE}" ]; then
60                 echo "Failed building RAW image"
61                 exit 1
62             fi
63         fi
64     else
65         if [ ! -f "${QCOW_IMAGE}" ];then
66             ANSIBLE_SCRIPTS="${0%/*}/../../ansible"
67             cd ${ANSIBLE_SCRIPTS} &&\
68             ansible-playbook \
69                      -e img_modify_playbook='ubuntu_server_cloudimg_modify.yml' \
70                      -e target_os='Ubuntu' \
71                      -e YARD_IMG_ARCH='amd64' \
72                      -e ubuntu_img_file="${QCOW_IMAGE}" \
73                      -vvv -i inventory.ini build_yardstick_image.yml
74
75             if [ ! -f "${QCOW_IMAGE}" ]; then
76                 echo "Failed building QCOW image"
77                 exit 1
78             fi
79         fi
80     fi
81 }
82
83 load_yardstick_image()
84 {
85     echo
86     echo "========== Loading yardstick cloud image =========="
87     EXTRA_PARAMS=""
88     if [[ "${YARD_IMG_ARCH}" == "arm64" ]]; then
89         EXTRA_PARAMS="--property hw_video_model=vga"
90     fi
91
92     # VPP requires guest memory to be backed by large pages
93     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
94         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
95     fi
96
97     if [[ -n "${HW_FW_TYPE}" ]]; then
98         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_firmware_type=${HW_FW_TYPE}"
99     fi
100
101     if [[ "$DEPLOY_SCENARIO" == *"-lxd-"* ]]; then
102         output=$(eval openstack ${SECURE} image create \
103             --public \
104             --disk-format raw \
105             --container-format bare \
106             ${EXTRA_PARAMS} \
107             --file ${RAW_IMAGE} \
108             yardstick-image)
109     else
110         output=$(eval openstack ${SECURE} image create \
111             --public \
112             --disk-format qcow2 \
113             --container-format bare \
114             ${EXTRA_PARAMS} \
115             --file ${QCOW_IMAGE} \
116             yardstick-image)
117     fi
118
119     echo "$output"
120
121     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
122
123     if [ -z "$GLANCE_IMAGE_ID" ]; then
124         echo 'Failed uploading image to cloud'.
125         exit 1
126     fi
127
128     echo "Glance image id: $GLANCE_IMAGE_ID"
129 }
130
131 load_cirros_image()
132 {
133     EXTRA_PARAMS=""
134     if [[ "${YARD_IMG_ARCH}" == "arm64" ]]; then
135         CIRROS_IMAGE_VERSION="cirros-d161201"
136         CIRROS_IMAGE_PATH="/home/opnfv/images/cirros-d161201-aarch64-disk.img"
137         EXTRA_PARAMS="--property hw_video_model=vga --property short_id=ubuntu16.04"
138     else
139         CIRROS_IMAGE_VERSION="Cirros-0.3.5"
140         CIRROS_IMAGE_PATH="/home/opnfv/images/cirros-0.3.5-x86_64-disk.img"
141     fi
142
143     if [[ -n $(openstack ${SECURE} image list | grep -e "${CIRROS_IMAGE_VERSION}") ]]; then
144         echo "${CIRROS_IMAGE_VERSION} image already exist, skip loading cirros image"
145     else
146         echo
147         echo "========== Loading cirros cloud image =========="
148
149         local image_file="${CIRROS_IMAGE_PATH}"
150
151         # VPP requires guest memory to be backed by large pages
152         if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
153             EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
154         fi
155
156         if [[ -n "${HW_FW_TYPE}" ]]; then
157             EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_firmware_type=${HW_FW_TYPE}"
158         fi
159
160         output=$(openstack ${SECURE} image create \
161             --disk-format qcow2 \
162             --container-format bare \
163             ${EXTRA_PARAMS} \
164             --file ${image_file} \
165             cirros-0.3.5)
166         echo "$output"
167
168         CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
169         if [ -z "$CIRROS_IMAGE_ID" ]; then
170             echo 'Failed uploading cirros image to cloud'.
171             exit 1
172         fi
173
174         echo "Cirros image id: $CIRROS_IMAGE_ID"
175     fi
176 }
177
178 load_ubuntu_image()
179 {
180     echo
181     echo "========== Loading ubuntu cloud image =========="
182
183     local ubuntu_image_file=/home/opnfv/images/xenial-server-cloudimg-amd64-disk1.img
184
185     EXTRA_PARAMS=""
186     # VPP requires guest memory to be backed by large pages
187     if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
188         EXTRA_PARAMS=$EXTRA_PARAMS" --property hw_mem_page_size=large"
189     fi
190
191     output=$(openstack ${SECURE} image create \
192         --disk-format qcow2 \
193         --container-format bare \
194         $EXTRA_PARAMS \
195         --file $ubuntu_image_file \
196         Ubuntu-16.04)
197     echo "$output"
198
199     UBUNTU_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
200
201     if [ -z "$UBUNTU_IMAGE_ID" ]; then
202         echo 'Failed uploading UBUNTU image to cloud'.
203         exit 1
204     fi
205
206     echo "Ubuntu image id: $UBUNTU_IMAGE_ID"
207 }
208
209 create_nova_flavor()
210 {
211     if ! openstack ${SECURE} flavor list | grep -q yardstick-flavor; then
212         echo
213         echo "========== Creating yardstick-flavor =========="
214         # Create the nova flavor used by some sample test cases
215         openstack ${SECURE} flavor create --id 100 --ram 1024 --disk 10 --vcpus 1 yardstick-flavor
216         # DPDK-enabled OVS requires guest memory to be backed by large pages
217         if [[ $DEPLOY_SCENARIO == *[_-]ovs[_-]* ]]; then
218             openstack ${SECURE} flavor set --property hw:mem_page_size=large yardstick-flavor
219         fi
220         # VPP requires guest memory to be backed by large pages
221         if [[ "$DEPLOY_SCENARIO" == *"-fdio-"* ]]; then
222             openstack ${SECURE} flavor set --property hw:mem_page_size=large yardstick-flavor
223         fi
224     fi
225
226     if ! openstack ${SECURE} flavor list | grep -q storperf; then
227         echo
228         echo "========== Creating storperf flavor =========="
229         # Create the nova flavor used by storperf test case
230         openstack ${SECURE} flavor create --id auto --ram 8192 --disk 4 --vcpus 2 storperf
231     fi
232 }
233
234 main()
235 {
236     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-image.img"
237     RAW_IMAGE="/tmp/workspace/yardstick/yardstick-image.tar.gz"
238
239     if [ -f /home/opnfv/images/yardstick-image.img ];then
240         QCOW_IMAGE='/home/opnfv/images/yardstick-image.img'
241     fi
242     if [ -f /home/opnfv/images/yardstick-image.tar.gz ];then
243         RAW_IMAGE='/home/opnfv/images/yardstick-image.tar.gz'
244     fi
245
246     if [ $OS_INSECURE ] && [ "$(echo $OS_INSECURE | tr '[:upper:]' '[:lower:]')" = "true" ]; then
247         SECURE="--insecure"
248     else
249         SECURE=""
250     fi
251
252     build_yardstick_image
253     load_yardstick_image
254     if [ "${YARD_IMG_ARCH}" == "arm64" ]; then
255         #We have overlapping IP with the real network
256         for filename in tests/opnfv/test_cases/*; do
257             sed -i "s/cidr: '10.0.1.0\/24'/cidr: '10.3.1.0\/24'/g" "${filename}"
258         done
259     else
260         load_ubuntu_image
261     fi
262     load_cirros_image
263     create_nova_flavor
264 }
265
266 main