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