Thinking inside, outside and around the box is a big part of what I do as a Cloud Solution Architect. For the last few weeks, I have been exploring stuff related to Azure, Kubernetes and automating certain tasks which the current ways of automation do not allow.
Last week, this quest lead me to two awesome projects on GitHub - ttyd and GoTTY. Both these projects allow you to turn any command line tool to a web app. This got me started with building something that uses this newfound power to do things in a new way and I came up with UniTTY.
UniTTY is a Linux container image that comes preintalled with Azure CLI, PowerShell Core 7 and GoTTY. You can head over to the project here - https://github.com/ashisa/unitty - to read more about UniTTY and learn how to use it because here we will just see how I am using it for automation.
The problem that I want to solve with UniTTY deals with deploying services on Azure. You’d think why is that a big deal? And, you are right! - deploying Azure services and automating them isn’t a big deal and there are a lot of ways to do that - ARM templates, Azure CLI, PowerShell, Ansible, Terraform, REST APIs and more but what I need to solve for is that the ISV partner I am working with, needs to deploy a set of Azure Services on their customers’ subscriptions and configure them uniquely for each customer.
The tricky part is it also needs to register a resource provider so I needed something that goes beyond the current scope of automation possible today.
How does UniTTY solve it for me? With the simple matter of converting my shell scripts to a web app and allowing me to send a bunch of arguments using the URL!
Let’s take an example where I use UniTTY to create a resource group and virtual machine and send the names of these entities using the URL.
We can accomplish this in two ways - first, we send everything using the URL or we can create a script which can be included with the image by adding the steps to the Dockerfile available in the UniTTY’s GitHub repo.
In each case, we need to run the following commands -
az login
az group create --name myrg --location southindia
az vm create --name myvm01 --resource-group myrg --image UbuntuLTS
To run these commands as a web app, you can run UniTTY using Docker as following -
docker run -it -p 8080:8080 docker.io/ashisa/unitty gotty /bin/bash -c "az login && az group create --name myrg --location southindia && az vm create --name myvm01 --resource-group myrg --image UbuntuLTS --admin-username 'vmadmin' --admin-password 'R$ND0MPA%%WD'"
Single quotes are needed since there are special characters in the password.
Once you run it and browse to http://127.0.0.1:8080, you will see that you are prompted to connect to your Azure subscription which will be followed by the resource group and VM creation steps.
If you want to do this using a script, it would look like the following -
#!/bin/bash
RG=$1
VM=$2
if [ "$RG" == "" ] || [ "$VM" == "" ]
then
echo "syntax: $0 resourcegroup-name vm-name"
echo
echo "Send the argument using this URL - http://127.0.0.1:8080/arg=myrg%20myvm01"
else
az login
az group create --name $RG --location southindia
az vm create --name $VM --resource-group $RG --image UbuntuLTS --admin-username 'vmadmin' --admin-password 'R$ND0MPA%%WD'
fi
This script requires two arguments - name of the resource group and the name of the VM and the syntax will look like the following -
<scriptname> <resource group name> <vm name>
If you decide to use the script route, you will need to add the script to the image and build UniTTY with the following diretives in the Dockerfile -
# Downloading the script from GitHub
RUN curl https://raw.githubusercontent.com/ashisa/unitty/master/script-cmd/azurecli-script.sh -o ~/azurecli-script.sh \
&& chmod a+x ~/azurecli-script.sh
# Running the script as CMD
CMD [ "/usr/bin/gotty", "--permit-arguments", "/root/azurecli-script.sh" ]
An example is available at https://github.com/ashisa/unitty/tree/master/script-cmd. You can clone the repo and make changes to the script as you wish.
Build the image -
docker build -t ashisa/unitty-script .
Once you’ve built the image, you can run it as following -
docker run -it -p 8080:8080 ashisa/unitty-script
Now you can launch the browser and provide the script arguments using the following URL -
http://127.0.0.1:8080/?arg=myrg&arg=myvm02
You can use GoTTY options such as restricting how many clients can connect to it and how many times; as well as randomizing the URL and adding username/password to access the URL so that should cover the essentials when it comes to security.
Stay tuned for more things that become easy and possible with UniTTY.