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

Google leaps language barrier with translator phone

GOOGLE is developing software for the first phone capable of translating foreign languages almost instantly — like the Babel Fish in The Hitchhiker’s Guide to the Galaxy. By building on existing technologies in voice recognition and automatic translation, Google hopes to have a basic system ready within a couple of years. If it works, it could eventually transform communication among speakers of the world’s 6,000-plus languages. The company has already created an automatic system for translating text on computers, which is being honed by scanning millions of multi-lingual websites and documents. So far it covers 52 languages, adding Haitian Creole last week. Google also has a voice recognition system that enables phone users to conduct web searches by speaking commands into their phones rather than typing them in. Now it is working on combining the two technologies to produce software capable of understanding a caller’s voice and translating it into a synthetic equivalent in a foreign

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

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