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