#! /bin/sh # MCSVolumeCmds.sh # describe, detach and attach Volume Storage from a Cloud environment # # 1. Amazon EC2 #get temp directory tmpdir=`mcsGetConfig SystemConfig SystemTempFileDir` #check command if [ "$1" = "" ]; then echo "Enter Command Name: {create|describe|detach|attach|delete|createTag}" exit 1 fi if [ "$1" = "create" ]; then if [ "$2" = "" ]; then echo "Enter size of the volume, in GiB (1-1024)" exit 1 fi volumeSize="$2" #get module-type if [ "$3" = "" ]; then echo "Enter Module Type" exit 1 fi moduleType="$3" fi if [ "$1" = "describe" ]; then if [ "$2" = "" ]; then echo "Enter Volume Name" exit 1 fi volumeName="$2" fi if [ "$1" = "detach" ]; then if [ "$2" = "" ]; then echo "Enter Volume Name" exit 1 fi volumeName="$2" fi if [ "$1" = "attach" ]; then if [ "$2" = "" ]; then echo "Enter Volume Name" exit 1 fi volumeName="$2" #get instance-name and device-name if [ "$3" = "" ]; then echo "Enter Instance Name" exit 1 fi instanceName="$3" if [ "$4" = "" ]; then echo "Enter Device Name" exit 1 fi deviceName="$4" fi if [ "$1" = "delete" ]; then if [ "$2" = "" ]; then echo "Enter Volume Name" exit 1 fi volumeName="$2" fi if [ "$1" = "createTag" ]; then if [ "$2" = "" ]; then echo "Enter Resource Name" exit 1 fi resourceName="$2" if [ "$3" = "" ]; then echo "Enter Tag Name" exit 1 fi tagName="$3" if [ "$4" = "" ]; then echo "Enter Tag Value" exit 1 fi tagValue="$4" fi . @ENGINE_SUPPORTDIR@/columnstore_functions AWSCLI="aws ec2 " MCSgetCredentials.sh >/dev/null 2>&1 #get Region Region=`MCSInstanceCmds.sh getRegion` checkInfostatus() { #check if attached cat ${tmpdir}/volumeInfo_$volumeName | grep attached > ${tmpdir}/volumeStatus_$volumeName if [ `cat ${tmpdir}/volumeStatus_$volumeName | wc -c` -ne 0 ]; then STATUS="attached" RETVAL=0 return fi #check if available cat ${tmpdir}/volumeInfo_$volumeName | grep available > ${tmpdir}/volumeStatus_$volumeName if [ `cat ${tmpdir}/volumeStatus_$volumeName | wc -c` -ne 0 ]; then STATUS="available" RETVAL=0 return fi #check if detaching cat ${tmpdir}/volumeInfo_$volumeName | grep detaching > ${tmpdir}/volumeStatus_$volumeName if [ `cat ${tmpdir}/volumeStatus_$volumeName | wc -c` -ne 0 ]; then STATUS="detaching" RETVAL=0 return fi #check if attaching cat ${tmpdir}/volumeInfo_$volumeName | grep attaching > ${tmpdir}/volumeStatus_$volumeName if [ `cat ${tmpdir}/volumeStatus_$volumeName | wc -c` -ne 0 ]; then STATUS="attaching" RETVAL=0 return fi #check if doesn't exist cat ${tmpdir}/volumeInfo_$volumeName | grep "does not exist" > ${tmpdir}/volumeStatus_$volumeName if [ `cat ${tmpdir}/volumeStatus_$volumeName | wc -c` -ne 0 ]; then STATUS="does-not-exist" RETVAL=1 return fi #check if reports already attached from attach command cat ${tmpdir}/volumeInfo_$volumeName | grep "already attached" > ${tmpdir}/volumeStatus_$volumeName if [ `cat ${tmpdir}/volumeStatus_$volumeName | wc -c` -ne 0 ]; then STATUS="already-attached" RETVAL=1 return fi #any other, unknown error STATUS="unknown" RETVAL=1 return } createvolume() { # get zone zone=`MCSInstanceCmds.sh getZone` if [ $moduleType == "um" ]; then # get type volumeType=`mcsGetConfig Installation UMVolumeType` if [ $volumeType == "io1" ]; then # get IOPS volumeIOPS=`mcsGetConfig Installation UMVolumeIOPS` fi else # pm # get type volumeType=`mcsGetConfig Installation PMVolumeType` if [ $volumeType == "io1" ]; then # get IOPS volumeIOPS=`mcsGetConfig Installation PMVolumeIOPS` fi fi #create volume if [ $volumeType == "io1" ]; then volume=`$AWSCLI create-volume --region $Region --availability-zone $zone --size $volumeSize --volume-type $volumeType --iops $volumeIOPS --output text --query VolumeId` else volume=`$AWSCLI create-volume --region $Region --availability-zone $zone --size $volumeSize --volume-type $volumeType --output text --query VolumeId` fi echo $volume return } describevolume() { #describe volume $AWSCLI describe-volumes --volume-ids $volumeName --region $Region > ${tmpdir}/volumeInfo_$volumeName 2>&1 checkInfostatus echo $STATUS return } detachvolume() { #detach volume $AWSCLI detach-volume --volume-id $volumeName --region $Region > ${tmpdir}/volumeInfo_$volumeName 2>&1 checkInfostatus if [ $STATUS == "detaching" ]; then retries=1 while [ $retries -ne 10 ]; do #retry until it's attached $AWSCLI detach-volume --volume-id $volumeName --region $Region > ${tmpdir}/volumeInfo_$volumeName 2>&1 checkInfostatus if [ $STATUS == "available" ]; then echo "available" exit 0 fi ((retries++)) sleep 1 done . @ENGINE_SUPPORTDIR@/columnstore_functions cplogger -w 6 "MCSVolume: detachvolume failed: $STATUS" echo "failed" exit 1 fi if [ $STATUS == "available" ]; then echo "available" exit 0 fi . @ENGINE_SUPPORTDIR@/columnstore_functions cplogger -w 6 "MCSVolume: detachvolume failed status: $STATUS" echo $STATUS exit 1 } attachvolume() { #detach volume $AWSCLI attach-volume --volume-id $volumeName --instance-id $instanceName --device $deviceName --region $Region > ${tmpdir}/volumeInfo_$volumeName 2>&1 checkInfostatus if [ $STATUS == "attaching" -o $STATUS == "already-attached" ]; then retries=1 while [ $retries -ne 10 ]; do #check status until it's attached describevolume if [ $STATUS == "attached" ]; then echo "attached" exit 0 fi ((retries++)) sleep 1 done . @ENGINE_SUPPORTDIR@/columnstore_functions cplogger -w 6 "MCSVolume: attachvolume failed: $STATUS" echo "failed" exit 1 fi if [ $STATUS == "attached" ]; then echo "attached" exit 0 fi . @ENGINE_SUPPORTDIR@/columnstore_functions cplogger -w 6 "MCSVolume: attachvolume failed: $STATUS" echo $STATUS exit 1 } deletevolume() { #delete volume $AWSCLI delete-volume --volume-id $volumeName --region $Region > ${tmpdir}/deletevolume_$volumeName 2>&1 return } createTag() { #create tag $AWSCLI create-tags --resources $resourceName --tags Key=$tagName,Value=$tagValue --region $Region > ${tmpdir}/createTag_$resourceName 2>&1 return } case "$1" in create) createvolume ;; describe) describevolume ;; detach) detachvolume ;; attach) attachvolume ;; delete) deletevolume ;; createTag) createTag ;; *) echo $"Usage: $0 {create|describe|detach|attach|delete|}" exit 1 esac exit $?