blob: b79fec7555f04ff621d597168b548d618a12041f [file] [log] [blame]
Alex Savatieievec703692019-06-07 15:33:31 -05001#!/bin/bash
2export OS_INTERFACE='admin'
3
4# local vars
5name_prefix=cvp
6filename=${name_prefix}.manifest
Alex Savatieiev2ddfe162019-07-02 12:05:51 -05007rcfile=${name_prefix}rc
Alex Savatieievec703692019-06-07 15:33:31 -05008huge_pages=false
Alex Savatieiev2ddfe162019-07-02 12:05:51 -05009logfile=prepare.log
Alex Savatieievec703692019-06-07 15:33:31 -050010
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
Alex6892c8c2021-01-28 16:07:29 -060030flavor_h=${name_prefix}.high
Alex Savatieievec703692019-06-07 15:33:31 -050031
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
41# Images: cirros (3.5, 4.0), ubuntu (16.04)
42cirros3=${name_prefix}.cirros.35
43cirros4=${name_prefix}.cirros.40
44ubuntu16=${name_prefix}.ubuntu.1604
Alex6892c8c2021-01-28 16:07:29 -060045ubuntuspt=${name_prefix}.ubuntu.spt
Alex Savatieievec703692019-06-07 15:33:31 -050046
47cirros3_link=http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img
48cirros4_link=http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-aarch64-disk.img
49ubuntu16_link=https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img
Alex6892c8c2021-01-28 16:07:29 -060050ubuntuspt_link=ubuntuspt
Alex Savatieievec703692019-06-07 15:33:31 -050051
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}
Alex Savatieiev2ddfe162019-07-02 12:05:51 -050098 printf "\n\n# Checking and filling manifest: $(pwd)/${filename}\n"
99 put project_name ${project}
Alex Savatieievec703692019-06-07 15:33:31 -0500100 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})
Alex6892c8c2021-01-28 16:07:29 -0600126 put flavor_high_name ${flavor_h}
127 put flavor_high_id $(ol1 flavor ${flavor_h})
Alex Savatieievec703692019-06-07 15:33:31 -0500128
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
148 put cirros35_name ${cirros3}
149 put cirros35_id $(ol1 image ${cirros3})
150 put cirros40_name ${cirros4}
151 put cirros40_id $(ol1 image ${cirros4})
Alex6892c8c2021-01-28 16:07:29 -0600152 put ubuntu16_name ${ubuntu16}
Alex Savatieievec703692019-06-07 15:33:31 -0500153 put ubuntu16_id $(ol1 image ${ubuntu16})
Alex6892c8c2021-01-28 16:07:29 -0600154 put ubuntuspt_name ${ubuntuspt}
155 put ubuntuspt_id $(ol1 image ${ubuntuspt})
Alex Savatieievec703692019-06-07 15:33:31 -0500156}
157
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500158# 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
Alex Savatieievec703692019-06-07 15:33:31 -0500183function process_cmds() {
184 if [ -s ${cmds} ]; then
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500185 cat ${cmds} | tr '\n' '\0' | xargs -P 1 -n 1 -0 echo | tee /dev/tty | openstack -v 2>&1 >>${logfile}
Alex Savatieievec703692019-06-07 15:33:31 -0500186 truncate -s 0 ${cmds}
187 fi
188}
189
190function _project() {
Alexd844aa92021-03-19 15:29:40 -0500191 echo project create ${project} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500192 echo role add --user admin --project ${project} admin >>${cmds}
193}
194
195function _users() {
196 echo user create --project ${project} --password ${password} ${user} >>${cmds}
197 echo user create --project ${project} --password ${password} ${admin} >>${cmds}
198 echo role add --user ${admin} --project ${project} admin >>${cmds}
199}
200
201function _sg_all() {
Alexd844aa92021-03-19 15:29:40 -0500202 echo security group create --project ${project} ${sg_all} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500203 # icmp
204 echo security group rule create --protocol icmp ${sg_all} >>${cmds}
205 # ssh
206 echo security group rule create --protocol tcp --dst-port 22 ${sg_all} >>${cmds}
207 # iperf
208 echo security group rule create --protocol tcp --dst-port 5001 ${sg_all} >>${cmds}
209 # iperf3
210 echo security group rule create --protocol tcp --dst-port 5201 ${sg_all} >>${cmds}
211 # nc connectivity
212 echo security group rule create --protocol tcp --dst-port 3000 ${sg_all} >>${cmds}
213 # http
214 echo security group rule create --protocol tcp --dst-port 80 ${sg_all} >>${cmds}
Alex6892c8c2021-01-28 16:07:29 -0600215 # https
216 echo security group rule create --protocol tcp --dst-port 443 ${sg_all} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500217}
218
219function _sg_icmp() {
Alexd844aa92021-03-19 15:29:40 -0500220 echo security group create --project ${project} ${sg_icmp} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500221 echo security group rule create --protocol icmp ${sg_icmp} >>${cmds}
222}
223
224function _sg_ssh() {
Alexd844aa92021-03-19 15:29:40 -0500225 echo security group create --project ${project} ${sg_ssh} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500226 # icmp
227 echo security group rule create --protocol icmp ${sg_ssh} >>${cmds}
228 # ssh
229 echo security group rule create --protocol tcp --dst-port 22 ${sg_ssh} >>${cmds}
230}
231
232function _sg_iperf() {
Alexd844aa92021-03-19 15:29:40 -0500233 echo security group create --project ${project} ${sg_iperf} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500234 # icmp
235 echo security group rule create --protocol icmp ${sg_iperf} >>${cmds}
236 # iperf
237 echo security group rule create --protocol tcp --dst-port 5001 ${sg_iperf} >>${cmds}
238 # iperf3
239 echo security group rule create --protocol tcp --dst-port 5201 ${sg_iperf} >>${cmds}
240}
241
242function create_keypair() {
243 echo "# Creating keypair"
244 openstack keypair create ${key} >${key}
245 chmod 600 ${key}
246 echo "-> created keyfile: $(pwd)/${key}"
247}
248
249function _flavors() {
250 # huge paged flavors
251 if [ "$huge_pages" = true ]; then
252 echo flavor create --id 1 --ram 64 --disk 1 --vcpus 1 ${flavor_t} --property hw:mem_page_size=large >>${cmds}
Alex6892c8c2021-01-28 16:07:29 -0600253 echo flavor create --id 2 --ram 256 --disk 2 --vcpus 1 ${flavor_s} --property hw:mem_page_size=large >>${cmds}
254 echo flavor create --id 3 --ram 2048 --disk 10 --vcpus 2 ${flavor_m} --property hw:mem_page_size=large >>${cmds}
255 echo flavor create --id 4 --ram 2048 --disk 20 --vcpus 4 ${flavor_h} --property hw:mem_page_size=large >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500256 else
Alex6892c8c2021-01-28 16:07:29 -0600257 echo flavor create --id 1 --ram 64 --disk 1 --vcpus 1 ${flavor_t} >>${cmds}
258 echo flavor create --id 2 --ram 256 --disk 2 --vcpus 1 ${flavor_s} >>${cmds}
259 echo flavor create --id 3 --ram 2048 --disk 10 --vcpus 2 ${flavor_m} >>${cmds}
260 echo flavor create --id 4 --ram 2048 --disk 20 --vcpus 4 ${flavor_h} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500261 fi
262}
263
264function _volumes() {
265 echo volume create --size 2 ${volume} >>${cmds}
266}
267
268function create_fixed_nets() {
269 echo "# Creating fixed networks"
270 echo network create --project ${project} ${net_left} >>${cmds}
271 echo subnet create ${subnet1} --network ${net_left} --subnet-range 10.10.11.0/24 >>${cmds}
272 echo network set --share ${net_left} >>${cmds}
273 echo network create --project ${project} ${net_right} >>${cmds}
274 echo subnet create ${subnet2} --network ${net_right} --subnet-range 10.10.12.0/24 >>${cmds}
275 echo network set --share ${net_right} >>${cmds}
276 process_cmds
277
278 # get subnet ids
279 subnet1_id=$(openstack subnet list --network ${net_left} -c ID -f value)
280 subnet2_id=$(openstack subnet list --network ${net_right} -c ID -f value)
281
282 echo router create --project ${project} ${router} >>${cmds}
283 process_cmds
284
285 router_id=$(openstack router list -c ID -c Name -f value | grep ${router} | cut -d' ' -f1)
286 echo router add subnet ${router_id} ${subnet1_id} >>${cmds}
287 echo router add subnet ${router_id} ${subnet2_id} >>${cmds}
288 process_cmds
289
290 # TODO: Search for external net
Alex6892c8c2021-01-28 16:07:29 -0600291 external=public
Alex Savatieievec703692019-06-07 15:33:31 -0500292 echo router set ${router} --external-gateway ${external} >>${cmds}
293 process_cmds
294}
295
296function _get_image() {
297 # build vars for name and link
298 name="${1}"
299 link="${1}_link"
Alex6892c8c2021-01-28 16:07:29 -0600300 which wget >/dev/null
Alex Savatieievec703692019-06-07 15:33:31 -0500301 if [ $? -ne 0 ]; then
302 printf "\nERROR: 'wget' not detected. Download skipped: ${!name}\n"
303 else
304 # no redownloads, quet, save named and show progress
305 r=$(wget --no-check-certificate -nc -q -O ./${!name} --show-progress ${!link})
306 if [ $? -ne 0 ]; then
307 # non-empty output on error
308 echo ${r}
309 fi
310 fi
311}
312
313function create_image() {
314 name="${1}"
315 # Check if image is in the cloud
316 echo "# Checking image '${!name}'"
317 ids=( $(ol1 image ${!name}) )
Alex6892c8c2021-01-28 16:07:29 -0600318 # if array is empty, download and upload it
Alex Savatieievec703692019-06-07 15:33:31 -0500319 if [ ${#ids[@]} -eq 0 ]; then
320 # check and download
321 if [ ! -f ${!name} ]; then
322 r=$(_get_image ${1})
323 else
324 r=""
325 fi
326 # check if output is not empty
327 if [ ${#r} -eq 0 ]; then
328 image_id=$(openstack image create --public --disk-format qcow2 --container-format bare --file ${!name} ${!name} -c id -f value)
329 echo "-> created ${!name} (${image_id})"
330 else
331 printf "\n-> Error detected, creation skipped\n"
332 fi
333 else
334 # image(s) already there, list them
335 for id in ${ids[@]}; do
336 echo "-> found ${!name} with ID of '${id}'"
337 done
338 fi
339}
340
341###################
342### Main
343###################
344if [[ -z ${working_folder+x} ]]; then
345 # cwd into working dir
346 cd ${working_folder}
347fi
348
349cmds=$(mktemp)
350trap "rm -f ${cmds}" EXIT
351echo "Using tempfile: '${cmds}'"
352
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500353touch ${logfile}
354echo "Using log file: '${logfile}'"
355
356# Create
357echo "# Creating project and users"
Alex Savatieievec703692019-06-07 15:33:31 -0500358_project
359_users
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500360process_cmds
361
362echo "# Creating 'rc' and switching"
363putrc "./adminrc"
364updatesession
365putrc "./${rcfile}"
366
367echo "# Creating basic resources"
368# not dependent stuff
Alex Savatieievec703692019-06-07 15:33:31 -0500369_sg_all
370_sg_icmp
371_sg_ssh
372_sg_iperf
373_flavors
374_volumes
375process_cmds
376
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500377# sophisticated, step dependent stuff
Alex Savatieievec703692019-06-07 15:33:31 -0500378create_keypair
379create_fixed_nets
380
381# images
382create_image cirros3
383create_image cirros4
384create_image ubuntu16
Alex6892c8c2021-01-28 16:07:29 -0600385openstack image create --public --disk-format qcow2 --container-format bare --file ubuntuspt cvp.ubuntu.spt -c id -f value
Alex Savatieievec703692019-06-07 15:33:31 -0500386
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500387### Manifest and fall back to original rc
Alex Savatieievec703692019-06-07 15:33:31 -0500388print_manifest
Alex6892c8c2021-01-28 16:07:29 -0600389printf ="\n\nSetting quota\n"
390openstack quota set --cores -1 --ram -1 --instances -1 --volumes -1 --gigabytes -1 cvp.project
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500391source "./adminrc"
Alex6892c8c2021-01-28 16:07:29 -0600392printf "\n\nOriginal rc preserved and backed up in 'adminrc'\nNew rc is '${rcfile}'\n"