Skip to main content

Liferay Azure Automation With Runbooks

What is Azure Automation? Released to General Availability in October 2014. This feature allows you to automate many common aspects of using the Azure platform, integrating with other features such as web sites, cloud services and virtual machines.
Let's see how we can leverage Azure Automation to ensure usage costs are minimized, by only running Virtual Machines when they are going to be needed (Set to normal office hours 9 AM to 6 PM). This post shows how to schedule starting and stopping of virtual machines in Azure.
Step 1: Let's start off by accessing our Azure Automation section. Create an Automation account.





Step 2: Create Secure Assets for our new Automation Account.
What are Secure Assets? These include credentials, certificates, connections, and encrypted variables. These assets are encrypted and stored in the Azure Automation using a unique key that is generated for each automation account.
Let's firstly create credential named "MyAzureCredential", this helps us holding our Azure Credential and used as a dynamic variable utilized for our PowerShell Runbooks. See how it is used in Step 5.




Step 3: Create a variable named "SubscriptionName", this will helps us holding our Azure Subscription Name and used as a dynamic variable utilized for our PowerShell Runbooks. See how it is used in Step 5.




Step 4: Create our Runbook and provided the runbook name as "StartVM"



Step 5: Writing the Runbook PowerShell command. Select "Author" click on "Draft" input the PowerShell command.


Add the below PowerShell Script to the Draft section
workflow StartVM {
          #Input Parameters
          Param (
                   [parameter(Mandatory=$true)] [String] $VMName,  
                    [parameter(Mandatory=$true)] [String] $ServiceName
          )
          #Check for Weekend and exit the workflow in case of weekend
          $day = (Get-Date).DayOfWeek
          if ($day -eq 'Saturday' -or $day -eq 'Sunday'){ exit }
          #Fetch My Azure Credential stored in Assets of our Automation Account
          $credential = Get-AutomationPSCredential –Name "MyAzureCredential"
          #Add the Azure Account
          Add-AzureAccount -Credential $credential
          InlineScript {
                    #Fetch Subscription Name stored in Assets of our Automation Account
                    $subscriptionName = Get-AutomationVariable -Name "SubscriptionName"
                    #Select the Azure Subscription
                    Select-AzureSubscription -SubscriptionName $subscriptionName
          }
          #Start the Azure Machine
          Start-AzureVM -Name $VMName -ServiceName $ServiceName
}
Click on Test, this should the Strat Up the Virtual machine specified as the "VMName" & "ServiceName".
Once the PowerShell Script is working as expected, click on Publish.
Step 6: For the Runbook "StartVM" select "Schedule" à "Link" à "Link to a New Schedule".
We would set the Type (One Time / Hourly / Daily), & Start Time as required.




Now consider the time I specified is 9:00 AM, when the scheduled time arrives my specified Virtual Machine would Start Up.
For stopping the Virtual machine at a scheduled time of 6:00 PM, I would have to repeat the above procedure from Step 4 to Step 6.
The PowerShell Script would be modified as mentioned below:
workflow StopVM {
          #Input Parameters
          Param (
                    [parameter(Mandatory=$true)] [String] $VMName,  
                    [parameter(Mandatory=$true)] [String] $ServiceName
          )
          #Check for Weekend and exit the workflow in case of weekend
          $day = (Get-Date).DayOfWeek
          if ($day -eq 'Saturday' -or $day -eq 'Sunday'){ exit }
          #Fetch My Azure Credential stored in Assets of our Automation Account
          $credential = Get-AutomationPSCredential –Name "MyAzureCredential"
          #Add the Azure Account
          Add-AzureAccount -Credential $credential
          InlineScript {
                    #Fetch Subscription Name stored in Assets of our Automation Account
                    $subscriptionName = Get-AutomationVariable -Name "SubscriptionName"
                    #Select the Azure Subscription
                    Select-AzureSubscription -SubscriptionName $subscriptionName
          }
          #Stop the Azure Machine
          Stop-AzureVM -Name $VMName -ServiceName $ServiceName
}

Comments

Popular posts from this blog

Everything about Java 8

The following post is a comprehensive summary of the developer-facing changes coming in Java 8. This next iteration of the JDK is currently scheduled for general availability in  September 2013 . Read More

Hands-on with Mozilla’s Web-based “Firefox OS” for smartphones

Launching a new mobile OS is a difficult project since the market leaders, Android and iOS, have such  a big lead. Even Microsoft, with its near-infinite financial resources and vast ecosystem of complementary products, has struggled to gain traction. And new entrants face a chicken-and-egg problem: developers don't want to write apps for a platform without many users, while users don't want to buy a phone without many apps. Mozilla, the non-profit foundation behind Firefox, believes it can tackle this dilemma. In 2011, it announced a new project  called Boot2Gecko to build an operating system around its browser. Last year the project was  re-branded Firefox OS, and Mozilla began preparations for a major push into the mobile phone market. In February, Mozilla  unveiled an impressive initial list  of hardware and network partners. If all goes according to plan, Firefox OS phones will be available in a number of countries, mostly in the developing world, la...

Three reasons Microsoft wants to kill the Windows Desktop

Microsoft's Windows Blue update to Windows 8  makes it increasingly clear that Microsoft wants to kill the Desktop.  That may seem self-defeating, but there's method in Microsoft's madness. Here are three reasons I think it wants to eventually kill the Desktop. Help Windows Phone and Windows tablets gain market share Unify the operating system Lock enterprises into future versions of Windows Read More