Friday 21 September 2012

Exalogic Virtual Tea Break Snippets - Scripting Asset Creation

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.

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
  1. akm-describe-accounts
  2. akm-create-access-key
  3. iaas-create-key-pair
  4. iaas-describe-vnets
  5. iaas-describe-vserver-types
  6. iaas-describe-server-templates
  1. Username
  2. Password
Production:Upload|ServerTemplate
  1. iaas-create-server-template-from-url
  1. Template Name
  2. URL to the template (best if placed on the internal ZFS)
Production:Create|VirtualNetwork
  1. iaas-create-vnet
  1. Network Name
  2. Size (Number of IPs)
Production:Create|DistributionGroup
  1. iaas-create-distribution-group
  1. Distribution Group Name
When creating Distribution Groups from the command line they will have a size of 50,000.
Production:Create|vServer
  1. iaas-run-vserver
  1. vServer Name
  2. vServer Type Name
  3. Template Name
  4. Comma separated list of network names which the vServer will connect to.
  5. Comma separated list of IPs for the specified networks. If the IP is to be assigned automaticallty the a "*" should be entered.
  6. Distribution Group [Optional]
Production:Create|Volume
  1. iaas-create-volume
  1. Volume Name
  2. Volume Size
Production:Attach|Volume
  1. iaas-attach-volumes-to-vserver
  1. vServer Name
  2. Comma separated list of volume names
Production:Disconnect
  1. iaas-delete-key-pair
  2. akm-delete-access-key
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

Download

#!/bin/bash

################################################################################
#
#  Exalogic EL X2-2 2.0.0.4 (Linux x86-64) Configuration Script.
#
#  HEADER START
# 
#  THIS SCRIPT IS PROVIDED ON AN AS IS BASIS, WITHOUT WARRANTY OF ANY KIND, 
#  EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT 
#  THE COVERED SCRIPT IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR 
#  PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 
#  OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE 
#  DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER 
#  CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION.
#  NO USE OF ANY COVERED SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS 
#  DISCLAIMER.
#
#  When distributing this Code, include this HEADER in each file.
#  If applicable, add the following below this this HEADER, with the fields
#  enclosed by brackets "[]" replaced with your own identifying information:
#       Portions Copyright [yyyy] [name of copyright owner]
# 
#  HEADER END
# 
# 
#       Copyright 2011 Andrew Hopkinson, Oracle Corporation UK Ltd.
#
################################################################################

export OCCLI=/opt/sun/occli/bin
export IAAS_HOME=/opt/oracle/iaas/cli
export IAAS_BASE_URL=https://127.0.0.1
export BASE_IAAS_ACCESS_KEY_FILE=iaas_access.key
export BASE_KEY_NAME=cli.asset.create
export BASE_KEY_FILE=iaas_access.pub
export RUN_DATE=`date +"%Y%m%d-%H%M"`
#CloudUser used to create vServers & Volumes
export IAAS_USER=exaprod
export IAAS_PASSWORD_FILE=root.pwd
export INPUT_FILE=CreateAssets.in

export ACCOUNTS_FILE=accounts.out
export VOLUMES_FILE=volumes.out
export DISTGRPS_FILE=distgrp.out
export VNETS_FILE=vnets.out
export VSERVER_TYPES_FILE=vstype.out
export VSERVER_FILE=vserver.out
export VSERVER_TEMPLATES=template.out
export NETWORK_STATIC_IPS=staticips.out
export KEY_PAIRS=keypairs.out


PROCESSING_ACCOUNT=""

function cleanTempFiles() {
 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
 #$KEY_FILE
}

function connectToAccount() {
 if [[ "$ACCOUNT" != "$PROCESSING_ACCOUNT" ]]
 then
  if [[ "" != "$PROCESSING_ACCOUNT" ]]
  then
   $IAAS_HOME/bin/iaas-delete-key-pair --key-name $KEY_NAME --access-key-file $IAAS_ACCESS_KEY_FILE
   $IAAS_HOME/bin/akm-delete-access-key $AK
  fi
  # Set run specific key information
  export IAAS_ACCESS_KEY_FILE=$ACCOUNT"."$RUN_DATE"."$BASE_IAAS_ACCESS_KEY_FILE
  export KEY_NAME=$ACCOUNT"."$RUN_DATE"."$BASE_KEY_NAME
  export KEY_FILE=$ACCOUNT"."$RUN_DATE"."$BASE_KEY_FILE
  #echo "IAAS_ACCESS_KEY_FILE=$IAAS_ACCESS_KEY_FILE"
  #echo "KEY_NAME=$KEY_NAME"
  echo "KEY_FILE=$KEY_FILE"
  # Save current processing account
  PROCESSING_ACCOUNT=$ACCOUNT
  IAAS_USER=$ACCOUNT_USER
  echo "$ACCOUNT_PASSWORD" > $IAAS_PASSWORD_FILE
  $IAAS_HOME/bin/akm-describe-accounts --sep "|" > $ACCOUNTS_FILE
  while read line
  do
   ACCOUNT_ID=${line%%|*}
   line=${line#*|}
   ACCOUNT_NAME=${line%%|*}
 #  echo "Id = $ACCOUNT_ID"
 #  echo "Name = $ACCOUNT_NAME"
   if [[ "$ACCOUNT_NAME" == "$ACCOUNT" ]]
   then
    #echo "Found Account $line"
    AK=`$IAAS_HOME/bin/akm-create-access-key --account $ACCOUNT_ID --access-key-file $IAAS_ACCESS_KEY_FILE`
    KEYPAIR=`$IAAS_HOME/bin/iaas-create-key-pair --key-name $KEY_NAME --key-file $KEY_FILE`
    echo "Connected to $ACCOUNT_NAME"
    
    #cp $IAAS_ACCESS_KEY_FILE $ACCOUNT_NAME$IAAS_ACCESS_KEY_FILE
    #cp $KEY_FILE $ACCOUNT_NAME$KEY_FILE
    break
   fi    
  done < $ACCOUNTS_FILE 
 fi
}

function disconnectFromAccount() {
  $IAAS_HOME/bin/iaas-delete-key-pair --key-name $KEY_NAME --access-key-file $IAAS_ACCESS_KEY_FILE
  $IAAS_HOME/bin/akm-delete-access-key $AK
  PROCESSING_ACCOUNT=""
  # Clean Up
  cleanTempFiles
}

function getDistributionGroups() {
 $IAAS_HOME/bin/iaas-describe-distribution-groups --sep "|" > $DISTGRPS_FILE
}

function getNetworks() {
 $IAAS_HOME/bin/iaas-describe-vnets --sep "|" > $VNETS_FILE
}

function getVSTypes() {
 $IAAS_HOME/bin/iaas-describe-vserver-types --sep "|" > $VSERVER_TYPES_FILE
}

function getTemplates() {
 $IAAS_HOME/bin/iaas-describe-server-templates --sep "|" > $VSERVER_TEMPLATES
}

function getVolumes() {
 $IAAS_HOME/bin/iaas-describe-volumes --sep "|" > $VOLUMES_FILE
}

function getVServers() {
 $IAAS_HOME/bin/iaas-describe-vservers --sep "|" > $VSERVER_FILE
}

function getNetworkStaticIPs() {
 $IAAS_HOME/bin/iaas-describe-ip-addresses --filters vnet=$NETWORK_ID --sep "|" > $NETWORK_STATIC_IPS
}

#############################################################
##
## getDistributionGroupId
## ======================
##
## Get the Distribution Group id based on the supplied name.
##
#############################################################

function getDistributionGroupId() {
 while read line
 do
  DISTGROUP_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  if [[ "$NAME" == "$DISTGROUP_NAME" ]]
  then
   break
  fi
  DISTGROUP_ID=""
 done < $DISTGRPS_FILE
}

#############################################################
##
## getNetworkId
## ============
##
## Get the Network id based on the supplied name.
##
#############################################################

function getNetworkId() {
 while read line
 do
  NETWORK_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  if [[ "$NAME" == "$NETWORK_NAME" ]]
  then
   break
  fi
  NETWORK_ID=""
 done < $VNETS_FILE
}

#############################################################
##
## getIPAddress
## ============
##
## Get a static IP Address for a given network if an * is 
## supplied. If an IP Address is supplied it simple returns
## specified IP.
##
#############################################################

function getIPAddress() {
 echo "Checking IP Address $IP_ADDRESS"
 if [[ "$IP_ADDRESS" == "*" ]]
 then
  allocateIPAddress
#  getFirstAllocatedIPAddress
 fi
 echo "Returning IP Address $IP_ADDRESS"
}

#############################################################
##
## allocateIPAddress
## =================
##
## Allocate a single IP Address from a specified Network.
##
#############################################################

function allocateIPAddress() {
 IP_ADDRESS=`$IAAS_HOME/bin/iaas-allocate-ip-addresses --vnet $NETWORK_ID --num 1`
}

function allocateIPAddresses() {
 $IAAS_HOME/bin/iaas-allocate-ip-addresses --vnet $NETWORK_ID --num $IP_COUNT
}

#############################################################
##
## getFirstAllocatedIPAddress
## ==========================
##
## Get the first static IP Address for a given Network Id.
##
#############################################################

function getFirstAllocatedIPAddress() {
 getNetworkStaticIPs
 while read line
 do
  IP_ADDRESS=${line%%|*}
  break
 done < $NETWORK_STATIC_IPS
}

#############################################################
##
## getVSTypeId
## ===========
##
## Get the VServer Type id based on the supplied name.
##
#############################################################

function getVSTypeId() {
 while read line
 do
  VSTYPE_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  if [[ "$VSTYPE_NAME" == "$NAME" ]]
  then
   break
  fi
  VSTYPE_ID=""
 done < $VSERVER_TYPES_FILE
}

#############################################################
##
## getTemplateId
## =============
##
## Get the Template id based on the supplied name.
##
#############################################################

function getTemplateId() {
 while read line
 do
  TEMPLATE_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  if [[ "$TEMPLATE_NAME" == "$NAME" ]]
  then
   break
  fi
  TEMPLATE_ID=""
 done < $VSERVER_TEMPLATES
}

#############################################################
##
## getVolumeId
## ===========
##
## Get the Volume id based on the supplied name.
##
#############################################################

function getVolumeId() {
 while read line
 do
  VOLUME_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  if [[ "$NAME" == "$VOLUME_NAME" ]]
  then
   break;
  fi
  VOLUME_ID=""
 done < $VOLUMES_FILE
}

#############################################################
##
## getVServerId
## ============
##
## Get the VServer id based on the supplied name.
##
#############################################################

function getVServerId() {
 while read line
 do
  VSERVER_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  if [[ "$VSERVER_NAME" == "$NAME" ]]
  then
   break;
  fi
  VSERVER_ID=""
 done < $VSERVER_FILE
}

function getVServerState() {
 getVServers
 while read line
 do
  VSERVER_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  line=${line#*|}
  DESCRIPTION=${line%%|*}
  line=${line#*|}
  VSERVER_STATE=${line%%|*}
  if [[ "$VSERVER_NAME" == "$NAME" ]]
  then
   break;
  fi
 done < $VSERVER_FILE
}

function pauseUntilVServerRunning() {
 # Wait until the Server is running before creating the next
 echo "Pausing until vServer is Running"
  getVServerState
  while [[ "$VSERVER_STATE" != "RUNNING" ]]
  do
   echo "$NAME $VSERVER_STATE"
   if [[ "$VSERVER_STATE" != "RUNNING" ]]
   then
    echo "Sleeping......."
    sleep 30
   fi
   if [[ "$VSERVER_STATE" == "FAILED" ]]
   then
    echo "$NAME Will Delete Automatically after remaining Failed for a period....."
    #echo "Will Delete $NAME in 5 Minutes....."
    #sleep 300
    #deleteVServer
    #echo "Deleted $NAME waiting 5 Minutes....."
    #sleep 300
    break
   fi
   getVServerState
   #echo "Description: [$DESCRIPTION]"
  done
  echo "$NAME $VSERVER_STATE"
  # Lets pause for a minute or two
  echo "Just Chilling......"
  sleep 30
  echo "Ahhhhh we're getting there......."
  sleep 30
  echo "I'm almost at one with the universe......."
  sleep 30
  echo "Bong Reality Check !"
}

function deleteVServer() {
 $IAAS_HOME/bin/iaas-terminate-vservers --force --vserver-ids $VSERVER_ID
}

function createVServer() {
 VSERVER_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 VSTYPE_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 TEMPLATE_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 NETWORK_NAMES=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 IP_ADDRESSES=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 DISTGROUP_NAME=${ASSET_DETAILS%%|*}
 
 echo "Creating vServer $VSERVER_NAME"
 
 # Get Ids associated with names
 getVSTypeId
 getTemplateId
 # Convert Network Names to Ids
 NETWORK_IDS=""
 # Validated IPs
 NETWORK_IPS=""
 # Reset SSH IP Address it will be used to disable SSH Key
 SSH_IP_ADDRESS=""
 while true
 do
  # Get ID and add to list
  NETWORK_NAME=${NETWORK_NAMES%%,*}
  NETWORK_NAMES=${NETWORK_NAMES#*,}
  getNetworkId
  if [[ "$NETWORK_IDS" != "" ]]
  then
   NETWORK_IDS="$NETWORK_IDS,$NETWORK_ID"
  else
   NETWORK_IDS=$NETWORK_ID
  fi
  # Check IPs
  IP_ADDRESS=${IP_ADDRESSES%%,*}
  IP_ADDRESSES=${IP_ADDRESSES#*,}
  getIPAddress
  if [[ "$NETWORK_IPS" != "" ]]
  then
   NETWORK_IPS="$NETWORK_IPS,$IP_ADDRESS"
  else
   NETWORK_IPS=$IP_ADDRESS
  fi
  # Set the SSH IP to the first IP addres we will assume the server is accessible via this IP
  if [[ "$SSH_IP_ADDRESS" == "" ]]
  then
   SSH_IP_ADDRESS=$IP_ADDRESS
  elif [[ "$NETWORK_NAME" == "IPoIB-vserver-shared-storage" ]]
  then
   # Prefer the IPoIB-vserver-shared-storage if this is used
   SSH_IP_ADDRESS=$IP_ADDRESS
  fi
  # If I've processed all then exit
  if [[ "$NETWORK_NAME" == "$NETWORK_NAMES" ]]
  then
   break
  fi
 done
 getDistributionGroupId
 
 # Create vServer
 if [[ "$DISTGROUP_ID" != "" ]]
 then
  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"
  
  $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 --desc "Created By CreateAsset.sh"
 else
  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"
  
  $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 --desc "Created By CreateAsset.sh"
 fi
 pauseUntilVServerRunning
 if [[ "$REMOVE_SSH_KEYS" == "true" ]]
 then
  removeSshKeyRequirement
 fi
 
 echo "vServer $VSERVER_NAME has been created"
}

function removeSshKeyRequirement() {
 SSH_FLAGS="-i $KEY_FILE -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
 SSH_FLAGS="-i $KEY_FILE -o StrictHostKeyChecking=no"
 # Create Variables as environment variables
 #export NETWORK_IPS=$NETWORK_IPS
 #export VSERVER_NAME=$VSERVER_NAME

 SSH_IP_ADDRESS=""
 for IP in ${NETWORK_IPS//,/ }
 do
  SSH_RESULT=$(ssh $SSH_FLAGS root@$IP "hostname")
  echo "SSH Result $SSH_RESULT"
  if [[ "$SSH_RESULT" == "$VSERVER_NAME" ]]
  then
   echo "$IP Address works for ssh"
   SSH_IP_ADDRESS=$IP
   break;
  else
   echo "$IP Address does not work for ssh"
  fi
 done
 
 if [[ "$SSH_IP_ADDRESS" != "" ]]
 then
  echo "Removing ssh key requirement for $VSERVER_NAME on $SSH_IP_ADDRESS"
  
  ssh $SSH_FLAGS root@$SSH_IP_ADDRESS "cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig"
  ssh $SSH_FLAGS root@$SSH_IP_ADDRESS "sed 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config.orig > /etc/ssh/sshd_config"
  ssh $SSH_FLAGS root@$SSH_IP_ADDRESS "service sshd restart"

  echo "Removed ssh key requirement for $VSERVER_NAME"
 else
  echo "Unable to find a route to $VSERVER_NAME to remove the ssh key requirement you will need to do the following"
  echo ""
  echo "1. ssh into the vServer using: ssh -i $KEY_FILE -l root <IP Address>"
  echo "2. Edit /etc/ssh/sshd_config and replace \"PasswordAuthentication no\" with \"PasswordAuthentication yes\""
  echo "3. Restart sshd service: service sshd restart"
  echo ""
  echo "cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig"
  echo "sed \'s/PasswordAuthentication no/PasswordAuthentication yes/\' /etc/ssh/sshd_config.orig > /etc/ssh/sshd_config"
  echo "service sshd restart"
  echo ""
 fi
}

function pauseUntilDistributionGroupCreated() {
 getDistributionGroups
 getDistributionGroupId
 while [[ "$DISTGROUP_ID" == "" ]]
  do
   # Lets pause
   echo "Just Waiting 30 Seconds......"
   sleep 30
   getDistributionGroups
   getDistributionGroupId
  done
}

function createDistributionGroup() {
 DISTGROUP_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 # Size is never specified
 # DISTGROUP_SIZE=${ASSET_DETAILS%%|*}
 # Create Volume
 echo "About to execute : $IAAS_HOME/bin/iaas-create-distribution-group --name $DISTGROUP_NAME"
 $IAAS_HOME/bin/iaas-create-distribution-group --name $DISTGROUP_NAME
 # Lets pause
 pauseUntilDistributionGroupCreated
}

function pauseUntilVolumeCreated() {
 getVolumes
 getVolumeId
 while [[ "$VOLUME_ID" == "" ]]
  do
   # Lets pause
   echo "Just Waiting 30 Seconds......"
   sleep 30
   getVolumes
   getVolumeId
  done

}

function createVolume() {
 VOLUME_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 VOLUME_SIZE=${ASSET_DETAILS%%|*}
 # Create Volume
 echo "About to execute : $IAAS_HOME/bin/iaas-create-volume --name $VOLUME_NAME --size $VOLUME_SIZE"
 $IAAS_HOME/bin/iaas-create-volume --name $VOLUME_NAME --size $VOLUME_SIZE
  # Lets pause
  pauseUntilVolumeCreated
}

function attachVolume() {
 VSERVER_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 VOLUME_NAMES=${ASSET_DETAILS%%|*}
 # Get vServer Id
 getVServerId
 # Convert Volume Names to Ids
 VOLUME_IDS=""
 while true
 do
  VOLUME_NAME=${VOLUME_NAMES%%,*}
  VOLUME_NAMES=${VOLUME_NAMES#*,}
  getVolumeId
  if [[ "$VOLUME_IDS" != "" ]]
  then
   VOLUME_IDS="$VOLUME_IDS,$VOLUME_ID"
  else
   VOLUME_IDS=$VOLUME_ID
  fi
  if [[ "$VOLUME_NAME" == "$VOLUME_NAMES" ]]
  then
   break
  fi
 done
 # Attach Volumes
 echo "About to execute : $IAAS_HOME/bin/iaas-attach-volumes-to-vserver --vserver-id $VSERVER_ID --volume-ids $VOLUME_IDS"
 $IAAS_HOME/bin/iaas-attach-volumes-to-vserver --vserver-id $VSERVER_ID --volume-ids $VOLUME_IDS
  # Lets pause
  echo "Just Waiting 30 Seconds......"
  sleep 30
}

#############################################################
##
## getTemplateState
## ================
##
## Loop through the Template associated with the Account 
## checking to see if the upload has completed and the 
## template has a status of OK. At this point return.
##
#############################################################

function getTemplateState() {
 getTemplates
 while read line
 do
  TEMPLATE_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  line=${line#*|}
  line=${line#*|}
  TEMPLATE_STATE=${line%%|*}
  if [[ "$TEMPLATE_NAME" == "$NAME" ]]
  then
   break;
  fi
 done < $VSERVER_TEMPLATES
}

#############################################################
##
## pauseUntilServerTemplateUploaded
## ================================
##
## Pause the script until the Template file has been uploaded
## to the Account.
##
#############################################################

function pauseUntilServerTemplateUploaded() {
 echo "Pausing until Template upload has completed"
  getTemplateState
  while [[ "$TEMPLATE_STATE" != "OK" ]]
  do
   echo "$NAME $TEMPLATE_STATE"
   if [[ "$TEMPLATE_STATE" != "SCHEDULED" ]]
   then
    echo "Sleeping......."
    sleep 30
   elif [[ "$TEMPLATE_STATE" != "RUNNING" ]]
   then
    echo "Sleeping......."
    sleep 30
   elif [[ "$TEMPLATE_STATE" != "FAILED" ]]
   then
    deleteServerTemplate
    echo "Sleeping......."
    sleep 30
   fi
   getTemplateState
  done
}

#############################################################
##
## uploadServerTemplate
## ====================
##
## Upload a tgz file that defines a server template. It is 
## recommended these be copied to the ZFS first and then the
## appropriate URL from the ZFS be used.
##
#############################################################

function uploadServerTemplate() {
 TEMPLATE_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 TEMPLATE_URL=${ASSET_DETAILS%%|*}
 # Upload Template
 echo "About to execute : $IAAS_HOME/bin/iaas-create-server-template-from-url --name $TEMPLATE_NAME --url $TEMPLATE_URL"
 $IAAS_HOME/bin/iaas-create-server-template-from-url --name $TEMPLATE_NAME --url $TEMPLATE_URL
  # Lets pause
 pauseUntilServerTemplateUploaded
}

function deleteServerTemplate() {
 $IAAS_HOME/bin/iaas-delete-server-template --force --server-template-id $TEMPLATE_ID
}

#############################################################
##
## getVNetworkState
## ================
##
## Loop through the Networks associated with the Account 
## checking to see if the creation has completed and the 
## network has a status of OK. At this point return.
##
#############################################################

function getVNetworkState() {
 getNetworks
 while read line
 do
  NETWORK_ID=${line%%|*}
  line=${line#*|}
  NAME=${line%%|*}
  line=${line#*|}
  line=${line#*|}
  NETWORK_STATE=${line%%|*}
  if [[ "$NETWORK_NAME" == "$NAME" ]]
  then
   break;
  fi
 done < $VNETS_FILE
}

#############################################################
##
## pauseUntilVirtualNetworkCreated
## ===============================
##
## Pause the script until the Virtual Private Network has
## been created.
##
#############################################################

function pauseUntilVirtualNetworkCreated() {
 echo "Pausing until Virtual Network creation has completed"
  getVNetworkState
  while [[ "$NETWORK_STATE" != "OK" ]]
  do
   echo "$NAME $NETWORK_STATE"
   if [[ "$NETWORK_STATE" != "SCHEDULED" ]]
   then
    echo "Sleeping......."
    sleep 30
   elif [[ "$NETWORK_STATE" != "RUNNING" ]]
   then
    echo "Sleeping......."
    sleep 30
   fi
   getVNetworkState
  done
}

#############################################################
##
## createVirtualNetwork
## ====================
##
## Create a Virtual Private Network based on the name 
## supplied.
##
#############################################################

function createVirtualNetwork() {
 NETWORK_NAME=${ASSET_DETAILS%%|*}
 ASSET_DETAILS=${ASSET_DETAILS#*|}
 NETWORK_IPS=${ASSET_DETAILS%%|*}
 # 
 echo "About to execute : $IAAS_HOME/bin/iaas-create-vnet --name $NETWORK_NAME --size $NETWORK_IPS "
 $IAAS_HOME/bin/iaas-create-vnet --name $NETWORK_NAME --size $NETWORK_IPS
  # Lets pause
 pauseUntilVirtualNetworkCreated
}

#############################################################
##
## processAssets
## =============
##
## This function loops through the information defined in
## the input file looking for actions to be executed. It will
## process the entries sequentially and simply call the
## appropriate sub-function to execute the iaas commands.
## Entries with invalid Actions will simply be ignored along
## with blank lines.
##
#############################################################

function processAssets() {
 # Read Entries into an Array
 assetArray=( $(grep ":" $INPUT_FILE) )
 # Process Array
 for line in "${assetArray[@]}"
 #while read line
 do
  #echo "Processing Line: $line"
  ACCOUNT=${line%%:*}
  line=${line#*:}
  ACTION=${line%%|*}
  line=${line#*|}
  if [[ "$ACTION" == "Connect" ]]
  then
   ACCOUNT_USER=${line%%|*}
   line=${line#*|}
   ACCOUNT_PASSWORD=${line%%|*}
   connectToAccount
   
   ## Account Info
   getNetworks
   getVSTypes
   getTemplates
  elif [[ "$ACTION" == "Create" ]]
  then
   ASSET=${line%%|*}
   line=${line#*|}
   ASSET_DETAILS=$line
   if [[ "$ASSET" == "vServer" ]]
   then
    getDistributionGroups
    createVServer
   elif [[ "$ASSET" == "vServers" ]]
   then
    getDistributionGroups
    createVServers
   elif [[ "$ASSET" == "Volume" ]]
   then
    createVolume
   elif [[ "$ASSET" == "DistributionGroup" ]]
   then
    createDistributionGroup
   elif [[ "$ASSET" == "VirtualNetwork" ]]
   then
    createVirtualNetwork
   fi
#   continue
  elif [[ "$ACTION" == "Upload" ]]
  then
   ASSET=${line%%|*}
   line=${line#*|}
   ASSET_DETAILS=$line
   if [[ "$ASSET" == "ServerTemplate" ]]
   then
    uploadServerTemplate
   fi
#   continue
  elif [[ "$ACTION" == "Attach" ]]
  then
   ASSET=${line%%|*}
   line=${line#*|}
   ASSET_DETAILS=$line
   if [[ "$ASSET" == "Volume" ]]
   then
    getVolumes
    getVServers
    attachVolume
   fi
#   continue
  elif [[ "$ACTION" == "Disconnect" ]]
  then
   disconnectFromAccount
#   continue
  fi
 #done < $INPUT_FILE
 done
}

#############################################################
##
## usage
## =====
##
## Show usage.
##
#############################################################

function usage() {
 echo ""
 echo >&2 "usage: $0 [-f <Asset Definition File>] [-r]"
 echo >&2 ""
 echo >&2 "          -f <Asset Definition File> (Default is CreateAssets.in)"
 echo >&2 "          -r Indicates that the ssh keys should be removed"
 echo ""
 exit 1
}

###############################################################
##
## Simple start for the script that will extract the parameters
## and call the appriate start function.
##
###############################################################

REMOVE_SSH_KEYS=false
while [ $# -gt 0 ]
do
 case "$1" in 
  -f) INPUT_FILE="$2"; shift;;
  -r) REMOVE_SSH_KEYS=true;;
  *) usage;;
  *) break;;
 esac
 shift
done

# Check if the JAVA_HOME is set
if [[ "$JAVA_HOME" == "" ]]
then
 export JAVA_HOME=/usr/java/latest
 echo "JAVA_HOME is not defined using $JAVA_HOME"
fi
# Processing function call
processAssets

echo "**************************************"
echo "*****  Finished Creating Assets  *****"
echo "**************************************"

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:

  1. Open EMOC.
  2. Right-click on your new vServer and select "Launch Virtual Console" and wait until it opens.
  3. Edit the /etc/ssh/sshd_config file and set the following values:
    1. PasswordAuthentication yes
  4. Restart the sshd using:

    /etc/init.d/sshd reload

1 comment:

  1. Andrew, this is great! Quick and easy to use, also saves a heap of time!

    ReplyDelete