Sunday 16 November 2014

The New Script - The Cloudbusters are busting into the Cloud!!!!

Busting into the Cloud

This will be the final script for this weekend, with all the functions that we had decided in it.

Just to remind you of the algorithm:

1. Check Capacity of AWS environment
2. If AWS has available capacity then deploy instance to AWS
3. Else deploy instance to Azure

Our capacity counter is now core count.

Lets do the script:

#This script will deploy an instance to our Cloud, private or public.
#Where the instance is deployed to is determined whether we have reached our
#Capacity limit. Our capacity limit is the number of cpus which we have
#available to us in our environment. In this script, the number is set at 6.

#$ec2instanceinfo is the info from aws about the vcpus deployed
#to each instancetype

#Clearing out any variables in case they were in use somewhere else

Clear-Variable ec2instanceinfo
Clear-Variable Filter
Clear-Variable ec2numbercpus
Clear-Variable numberAWSInstances

#This Variable ec2instanceinfo has the cpu details for each ec2 instancetype

$ec2instanceinfo = @{
"t2.micro"=1;"t2.small"=1;"t2.medium"=2;
"m3.medium"=1;"m3.large"=2;"m3.xlarge"=4;
"m3.2xlarge"=8;"c3.large"=2;"c3.xlarge"=4;
"c3.2xlarge"=8;"c3.4xlarge"=16;"c3.8xlarge"=32;
"g2.2xlarge"=8;"r3.large"=2;"r3.xlarge"=4;
"r3.2xlarge"=8;"r3.4xlarge"=16;"r3.8xlarge"=32;
"i2.xlarge"=4;"i2.2xlarge"=8;"i2.4xlarge"=16;
"i2.8xlarge"=32;"hs1.8xlarge"=16}

#Need to make sure we're doing the right region:

Set-DefaultAWSRegion -Region us-west-2

#$numberAWSInstances = Get-EC2Instance -region us-west-2

#Get the list of instances, and add them to a table - along with some other info
#had to reference this bit from
#http://stackoverflow.com/questions/18090022/how-to-access-list-value-in-get-ec2instances-runninginstance-method

$instances = Get-EC2Instance `
             |%{ $_.RunningInstance } `
             | Select-Object InstanceId
           

#This bit loops through each instanceid returned in the instances variable
#and returns the number of cpus in each instance, by referencing the number
#of cpus against the instancetype held in the hash table above.
#It then counts up the total number of cpus deployed and leaves that in the
#variable $ec2numbercpus

foreach ($i in $instances.InstanceId)
{

$instance_type = Get-EC2InstanceAttribute -InstanceId $i -Attribute instanceType

$ec2numbercpus = $ec2numbercpus + $ec2instanceinfo[$instance_type.InstanceType]


}

#Setting our Capacity Limit

$CapacityLimit = 6

echo 'Number of Cpus in use is' $ec2numbercpus

#If we've reached the ceiling:

If ($ec2numbercpus -ge $CapacityLimit)
{

#Deploy an Ubuntu Instance to Azure

echo "Cloudbusting!!! :) - Deploying Azure Instance"

Set-AzureSubscription -SubscriptionName (Get-AzureSubscription).SubscriptionName ` -CurrentStorageAccountName (Get-AzureStorageAccount).Label -PassThru
$azureimage = Get-AzureVMImage | Where ImageName -Match "Ubuntu-14.04" | sort PublishedDate | Select ImageName -First 1
$azurevm = New-AzureVMConfig -Name richiesubuntu -ImageName $azureimage.ImageName -InstanceSize Small | Add-AzureProvisioningConfig -Linux -LinuxUser XXXXX -Password XXXXX | Set-AzureSubnet -SubnetNames VMNetwork
New-AzureVM -VMs $azurevm -ServiceName richiescloud -VNetName AzureNetwork -WaitForBoot

}

Else

{

#Otherwise Deploy to AWS

echo "No Cloudbusting :( - Deploying EC2 Instance"

$amazonimage = Get-EC2ImageByName Windows_2008_Base
New-EC2Instance -ImageId $amazonimage.ImageId -MinCount 1 -MaxCount 1 -InstanceType t2.micro -SecurityGroupId sg-82dcb0e7 -SubnetId subnet-50ec2e27

}


And that works!! Heres the output (script ran twice, one to deploy an EC2 Instance when I have 5 cpus deployed in AWS, and then another one straight after when the results of the first run leaves me with 6 cpus deployed) - I XXXed out anything which might be construed as a security risk.

PS C:\Users\Richie> C:\Users\Richie\Documents\MSc Cloud Computing\Software\Scripts\CloudbustersAutoDeploy.ps1
Number of Cpus in use is
5
No Cloudbusting :( - Deploying EC2 Instance


GroupNames    : {}
Groups        : {}
Instances     : {}
OwnerId       : xxxxxxxxxxxxxxx
RequesterId   :
ReservationId : r-20e6d02d

Instance Deployed to AWS



PS C:\Users\Richie> C:\Users\Richie\Documents\MSc Cloud Computing\Software\Scripts\CloudbustersAutoDeploy.ps1
Number of Cpus in use is
6
Cloudbusting!!! :) - Deploying Azure Instance
WARNING: GeoReplicationEnabled property will be deprecated in a future release of Azure PowerShell. The value will be
merged into the AccountType property.


Id          : xxxxxxxxxxxxxxxxxxxx
Name        : xxxxxxxxxxxxx
Environment : AzureCloud
Account     : xxxxxxxxxxxxx
Properties  : {[SupportedModes, AzureServiceManagement,AzureResourceManager], [Tenants,
              xxxxxxxxxxxxxxxxxx], [Default, True], [StorageAccount, richiesstorageaccount]}

OperationDescription : New-AzureVM
OperationId          : 154573c6-656a-3c0b-91cf-67d6ea87f2fa
OperationStatus      : Succeeded

Instance Deployed to Azure



So - thats it. The script is done.

We may clean up the environment a little if we have time,

Thats it from me this weekend.

Richie

No comments:

Post a Comment