In my last post I showed you how to write a reusable Powershell script for most of the IaaS VM provisioning you need to do and how to use CustomScriptExtension to install stuff inside the VM at creation time. I deliberatly skipped over Linux although I hinted the Powershell script handled it.
Now it’s time to show you how CustomScriptExtension work for Linux and how to install Apache webserver on a SUSE Linux VM at creation time. There are a number of really good blogs about this, and I have noted them at the end, but being a dev guy I found they miss what actually happens when the VM is created.
The end result – an autodeployed Apache webserver
The end result is getting the Apache webserver to respond with the below test page when running the provisioning Powershell script from my laptop. It is a SUSE Linux Enterprise Server 12 that we will deploy to Azure and where we will execute a bash script as a CustomScriptExtension to install Apache on the fly and create the testpage.html file.
To achieve this, I run the reusable Powershell deployment script like below
The xml in the config file for provisioning the SUSE server looks likes this. The difference from a Windows server is the ImageFamily and the FileName in the CustomScriptExtension. The FileName is in this case not a Powershell but a bash script, but it is uploaded to the current storage account in the ContainerName as the Powershell scripts for Windows servers, which makes the config very similar.
Inside the reusable Powershell deployment script, the way a CustomScriptExtension is handled for Linux is different. Check out the links I provide at end to get a more detailed explanation of the other arguments.
It is a more low level way it is handled for Linux servers and we have to create a PrivateConfiguration and a PublicConfiguration . The private one contains the name and key for the storage account so that CustomScript Handler inside the Linux machine can access the blob storage account we are pointing to. The Public one holds a JSON datasctructure with the URI for the bash script in the blob storage container and the commandToExecute, which is the exact command that you start of the script. Some Microsoft examples just use sh as the shell script, but since the script I have created is a bash script I invoke it with bash.
The bash script and what happens inside the Linux server
The bash script can contain anything you like to do inside the Linux machine and is probably obvious for an Open Source guy. For a Windows dev moonlightning on Linux, it can be more challanging and one thing to remember is to make sure that the lines end with Unix style ending and not \r\n like in Windows. If you get this wrong your script will most probably not work. If you like me use Sublime Text, you can change the line endings under menu View > Line Endings > Unix
My bash script is simple enough so I can prove my point. I installs Apache webserver, creates a tiny html page and restarts Apache.
During provisining of the Linux server in Azure, you will see the custom script executing if you refresh frequently.
What the other links do not explain too good, in my opinion, is what happens inside the server with your script. If you come from a Windows background, you might find the “find / -name ‘myfile*’” command helpful since it is the equivalent of Windows “dir /S \myfile*.*” and you can snoop around to see where the script landed.
To begin with, to check what happened when you script executed (or not) can be found in the folder
/var/log/azure/Microsoft.OSTCExtensions.CustomScriptForLinux/1.2.2.0
There you have a log file named extension.log that will be of interest when you debug your deployment script. It is in this logfile I found out the problem I had with line endings
You bash script will be downloaded to another folder, which is
/var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.2.2.0/download/0/
OSTC stands for Open Source Technology Center and is the development team within Microsoft that does all the open source stuff such as this one.
You don’t need to do alot of sudo stuff in the bash script since according to my testing the script runs with high enough priviledges to install software as needed.
Summary
As you can see, CustomScriptExtensions is not just for Windows IaaS VMs and you can use it to automate the deployment of software inside a Linux server. I showed you that my Powershell deployment script can create a SUSE Linux server with automatic installation of the Apache webserver. Pretty basic, but I’m sure you open source guys out there see the potential of what you can do.
Links to CustomScriptExtensions for Linux
Automate Linux VM Customization Tasks Using CustomScriptExtension
http://azure.microsoft.com/blog/2014/08/20/automate-linux-vm-customization-tasks-using-customscript-extension/
Installing the LAMP stack on a Linux VM in Azure
http://azure.microsoft.com/sv-se/documentation/articles/virtual-machines-linux-install-lamp-stack/#installing-on-suse-linux-enterprise-server
Formating DataDisks with Azure CustomScriptExtensions for Linux
http://blog.fullscale180.com/format-data-disks-with-azure-vm-custom-script-extension-for-linux/
Sources for this blog post can be found here
http://data.redbaronofazure.com/public/IaaS_CSE.zip