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