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