1#! /bin/sh 2# MCSInstanceCmds.sh 3# get-local-instance-ID, get-zone, getPrivateIP from a Cloud environment 4# 5# 1. Amazon EC2 6 7#get temp directory 8tmpdir=`mcsGetConfig SystemConfig SystemTempFileDir` 9 10#check command 11if [ "$1" = "" ]; then 12 echo "Enter Command Name: {launchInstance|getInstance|getZone|getPrivateIP|getKey|getAMI|getType|terminateInstance|startInstance|assignElasticIP|deassignElasticIP|getProfile|stopInstance|getGroup|getSubnet|getVpc|getRegion|getRole}" 13 exit 1 14fi 15 16if [ "$1" = "getPrivateIP" ]; then 17 if [ "$2" = "" ]; then 18 echo "Enter Instance Name" 19 exit 1 20 fi 21 instanceName="$2" 22fi 23 24if [ "$1" = "launchInstance" ]; then 25 if [ "$2" = "" ]; then 26 IPaddress="unassigned" 27 else 28 IPaddress="$2" 29 fi 30 if [ "$3" = "" ]; then 31 instanceType="unassigned" 32 else 33 instanceType="$3" 34 fi 35 if [ "$4" = "" ]; then 36 groupid="unassigned" 37 else 38 groupid="$4" 39 fi 40fi 41 42if [ "$1" = "terminateInstance" ]; then 43 if [ "$2" = "" ]; then 44 echo "Enter Instance Name" 45 exit 1 46 fi 47 instanceName="$2" 48fi 49 50if [ "$1" = "stopInstance" ]; then 51 if [ "$2" = "" ]; then 52 echo "Enter Instance Name" 53 exit 1 54 fi 55 instanceName="$2" 56fi 57 58if [ "$1" = "startInstance" ]; then 59 if [ "$2" = "" ]; then 60 echo "Enter Instance Name" 61 exit 1 62 fi 63 instanceName="$2" 64fi 65 66if [ "$1" = "assignElasticIP" ]; then 67 if [ "$2" = "" ]; then 68 echo "Enter Instance Name" 69 exit 1 70 else 71 instanceName="$2" 72 fi 73 if [ "$3" = "" ]; then 74 echo "Enter Elastic IP Address" 75 exit 1 76 else 77 IPAddress="$3" 78 fi 79fi 80 81if [ "$1" = "deassignElasticIP" ]; then 82 if [ "$2" = "" ]; then 83 echo "Enter Elastic IP Address" 84 exit 1 85 else 86 IPAddress="$2" 87 fi 88fi 89 90 91MCSgetCredentials.sh >/dev/null 2>&1 92 93. @ENGINE_SUPPORTDIR@/columnstore_functions 94 95#default instance to null 96instance="" 97 98AWSCLI="aws ec2 " 99 100getRegion() { 101 Region=`curl --fail --silent /dev/null http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | cut -d':' -f2 | sed 's/\"//g' | sed 's/\,//g' | sed -e 's/^[ \t]*//'` 102 103 echo $Region 104 return 105} 106 107getRole() { 108 #check for iam folder 109 iam=`curl -s http://169.254.169.254/latest/meta-data/ | grep iam` 110 111 if [ -z "$iam" ]; then 112 return ""; 113 fi 114 115 Role=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/` 116 117 if [ -z "$Role" ]; then 118 return ""; 119 fi 120 121 echo $Role 122 return 123} 124 125getInstance() { 126 if [ "$instanceName" != "" ]; then 127 echo $instanceName 128 return 129 fi 130 131 instanceName=`curl -s http://169.254.169.254/latest/meta-data/instance-id` 132 133 echo $instanceName 134 return 135} 136 137 138getZone() { 139 zone=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone` 140 141 echo $zone 142 return 143} 144 145getPrivateIP() { 146 #get region 147 getRegion >/dev/null 2>&1 148 149 #check if running or terminated 150 state=`aws ec2 describe-instances --instance-ids $instanceName --region $Region --output text --query 'Reservations[*].Instances[*].State.Name'` 151 if [ "$state" != "running" ]; then 152 # not running 153 if [ "$state" == "stopped" ]; then 154 echo "stopped" 155 exit 1 156 else 157 if [ "$state" == "terminated" ]; then 158 echo "terminated" 159 exit 1 160 else 161 if [ "$state" == "shutting-down" ]; then 162 echo "terminated" 163 exit 1 164 else 165 echo "stopped" 166 exit 1 167 fi 168 fi 169 fi 170 fi 171 172 #running, get priviate IP Address 173 IpAddr=`aws ec2 describe-instances --instance-ids $instanceName --region $Region --output text --query 'Reservations[*].Instances[*].PrivateIpAddress'` 174 175 echo $IpAddr 176 exit 0 177} 178 179getType() { 180 #get local Instance ID 181 instanceType=`curl -s http://169.254.169.254/latest/meta-data/instance-type` 182 183 echo $instanceType 184 return 185} 186 187getKey() { 188 #get region 189 getRegion >/dev/null 2>&1 190 191 #get local Instance ID 192 getInstance >/dev/null 2>&1 193 194 key=`aws ec2 describe-instances --instance-ids $instanceName --region $Region --output text --query 'Reservations[*].Instances[*].KeyName'` 195 196 echo $key 197 return 198} 199 200getVpc() { 201 #get region 202 getRegion >/dev/null 2>&1 203 204 #get local Instance ID 205 getInstance >/dev/null 2>&1 206 207 #get VCP 208 vpc=`aws ec2 describe-instances --instance-ids $instanceName --output text --region $Region --query 'Reservations[*].Instances[*].VpcId'` 209 210 echo $vpc 211 return 212} 213 214getAMI() { 215 #get local Instance ID 216 ami=`curl -s http://169.254.169.254/latest/meta-data/ami-id` 217 218 echo $ami 219 return 220} 221 222getGroup() { 223 #get region 224 getRegion >/dev/null 2>&1 225 226 #get group id 227 groupid=`aws ec2 describe-instances --instance-ids $instanceName --region $Region --output text --query 'Reservations[*].Instances[*].SecurityGroups[*].GroupId' | grep -m 1 sg` 228 echo $groupid 229 return 230} 231 232getProfile() { 233 #get region 234 getRegion >/dev/null 2>&1 235 236 # get profile 237 instanceProfile=`curl -s http://169.254.169.254/latest/meta-data/profile` 238 239 echo $instanceProfile 240 return 241} 242 243launchInstance() { 244 #get region 245 getRegion >/dev/null 2>&1 246 247 #get publickey 248 getKey >/dev/null 2>&1 249 if [ "$groupid" = "unassigned" ]; then 250 #get group 251 getGroup >/dev/null 2>&1 252 fi 253 254 #get AMI 255 getAMI >/dev/null 2>&1 256 257 #get Zone 258 getZone >/dev/null 2>&1 259 if [ "$instanceType" = "unassigned" ]; then 260 #get type 261 getType >/dev/null 2>&1 262 fi 263 264 #get Subnet 265 getSubnet >/dev/null 2>&1 266 267 #get IAM Role 268 getRole >/dev/null 2>&1 269 270 if [ "$Role" = "" ] || [ "$Role" = "default" ]; then 271 if [ "$groupid" != "default" ]; then 272 if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ] ; then 273 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` 274 else 275 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` 276 fi 277 else 278 if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then 279 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` 280 else 281 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` 282 fi 283 fi 284 else 285 if [ "$groupid" != "default" ]; then 286 if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then 287 newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile "Name=$Role" --subnet-id $subnet --image-id $ami --security-group-ids $groupid --query 'Instances[*].InstanceId' --output text` 288 else 289 newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile "Name=$Role" --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --security-group-ids $groupid --query 'Instances[*].InstanceId' --output text` 290 fi 291 else 292 if [ "$IPaddress" = "autoassign" ] || [ "$IPaddress" = "unassigned" ]; then 293 newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile "Name=$Role" --subnet-id $subnet --image-id $ami --query 'Instances[*].InstanceId' --output text` 294 else 295 newInstance=`$AWSCLI run-instances --region $Region --key-name $key --instance-type $instanceType --placement AvailabilityZone=$zone --iam-instance-profile "Name=$Role" --subnet-id $subnet --private-ip-address $IPaddress --image-id $ami --query 'Instances[*].InstanceId' --output text` 296 fi 297 fi 298 fi 299 300 echo $newInstance 301 return 302} 303 304terminateInstance() { 305 #get region 306 getRegion >/dev/null 2>&1 307 308 #terminate Instance 309 $AWSCLI terminate-instances --instance-ids $instanceName --region $Region > ${tmpdir}/termInstanceInfo_$instanceName 2>&1 310 return 311} 312 313stopInstance() { 314 #get region 315 getRegion >/dev/null 2>&1 316 317 #terminate Instance 318 $AWSCLI stop-instances --instance-ids $instanceName --region $Region > ${tmpdir}/stopInstanceInfo_$instanceName 2>&1 319 return 320} 321 322startInstance() { 323 #get region 324 getRegion >/dev/null 2>&1 325 326 #terminate Instance 327 $AWSCLI start-instances --instance-ids $instanceName --region $Region > ${tmpdir}/startInstanceInfo_$instanceName 2>&1 328 329 cat ${tmpdir}/startInstanceInfo_$instanceName | grep InstanceId > ${tmpdir}/startInstanceStatus_$instanceName 330 if [ `cat ${tmpdir}/startInstanceStatus_$instanceName | wc -c` -eq 0 ]; then 331 echo "Failed, check ${tmpdir}/startInstanceInfo_$instanceName" 332 exit 1 333 fi 334 echo "Success" 335 exit 0 336} 337 338assignElasticIP() { 339 #get region 340 getRegion >/dev/null 2>&1 341 342 EIP=`$AWSCLI describe-addresses --region $Region --public-ips $IPAddress --query 'Addresses[*].AllocationId' --output text` 343 344 $AWSCLI associate-address --region $Region --instance-id $instanceName --allocation-id $EIP > ${tmpdir}/assignElasticIPInfo_$IPAddress 2>&1 345 346 cat ${tmpdir}/assignElasticIPInfo_$IPAddress | grep error > ${tmpdir}/assignElasticIPStatus_$IPAddress 347 if [ `cat ${tmpdir}/assignElasticIPStatus_$IPAddress | wc -c` -ne 0 ]; then 348 echo "Failed, check ${tmpdir}/assignElasticIPInfo_$IPAddress" 349 exit 1 350 fi 351 352 echo "Success" 353 exit 0 354} 355 356deassignElasticIP() { 357 #get region 358 getRegion >/dev/null 2>&1 359 360 EIP=`$AWSCLI describe-addresses --region $Region --public-ips $IPAddress --query 'Addresses[*].AssociationId' --output text` 361 362 $AWSCLI disassociate-address --region $Region --association-id $EIP > ${tmpdir}/deassignElasticIPInfo_$IPAddress 2>&1 363 cat ${tmpdir}/deassignElasticIPInfo_$IPAddress | grep error > ${tmpdir}/deassignElasticIPStatus_$IPAddress 364 if [ `cat ${tmpdir}/deassignElasticIPStatus_$IPAddress | wc -c` -ne 0 ]; then 365 echo "Failed, check ${tmpdir}/deassignElasticIPStatus_$IPAddress" 366 exit 1 367 fi 368 369 echo "Success" 370 exit 0 371} 372 373getSubnet() { 374 #get region 375 getRegion >/dev/null 2>&1 376 377 #get local Instance ID 378 getInstance >/dev/null 2>&1 379 #get Subnet 380 subnet=`aws ec2 describe-instances --instance-ids $instanceName --region $Region --output text --query 'Reservations[*].Instances[*].SubnetId'` 381 382 if [[ $subnet == *"subnet"* ]] 383 then 384 echo $subnet 385 else 386 echo "failed" 387 fi 388 389 return 390} 391 392 393case "$1" in 394 getInstance) 395 getInstance 396 ;; 397 getZone) 398 getZone 399 ;; 400 getPrivateIP) 401 getPrivateIP 402 ;; 403 getKey) 404 getKey 405 ;; 406 getAMI) 407 getAMI 408 ;; 409 getType) 410 getType 411 ;; 412 launchInstance) 413 launchInstance 414 ;; 415 terminateInstance) 416 terminateInstance 417 ;; 418 stopInstance) 419 stopInstance 420 ;; 421 startInstance) 422 startInstance 423 ;; 424 assignElasticIP) 425 assignElasticIP 426 ;; 427 deassignElasticIP) 428 deassignElasticIP 429 ;; 430 getProfile) 431 getProfile 432 ;; 433 getGroup) 434 getGroup 435 ;; 436 getSubnet) 437 getSubnet 438 ;; 439 getVpc) 440 getVpc 441 ;; 442 getRegion) 443 getRegion 444 ;; 445 getRole) 446 getRole 447 ;; 448 *) 449 echo $"Usage: $0 {launchInstance|getInstance|getZone|getPrivateIP|getType|getKey|getAMI|terminateInstance|startInstance|assignElasticIP|deassignElasticIP|getProfile|stopInstance|getGroup|getSubnet|getVpc|getRegion|getRole}" 450 exit 1 451esac 452 453exit $? 454