Last active
April 18, 2023 20:10
-
-
Save benc-uk/d73d3bbf9a2b330f7c189480b0d3f267 to your computer and use it in GitHub Desktop.
Azure snippets
This file contains hidden or 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 | |
echo -e "\e[34m»»»\e[32m 🧹 Azure resource group clearer\e[0m" | |
if [ -z "$1" ]; then | |
echo -e "\e[34m»»»\e[0m Usage: $0 \e[33m<resource group substring>" | |
exit 1 | |
fi | |
groups=$(az group list --query "[].name" -o tsv) | |
wildcard=$1 | |
delGroups=() | |
for group in $groups; do | |
if [[ "${group,,}" =~ .*"$wildcard".* ]]; then | |
delGroups+=("$group,") | |
fi | |
done | |
if (( "${#delGroups[@]}" <= 0 )); then | |
echo -e "\e[34m»»»\e[31m 😩 No resource groups found matching '$wildcard'\e[0m" | |
exit 1 | |
fi | |
IFS=$',' | |
for group in $delGroups; do | |
echo -e "\e[31m»»» 💥 Going to delete resource group: \e[0m$group" | |
read -n 1 -s -r -p $'Press any key to continue, or ctrl-c to exit\n' | |
echo -e "\e[33m»»» 👋 az group delete -g $group --no-wait --yes" | |
az group delete -g "$group" --no-wait --yes | |
done | |
This file contains hidden or 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 | |
##### deploy.sh ################################################################ | |
# Purpose: General purpose ARM template deployment script for Azure CLI | |
# Author: Ben Coleman | |
# Date: 23-06-2017, Updated 09-07-2019 | |
# Version: 2.0.1 | |
################################################################################ | |
# Defaults | |
DEFAULT_LOC='West Europe' | |
HELP=false | |
VALIDATE=false | |
DATE=`date +%Y%m%d_%H.%M.%S` | |
DEPLOY_NAME="az-deploy_$DATE" | |
SUB="" | |
GRP_PREFIX="Temp.Deployment." | |
# Display usage | |
usage(){ | |
echo -e "\ | |
\e[34m════════════════════════════════════════════════════════════ | |
az-deploy.sh - Deploy Azure Resource Manager (ARM) templates | |
════════════════════════════════════════════════════════════ | |
\e[39mParameters: | |
-t, --template \e[33mInput template file (required)\e[39m | |
[-g, --group] \e[33mResource group to deploy to, will be created\e[39m | |
[-l, --location] \e[33mRegion or location for the resource group, default: westeurope\e[39m | |
[-p, --parameters] \e[33mParameter file, if ommited then <inputfile>.parameters.json is used\e[39m | |
[-s, --subscription] \e[33mAzure subscription (name or ID) to use\e[39m | |
[-h] \e[33mShow this help text\e[39m | |
" | column -t -s ";" | |
} | |
# Param handling stuff | |
OPTS=`getopt -o t:g:p:l:s:h --long template:,group:,location:,subscription:,parameters:,help -n 'parse-options' -- "$@"` | |
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; usage; exit 1 ; fi | |
eval set -- "$OPTS" | |
# Param handling stuff | |
while true; do | |
case "$1" in | |
-t | --template ) FILE="$2"; shift; shift;; | |
-g | --group ) GROUP="$2"; shift; shift;; | |
-p | --parameters ) PARAMS="$2"; shift; shift;; | |
-l | --location ) LOC="$2"; shift; shift;; | |
-s | --subscription ) SUB="$2"; shift; shift;; | |
-h | --help ) HELP=true; shift ;; | |
-- ) shift; break ;; | |
* ) break ;; | |
esac | |
done | |
if [ ${HELP} = true ]; then | |
usage | |
exit 0 | |
fi | |
# We need the template file param at a minimum | |
if [ -z ${FILE} ]; then | |
usage | |
exit 1 | |
fi | |
echo -e "\n\e[34m╔══════════════════════════════════╗" | |
echo -e "║\e[33m ARM Template Deployer\e[34m ║" | |
echo -e "╚══════════════════════════════════╝" | |
echo -e "\e[35mBen Coleman, 2019 \e[39mv2.0.1 🚀 🚀 🚀\n" | |
echo -e "\e[34m»»» 🍳 \e[32mRunning pre-req checks\e[0m..." | |
az > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo -e "\e[31m»»» ⚠️ Azure CLI is not installed! 😥 Please go to http://aka.ms/cli to set it up" | |
exit | |
fi | |
# If not provided, infer parameter file name from the template file name | |
if [ -z ${PARAMS} ]; then | |
PARAMS="${FILE::-5}.parameters.json" | |
echo -e "\e[34m»»» 🚩 \e[32mParameter file not provided, \e[33m'$PARAMS'\e[32m is infered" | |
fi | |
# Check file existence | |
if ! [[ -e ${FILE} ]]; then | |
echo -e "\e[31m»»» ⚠️ Template file \e[33m'$FILE'\e[31m not found, exiting now!" | |
exit | |
fi | |
if ! [[ -e ${PARAMS} ]]; then | |
echo -e "\e[31m»»» ⚠️ Parameters file \e[33m'$PARAMS'\e[31m not found, exiting now!" | |
exit | |
fi | |
# Append subscription parameter to commands if provided | |
APPEND_SUB="" | |
if [ -z "${SUB}" ]; then | |
echo -e "\e[34m»»» 🔑 \e[32mUsing default subscription \e[33m'$(az account show --query name -o tsv)'\e[39m" | |
else | |
echo -e "\e[34m»»» 🔑 \e[32mUsing specified subscription \e[33m'$SUB'\e[39m" | |
APPEND_SUB="--subscription '${SUB}'" | |
fi | |
# Set default location | |
if [ -z ${LOC} ]; then | |
LOC=$DEFAULT_LOC | |
echo -e "\e[34m»»» 🚩 \e[32mLocation not provided, default \e[33m'$DEFAULT_LOC'\e[32m will be used" | |
fi | |
# Make up a res group name if it's not supplied, appended with a random number | |
if [ -z ${GROUP} ]; then | |
GROUP=$GRP_PREFIX$(shuf -i 1-100000 -n 1) | |
echo -e "\e[34m»»» 🚩 \e[32mGroup name not provided, \e[33m'$GROUP'\e[32m will be used" | |
fi | |
# Create resource group | |
echo -e "\e[34m»»» 🔨 \e[32mCreating resource group \e[33m'$GROUP'\e[39m..." | |
cmd="az group create -n $GROUP -l '$LOC' $APPEND_SUB -o none" | |
# echo $cmd | |
eval $cmd | |
# It all leads up to this one command! Start the deployment | |
echo -e "\e[34m»»» 🔨 \e[32mStarting deployment \e[33m'$DEPLOY_NAME'\e[32m Please wait\e[39m...\e[36m" | |
cmd="az group deployment create -g $GROUP $APPEND_SUB --template-file '$FILE' --parameters '@${PARAMS}' --verbose --name '$DEPLOY_NAME' -o yaml --query '[properties.{outputs: outputs},properties.{status: provisioningState}]'" | |
# echo $cmd | |
eval $cmd |
This file contains hidden or 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
# | |
# Set up your .env file before running any of these requests | |
# Set AZURE_TENANT_ID, AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET | |
# | |
### Get access token to call Azure ARM API | |
# @name getToken | |
POST https://login.microsoftonline.com/{{$dotenv %AZURE_TENANT_ID}}/oauth2/token | |
Content-Type: application/x-www-form-urlencoded | |
grant_type=client_credentials | |
&resource=https://management.azure.com/ | |
&client_id={{$dotenv %AZURE_CLIENT_ID}} | |
&client_secret={{$dotenv %AZURE_CLIENT_SECRET}} | |
### Catpure access_token from getToken response & set other globals | |
@authToken = {{getToken.response.body.access_token}} | |
@apiVersion = 2016-06-01 | |
@subId = {{$dotenv %AZURE_SUBSCRIPTION_ID}} | |
### Get something from Azure API using token | |
GET https://management.azure.com/subscriptions/{{subId}}/resourceGroups/blahblahblah | |
Authorization: Bearer {{authToken}} |
This file contains hidden or 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 | |
# Create a new Azure SSH key, from your local SSH public key. | |
az resource create -g my-res-grp -n my-ssh-key --resource-type Microsoft.Compute/sshPublicKeys --properties "{\"publicKey\": \"$(cat ~/.ssh/id_rsa.pub)\"}" |
This file contains hidden or 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
#!/usr/bin/env bash | |
set -euo pipefail | |
if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi | |
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then | |
echo -e "Usage: $(basename "$0") some-args\nBlah blah change this text" | |
exit | |
fi | |
# Easy way to make files are relative to the script | |
cd "$(dirname "$0")" | |
# Check CLI is installed | |
which az > /dev/null || { echo -e "💥 Error! Azure CLI is not installed. https://aka.ms/azure-cli"; exit 1; } | |
SUB_NAME=$(az account show --query name -o tsv) | |
test "$SUB_NAME" || { echo -e "💥 \e[31mYou are not logged into Azure!"; exit 1; } | |
TENANT_ID=$(az account show --query tenantId -o tsv) | |
echo -e "\e[32m⛅ Azure details: \e[0m" | |
echo -e " 🔑 \e[34mSubscription: \e[33m$SUB_NAME\e[0m" | |
echo -e " 🌐 \e[34mTenant: \e[33m$TENANT_ID\e[0m" | |
if [[ "${NOPROMPT-0}" != "1" ]]; then | |
read -r -p "🤔 Are these details are correct? [Y/n] " response | |
response=${response,,} # tolower | |
if [[ ! "$response" =~ ^(yes|y|"")$ ]]; then echo -e "\e[31m👋 Exiting...\e[0m"; exit 1; fi | |
fi | |
# Rest of script :) |
This file contains hidden or 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
# Change these five parameters as needed | |
SUFFIX=blah | |
ACI_PERS_RESOURCE_GROUP=Temp.CloudCode | |
ACI_PERS_STORAGE_ACCOUNT_NAME=cloudcode$SUFFIX | |
ACI_PERS_LOCATION=westeurope | |
ACI_PERS_SHARE_NAME=acishare | |
# Create the res group | |
az group create -n $ACI_PERS_RESOURCE_GROUP -l $ACI_PERS_LOCATION --output table | |
# Create the storage account with the parameters | |
az storage account create \ | |
--resource-group $ACI_PERS_RESOURCE_GROUP \ | |
--name $ACI_PERS_STORAGE_ACCOUNT_NAME \ | |
--location $ACI_PERS_LOCATION \ | |
--sku Standard_LRS | |
# Create the file share | |
az storage share create --name $ACI_PERS_SHARE_NAME --account-name $ACI_PERS_STORAGE_ACCOUNT_NAME | |
STORAGE_KEY=$(az storage account keys list --resource-group $ACI_PERS_RESOURCE_GROUP --account-name $ACI_PERS_STORAGE_ACCOUNT_NAME --query "[0].value" --output tsv) | |
# Deploy VS Code! | |
az container create \ | |
--resource-group $ACI_PERS_RESOURCE_GROUP \ | |
--name cloudcode \ | |
--image codercom/code-server \ | |
--command-line "code-server --allow-http" \ | |
--cpu 1 \ | |
--memory 1.5 \ | |
--dns-name-label cloudcode-$SUFFIX \ | |
--ports 8443 \ | |
--azure-file-volume-account-name $ACI_PERS_STORAGE_ACCOUNT_NAME \ | |
--azure-file-volume-account-key $STORAGE_KEY \ | |
--azure-file-volume-share-name $ACI_PERS_SHARE_NAME \ | |
--azure-file-volume-mount-path /root/project | |
IPADDRESS=$(az container show -g $ACI_PERS_RESOURCE_GROUP -n cloudcode --query "ipAddress.ip" -o tsv) | |
# Sleeping, then show logs | |
az container logs -g $ACI_PERS_RESOURCE_GROUP -n cloudcode | |
echo "### Connect to VS Code in the Cloud here! http://$IPADDRESS:8443/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment