共用方式為


Step-by-Step: Automated Provisioning for Linux in the Cloud with Microsoft Azure, XPlat CLI, JSON and Node.js ( Part 1 )

Since previously publishing the Quick Start Guide for Building Highly Available Linux Servers in the Cloud on Microsoft Azure, several people have asked me about ways in which Linux workload provisioning can be automated with Azure.

Automated Provisioning for Linux in the Cloud with Microsoft Azure, XPlat CLI, JSON and Node.js

There's lots of tools that can be leveraged for automating Linux workloads on Microsoft Azure, including Azure Automation, PowerShell DSC for Linux, VM Agent Custom Scripts, Cloud-Init, XPlat CLI for Azure, Vagrant, Docker and third-party tools such as Chef and Puppet.  The Azure team provides a wide variety of automation options so that you can choose the tools with which you're most familiar and, in some cases, may already have an existing investment.

This article is part 1 of a two-part series.  In this article, we'll step through the process for getting our Linux admin workstation setup for Azure cloud automation using the XPlat-CLI for Azure and Cloud-Init

In part 2 of this series, we'll leverage these tools for automatically provisioning a highly available Linux server farm environment using the scenario outlined in the Quick Start Guide referenced above.

What is the XPlat CLI for Azure?

The XPlat CLI is a cross-platform Azure command-line interface for Linux, Mac and Windows administrators. If you're familiar with writing shell scripts, the XPlat CLI for Azure provides an easy way to get started with end-to-end automation of Linux environments on Azure.  Using the XPlat CLI, you can easily provision Azure fabric resources, such as storage, networking and VMs, along with OS and application configurations inside each VM.

The XPlat CLI is written in JavaScript and is implemented using the Azure SDK for Node.js. It's been released under an Apache 2.0 license and the project repository is located at https://github.com/Azure/azure-xplat-cli.

Let's Get Started ...

To prepare for following along with this article, be sure to complete the following steps:

  1. Activate your Azure subscription. If you don't yet have an active Microsoft Azure subscription, sign-up for a free trial subscription.
     
  2. Download the Quick Start Guide . This will be a helpful resource with which you can follow along if you're not familiar with how Linux VMs work with Azure.
     
  3. Setup a Linux admin workstation. In this article, I'll be using Ubuntu 14.04 LTS for my admin workstation. If you don't currently have an admin machine setup, use the steps in the Quick Start Guide to get yours setup in the Cloud on Azure. 
     
    Note: If you're using a different Linux distro for your admin workstation than Ubuntu 14.04, the installation steps below will be a bit different.  However, the XPlat CLI commands for provisioning Azure resources will be exactly the same after you've gotten everything installed.

Installing the XPlat CLI for Azure

The XPlat CLI for Azure requires node.js and npm, so we'll need to install those first. On Ubuntu 14.04, we'll also need to install the nodejs-legacy package.

sudo apt-get update
sudo apt-get install -y nodejs nodejs-legacy npm

After installation, you can confirm the versions of node and npm that is installed by using the following commands:

node --version
npm --version

My admin machine is running node v0.10.25 and npm v1.3.10.  If you're running a very old version of node and/or npm, be sure to update to the latest versions of each before continuing.

After the nodejs and npm packages are installed, we can install the XPlat CLI for Azure using npm.

sudo npm install azure-cli --global

After the azure-cli package is installed, you can check the version of the XPlat CLI using the following command:

azure --version

The XPlat CLI is updated on a regular basis.  Check the ChangeLog to make sure that you're using the latest version.  If you find that you need to update the azure-cli package, you can easily do so with the following command:

sudo npm update azure-cli --global

After installing the azure-cli package, you may wish to enable auto-completion of command lines to make it easier to work with this CLI tool.  You can enable auto-complete each time you login by using these commands:

azure --completion >> ~/azure.completion.sh
echo 'source ~/azure.completion.sh' >> .bash_profile

To enable command line auto-completion for your current session without needing to logout/login, run this command:

source ~/azure.completion.sh

What can I do with the XPlat CLI for Azure?

The XPlat CLI includes some pretty extensive built-in help for the various CLI commands and options.  To get help on the list of available commands, you can run:

azure --help

To get help on the various actions and options available for each command, simply run the command with the --help option specified at the end of the command line. For example, to see help on creating VM's via the CLI, you could run:

azure vm --help

or

azure vm create --help

You may have noticed in the help ouput that the XPlat CLI includes a --json option to output information in JSON format. This brings us to the next step ...

Installing JSON command line tools

After installing the XPlat CLI, you may also wish to install a JSON command line tool, such as jsawk or jq.  The XPlat CLI can return information in JSON format when using the --json option, and it's helpful to have command line tools that can easily parse JSON ouput when building shell scripts.  In this article series, we'll be using jsawk.

Before installing jsawk, we'll first need to install the spidermonkey-bin package, which jsawk depends upon.  Use these commands to download, build and install the spidermonkey-bin package.

sudo apt-get install mercurial autoconf2.13 -y
hg clone
https://hg.mozilla.org/mozilla-central/
cd mozilla-central/js/src
autoconf2.13
./configure
make
sudo make install

After you've completed these steps, you can confirm that the spidermonkey-bin js interpreter is correctly installed by running this command:

js --help

Now, we can install jsawk with the following commands:

curl -L https://github.com/micha/jsawk/raw/master/jsawk > jsawk
chmod 755 jsawk
sudo mv jsawk /usr/bin/

We can confirm that the jsawk command line tool is properly installed by running this command:

echo | jsawk 'return "jsawk is installed"'

Next steps ...

Our Linux workstation is now prepared with the tools necessary to automate Linux workload provisioning on Azure.  In part 2 of this article series, we will continue with creating a script that will authenticate to our Azure subscription and build the highly available Linux server farm environment depicted in our Quick Start Guide.

Stay tuned for more in the Clouds!

Keith