blob: 0a04eecd198b9cb38d7c0455e15312ec3a1bd141 [file] [log] [blame]
Alexa4437742022-02-16 14:42:38 -06001#!/bin/bash
2export OS_INTERFACE='admin'
3
Ievgeniia Zadorozhna2bbb1cc2024-02-29 18:48:21 +01004# Prepare clouds.yaml file for the future cleanup, with original admin creds
5sed -i "s#AUTH_URL#${OS_AUTH_URL}#g; s#USERNAME#${OS_USERNAME}#g; s#USER_PASSWORD#${OS_PASSWORD}#g; s#PROJECT_NAME#${OS_PROJECT_NAME}#g; s#PROJECT_DOMAIN_NAME#${OS_PROJECT_DOMAIN_NAME}#g; s#USER_DOMAIN_NAME#${OS_USER_DOMAIN_NAME}#g; s#REGION_NAME#${OS_REGION_NAME}#g" /opt/res-files/clouds.yaml
6
Alexa4437742022-02-16 14:42:38 -06007# local vars
8name_prefix=cvp
9filename=${name_prefix}.manifest
10rcfile=${name_prefix}rc
11huge_pages=false
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +020012set_gw_heat_router=false
Alexa4437742022-02-16 14:42:38 -060013logfile=prepare.log
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +020014working_folder=$(pwd)
15
16# Cloud admin details
17admin_username=${OS_USERNAME}
Alexa4437742022-02-16 14:42:38 -060018
19# Project, User, Roles
20project=${name_prefix}.project
21user=${name_prefix}.user
22admin=${name_prefix}.admin
23password=mcp1234
Ievgeniia Zadorozhna24e7e852024-08-06 17:32:44 +020024domain=${OS_PROJECT_DOMAIN_NAME}
Alexa4437742022-02-16 14:42:38 -060025
26# Security group
27sg_all=${name_prefix}.sg.all
28sg_icmp=${name_prefix}.sg.icmp
29sg_ssh=${name_prefix}.sg.ssh
30sg_iperf=${name_prefix}.sg.perf
31
32# Testkey
33key=${name_prefix}_testkey
34
35# Flavors: tiny, small (cirrus and migration), medium (ubuntu and volume/stress activities)
36flavor_t=${name_prefix}.tiny
37flavor_s=${name_prefix}.small
38flavor_m=${name_prefix}.medium
39flavor_h=${name_prefix}.high
40
41# Fixed Networks (2, for testing router interconnection)
42net_left=${name_prefix}.net.1
43net_right=${name_prefix}.net.2
44subnet1=${name_prefix}.subnet.1
45subnet2=${name_prefix}.subnet.2
46
47# Router
48router=${name_prefix}.router
49
Ievgeniia Zadorozhna1cb5b102024-01-19 04:31:09 +010050# Images: cirros (6.0, 6.2), ubuntu (16.04, 20.04)
51cirros61=${name_prefix}.cirros.61
52cirros62=${name_prefix}.cirros.62
Alexa4437742022-02-16 14:42:38 -060053ubuntu16=${name_prefix}.ubuntu.1604
Alexdb7786b2022-02-21 17:58:29 -060054ubuntu20=${name_prefix}.ubuntu.2004
Alexa4437742022-02-16 14:42:38 -060055
Ievgeniia Zadorozhna1cb5b102024-01-19 04:31:09 +010056cirros61_link=https://download.cirros-cloud.net/0.6.1/cirros-0.6.1-x86_64-disk.img
57cirros62_link=https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img
Alexa4437742022-02-16 14:42:38 -060058ubuntu16_link=https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img
Alexdb7786b2022-02-21 17:58:29 -060059ubuntu20_link=https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
Alexa4437742022-02-16 14:42:38 -060060
61# Volume (2GB)
62volume=${name_prefix}.volume
63
64function show_help {
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +020065 printf "QA verification: Resources creation script\n\t-h, -?\t\tShow this help\n"
Alexa4437742022-02-16 14:42:38 -060066 printf "\t-H\t\tAdds '--property hw:mem_page_size=large' to flavors, i.e. huge_pages for DPDK\n"
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +020067 printf "\t-w <path>\tSets working folder (default: ${working_folder})\n"
68 printf "\t-g\t\tTo set external_gateway_info to heat-router with the external network (if not set yet)\n"
Alexa4437742022-02-16 14:42:38 -060069}
70
71OPTIND=1 # Reset in case getopts has been used previously in the shell.
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +020072while getopts ":gHw:h?" opt; do
Alexa4437742022-02-16 14:42:38 -060073 case "$opt" in
74 h|\?)
75 show_help
76 exit 0
77 ;;
78 w) working_folder=${OPTARG}
79 printf "# Working folder is ${working_folder}\n"
80 ;;
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +020081 H) huge_pages=true
Alexa4437742022-02-16 14:42:38 -060082 printf "# Using 'huge_pages' property in flavors\n"
83 ;;
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +020084 g) set_gw_heat_router=true
85 printf "# Setting external_gateway_info to heat-router with the external network (if not set yet)\n\n"
86 ;;
Alexa4437742022-02-16 14:42:38 -060087 esac
88done
89
90shift $((OPTIND-1))
91[ "${1:-}" = "--" ] && shift
92
93function put() {
94 echo "$1=$2" | tee -a ${filename}
95}
96
97# now, some hard to understand stuff...
98# f1 $(<command with output to cut>)
99function f1() { echo $1 | cut -d' ' -f1; };
100# <commands with output to cut> | p1
101function p1() { while read input; do echo ${input} | cut -d' ' -f1; done; };
102# ol1 is short for openstack list with 1 param. Also grep and cut
103# "ol1 network public" will list all networks, grep by name public and return IDs
104function ol1() { echo $(openstack $1 list -c ID -c Name -f value | grep $2 | cut -d' ' -f1); }
105# same as ol1 but with 2 initial commands before list
106function ol2() { echo $(openstack $1 $2 list -c ID -c Name -f value | grep $3 | cut -d' ' -f1); }
107
108function print_manifest() {
109 touch ./${filename}
110 truncate -s 0 ${filename}
111 printf "\n\n# Checking and filling manifest: $(pwd)/${filename}\n"
112 put project_name ${project}
113 put project_id $(ol1 project ${project})
114 put user_name ${user}
115 put user_id $(ol1 user ${user})
116 put admin_name ${admin}
117 put admin_id $(ol1 user ${admin})
118 # sg
119 put secgroup_all_name ${sg_all}
120 put secgroup_all_id $(ol2 security group ${sg_all})
121 put secgroup_icmp_name ${sg_icmp}
122 put secgroup_icmp_id $(ol2 security group ${sg_icmp})
123 put secgroup_ssh_name ${sg_ssh}
124 put secgroup_ssh_id $(ol2 security group ${sg_ssh})
125 put secgroup_iperf_name ${sg_iperf}
126 put secgroup_iperf_id $(ol2 security group ${sg_iperf})
127
128 # keypair
129 put keypair_name ${key}
130 put keypair_id $(ol1 keypair ${key})
131
132 # flavors
133 put flavor_tiny_name ${flavor_t}
134 put flavor_tiny_id $(ol1 flavor ${flavor_t})
135 put flavor_small_name ${flavor_s}
136 put flavor_small_id $(ol1 flavor ${flavor_s})
137 put flavor_medium_name ${flavor_m}
138 put flavor_medium_id $(ol1 flavor ${flavor_m})
139 put flavor_high_name ${flavor_h}
140 put flavor_high_id $(ol1 flavor ${flavor_h})
141
142 # fixed nets
143 put fixed_net_left_name ${net_left}
144 put fixed_net_left_id $(ol1 network ${net_left})
145 put fixed_net_right_name ${net_right}
146 put fixed_net_right_id $(ol1 network ${net_right})
147 put fixed_net_left_subnet_name ${subnet1}
148 put fixed_net_left_subnet_id $(openstack subnet list --network ${net_left} -c ID -f value | p1)
149 put fixed_net_right_subnet_name ${subnet2}
150 put fixed_net_right_subnet_id $(openstack subnet list --network ${net_right} -c ID -f value | p1)
151
152 # router
153 put router_name ${router}
154 put router_id $(ol1 router ${router})
155
156 # volumes
157 put volume_name ${volume}
158 put volume_id $(ol1 volume ${volume})
159
160 # images
Ievgeniia Zadorozhna1cb5b102024-01-19 04:31:09 +0100161 put cirros61_name ${cirros61}
162 put cirros61_id $(ol1 image ${cirros61})
163 put cirros62_name ${cirros62}
164 put cirros62_id $(ol1 image ${cirros62})
Alexa4437742022-02-16 14:42:38 -0600165 put ubuntu16_name ${ubuntu16}
166 put ubuntu16_id $(ol1 image ${ubuntu16})
Alexdb7786b2022-02-21 17:58:29 -0600167 put ubuntu20_name ${ubuntu20}
168 put ubuntu20_id $(ol1 image ${ubuntu20})
Alexa4437742022-02-16 14:42:38 -0600169}
170
171# create rc file out of current ENV vars
172function putrc() {
173 printf "# Saving ${1} file\n"
174 echo "export OS_IDENTITY_API_VERSION=${OS_IDENTITY_API_VERSION:-3}" >${1}
175 echo "export OS_AUTH_URL=${OS_AUTH_URL}" >>${1}
176 echo "export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME}" >>${1}
177 echo "export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME}" >>${1}
178 echo "export OS_PROJECT_NAME=${OS_PROJECT_NAME}" >>${1}
179 echo "export OS_TENANT_NAME=${OS_TENANT_NAME}" >>${1}
180 echo "export OS_USERNAME=${OS_USERNAME}" >>${1}
181 echo "export OS_PASSWORD=${OS_PASSWORD}" >>${1}
182 echo "export OS_REGION_NAME=${OS_REGION_NAME}" >>${1}
183 echo "export OS_INTERFACE=${OS_INTERFACE}" >>${1}
184 echo "export OS_ENDPOINT_TYPE=${OS_ENDPOINT_TYPE}" >>${1}
185 echo "export OS_CACERT=${OS_CACERT}" >>${1}
186}
187
188# update ENV vars to newly created project
189function updatesession() {
190 export OS_PROJECT_NAME=${project}
191 export OS_TENANT_NAME=${project}
192 export OS_USERNAME=${admin}
193 export OS_PASSWORD=${password}
194}
195
196function process_cmds() {
197 if [ -s ${cmds} ]; then
198 cat ${cmds} | tr '\n' '\0' | xargs -P 1 -n 1 -0 echo | tee /dev/tty | openstack -v 2>&1 >>${logfile}
199 truncate -s 0 ${cmds}
200 fi
201}
202
203function _project() {
204 echo project create ${project} >>${cmds}
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +0200205 # admin_username=$(openstack user list --project admin --domain ${domain} -c Name -f value | grep admin)
206 # user admin_username from the initial env vars of the pod
Ievgeniia Zadorozhnaa76c8522023-08-03 17:00:54 +0300207 echo role add --user ${admin_username} --project ${project} admin >>${cmds}
Alexa4437742022-02-16 14:42:38 -0600208}
209
210function _users() {
211 echo user create --project ${project} --password ${password} ${user} >>${cmds}
212 echo user create --project ${project} --password ${password} ${admin} >>${cmds}
213 echo role add --user ${admin} --project ${project} admin >>${cmds}
214 echo role add --user ${admin} --project ${project} creator >>${cmds}
215 echo role add --user ${user} --project ${project} member >>${cmds}
216 echo role add --user ${user} --project ${project} creator >>${cmds}
Ievgeniia Zadorozhna5452a372023-07-10 20:54:13 +0300217 echo role add --user ${user} --project ${project} load-balancer_member >>${cmds}
Alexa4437742022-02-16 14:42:38 -0600218
219}
220
221function _sg_all() {
222 echo security group create --project ${project} ${sg_all} >>${cmds}
223 # icmp
224 echo security group rule create --protocol icmp ${sg_all} >>${cmds}
225 # ssh
226 echo security group rule create --protocol tcp --dst-port 22 ${sg_all} >>${cmds}
227 # iperf
228 echo security group rule create --protocol tcp --dst-port 5001 ${sg_all} >>${cmds}
229 # iperf3
230 echo security group rule create --protocol tcp --dst-port 5201 ${sg_all} >>${cmds}
231 # nc connectivity
232 echo security group rule create --protocol tcp --dst-port 3000 ${sg_all} >>${cmds}
233 # http
234 echo security group rule create --protocol tcp --dst-port 80 ${sg_all} >>${cmds}
235 # https
236 echo security group rule create --protocol tcp --dst-port 443 ${sg_all} >>${cmds}
237}
238
239function _sg_icmp() {
240 echo security group create --project ${project} ${sg_icmp} >>${cmds}
241 echo security group rule create --protocol icmp ${sg_icmp} >>${cmds}
242}
243
244function _sg_ssh() {
245 echo security group create --project ${project} ${sg_ssh} >>${cmds}
246 # icmp
247 echo security group rule create --protocol icmp ${sg_ssh} >>${cmds}
248 # ssh
249 echo security group rule create --protocol tcp --dst-port 22 ${sg_ssh} >>${cmds}
250}
251
252function _sg_iperf() {
253 echo security group create --project ${project} ${sg_iperf} >>${cmds}
254 # icmp
255 echo security group rule create --protocol icmp ${sg_iperf} >>${cmds}
256 # iperf
257 echo security group rule create --protocol tcp --dst-port 5001 ${sg_iperf} >>${cmds}
258 # iperf3
259 echo security group rule create --protocol tcp --dst-port 5201 ${sg_iperf} >>${cmds}
260}
261
262function create_keypair() {
263 echo "# Creating keypair"
264 openstack keypair create ${key} >${key}
265 chmod 600 ${key}
266 echo "-> created keyfile: $(pwd)/${key}"
267}
268
269function _flavors() {
270 # huge paged flavors
271 if [ "$huge_pages" = true ]; then
Alexc7f187c2022-04-28 10:02:27 -0500272 echo flavor create --id 1 --ram 256 --disk 5 --vcpus 1 ${flavor_t} --property hw:mem_page_size=large >>${cmds}
273 echo flavor create --id 2 --ram 512 --disk 10 --vcpus 2 ${flavor_s} --property hw:mem_page_size=large >>${cmds}
274 echo flavor create --id 3 --ram 2048 --disk 20 --vcpus 4 ${flavor_m} --property hw:mem_page_size=large >>${cmds}
275 echo flavor create --id 4 --ram 4096 --disk 30 --vcpus 6 ${flavor_h} --property hw:mem_page_size=large >>${cmds}
Alexa4437742022-02-16 14:42:38 -0600276 else
Alexc7f187c2022-04-28 10:02:27 -0500277 echo flavor create --id 1 --ram 256 --disk 5 --vcpus 1 ${flavor_t} >>${cmds}
278 echo flavor create --id 2 --ram 512 --disk 10 --vcpus 2 ${flavor_s} >>${cmds}
279 echo flavor create --id 3 --ram 2048 --disk 20 --vcpus 4 ${flavor_m} >>${cmds}
280 echo flavor create --id 4 --ram 4096 --disk 30 --vcpus 6 ${flavor_h} >>${cmds}
Alexa4437742022-02-16 14:42:38 -0600281 fi
282}
283
284function _volumes() {
285 echo volume create --size 2 ${volume} >>${cmds}
286}
287
288function create_fixed_nets() {
289 echo "# Creating fixed networks"
290 echo network create --project ${project} ${net_left} >>${cmds}
291 echo subnet create ${subnet1} --network ${net_left} --subnet-range 10.10.11.0/24 >>${cmds}
292 echo network set --share ${net_left} >>${cmds}
293 echo network create --project ${project} ${net_right} >>${cmds}
294 echo subnet create ${subnet2} --network ${net_right} --subnet-range 10.10.12.0/24 >>${cmds}
295 echo network set --share ${net_right} >>${cmds}
296 process_cmds
297
298 # get subnet ids
299 subnet1_id=$(openstack subnet list --network ${net_left} -c ID -f value)
300 subnet2_id=$(openstack subnet list --network ${net_right} -c ID -f value)
301
302 echo router create --project ${project} ${router} >>${cmds}
303 process_cmds
304
305 router_id=$(openstack router list -c ID -c Name -f value | grep ${router} | cut -d' ' -f1)
306 echo router add subnet ${router_id} ${subnet1_id} >>${cmds}
307 echo router add subnet ${router_id} ${subnet2_id} >>${cmds}
308 process_cmds
309
Ievgeniia Zadorozhnadbf166a2022-03-09 18:52:36 +0300310 # get external network name
Ievgeniia Zadorozhna7bc54052024-02-20 00:15:35 +0100311 if [ -n "${CUSTOM_PUBLIC_NET_NAME:-}" ]; then
312 # if CUSTOM_PUBLIC_NET_NAME is set to some specific net, check it is present on the cloud and use it
313 echo "# Checking that the external network ${CUSTOM_PUBLIC_NET_NAME} is present on the cloud"
314 network_exists=$(openstack network show "$CUSTOM_PUBLIC_NET_NAME" -c id -f value 2>/dev/null)
315 if [ -n "$network_exists" ]; then
Ievgeniia Zadorozhnac21d5522024-07-26 18:42:49 +0200316 TEST_PUBLIC_NET=${CUSTOM_PUBLIC_NET_NAME}
Ievgeniia Zadorozhna7bc54052024-02-20 00:15:35 +0100317 echo router set ${router} --external-gateway ${CUSTOM_PUBLIC_NET_NAME} >>${cmds}
318 process_cmds
319 else
320 echo "# The network ${CUSTOM_PUBLIC_NET_NAME} does not exist"
321 CUSTOM_PUBLIC_NET_NAME=""
322 fi
323 fi
324 if [ -z "${CUSTOM_PUBLIC_NET_NAME:-}" ]; then
325 echo "# Selecting a random external network as an external gateway for the router"
326 # if the custom network is not set or is empty, select the first external network
327 external=$(openstack network list --external -c Name -f value | head -n1)
Ievgeniia Zadorozhnac21d5522024-07-26 18:42:49 +0200328 TEST_PUBLIC_NET=${external}
Ievgeniia Zadorozhna7bc54052024-02-20 00:15:35 +0100329 echo router set ${router} --external-gateway ${external} >>${cmds}
330 process_cmds
331 fi
Ievgeniia Zadorozhnac21d5522024-07-26 18:42:49 +0200332
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +0200333 if [ "$set_gw_heat_router" = true ]; then
334 echo "# Variable 'set_gw_heat_router' is true; setting the external gateway info for heat-router (if not set yet)..."
335 # set external gateway info for the Heat router if it is not set (required for Heat Tempest tests)
336 external_gateway_info=$(openstack router show heat-router -f json -c external_gateway_info | jq -r '.external_gateway_info')
337 if [[ "$external_gateway_info" == "null" ]]; then
338 echo "# Setting external gw info for heat-router using ${TEST_PUBLIC_NET}"
339 openstack router set --external-gateway ${TEST_PUBLIC_NET} heat-router
340 if [[ $? -eq 0 ]]; then
341 echo "# External gateway set successfully for heat-router"
342 openstack router show heat-router -c external_gateway_info
343 else
344 echo "# Failed to set external gateway for heat-router"
345 fi
Ievgeniia Zadorozhnac21d5522024-07-26 18:42:49 +0200346 else
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +0200347 echo "# Router heat-router already has an external gateway"
348 openstack router show heat-router -c external_gateway_info
Ievgeniia Zadorozhnac21d5522024-07-26 18:42:49 +0200349 fi
350 else
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +0200351 echo "# Variable 'set_gw_heat_router' is not true, skipping setting external_gateway_info for heat-router..."
Ievgeniia Zadorozhnac21d5522024-07-26 18:42:49 +0200352 fi
Alexa4437742022-02-16 14:42:38 -0600353}
354
355function _get_image() {
356 # build vars for name and link
357 name="${1}"
358 link="${1}_link"
359 which wget >/dev/null
360 if [ $? -ne 0 ]; then
361 printf "\nERROR: 'wget' not detected. Download skipped: ${!name}\n"
362 else
363 # no redownloads, quet, save named and show progress
364 r=$(wget --no-check-certificate -nc -q -O ./${!name} --show-progress ${!link})
365 if [ $? -ne 0 ]; then
366 # non-empty output on error
367 echo ${r}
368 fi
369 fi
370}
371
372function create_image() {
373 name="${1}"
374 # Check if image is in the cloud
375 echo "# Checking image '${!name}'"
376 ids=( $(ol1 image ${!name}) )
377 # if array is empty, download and upload it
378 if [ ${#ids[@]} -eq 0 ]; then
379 # check and download
380 if [ ! -f ${!name} ]; then
381 r=$(_get_image ${1})
382 else
383 r=""
384 fi
385 # check if output is not empty
386 if [ ${#r} -eq 0 ]; then
387 image_id=$(openstack image create --public --disk-format qcow2 --container-format bare --file ${!name} ${!name} -c id -f value)
388 echo "-> created ${!name} (${image_id})"
389 else
390 printf "\n-> Error detected, creation skipped\n"
391 fi
392 else
393 # image(s) already there, list them
394 for id in ${ids[@]}; do
395 echo "-> found ${!name} with ID of '${id}'"
396 done
397 fi
398}
399
400###################
401### Main
402###################
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +0200403echo "Using working folder: ${working_folder}"
Alexa4437742022-02-16 14:42:38 -0600404if [[ -z ${working_folder+x} ]]; then
405 # cwd into working dir
406 cd ${working_folder}
407fi
408
409cmds=$(mktemp)
410trap "rm -f ${cmds}" EXIT
411echo "Using tempfile: '${cmds}'"
412
413touch ${logfile}
414echo "Using log file: '${logfile}'"
415
416# Create
417echo "# Creating project and users"
418_project
419_users
420process_cmds
421
422echo "# Creating 'rc' and switching"
423putrc "./adminrc"
424updatesession
425putrc "./${rcfile}"
426
427echo "# Creating basic resources"
428# not dependent stuff
429_sg_all
430_sg_icmp
431_sg_ssh
432_sg_iperf
433_flavors
434_volumes
435process_cmds
436
437# sophisticated, step dependent stuff
438create_keypair
439create_fixed_nets
440
441# images
Ievgeniia Zadorozhna1cb5b102024-01-19 04:31:09 +0100442create_image cirros61
443create_image cirros62
Alexdb7786b2022-02-21 17:58:29 -0600444create_image ubuntu16
445create_image ubuntu20
Alexa4437742022-02-16 14:42:38 -0600446
447### Manifest and fall back to original rc
448print_manifest
449printf ="\n\nSetting quota\n"
Ievgeniia Zadorozhnacf3140b2024-09-18 15:11:20 +0200450openstack quota set --cores -1 --ram -1 --instances -1 --volumes -1 --gigabytes -1 --server-groups -1 --server-group-members -1 cvp.project
Alexa4437742022-02-16 14:42:38 -0600451source "./adminrc"
452printf "\n\nOriginal rc preserved and backed up in 'adminrc'\nNew rc is '${rcfile}'\n"