Simple bash scripting for “azure” cli

The azure cli is a great tool for easily working with Windows Azure interactively. You can also use it for scripting  right from bash/gitbash. The bash shell offers a bunch of tools at your disposal like grep, awk, sed, and xargs which are designed specifically to make your scripting life easier.

If you don’t know about these tools, here is a one liner for each.

  • grep takes incoming text and pulls out lines that contain a matching pattern or regular expression. It returns a file that has the matched lines. It can also do an inverse match returning all lines that don’t match.
  • awk takes delimited text, parses the delimited data, and feeds the values into a script. 
  • sed takes an incoming stream of characters and feeds the stream to a script.
  • xargs takes an incoming set of arguments and invokes a command passing in the args.
  • cut takes incoming text and lets you cut specific text blocks out.

To script with our cli, you will call “azure” and pipe the results into one or more of the tools I listed (or similar tools) which applies some filtering or transformation and ultimately makes additional “azure” calls. From my own playing around, I found that a combination of “grep” and “awk” were basically sufficient because our cli returns data in a columnar format. However any of the tools above can work.

Below is a list of some common simple scripting scenarios for websites, which includes the command and a small explanation of what each is doing. This should get you started and you can easily take the pattern and apply to other parts of our cli.

Disclaimer, I am not at all a bash expert, so there probably are better ways to do what I am showing you.

Stop all running sites

  • Lists out all websites.
  • Pipes to grep which then matches on all lines that have the word “Running”. This filters out anything but the lines for running sites.
  • Pipes to awk and then for each row calls “azure site stop” passing the site name (the second column that was parsed)

Start all stopped sites

  • Lists out all websites.
  • Pipes to grep which then matches on all sites that have a status of “Stopped”. This filters out anything but the lines for stopped sites.
  • Pipes to awk and then for each row calls “azure site start” passing the site name.

Delete all running or stopped sites

  • Lists out all websites.
  • Pipes to grep which then matches on all sites that have a status of “Stopped” or “Running”.
  • Pipes to awk and then for each row calls “azure site delete” passing the site name. –q is passed as well in order to override the prompt for deleting.

Clear and add an app setting to every site

  • Lists out all websites.
  • Pipes to grep which then matches on all sites that have a status of “Running”.
  • Pipes to awk and then for each row calls “azure site config clear foo ” passing the site name. This removes the setting if it previously exists.
  • Retrieves running sites again and then pipes to awk to call “azure site config add foo=bar” passing the site name. This adds the foo setting to all sites.

What about the other tools?

When I first started playing around I look at xargs and cut. The approach is basically using cut to pull the site name and then pass it to xargs. I almost went with that approach until I realized it was not necessary as awk will parse delimiters. I am much happy not having to use char positions as that is more brittle.

Here’s the code

  • Pipes the list of websites to cut which extracts the text between char positions 10-32 for each line that is piped in.
  • Pipes the resulting list to xargs which invokes “azure site start” passing in the arguments. By default xargs will pass all the lines to the the command, “-n 1” says only pass a max of one line to the command, thus “azure” will get called for each line.

Getting azure calls to run in the background.

If you run all the previous calls, you will see that everything executes synchronously meaning sites are deleted one after the other. You can force them to run in the background, though when you do the console output may look a little wacky. To do that you just add an ‘&’ at the end of the shell command, for example below I have modified the delete sites script to run in the background.

Merry scripting

This is just the tip of the iceberg of what you can once you start scripting the cli. My coworker Josh also just blogged about how you can use the same approach for managing your Azure Mobile Service scripts. If you are using Mobile Services, be sure to check it out!

If you’ve got any cool scripts you wanna share, post links in the comments. Also let if there are specific things you’d like to see to improve the scripting experience.

Thanks to @xpaulbettsx, @subdigital@davidmfoley, and @einaros for their scripting help!

Happy Holidays!

This entry was posted in azure, cli. Bookmark the permalink. Follow any comments here with the RSS feed for this post.