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