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