Created
August 9, 2021 08:49
-
-
Save mitchdenny/dc8f2a3858968e6dbdf181b641ef0c59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check that the jq utility is installed. | |
which jq > /dev/null | |
if [ $? -ne 0 ]; then | |
echo "jq package not detected. Install using: sudo apt install jq" | |
exit 1 | |
fi | |
# Check that the Azure CLI is installed. | |
which az > /dev/null | |
if [ $? -ne 0 ]; then | |
echo "Azure CLI not detected. Install using: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash" | |
exit 1 | |
fi | |
# Check that the Azure CLI is logged in. | |
az account show > /dev/null | |
if [ $? -ne 0 ]; then | |
echo "Azure CLI is not logged in. Login using: az login --use-device-code" | |
exit 2 | |
fi | |
# Check that we have a public SSH key that we can use. | |
if [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then | |
echo "SSH keys are not configured." | |
exit 2 | |
fi | |
# Compute resource prefix | |
WHOAMI=$(whoami) | |
WHEREAMI=$(hostname) | |
WHENAMI=$(($(date '+%s%N') / 1000000)) # Credit: https://unix.stackexchange.com/questions/69322/how-to-get-milliseconds-since-unix-epoch | |
WHATAMI=$(uname -a) | |
PREFIX=$(echo $WHOAMI+$WHEREAMI+$WHENAMI+$WHATAMI | sha1sum | sed 's/\(.\{8\}\).*/\1/') | |
# Create compute resource group | |
COMPUTE_RESOURCE_GROUP_NAME=${PREFIX}computerg | |
COMPUTE_RESOURCE_GROUP_JSON=$(az group create --name $COMPUTE_RESOURCE_GROUP_NAME --location westus2) | |
if [ $? -ne 0 ]; then | |
echo "Failed to create resource group: $COMPUTE_RESOURCE_GROUP_NAME" | |
exit 3 | |
fi | |
# Create sandbox resource group | |
SANDBOX_RESOURCE_GROUP_NAME=${PREFIX}sandboxrg | |
SANDBOX_RESOURCE_GROUP_JSON=$(az group create --name $SANDBOX_RESOURCE_GROUP_NAME --location westus2) | |
if [ $? -ne 0 ]; then | |
echo "Failed to create resource group: $SANDBOX_RESOUREC_GROUP_NAME" | |
exit 3 | |
fi | |
SANDBOX_RESOURCE_GROUP_ID=$(echo $SANDBOX_RESOURCE_GROUP_JSON | jq --raw-output .id) | |
# Create virtual machine. | |
VM_NAME=${PREFIX}vm | |
VM_IDENTITY_SCOPE=$(echo $COMPUTE_RESOURCE_GROUP_JSON | jq --raw-output .id) | |
VM_JSON=$(az vm create --name ${VM_NAME} --resource-group ${COMPUTE_RESOURCE_GROUP_NAME} --image "Canonical:UbuntuServer:18.04-LTS:latest" --location westus2 --ssh-key-values $HOME/.ssh/id_rsa.pub --assign-identity "[system]" --scope $VM_IDENTITY_SCOPE --role Reader) | |
VM_PUBLIC_IP_ADDRESS=$(echo $VM_JSON | jq --raw-output .publicIpAddress) | |
# Give managed identity time to be created. | |
sleep 60 | |
VM_IDENTITY_PRINCIPAL_ID=$(az vm list --query "[?name=='$VM_NAME'].identity.principalId" --output tsv) | |
# Assign permissions to sandbox resource group. | |
az role assignment create --role Owner --assignee-object-id $VM_IDENTITY_PRINCIPAL_ID --scope $SANDBOX_RESOURCE_GROUP_ID | |
# Update VM | |
az vm run-command invoke --command-id RunShellScript --scripts "sudo apt update && sudo apt upgrade -y && apt install jq -y && curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash" --name $VM_NAME --resource-group $COMPUTE_RESOURCE_GROUP_NAME | |
# Launch SSH session. | |
#ssh -o "StrictHostKeyChecking no" $VM_PUBLIC_IP_ADDRESS | |
ssh $VM_PUBLIC_IP_ADDRESS | |
az group delete --name $COMPUTE_RESOURCE_GROUP_NAME --yes --no-wait | |
az group delete --name $SANDBOX_RESOURCE_GROUP_NAME --yes --no-wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment