blob: bde9605294a39bb0816343f6b740c39d5522271a [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
50
51# Volume (2GB)
52volume=${name_prefix}.volume
53
54function show_help {
55 printf "CVP Pipeline: Resource creation script\n\t-h, -?\t\tShow this help\n"
56 printf "\t-H\t\tAdds '--property hw:mem_page_size=large' to flavors, i.e. huge_pages for DPDK\n"
57 printf "\t-w <path>\tSets working folder"
58}
59
60OPTIND=1 # Reset in case getopts has been used previously in the shell.
61while getopts "h?:Hw:" opt; do
62 case "$opt" in
63 h|\?)
64 show_help
65 exit 0
66 ;;
67 w) working_folder=${OPTARG}
68 printf "# Working folder is ${working_folder}\n"
69 ;;
70 h) huge_pages=true
71 printf "# Using 'huge_pages' property in flavors\n"
72 ;;
73 esac
74done
75
76shift $((OPTIND-1))
77[ "${1:-}" = "--" ] && shift
78
79function put() {
80 echo "$1=$2" | tee -a ${filename}
81}
82
83# now, some hard to understand stuff...
84# f1 $(<command with output to cut>)
85function f1() { echo $1 | cut -d' ' -f1; };
86# <commands with output to cut> | p1
87function p1() { while read input; do echo ${input} | cut -d' ' -f1; done; };
88# ol1 is short for openstack list with 1 param. Also grep and cut
89# "ol1 network public" will list all networks, grep by name public and return IDs
90function ol1() { echo $(openstack $1 list -c ID -c Name -f value | grep $2 | cut -d' ' -f1); }
91# same as ol1 but with 2 initial commands before list
92function ol2() { echo $(openstack $1 $2 list -c ID -c Name -f value | grep $3 | cut -d' ' -f1); }
93
94function print_manifest() {
95 touch ./${filename}
96 truncate -s 0 ${filename}
Alex Savatieiev2ddfe162019-07-02 12:05:51 -050097 printf "\n\n# Checking and filling manifest: $(pwd)/${filename}\n"
98 put project_name ${project}
Alex Savatieievec703692019-06-07 15:33:31 -050099 put project_id $(ol1 project ${project})
100 put user_name ${user}
101 put user_id $(ol1 user ${user})
102 put admin_name ${admin}
103 put admin_id $(ol1 user ${admin})
104 # sg
105 put secgroup_all_name ${sg_all}
106 put secgroup_all_id $(ol2 security group ${sg_all})
107 put secgroup_icmp_name ${sg_icmp}
108 put secgroup_icmp_id $(ol2 security group ${sg_icmp})
109 put secgroup_ssh_name ${sg_ssh}
110 put secgroup_ssh_id $(ol2 security group ${sg_ssh})
111 put secgroup_iperf_name ${sg_iperf}
112 put secgroup_iperf_id $(ol2 security group ${sg_iperf})
113
114 # keypair
115 put keypair_name ${key}
116 put keypair_id $(ol1 keypair ${key})
117
118 # flavors
119 put flavor_tiny_name ${flavor_t}
120 put flavor_tiny_id $(ol1 flavor ${flavor_t})
121 put flavor_small_name ${flavor_s}
122 put flavor_small_id $(ol1 flavor ${flavor_s})
123 put flavor_medium_name ${flavor_m}
124 put flavor_medium_id $(ol1 flavor ${flavor_m})
Alex6892c8c2021-01-28 16:07:29 -0600125 put flavor_high_name ${flavor_h}
126 put flavor_high_id $(ol1 flavor ${flavor_h})
Alex Savatieievec703692019-06-07 15:33:31 -0500127
128 # fixed nets
129 put fixed_net_left_name ${net_left}
130 put fixed_net_left_id $(ol1 network ${net_left})
131 put fixed_net_right_name ${net_right}
132 put fixed_net_right_id $(ol1 network ${net_right})
133 put fixed_net_left_subnet_name ${subnet1}
134 put fixed_net_left_subnet_id $(openstack subnet list --network ${net_left} -c ID -f value | p1)
135 put fixed_net_right_subnet_name ${subnet2}
136 put fixed_net_right_subnet_id $(openstack subnet list --network ${net_right} -c ID -f value | p1)
137
138 # router
139 put router_name ${router}
140 put router_id $(ol1 router ${router})
141
142 # volumes
143 put volume_name ${volume}
144 put volume_id $(ol1 volume ${volume})
145
146 # images
147 put cirros35_name ${cirros3}
148 put cirros35_id $(ol1 image ${cirros3})
149 put cirros40_name ${cirros4}
150 put cirros40_id $(ol1 image ${cirros4})
Alex6892c8c2021-01-28 16:07:29 -0600151 put ubuntu16_name ${ubuntu16}
Alex Savatieievec703692019-06-07 15:33:31 -0500152 put ubuntu16_id $(ol1 image ${ubuntu16})
Alex6892c8c2021-01-28 16:07:29 -0600153 put ubuntuspt_name ${ubuntuspt}
154 put ubuntuspt_id $(ol1 image ${ubuntuspt})
Alex Savatieievec703692019-06-07 15:33:31 -0500155}
156
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500157# create rc file out of current ENV vars
158function putrc() {
159 printf "# Saving ${1} file\n"
160 echo "export OS_IDENTITY_API_VERSION=${OS_IDENTITY_API_VERSION:-3}" >${1}
161 echo "export OS_AUTH_URL=${OS_AUTH_URL}" >>${1}
162 echo "export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME}" >>${1}
163 echo "export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME}" >>${1}
164 echo "export OS_PROJECT_NAME=${OS_PROJECT_NAME}" >>${1}
165 echo "export OS_TENANT_NAME=${OS_TENANT_NAME}" >>${1}
166 echo "export OS_USERNAME=${OS_USERNAME}" >>${1}
167 echo "export OS_PASSWORD=${OS_PASSWORD}" >>${1}
168 echo "export OS_REGION_NAME=${OS_REGION_NAME}" >>${1}
169 echo "export OS_INTERFACE=${OS_INTERFACE}" >>${1}
170 echo "export OS_ENDPOINT_TYPE=${OS_ENDPOINT_TYPE}" >>${1}
171 echo "export OS_CACERT=${OS_CACERT}" >>${1}
172}
173
174# update ENV vars to newly created project
175function updatesession() {
176 export OS_PROJECT_NAME=${project}
177 export OS_TENANT_NAME=${project}
178 export OS_USERNAME=${admin}
179 export OS_PASSWORD=${password}
180}
181
Alex Savatieievec703692019-06-07 15:33:31 -0500182function process_cmds() {
183 if [ -s ${cmds} ]; then
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500184 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 -0500185 truncate -s 0 ${cmds}
186 fi
187}
188
189function _project() {
Alexd844aa92021-03-19 15:29:40 -0500190 echo project create ${project} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500191 echo role add --user admin --project ${project} admin >>${cmds}
192}
193
194function _users() {
195 echo user create --project ${project} --password ${password} ${user} >>${cmds}
196 echo user create --project ${project} --password ${password} ${admin} >>${cmds}
197 echo role add --user ${admin} --project ${project} admin >>${cmds}
198}
199
200function _sg_all() {
Alexd844aa92021-03-19 15:29:40 -0500201 echo security group create --project ${project} ${sg_all} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500202 # icmp
203 echo security group rule create --protocol icmp ${sg_all} >>${cmds}
204 # ssh
205 echo security group rule create --protocol tcp --dst-port 22 ${sg_all} >>${cmds}
206 # iperf
207 echo security group rule create --protocol tcp --dst-port 5001 ${sg_all} >>${cmds}
208 # iperf3
209 echo security group rule create --protocol tcp --dst-port 5201 ${sg_all} >>${cmds}
210 # nc connectivity
211 echo security group rule create --protocol tcp --dst-port 3000 ${sg_all} >>${cmds}
212 # http
213 echo security group rule create --protocol tcp --dst-port 80 ${sg_all} >>${cmds}
Alex6892c8c2021-01-28 16:07:29 -0600214 # https
215 echo security group rule create --protocol tcp --dst-port 443 ${sg_all} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500216}
217
218function _sg_icmp() {
Alexd844aa92021-03-19 15:29:40 -0500219 echo security group create --project ${project} ${sg_icmp} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500220 echo security group rule create --protocol icmp ${sg_icmp} >>${cmds}
221}
222
223function _sg_ssh() {
Alexd844aa92021-03-19 15:29:40 -0500224 echo security group create --project ${project} ${sg_ssh} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500225 # icmp
226 echo security group rule create --protocol icmp ${sg_ssh} >>${cmds}
227 # ssh
228 echo security group rule create --protocol tcp --dst-port 22 ${sg_ssh} >>${cmds}
229}
230
231function _sg_iperf() {
Alexd844aa92021-03-19 15:29:40 -0500232 echo security group create --project ${project} ${sg_iperf} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500233 # icmp
234 echo security group rule create --protocol icmp ${sg_iperf} >>${cmds}
235 # iperf
236 echo security group rule create --protocol tcp --dst-port 5001 ${sg_iperf} >>${cmds}
237 # iperf3
238 echo security group rule create --protocol tcp --dst-port 5201 ${sg_iperf} >>${cmds}
239}
240
241function create_keypair() {
242 echo "# Creating keypair"
243 openstack keypair create ${key} >${key}
244 chmod 600 ${key}
245 echo "-> created keyfile: $(pwd)/${key}"
246}
247
248function _flavors() {
249 # huge paged flavors
250 if [ "$huge_pages" = true ]; then
251 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 -0600252 echo flavor create --id 2 --ram 256 --disk 2 --vcpus 1 ${flavor_s} --property hw:mem_page_size=large >>${cmds}
253 echo flavor create --id 3 --ram 2048 --disk 10 --vcpus 2 ${flavor_m} --property hw:mem_page_size=large >>${cmds}
254 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 -0500255 else
Alex6892c8c2021-01-28 16:07:29 -0600256 echo flavor create --id 1 --ram 64 --disk 1 --vcpus 1 ${flavor_t} >>${cmds}
257 echo flavor create --id 2 --ram 256 --disk 2 --vcpus 1 ${flavor_s} >>${cmds}
258 echo flavor create --id 3 --ram 2048 --disk 10 --vcpus 2 ${flavor_m} >>${cmds}
259 echo flavor create --id 4 --ram 2048 --disk 20 --vcpus 4 ${flavor_h} >>${cmds}
Alex Savatieievec703692019-06-07 15:33:31 -0500260 fi
261}
262
263function _volumes() {
264 echo volume create --size 2 ${volume} >>${cmds}
265}
266
267function create_fixed_nets() {
268 echo "# Creating fixed networks"
269 echo network create --project ${project} ${net_left} >>${cmds}
270 echo subnet create ${subnet1} --network ${net_left} --subnet-range 10.10.11.0/24 >>${cmds}
271 echo network set --share ${net_left} >>${cmds}
272 echo network create --project ${project} ${net_right} >>${cmds}
273 echo subnet create ${subnet2} --network ${net_right} --subnet-range 10.10.12.0/24 >>${cmds}
274 echo network set --share ${net_right} >>${cmds}
275 process_cmds
276
277 # get subnet ids
278 subnet1_id=$(openstack subnet list --network ${net_left} -c ID -f value)
279 subnet2_id=$(openstack subnet list --network ${net_right} -c ID -f value)
280
281 echo router create --project ${project} ${router} >>${cmds}
282 process_cmds
283
284 router_id=$(openstack router list -c ID -c Name -f value | grep ${router} | cut -d' ' -f1)
285 echo router add subnet ${router_id} ${subnet1_id} >>${cmds}
286 echo router add subnet ${router_id} ${subnet2_id} >>${cmds}
287 process_cmds
288
289 # TODO: Search for external net
Alex6892c8c2021-01-28 16:07:29 -0600290 external=public
Alex Savatieievec703692019-06-07 15:33:31 -0500291 echo router set ${router} --external-gateway ${external} >>${cmds}
292 process_cmds
293}
294
295function _get_image() {
296 # build vars for name and link
297 name="${1}"
298 link="${1}_link"
Alex6892c8c2021-01-28 16:07:29 -0600299 which wget >/dev/null
Alex Savatieievec703692019-06-07 15:33:31 -0500300 if [ $? -ne 0 ]; then
301 printf "\nERROR: 'wget' not detected. Download skipped: ${!name}\n"
302 else
303 # no redownloads, quet, save named and show progress
304 r=$(wget --no-check-certificate -nc -q -O ./${!name} --show-progress ${!link})
305 if [ $? -ne 0 ]; then
306 # non-empty output on error
307 echo ${r}
308 fi
309 fi
310}
311
312function create_image() {
313 name="${1}"
314 # Check if image is in the cloud
315 echo "# Checking image '${!name}'"
316 ids=( $(ol1 image ${!name}) )
Alex6892c8c2021-01-28 16:07:29 -0600317 # if array is empty, download and upload it
Alex Savatieievec703692019-06-07 15:33:31 -0500318 if [ ${#ids[@]} -eq 0 ]; then
319 # check and download
320 if [ ! -f ${!name} ]; then
321 r=$(_get_image ${1})
322 else
323 r=""
324 fi
325 # check if output is not empty
326 if [ ${#r} -eq 0 ]; then
327 image_id=$(openstack image create --public --disk-format qcow2 --container-format bare --file ${!name} ${!name} -c id -f value)
328 echo "-> created ${!name} (${image_id})"
329 else
330 printf "\n-> Error detected, creation skipped\n"
331 fi
332 else
333 # image(s) already there, list them
334 for id in ${ids[@]}; do
335 echo "-> found ${!name} with ID of '${id}'"
336 done
337 fi
338}
339
340###################
341### Main
342###################
343if [[ -z ${working_folder+x} ]]; then
344 # cwd into working dir
345 cd ${working_folder}
346fi
347
348cmds=$(mktemp)
349trap "rm -f ${cmds}" EXIT
350echo "Using tempfile: '${cmds}'"
351
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500352touch ${logfile}
353echo "Using log file: '${logfile}'"
354
355# Create
356echo "# Creating project and users"
Alex Savatieievec703692019-06-07 15:33:31 -0500357_project
358_users
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500359process_cmds
360
361echo "# Creating 'rc' and switching"
362putrc "./adminrc"
363updatesession
364putrc "./${rcfile}"
365
366echo "# Creating basic resources"
367# not dependent stuff
Alex Savatieievec703692019-06-07 15:33:31 -0500368_sg_all
369_sg_icmp
370_sg_ssh
371_sg_iperf
372_flavors
373_volumes
374process_cmds
375
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500376# sophisticated, step dependent stuff
Alex Savatieievec703692019-06-07 15:33:31 -0500377create_keypair
378create_fixed_nets
379
380# images
381create_image cirros3
382create_image cirros4
383create_image ubuntu16
Ievgeniia Zadorozhna427c4052021-05-20 13:54:53 +0300384# update image name to correct one uploaded
385ubuntuspt_file="ubuntuspt.img"
386openstack image create --public --disk-format qcow2 --container-format bare --file ${ubuntuspt_file} ${ubuntuspt} -c id -f value
Alex Savatieievec703692019-06-07 15:33:31 -0500387
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500388### Manifest and fall back to original rc
Alex Savatieievec703692019-06-07 15:33:31 -0500389print_manifest
Alex6892c8c2021-01-28 16:07:29 -0600390printf ="\n\nSetting quota\n"
391openstack quota set --cores -1 --ram -1 --instances -1 --volumes -1 --gigabytes -1 cvp.project
Alex Savatieiev2ddfe162019-07-02 12:05:51 -0500392source "./adminrc"
Alex6892c8c2021-01-28 16:07:29 -0600393printf "\n\nOriginal rc preserved and backed up in 'adminrc'\nNew rc is '${rcfile}'\n"