So far in this series we have looked at creating asset within the
EMOC BUI but the Exalogic 2.0.1 installation also provide the Iaas
cli as an alternative to most of the common functionality
available within EMOC. The IaaS cli interface provides access to
the functions that are available to a user logged into the BUI
with the CloudUser Role.
As such not all functionality is available from the command line interface however having said that the IaaS cli provides all the functionality required to create the Assets within a specific Account (Tenure). Because these action are common and repeatable I decided to wrap the functionality within a simple script that takes a simple input file and creates the Asset.
Following the Script through will show us the required steps needed to create the various Assets within an Account and hence I will work through the various functions within the script below describing the steps.
You will note from the various steps within the script that it is designed to pause between actions allowing the proceeding action to complete. The reason for this is because we could swamp EMOC with a series of actions and may end up with a situation where we are trying to action a Volume attached before the creation of the vServer and Volume have completed.
At this point we now have all the information we need to access the specific named account.
As such not all functionality is available from the command line interface however having said that the IaaS cli provides all the functionality required to create the Assets within a specific Account (Tenure). Because these action are common and repeatable I decided to wrap the functionality within a simple script that takes a simple input file and creates the Asset.
Following the Script through will show us the required steps needed to create the various Assets within an Account and hence I will work through the various functions within the script below describing the steps.
You will note from the various steps within the script that it is designed to pause between actions allowing the proceeding action to complete. The reason for this is because we could swamp EMOC with a series of actions and may end up with a situation where we are trying to action a Volume attached before the creation of the vServer and Volume have completed.
processAssets()
This function simply reads through the passed input file identifying what assets need to be created. An example of the input file can be found below. It can be seen that the input file can be used to create Assets in multiple Accounts during a single run. The order of the entries define the functions that need to be actioned as follows:| Input Command | Iaas Actions | Parameters |
|---|---|---|
| Production:Connect |
|
|
| Production:Upload|ServerTemplate |
|
|
| Production:Create|VirtualNetwork |
|
|
| Production:Create|DistributionGroup |
|
|
| Production:Create|vServer |
|
|
| Production:Create|Volume |
|
|
| Production:Attach|Volume |
|
|
| Production:Disconnect |
|
None |
connectToAccount()
It can be seen from the connectToAccount function that before we can execute any Asset creation we must first connect to the appropriate account. To do this we will need the ID associated with the Account. This can be found by executing the akm-describe-accounts cli command which will return a list of all Accounts and there IDs. Once we have the Account ID we generate and Access key using the akm-create-access-key command and then a keypair with the iaas-create-key-pair command.At this point we now have all the information we need to access the specific named account.
createDistributionGroup()
Here we simply retrive the name of the Distribution Group from the input line and create a group. The size of the group is not specified and will always be 50000.createVServer()
This function simply retrieved the information from the input line and then will create the vServer using the iaas-run-vserver cli command. Reading the function you will notice that it takes the various input names for vServer Type, Template and Networks and converts them into the appropriate IDs. The IaaS cli will not work directly with component names and hence all IDs need to be found.createVolume()
Function that simply takes the Volume name and Size then executes the iaas-create-volume command to create the volume.attachVolume()
Takes the name of the Volume, which we may have just created, and a Volume then identifies the appropriate IDs before assigning the Volume to the vServer with the iaas-attach-volumes-to-vserver.disconnectFromAccount()
Once we have finished connecting to the Account we simply remove the key pair with iaas-delete-key-pair and the access key with akm-delete-access-key although it may be useful to keep this if ssh is required and you do not subsequently modify the sshd information to allow unsecured access. By default the key is required for ssh access when a vServer is created from the command-line.CreateAssets.sh
Usage
usage: ./CreateAssets.sh [-f <Asset Definition File>] [-r] -f <Asset Definition File> (Default is CreateAssets.in) -r Indicates that the ssh keys should be removed -h This message
Script
Download1 #!/bin/bash 2 3 ################################################################################ 4 # 5 # Exalogic EL X2-2 2.0.0.4 (Linux x86-64) Configuration Script. 6 # 7 # HEADER START 8 # 9 # THIS SCRIPT IS PROVIDED ON AN ?AS IS? BASIS, WITHOUT WARRANTY OF ANY KIND, 10 # EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT 11 # THE COVERED SCRIPT IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR 12 # PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 13 # OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE 14 # DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER 15 # CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. 16 # NO USE OF ANY COVERED SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS 17 # DISCLAIMER. 18 # 19 # When distributing this Code, include this HEADER in each file. 20 # If applicable, add the following below this this HEADER, with the fields 21 # enclosed by brackets "[]" replaced with your own identifying information: 22 # Portions Copyright [yyyy] [name of copyright owner] 23 # 24 # HEADER END 25 # 26 # 27 # Copyright 2011 Andrew Hopkinson, Oracle Corporation UK Ltd. 28 # 29 ################################################################################ 30 31 export OCCLI=/opt/sun/occli/bin 32 export IAAS_HOME=/opt/oracle/iaas/cli 33 if [[ "$JAVA_HOME" == "" ]] 34 then 35 export JAVA_HOME=/usr/java/latest 36 echo "JAVA_HOME is not defined using $JAVA_HOME" 37 fi 38 export IAAS_BASE_URL=https://127.0.0.1 39 export BASE_IAAS_ACCESS_KEY_FILE=iaas_access.key 40 export BASE_KEY_NAME=cli.asset.create 41 export BASE_KEY_FILE=iaas_access.pub 42 export RUN_DATE=`date +"%Y%m%d-%H%M"` 43 #CloudUser used to create vServers & Volumes 44 export IAAS_USER=exaprod 45 export IAAS_PASSWORD_FILE=root.pwd 46 export INPUT_FILE=CreateAssets.in 47 48 export ACCOUNTS_FILE=accounts.out 49 export VOLUMES_FILE=volumes.out 50 export DISTGRPS_FILE=distgrp.out 51 export VNETS_FILE=vnets.out 52 export VSERVER_TYPES_FILE=vstype.out 53 export VSERVER_FILE=vserver.out 54 export VSERVER_TEMPLATES=template.out 55 export NETWORK_STATIC_IPS=staticips.out 56 export KEY_PAIRS=keypairs.out 57 58 59 PROCESSING_ACCOUNT="" 60 61 function cleanTempFiles() { 62 rm -f $ACCOUNTS_FILE $VOLUMES_FILE $DISTGRPS_FILE $VNETS_FILE $VSERVER_TYPES_FILE $VSERVER_FILE $VSERVER_TEMPLATES $KEY_PAIRS $IAAS_PASSWORD_FILE $IAAS_ACCESS_KEY_FILE 63 #$KEY_FILE 64 } 65 66 function connectToAccount() { 67 if [[ "$ACCOUNT" != "$PROCESSING_ACCOUNT" ]] 68 then 69 if [[ "" != "$PROCESSING_ACCOUNT" ]] 70 then 71 $IAAS_HOME/bin/iaas-delete-key-pair --key-name $KEY_NAME --access-key-file $IAAS_ACCESS_KEY_FILE 72 $IAAS_HOME/bin/akm-delete-access-key $AK 73 fi 74 # Set run specific key information 75 export IAAS_ACCESS_KEY_FILE=$ACCOUNT"."$RUN_DATE"."$BASE_IAAS_ACCESS_KEY_FILE 76 export KEY_NAME=$ACCOUNT"."$RUN_DATE"."$BASE_KEY_NAME 77 export KEY_FILE=$ACCOUNT"."$RUN_DATE"."$BASE_KEY_FILE 78 echo "IAAS_ACCESS_KEY_FILE=$IAAS_ACCESS_KEY_FILE" 79 echo "KEY_NAME=$KEY_NAME" 80 echo "KEY_FILE=$KEY_FILE" 81 # Save current processing account 82 PROCESSING_ACCOUNT=$ACCOUNT 83 IAAS_USER=$ACCOUNT_USER 84 echo "$ACCOUNT_PASSWORD" > $IAAS_PASSWORD_FILE 85 $IAAS_HOME/bin/akm-describe-accounts --sep "|" > $ACCOUNTS_FILE 86 while read line 87 do 88 ACCOUNT_ID=${line%%|*} 89 line=${line#*|} 90 ACCOUNT_NAME=${line%%|*} 91 # echo "Id = $ACCOUNT_ID" 92 # echo "Name = $ACCOUNT_NAME" 93 if [[ "$ACCOUNT_NAME" == "$ACCOUNT" ]] 94 then 95 echo "Found Account $line" 96 AK=`$IAAS_HOME/bin/akm-create-access-key --account $ACCOUNT_ID --access-key-file $IAAS_ACCESS_KEY_FILE` 97 KEYPAIR=`$IAAS_HOME/bin/iaas-create-key-pair --key-name $KEY_NAME --key-file $KEY_FILE` 98 echo "Connected to $ACCOUNT_NAME" 99 100 #cp $IAAS_ACCESS_KEY_FILE $ACCOUNT_NAME$IAAS_ACCESS_KEY_FILE 101 #cp $KEY_FILE $ACCOUNT_NAME$KEY_FILE 102 break 103 fi 104 done < $ACCOUNTS_FILE 105 fi 106 } 107 108 function disconnectFromAccount() { 109 $IAAS_HOME/bin/iaas-delete-key-pair --key-name $KEY_NAME --access-key-file $IAAS_ACCESS_KEY_FILE 110 $IAAS_HOME/bin/akm-delete-access-key $AK 111 PROCESSING_ACCOUNT="" 112 # Clean Up 113 cleanTempFiles 114 } 115 116 function getDistributionGroups() { 117 $IAAS_HOME/bin/iaas-describe-distribution-groups --sep "|" > $DISTGRPS_FILE 118 } 119 120 function getNetworks() { 121 $IAAS_HOME/bin/iaas-describe-vnets --sep "|" > $VNETS_FILE 122 } 123 124 function getVSTypes() { 125 $IAAS_HOME/bin/iaas-describe-vserver-types --sep "|" > $VSERVER_TYPES_FILE 126 } 127 128 function getTemplates() { 129 $IAAS_HOME/bin/iaas-describe-server-templates --sep "|" > $VSERVER_TEMPLATES 130 } 131 132 function getVolumes() { 133 $IAAS_HOME/bin/iaas-describe-volumes --sep "|" > $VOLUMES_FILE 134 } 135 136 function getVServers() { 137 $IAAS_HOME/bin/iaas-describe-vservers --sep "|" > $VSERVER_FILE 138 } 139 140 function getNetworkStaticIPs() { 141 $IAAS_HOME/bin/iaas-describe-ip-addresses --filters vnet=$NETWORK_ID --sep "|" > $NETWORK_STATIC_IPS 142 } 143 144 ############################################################# 145 ## 146 ## getDistributionGroupId 147 ## ====================== 148 ## 149 ## Get the Distribution Group id based on the supplied name. 150 ## 151 ############################################################# 152 153 function getDistributionGroupId() { 154 while read line 155 do 156 DISTGROUP_ID=${line%%|*} 157 line=${line#*|} 158 NAME=${line%%|*} 159 if [[ "$NAME" == "$DISTGROUP_NAME" ]] 160 then 161 break 162 fi 163 DISTGROUP_ID="" 164 done < $DISTGRPS_FILE 165 } 166 167 ############################################################# 168 ## 169 ## getNetworkId 170 ## ============ 171 ## 172 ## Get the Network id based on the supplied name. 173 ## 174 ############################################################# 175 176 function getNetworkId() { 177 while read line 178 do 179 NETWORK_ID=${line%%|*} 180 line=${line#*|} 181 NAME=${line%%|*} 182 if [[ "$NAME" == "$NETWORK_NAME" ]] 183 then 184 break 185 fi 186 NETWORK_ID="" 187 done < $VNETS_FILE 188 } 189 190 ############################################################# 191 ## 192 ## getIPAddress 193 ## ============ 194 ## 195 ## Get a static IP Address for a given network if an * is 196 ## supplied. If an IP Address is supplied it simple returns 197 ## specified IP. 198 ## 199 ############################################################# 200 201 function getIPAddress() { 202 echo "Checking IP Address $IP_ADDRESS" 203 if [[ "$IP_ADDRESS" == "*" ]] 204 then 205 allocateIPAddress 206 # getFirstAllocatedIPAddress 207 fi 208 echo "Returning IP Address $IP_ADDRESS" 209 } 210 211 ############################################################# 212 ## 213 ## allocateIPAddress 214 ## ================= 215 ## 216 ## Allocate a single IP Address from a specified Network. 217 ## 218 ############################################################# 219 220 function allocateIPAddress() { 221 IP_ADDRESS=`$IAAS_HOME/bin/iaas-allocate-ip-addresses --vnet $NETWORK_ID --num 1` 222 } 223 224 function allocateIPAddresses() { 225 $IAAS_HOME/bin/iaas-allocate-ip-addresses --vnet $NETWORK_ID --num $IP_COUNT 226 } 227 228 ############################################################# 229 ## 230 ## getFirstAllocatedIPAddress 231 ## ========================== 232 ## 233 ## Get the first static IP Address for a given Network Id. 234 ## 235 ############################################################# 236 237 function getFirstAllocatedIPAddress() { 238 getNetworkStaticIPs 239 while read line 240 do 241 IP_ADDRESS=${line%%|*} 242 break 243 done < $NETWORK_STATIC_IPS 244 } 245 246 ############################################################# 247 ## 248 ## getVSTypeId 249 ## =========== 250 ## 251 ## Get the VServer Type id based on the supplied name. 252 ## 253 ############################################################# 254 255 function getVSTypeId() { 256 while read line 257 do 258 VSTYPE_ID=${line%%|*} 259 line=${line#*|} 260 NAME=${line%%|*} 261 if [[ "$VSTYPE_NAME" == "$NAME" ]] 262 then 263 break 264 fi 265 VSTYPE_ID="" 266 done < $VSERVER_TYPES_FILE 267 } 268 269 ############################################################# 270 ## 271 ## getTemplateId 272 ## ============= 273 ## 274 ## Get the Template id based on the supplied name. 275 ## 276 ############################################################# 277 278 function getTemplateId() { 279 while read line 280 do 281 TEMPLATE_ID=${line%%|*} 282 line=${line#*|} 283 NAME=${line%%|*} 284 if [[ "$TEMPLATE_NAME" == "$NAME" ]] 285 then 286 break 287 fi 288 TEMPLATE_ID="" 289 done < $VSERVER_TEMPLATES 290 } 291 292 ############################################################# 293 ## 294 ## getVolumeId 295 ## =========== 296 ## 297 ## Get the Volume id based on the supplied name. 298 ## 299 ############################################################# 300 301 function getVolumeId() { 302 while read line 303 do 304 VOLUME_ID=${line%%|*} 305 line=${line#*|} 306 NAME=${line%%|*} 307 if [[ "$NAME" == "$VOLUME_NAME" ]] 308 then 309 break; 310 fi 311 VOLUME_ID="" 312 done < $VOLUMES_FILE 313 } 314 315 ############################################################# 316 ## 317 ## getVServerId 318 ## ============ 319 ## 320 ## Get the VServer id based on the supplied name. 321 ## 322 ############################################################# 323 324 function getVServerId() { 325 while read line 326 do 327 VSERVER_ID=${line%%|*} 328 line=${line#*|} 329 NAME=${line%%|*} 330 if [[ "$VSERVER_NAME" == "$NAME" ]] 331 then 332 break; 333 fi 334 VSERVER_ID="" 335 done < $VSERVER_FILE 336 } 337 338 function getVServerState() { 339 getVServers 340 while read line 341 do 342 VSERVER_ID=${line%%|*} 343 line=${line#*|} 344 NAME=${line%%|*} 345 line=${line#*|} 346 line=${line#*|} 347 VSERVER_STATE=${line%%|*} 348 if [[ "$VSERVER_NAME" == "$NAME" ]] 349 then 350 break; 351 fi 352 done < $VSERVER_FILE 353 } 354 355 function pauseUntilVServerRunning() { 356 # Wait until the Server is running before creating the next 357 echo "Pausing until vServer is Running" 358 getVServerState 359 while [[ "$VSERVER_STATE" != "RUNNING" ]] 360 do 361 echo "$NAME $VSERVER_STATE" 362 if [[ "$VSERVER_STATE" != "RUNNING" ]] 363 then 364 echo "Sleeping......." 365 sleep 30 366 elif [[ "$VSERVER_STATE" == "FAILED" ]] 367 then 368 #echo "Will Delete $NAME in 5 Minutes....." 369 #sleep 300 370 #deleteVServer 371 #echo "Deleted $NAME waiting 5 Minutes....." 372 #sleep 300 373 break 374 fi 375 getVServerState 376 done 377 echo "$NAME $VSERVER_STATE" 378 # Lets pause for a minute or two 379 echo "Just Chilling......" 380 sleep 30 381 echo "Ahhhhh we're getting there......." 382 sleep 30 383 echo "I'm almost at one with the universe......." 384 sleep 30 385 echo "Bong Reality Check !" 386 } 387 388 function deleteVServer() { 389 $IAAS_HOME/bin/iaas-terminate-vservers --force --vserver-ids $VSERVER_ID 390 } 391 392 function createVServer() { 393 VSERVER_NAME=${ASSET_DETAILS%%|*} 394 ASSET_DETAILS=${ASSET_DETAILS#*|} 395 VSTYPE_NAME=${ASSET_DETAILS%%|*} 396 ASSET_DETAILS=${ASSET_DETAILS#*|} 397 TEMPLATE_NAME=${ASSET_DETAILS%%|*} 398 ASSET_DETAILS=${ASSET_DETAILS#*|} 399 NETWORK_NAMES=${ASSET_DETAILS%%|*} 400 ASSET_DETAILS=${ASSET_DETAILS#*|} 401 IP_ADDRESSES=${ASSET_DETAILS%%|*} 402 ASSET_DETAILS=${ASSET_DETAILS#*|} 403 DISTGROUP_NAME=${ASSET_DETAILS%%|*} 404 # Get Ids associated with names 405 getVSTypeId 406 getTemplateId 407 # Convert Network Names to Ids 408 NETWORK_IDS="" 409 # Validated IPs 410 NETWORK_IPS="" 411 # Reset SSH IP Address it will be used to disable SSH Key 412 SSH_IP_ADDRESS="" 413 while true 414 do 415 # Get ID and add to list 416 NETWORK_NAME=${NETWORK_NAMES%%,*} 417 NETWORK_NAMES=${NETWORK_NAMES#*,} 418 getNetworkId 419 if [[ "$NETWORK_IDS" != "" ]] 420 then 421 NETWORK_IDS="$NETWORK_IDS,$NETWORK_ID" 422 else 423 NETWORK_IDS=$NETWORK_ID 424 fi 425 # Check IPs 426 IP_ADDRESS=${IP_ADDRESSES%%,*} 427 IP_ADDRESSES=${IP_ADDRESSES#*,} 428 getIPAddress 429 if [[ "$NETWORK_IPS" != "" ]] 430 then 431 NETWORK_IPS="$NETWORK_IPS,$IP_ADDRESS" 432 else 433 NETWORK_IPS=$IP_ADDRESS 434 fi 435 # Set the SSH IP to the first IP addres we will assume the server is accessible via this IP 436 if [[ "$SSH_IP_ADDRESS" == "" ]] 437 then 438 SSH_IP_ADDRESS=$IP_ADDRESS 439 elif [[ "$NETWORK_NAME" == "IPoIB-vserver-shared-storage" ]] 440 then 441 # Prefer the IPoIB-vserver-shared-storage if this is used 442 SSH_IP_ADDRESS=$IP_ADDRESS 443 fi 444 # If I've processed all then exit 445 if [[ "$NETWORK_NAME" == "$NETWORK_NAMES" ]] 446 then 447 break 448 fi 449 done 450 getDistributionGroupId 451 452 # Create vServer 453 if [[ "$DISTGROUP_ID" != "" ]] 454 then 455 echo "About to execute : $IAAS_HOME/bin/iaas-run-vserver --name $VSERVER_NAME --key-name $KEY_NAME --vserver-type $VSTYPE_ID --server-template-id $TEMPLATE_ID --vnets $NETWORK_IDS --ip-addresses $NETWORK_IPS --dist-group $DISTGROUP_ID" 456 457 $IAAS_HOME/bin/iaas-run-vserver --name $VSERVER_NAME --key-name $KEY_NAME --vserver-type $VSTYPE_ID --server-template-id $TEMPLATE_ID --vnets $NETWORK_IDS --ip-addresses $NETWORK_IPS --dist-group $DISTGROUP_ID 458 else 459 echo "About to execute : $IAAS_HOME/bin/iaas-run-vserver --name $VSERVER_NAME --key-name $KEY_NAME --vserver-type $VSTYPE_ID --server-template-id $TEMPLATE_ID --vnets $NETWORK_IDS --ip-addresses $NETWORK_IPS" 460 461 $IAAS_HOME/bin/iaas-run-vserver --name $VSERVER_NAME --key-name $KEY_NAME --vserver-type $VSTYPE_ID --server-template-id $TEMPLATE_ID --vnets $NETWORK_IDS --ip-addresses $NETWORK_IPS 462 fi 463 pauseUntilVServerRunning 464 if [[ "$REMOVE_SSH_KEYS" == "true" ]] 465 then 466 removeSshKeyRequirement 467 fi 468 } 469 470 function removeSshKeyRequirement() { 471 SSH_IP_ADDRESS="" 472 for IP in ${NETWORK_IPS//,/ } 473 do 474 SSH_RESULT=$(ssh -i $KEY_FILE root@$IP "hostname") 475 echo "SSH Result $SSH_RESULT" 476 if [[ "$SSH_RESULT" == "$VSERVER_NAME" ]] 477 then 478 echo "$IP Address works for ssh" 479 SSH_IP_ADDRESS=$IP 480 break; 481 else 482 echo "$IP Address does not work for ssh" 483 fi 484 done 485 486 if [[ "$SSH_IP_ADDRESS" != "" ]] 487 then 488 echo "Removing ssh key requirement for $VSERVER_NAME on $SSH_IP_ADDRESS" 489 490 ssh -i $KEY_FILE root@$SSH_IP_ADDRESS "cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig" 491 ssh -i $KEY_FILE root@$SSH_IP_ADDRESS "sed 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config.orig > /etc/ssh/sshd_config" 492 ssh -i $KEY_FILE root@$SSH_IP_ADDRESS "service sshd restart" 493 else 494 echo "Unable to find a route to $VSERVER_NAME to remove the ssh key requirementyou will need to do the following" 495 echo "" 496 echo "1. ssh into the vServer using: ssh -i $KEY_FILE -l root <IP Address>" 497 echo "2. Edit /etc/ssh/sshd_config and replace \"PasswordAuthentication no\" with \"PasswordAuthentication yes\"" 498 echo "3. Restart sshd service: service sshd restart" 499 echo "" 500 echo "cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig" 501 echo "sed 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config.orig > /etc/ssh/sshd_config" 502 echo "service sshd restart" 503 echo "" 504 fi 505 } 506 507 function pauseUntilDistributionGroupCreated() { 508 getDistributionGroups 509 getDistributionGroupId 510 while [[ "$DISTGROUP_ID" == "" ]] 511 do 512 # Lets pause 513 echo "Just Waiting 30 Seconds......" 514 sleep 30 515 getDistributionGroups 516 getDistributionGroupId 517 done 518 } 519 520 function createDistributionGroup() { 521 DISTGROUP_NAME=${ASSET_DETAILS%%|*} 522 ASSET_DETAILS=${ASSET_DETAILS#*|} 523 # Size is never specified 524 # DISTGROUP_SIZE=${ASSET_DETAILS%%|*} 525 # Create Volume 526 echo "About to execute : $IAAS_HOME/bin/iaas-create-distribution-group --name $DISTGROUP_NAME" 527 $IAAS_HOME/bin/iaas-create-distribution-group --name $DISTGROUP_NAME 528 # Lets pause 529 pauseUntilDistributionGroupCreated 530 } 531 532 function pauseUntilVolumeCreated() { 533 getVolumes 534 getVolumeId 535 while [[ "$VOLUME_ID" == "" ]] 536 do 537 # Lets pause 538 echo "Just Waiting 30 Seconds......" 539 sleep 30 540 getVolumes 541 getVolumeId 542 done 543 544 } 545 546 function createVolume() { 547 VOLUME_NAME=${ASSET_DETAILS%%|*} 548 ASSET_DETAILS=${ASSET_DETAILS#*|} 549 VOLUME_SIZE=${ASSET_DETAILS%%|*} 550 # Create Volume 551 echo "About to execute : $IAAS_HOME/bin/iaas-create-volume --name $VOLUME_NAME --size $VOLUME_SIZE" 552 $IAAS_HOME/bin/iaas-create-volume --name $VOLUME_NAME --size $VOLUME_SIZE 553 # Lets pause 554 pauseUntilVolumeCreated 555 } 556 557 function attachVolume() { 558 VSERVER_NAME=${ASSET_DETAILS%%|*} 559 ASSET_DETAILS=${ASSET_DETAILS#*|} 560 VOLUME_NAMES=${ASSET_DETAILS%%|*} 561 # Get vServer Id 562 getVServerId 563 # Convert Volume Names to Ids 564 VOLUME_IDS="" 565 while true 566 do 567 VOLUME_NAME=${VOLUME_NAMES%%,*} 568 VOLUME_NAMES=${VOLUME_NAMES#*,} 569 getVolumeId 570 if [[ "$VOLUME_IDS" != "" ]] 571 then 572 VOLUME_IDS="$VOLUME_IDS,$VOLUME_ID" 573 else 574 VOLUME_IDS=$VOLUME_ID 575 fi 576 if [[ "$VOLUME_NAME" == "$VOLUME_NAMES" ]] 577 then 578 break 579 fi 580 done 581 # Attach Volumes 582 echo "About to execute : $IAAS_HOME/bin/iaas-attach-volumes-to-vserver --vserver-id $VSERVER_ID --volume-ids $VOLUME_IDS" 583 $IAAS_HOME/bin/iaas-attach-volumes-to-vserver --vserver-id $VSERVER_ID --volume-ids $VOLUME_IDS 584 # Lets pause 585 echo "Just Waiting 30 Seconds......" 586 sleep 30 587 } 588 589 ############################################################# 590 ## 591 ## getTemplateState 592 ## ================ 593 ## 594 ## Loop through the Template associated with the Account 595 ## checking to see if the upload has completed and the 596 ## template has a status of OK. At this point return. 597 ## 598 ############################################################# 599 600 function getTemplateState() { 601 getTemplates 602 while read line 603 do 604 TEMPLATE_ID=${line%%|*} 605 line=${line#*|} 606 NAME=${line%%|*} 607 line=${line#*|} 608 line=${line#*|} 609 TEMPLATE_STATE=${line%%|*} 610 if [[ "$TEMPLATE_NAME" == "$NAME" ]] 611 then 612 break; 613 fi 614 done < $VSERVER_TEMPLATES 615 } 616 617 ############################################################# 618 ## 619 ## pauseUntilServerTemplateUploaded 620 ## ================================ 621 ## 622 ## Pause the script until the Template file has been uploaded 623 ## to the Account. 624 ## 625 ############################################################# 626 627 function pauseUntilServerTemplateUploaded() { 628 echo "Pausing until Template upload has completed" 629 getTemplateState 630 while [[ "$TEMPLATE_STATE" != "OK" ]] 631 do 632 echo "$NAME $TEMPLATE_STATE" 633 if [[ "$TEMPLATE_STATE" != "SCHEDULED" ]] 634 then 635 echo "Sleeping......." 636 sleep 30 637 elif [[ "$TEMPLATE_STATE" != "RUNNING" ]] 638 then 639 echo "Sleeping......." 640 sleep 30 641 elif [[ "$TEMPLATE_STATE" != "FAILED" ]] 642 then 643 deleteServerTemplate 644 echo "Sleeping......." 645 sleep 30 646 fi 647 getTemplateState 648 done 649 } 650 651 ############################################################# 652 ## 653 ## uploadServerTemplate 654 ## ==================== 655 ## 656 ## Upload a tgz file that defines a server template. It is 657 ## recommended these be copied to the ZFS first and then the 658 ## appropriate URL from the ZFS be used. 659 ## 660 ############################################################# 661 662 function uploadServerTemplate() { 663 TEMPLATE_NAME=${ASSET_DETAILS%%|*} 664 ASSET_DETAILS=${ASSET_DETAILS#*|} 665 TEMPLATE_URL=${ASSET_DETAILS%%|*} 666 # Upload Template 667 echo "About to execute : $IAAS_HOME/bin/iaas-create-server-template-from-url --name $TEMPLATE_NAME --url $TEMPLATE_URL" 668 $IAAS_HOME/bin/iaas-create-server-template-from-url --name $TEMPLATE_NAME --url $TEMPLATE_URL 669 # Lets pause 670 pauseUntilServerTemplateUploaded 671 } 672 673 function deleteServerTemplate() { 674 $IAAS_HOME/bin/iaas-delete-server-template --force --server-template-id $TEMPLATE_ID 675 } 676 677 ############################################################# 678 ## 679 ## getVNetworkState 680 ## ================ 681 ## 682 ## Loop through the Networks associated with the Account 683 ## checking to see if the creation has completed and the 684 ## network has a status of OK. At this point return. 685 ## 686 ############################################################# 687 688 function getVNetworkState() { 689 getNetworks 690 while read line 691 do 692 NETWORK_ID=${line%%|*} 693 line=${line#*|} 694 NAME=${line%%|*} 695 line=${line#*|} 696 line=${line#*|} 697 NETWORK_STATE=${line%%|*} 698 if [[ "$NETWORK_NAME" == "$NAME" ]] 699 then 700 break; 701 fi 702 done < $VNETS_FILE 703 } 704 705 ############################################################# 706 ## 707 ## pauseUntilVirtualNetworkCreated 708 ## =============================== 709 ## 710 ## Pause the script until the Virtual Private Network has 711 ## been created. 712 ## 713 ############################################################# 714 715 function pauseUntilVirtualNetworkCreated() { 716 echo "Pausing until Virtual Network creation has completed" 717 getVNetworkState 718 while [[ "$NETWORK_STATE" != "OK" ]] 719 do 720 echo "$NAME $NETWORK_STATE" 721 if [[ "$NETWORK_STATE" != "SCHEDULED" ]] 722 then 723 echo "Sleeping......." 724 sleep 30 725 elif [[ "$NETWORK_STATE" != "RUNNING" ]] 726 then 727 echo "Sleeping......." 728 sleep 30 729 fi 730 getVNetworkState 731 done 732 } 733 734 ############################################################# 735 ## 736 ## createVirtualNetwork 737 ## ==================== 738 ## 739 ## Create a Virtual Private Network based on the name 740 ## supplied. 741 ## 742 ############################################################# 743 744 function createVirtualNetwork() { 745 NETWORK_NAME=${ASSET_DETAILS%%|*} 746 ASSET_DETAILS=${ASSET_DETAILS#*|} 747 NETWORK_IPS=${ASSET_DETAILS%%|*} 748 # 749 echo "About to execute : $IAAS_HOME/bin/iaas-create-vnet --name $NETWORK_NAME --size $NETWORK_IPS " 750 $IAAS_HOME/bin/iaas-create-vnet --name $NETWORK_NAME --size $NETWORK_IPS 751 # Lets pause 752 pauseUntilVirtualNetworkCreated 753 } 754 755 ############################################################# 756 ## 757 ## processAssets 758 ## ============= 759 ## 760 ## This function loops through the information defined in 761 ## the input file looking for actions to be executed. It will 762 ## process the entries sequentially and simply call the 763 ## appropriate sub-function to execute the iaas commands. 764 ## Entries with invalid Actions will simply be ignored along 765 ## with blank lines. 766 ## 767 ############################################################# 768 769 function processAssets() { 770 while read line 771 do 772 ACCOUNT=${line%%:*} 773 line=${line#*:} 774 ACTION=${line%%|*} 775 line=${line#*|} 776 if [[ "$ACTION" == "Connect" ]] 777 then 778 ACCOUNT_USER=${line%%|*} 779 line=${line#*|} 780 ACCOUNT_PASSWORD=${line%%|*} 781 connectToAccount 782 783 ## Account Info 784 getNetworks 785 getVSTypes 786 getTemplates 787 elif [[ "$ACTION" == "Create" ]] 788 then 789 ASSET=${line%%|*} 790 line=${line#*|} 791 ASSET_DETAILS=$line 792 if [[ "$ASSET" == "vServer" ]] 793 then 794 getDistributionGroups 795 createVServer 796 elif [[ "$ASSET" == "vServers" ]] 797 then 798 getDistributionGroups 799 createVServers 800 elif [[ "$ASSET" == "Volume" ]] 801 then 802 createVolume 803 elif [[ "$ASSET" == "DistributionGroup" ]] 804 then 805 createDistributionGroup 806 elif [[ "$ASSET" == "VirtualNetwork" ]] 807 then 808 createVirtualNetwork 809 fi 810 # continue 811 elif [[ "$ACTION" == "Upload" ]] 812 then 813 ASSET=${line%%|*} 814 line=${line#*|} 815 ASSET_DETAILS=$line 816 if [[ "$ASSET" == "ServerTemplate" ]] 817 then 818 uploadServerTemplate 819 fi 820 # continue 821 elif [[ "$ACTION" == "Attach" ]] 822 then 823 ASSET=${line%%|*} 824 line=${line#*|} 825 ASSET_DETAILS=$line 826 if [[ "$ASSET" == "Volume" ]] 827 then 828 getVolumes 829 getVServers 830 attachVolume 831 fi 832 # continue 833 elif [[ "$ACTION" == "Disconnect" ]] 834 then 835 disconnectFromAccount 836 # continue 837 fi 838 done < $INPUT_FILE 839 } 840 841 function usage() { 842 echo "" 843 echo >&2 "usage: $0 [-f <Asset Definition File>] [-r]" 844 echo >&2 "" 845 echo >&2 " -f <Asset Definition File> (Default is CreateAssets.in)" 846 echo >&2 " -r Indicates that the ssh keys should be removed" 847 echo "" 848 exit 1 849 } 850 851 ############################################################### 852 ## 853 ## Simple start for the script that will extract the parameters 854 ## and call the appriate start function. 855 ## 856 ############################################################### 857 858 REMOVE_SSH_KEYS=false 859 while [ $# -gt 0 ] 860 do 861 case "$1" in 862 -a) INPUT_FILE="$2"; shift;; 863 -r) REMOVE_SSH_KEYS=true;; 864 *) usage;; 865 *) break;; 866 esac 867 shift 868 done 869 870 # Processing function call 871 processAssets 872 873 echo "**************************************" 874 echo "***** Finished Creating Assets *****" 875 echo "**************************************" 876
CreateAssetsProd.in
Production:Connect|clouduser|welcome1 Production:Upload|ServerTemplate|Navstar40GBRootTemplate|http://172.17.0.9/shares/export/common/images/ah-templates/el_40gb_root_linux_vm_template_2.0.4.0.0_64.tgz Production:Create|VirtualNetwork|VN001|96 Production:Create|DistributionGroup|DG001 Production:Create|vServer|VS006|VSTProduction|Navstar40GBRootTemplate|EoIB1-client-access,IPoIB-vserver-shared-storage,VN001|10.242.96.69,172.17.0.34,*|DG001 Production:Create|vServer|VS007|VSTProduction|Navstar40GBRootTemplate|EoIB1-client-access,IPoIB-vserver-shared-storage,VN001|10.242.96.68,172.17.0.35,*|DG001 Production:Create|vServer|VS002|VSTProduction|Navstar40GBRootTemplate|EoIB1-client-access,IPoIB-vserver-shared-storage|10.242.96.72,* Production:Create|vServer|VS003|VSTProduction|Navstar40GBRootTemplate|EoIB1-client-access,IPoIB-vserver-shared-storage|10.242.96.75,* Production:Create|Volume|VS006|5 Production:Create|Volume|VS007|5 Production:Create|Volume|VS002|5 Production:Create|Volume|VS003|5 Production:Attach|Volume|VS006|VS006 Production:Attach|Volume|VS007|VS007 Production:Attach|Volume|VS002|VS002 Production:Attach|Volume|VS003|VS003 Production:Disconnect
Post Creation
By default the vServers are created, for security, with Public key Authentication enabled. If you want to turn this functionality off you will need to use the "-r" flag. If this fails because the script can not access the vServer from the location where it has been executed then you will need to do this manually as follows. Open a console started from EMOC to modify the sshd_config paramters; as follows:- Open EMOC.
- Right-click on your new vServer and select "Launch Virtual Console" and wait until it opens.
- Edit the /etc/ssh/sshd_config file and set the following values:
- PubkeyAuthentication no
- GSSAPIAuthentication no
- PasswordAuthentication yes
- Restart the sshd using:
/etc/init.d/sshd reload
Andrew, this is great! Quick and easy to use, also saves a heap of time!
ReplyDelete