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

OnePlus 3T in 2017

Hi there, I just bought OnePlus 3T, why? Even when OnePlus 5 is out and OnePlus 5T looming around the corner. Top five reasons why the 3T might just be a very sound investment if you are in the market for a new phone. 3T is going for a relatively cheap price. Its wickedly fast. Its 1080p display. Its software. OnePlus 5 isnt a huge update. Key Specs Display - 5.50-inch Processor - 1.6GHz quad-core Front Camera - 16-megapixel Resolution - 1080x1920 pixels RAM - 6GB OS - Android 7.1.1 Storage - 64GB Rear Camera - 16-megapixel Battery Capacity - 3400mAh Overall Rating 9/10 - Design 9/10 - Display 9/10 - Software 9/10 - Performance 9/10 - Battery life 9/10 - Camera 9/10 - Value for money Important - No other phone with all round 9's. Good Solid system and app performance Very good battery life Competent set of cameras Amazing premium build quality Good value for money Bad Nothing as yet

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

Google tablet to give Apple a touch of its own medicine

Google is trying to one-up Apple, showing off designs for a new tablet computer based on its Chrome operating system that would be a direct rival to the iPad. Just weeks after launching its own iPhone competitor in the US, the Nexus One, Google might soon extend its competition with Apple further as it seeks to push its search and other products on to as many devices as possible. Google's user interface designer, Glen Murphy, published mock-ups of a Google tablet on the search giant's Chromium.org website, along with a video of how users would interact with the device. Late last year Google announced Chrome OS, an operating system predominantly for small netbooks that would be based around the web browser, providing quick boot times and easy access to Google's array of online services. The first Chrome OS netbooks are due to arrive this year, but Google is now considering extending the platform to other devices including tablets, desktops and even big screen TVs. Chrome OS ...