Saturday 15 November 2014

Putting the scripts together

Putting all the scripts together for an autodeploy

In this we're going to put all the scripts together. So initially i'm not going to take in any arguments for the script. I'm setting the 'instance ceiling' at 5 (which means that the total number of AWS instances that i'm allowing in my AWS environment is 5).

So the algorithm is:

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

So - lets figure out how we are going to do this. New script please:

#This script determines the number of AWS instances deployed, and
#if the number deployed is equal to or greater than the instance ceiling,
#then we're deploying to Azure. If its lower, then we're deploying to AWS.

#Check the number of instances in AWS

$InstanceCeiling = 5

Clear-Variable numberAWSInstances

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

echo $numberAWSInstances.Count


#If we've reached the ceiling:

If (InstanceCeiling => $numberAWSInstances.Count)
{

#Deploy an Ubuntu Instance to Azure

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 XXXXXX | Set-AzureSubnet -SubnetNames VMSubnet
New-AzureVM -VMs $azurevm -ServiceName richiescloud -VNetName RichieNetwork -WaitForBoot

}

Else

{

#Otherwise Deploy to AWS

$amazonimage = Get-EC2ImageByName Windows_2008_Base
New-EC2Instance -ImageId $amazonimage.ImageId -MinCount 1 -MaxCount 1 -InstanceType t2.micro

}


So here we are with the scripts put together (i'll clean up what images i'm using later - i'm just concentrating on the logic for the moment)

Lets give it a go and see how we go. I've set the instance ceiling at 5 - and i have 5 instances deployed in aws - so this should deploy to azure.

Testing.

=> doesnt apply in powershell - its -ge (greater than or equal to)

I have the instance count and the instance ceiling variable the wrong way around - it should be:

If $numberAWSInstances.count -ge $InstanceCeiling

The logic is now good - but its asking me for the Add-AzureAccount to be run again.

I want to deploy it to 'AzureNetwork' as thats the vpn connected to aws. need to change that.
Also need to change it to VMNetwork as opposed to VMSubnet

On the AWS deploy its telling me i dont have a default vpc. Must have deleted it. So now i have to specify a security group and a subnet using the -SecurityGroupId and -SubnetId switches.

oh - and I was struggling getting it deployed because i had specfied the incorrect region. The region is so important for these scripts. Set the default every time and just make sure you're in the right region.

Heres our script:

#This script determines the number of AWS instances deployed, and
#if the number deployed is equal to or greater than the instance ceiling,
#then we're deploying to Azure. If its lower, then we're deploying to AWS.

#Check the number of instances in AWS

Set-DefaultAWSRegion us-west-2

$InstanceCeiling = 6

Clear-Variable numberAWSInstances

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

echo $numberAWSInstances.Count


#If we've reached the ceiling:

If ($numberAWSInstances.Count -ge $InstanceCeiling)
{

#Deploy an Ubuntu Instance to Azure

echo "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 "deploying aws 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

}


So Now - we have the script working .We have it deploying vms based on the Number of VMS in the private cloud (AWS). If there are too many - it deploys to azure.

Nice one.

Richie

No comments:

Post a Comment