From 9a7b048ba3855aa9befc79e675a9a9089f93f264 Mon Sep 17 00:00:00 2001 From: David Hill Date: Thu, 5 Jan 2017 16:19:05 -0600 Subject: [PATCH] MCOL-462 --- oam/cloud/MCSInstanceCmds.sh | 80 ++++++++++++++++++++++------------ oam/cloud/MCSgetCredentials.sh | 18 +++----- oam/oamcpp/liboamcpp.cpp | 42 +++++++++++++----- 3 files changed, 88 insertions(+), 52 deletions(-) diff --git a/oam/cloud/MCSInstanceCmds.sh b/oam/cloud/MCSInstanceCmds.sh index 079e6c50e..a053d3f77 100755 --- a/oam/cloud/MCSInstanceCmds.sh +++ b/oam/cloud/MCSInstanceCmds.sh @@ -8,8 +8,7 @@ prefix=/usr/local #check command if [ "$1" = "" ]; then - echo "Enter Command Name: {launchInstance|getInstance|getZone|getPrivateIP|getKey|getAMI|getType|terminateInstance|startInstance|assignElasticIP|deassignElasticIP|getProfile|stopInstance|getGroup|getSubnet|getVpc|getRegion} -}" + echo "Enter Command Name: {launchInstance|getInstance|getZone|getPrivateIP|getKey|getAMI|getType|terminateInstance|startInstance|assignElasticIP|deassignElasticIP|getProfile|stopInstance|getGroup|getSubnet|getVpc|getRegion|getRole}" exit 1 fi @@ -104,6 +103,24 @@ getRegion() { return } +getRole() { + #check for iam folder + iam=`curl -s http://169.254.169.254/latest/meta-data/ | grep iam` + + if [ -z "$iam" ]; then + exit 1; + fi + + Role=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/` + + if [ -z "$role" ]; then + exit 1; + fi + + echo $Role + return +} + getInstance() { if [ "$instanceName" != "" ]; then echo $instanceName @@ -232,49 +249,52 @@ launchInstance() { #get group getGroup >/dev/null 2>&1 fi + #get AMI getAMI >/dev/null 2>&1 + #get Zone getZone >/dev/null 2>&1 if [ "$instanceType" = "unassigned" ]; then #get type getType >/dev/null 2>&1 fi - #get AMI Profile - getProfile >/dev/null 2>&1 #get Subnet getSubnet >/dev/null 2>&1 - if [ "$instanceProfile" = "" ] || [ "$instanceProfile" = "default-hvm" ]; then - if [ "$groupid" != "default" ]; then - if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ] ; then - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --image-id $ami --security-group-ids $groupid --query 'Instances[*].InstanceId' --output text` - else - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` - fi + #get IAM Role + getRole >/dev/null 2>&1 + + if [ "$Role" = "" ] || [ "$Role" = "default" ]; then + if [ "$groupid" != "default" ]; then + if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ] ; then + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --image-id $ami --security-group-ids $groupid --query 'Instances[*].InstanceId' --output text` else - if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --image-id $ami --query 'Instances[*].InstanceId' --output text` - else - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` - fi + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` fi else - if [ "$groupid" != "default" ]; then - if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $instanceProfile --subnet-id $subnet --image-id $ami --query 'Instances[*].InstanceId' --output text` - else - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $instanceProfile --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` - fi + if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --image-id $ami --query 'Instances[*].InstanceId' --output text` else - if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $instanceProfile --subnet-id $subnet --image-id $ami --query 'Instances[*].InstanceId' --output text` - else - newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $instanceProfile --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` - fi + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` fi fi + else + if [ "$groupid" != "default" ]; then + if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $Role --subnet-id $subnet --image-id $ami --query 'Instances[*].InstanceId' --output text` + else + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $Role --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` + fi + else + if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $Role --subnet-id $subnet --image-id $ami --query 'Instances[*].InstanceId' --output text` + else + newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile $Role --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` + fi + fi + fi echo $newInstance return @@ -421,9 +441,11 @@ case "$1" in getRegion) getRegion ;; - + getRole) + getRole + ;; *) - echo $"Usage: $0 {launchInstance|getInstance|getZone|getPrivateIP|getType|getKey|getAMI|terminateInstance|startInstance|assignElasticIP|deassignElasticIP|getProfile|stopInstance|getGroup|getSubnet|getVpc|getRegion}" + echo $"Usage: $0 {launchInstance|getInstance|getZone|getPrivateIP|getType|getKey|getAMI|terminateInstance|startInstance|assignElasticIP|deassignElasticIP|getProfile|stopInstance|getGroup|getSubnet|getVpc|getRegion|getRole}" exit 1 esac diff --git a/oam/cloud/MCSgetCredentials.sh b/oam/cloud/MCSgetCredentials.sh index 6bc877547..2454529b6 100644 --- a/oam/cloud/MCSgetCredentials.sh +++ b/oam/cloud/MCSgetCredentials.sh @@ -6,28 +6,20 @@ if [ -f $HOME/.aws/credentials ]; then exit 0 fi -#check for iam folder -iam=`curl -s http://169.254.169.254/latest/meta-data/ | grep iam` +#get IAM Role +Role=`$prefix/mariadb/columnstore/bin/MCSInstanceCmds.sh getRole` - -if [ -z "$iam" ]; then +if [ -z "$Role" ]; then exit 1; fi -instance_profile=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/` -# - -if [ -z "$instance_profile" ]; then - exit 1; -fi - -aws_access_key_id=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep AccessKeyId | cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'` +aws_access_key_id=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | grep AccessKeyId | cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'` if [ -z "$aws_access_key_id" ]; then exit 1; fi -aws_secret_access_key=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep SecretAccessKey | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'` +aws_secret_access_key=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | grep SecretAccessKey | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'` if [ -z "$aws_secret_access_key" ]; then exit 1; diff --git a/oam/oamcpp/liboamcpp.cpp b/oam/oamcpp/liboamcpp.cpp index 828c9df9d..fc40b5462 100644 --- a/oam/oamcpp/liboamcpp.cpp +++ b/oam/oamcpp/liboamcpp.cpp @@ -5967,11 +5967,22 @@ namespace oam cout << " Create AWS Volume for UM #" << itoa(moduleID) << endl; - volumeName = createEC2Volume(UMVolumeSize, "um"); - if ( volumeName == "failed" ) { - writeLog("addModule: create volume failed", LOG_TYPE_CRITICAL); - exceptionControl("addUMdisk", API_FAILURE); - } + int retry = 0; + for ( ; retry < 5 ; retry++ ) + { + volumeName = createEC2Volume(UMVolumeSize, "um"); + + if ( volumeName == "failed" || volumeName.empty() ) + retry = retry; + else + break; + } + + if ( retry >= 5 ) + { + cout << " *** ERROR: Failed to create a Volume for um1 " << moduleID << endl; + exceptionControl("addUMdisk", API_FAILURE); + } //attach and format volumes device = "/dev/sdf"; @@ -5979,11 +5990,22 @@ namespace oam string localInstance = getEC2LocalInstance(); //attach volumes to local instance - writeLog("addUMdisk - Attach new Volume to local instance: " + volumeName, LOG_TYPE_DEBUG); - if (!attachEC2Volume(volumeName, device, localInstance)) { - writeLog("addUMdisk: volume failed to attach to local instance", LOG_TYPE_CRITICAL); - exceptionControl("addUMdisk", API_FAILURE); - } + writeLog("addUMdisk - Attach new Volume to local instance: " + volumeName, LOG_TYPE_DEBUG); + + retry = 0; + for ( ; retry < 5 ; retry++ ) + { + if (!attachEC2Volume(volumeName, device, localInstance)) + detachEC2Volume(volumeName); + else + break; + } + + if ( retry >= 5 ) + { + cout << " *** ERROR: Volume " << volumeName << " failed to attach to local instance" << endl; + exceptionControl("addUMdisk", API_FAILURE); + } //format attached volume writeLog("addUMdisk - Format new Volume for: " + volumeName, LOG_TYPE_DEBUG);