Azure from the Linux command line (part 1)
Hi!
Since i've been gowing up IT-wise with a unix command shell, I tend to do a lot of things with it. Also managing my Azure deployments since there's the great Azure command line interface or cross platform ("xplat") CLI.
(If you're interested in the details, this is all open source, released under an Apache license, and on github: https://github.com/WindowsAzure/azure-sdk-tools-xplat.)
This blog post documents a few tricks I've been using to get up and running fast.
First: You need to connect the xplat cli to your azure subscription. To do that simply run
$ azure download
after installing the cli. If you're on a remote machine via ssh, this will simply give you an URL to launch in your browser. Make sure you're already logged into the azure portal, otherwise you will need to login first when going to this URL.
The website will now give you a .publishsettings file for download. The same file is used when setting up a connection between Visual Studio and an Azure subscription.
Now get this file to your linux box (and make sure you keep it safe in transit, this file contains a management certificate key that can manage your subscription!) and import it into xplat cli:
$ azure account import <publishsettingsfile>
And now you're all set.
Now let's look around
$ azure help
info: Executing command help
info: _ _____ _ ___ ___
info: /_\ |_ / | | | _ \ __|
info: _ ___/ _ \__/ /| |_| | / _|___ _ _
info: (___ /_/ \_\/___|\___/|_|_\___| _____)
info: (_______ _ _) _ ______ _)_ _
info: (______________ _ ) (___ _ _)
info:
info: Windows Azure: Microsoft's Cloud Platform
info:
info: Tool version 0.7.4
help:
help: Display help for a given command
help: help [options] [command]
help:
help: Open the portal in a browser
help: portal [options]
help:
help: Commands:
help: account Commands to manage your account information and publish settings
help: config Commands to manage your local settings
help: hdinsight Commands to manage your HDInsight accounts
help: mobile Commands to manage your Mobile Services
help: network Commands to manage your Networks
help: sb Commands to manage your Service Bus configuration
help: service Commands to manage your Cloud Services
help: site Commands to manage your Web Sites
help: sql Commands to manage your SQL Server accounts
help: storage Commands to manage your Storage objects
help: vm Commands to manage your Virtual Machines
help:
help: Options:
help: -h, --help output usage information
help: -v, --version output the application version
That does not look to bad after all. Just remember azure help <command>,this is your first stop whenever you get stuck.
So let's set up a linux VM. First let's check what pre-configured linux images are available.
$ azure vm image list
Now you should see a lot of images. When I just ran this, I got more that 200 lines of output. Image names look like this:
b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-13_10-amd64-server-20140226-en-us-30GB
Now we could copy this name to our clipboard and paste it into the next command, but let's have the shell do that for us, here's the idea:
#!/bin/bash
IMAGENAME=`azure vm image list |grep -i Ubuntu-13_10-amd64-server |tail -1 | awk '{print $2}'`
Get the list of VM images, just select the ones we're interested in, then select the last (i.e. the most recent one) of that list and just give me the second string which is the image name. Easy, right? Note the back single quotes in the beginning and the end of that line, this is shell syntax for "take the output of that command and store it in that shell environment variable".
To use the VM, we need to login, so let's use a password for now:
PASSWORD="AtotallySECRET!PA55W0RD"
echo Password is $PASSWORD
Next, let's create the VM:
azure vm create -e -z extrasmall -l "West Europe" $1 $IMAGENAME azureuser "$PASSWORD"
Here's the output of running this shell script:
$ bash create_ubuntu_vm.sh contosolinux
Password is AtotallySECRET!PA55W0RD
info: Executing command vm create
+ Looking up image b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-13_10-amd64-server-20140226-en-us-30GB
+ Looking up cloud service
+ Creating cloud service
+ Retrieving storage accounts
+ Creating VM
info: vm create command OK
And after about two minutes I can ssh into contosolinux.cloudapp.net as "azureuser" with that super secret password.
Hope it helps,
H.
ps: to get rid of the VM again, I just type azure vm delete -b contosolinux
pps: in case that's too harsh, azure vm shutdown contosolinux, azure vm start contosolinux and azure vm restart contosolinux work as well. And azure vm list shows you what Azure thinks your VMs are doing right now.
ppps: And in case you were wondering why there was no root password set: just run sudo bash from this initial user account.