Last active
March 7, 2019 22:50
-
-
Save paulmey/29d0add51eac4e9d863bf5a41e5ac502 to your computer and use it in GitHub Desktop.
Create a VM from a SAS image
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 -e | |
usage() { | |
cat >&2 <<EOF | |
Usage: az-create-vm-from-sas-image -g resource-group -n vmname -l location -i image-sas-uri [-c custom-data] | |
EOF | |
exit -1 | |
} | |
vmcreateopts="" | |
while getopts g:n:l:i:c: opt; do | |
case $opt in | |
g) | |
rgname=$OPTARG | |
;; | |
n) | |
vmname=$OPTARG | |
;; | |
l) | |
location=$OPTARG | |
;; | |
i) | |
imageuri=$OPTARG | |
;; | |
c) | |
vmcreateopts="$vmcreateopts --custom-data $OPTARG" | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
[[ -n $rgname ]] | |
[[ -n $vmname ]] | |
[[ -n $imageuri ]] | |
blobname=$(basename $(cut -d? -f1 <<<$imageuri)) | |
imagename=$(basename $blobname .vhd) | |
echo "Creating resource group $rgname in location $location" | |
az group create -n $rgname -l $location >/dev/null | |
echo "Checking whether a storage account exists for region $location" | |
export AZURE_STORAGE_ACCOUNT=$(az storage account list -g $rgname --q "[?primaryLocation == '$location'].name|[0]" -o tsv) | |
if [[ -z $AZURE_STORAGE_ACCOUNT ]] ; then | |
AZURE_STORAGE_ACCOUNT=$(dd if=/dev/urandom bs=1 count=24 status=none|base64|tr A-Z a-z|tr -d '+/'|cut -c-13) | |
echo "Creating storage account $AZURE_STORAGE_ACCOUNT in location $location" | |
az storage account create -g $rgname -n $AZURE_STORAGE_ACCOUNT -l $location --sku Standard_LRS > /dev/null | |
fi | |
echo "Fetching keys for storage account $AZURE_STORAGE_ACCOUNT" | |
export AZURE_STORAGE_KEY=$(az storage account keys list -g $rgname -n $AZURE_STORAGE_ACCOUNT --query '[0].value' -o tsv) | |
echo "Copying image to storage account $AZURE_STORAGE_ACCOUNT with blob name $blobname" | |
az storage container create -n img > /dev/null | |
az storage blob copy start --source-uri "$imageuri" --destination-container img --destination-blob $blobname > /dev/null | |
if [[ -f ~/.azure_allowed_ips ]] ; then | |
echo "Checking if NSG default-$location already exists" | |
nsg=$(az network nsg list -g $rgname --q "[?name == 'default-$location'].name|[0]") | |
if [[ -z $nsg ]] ; then | |
echo "Creating NSG default-$location" | |
az network nsg create -g $rgname -n default-$location >/dev/null | |
az network nsg rule create -g $rgname --nsg-name default-$location -n CorpNet \ | |
--priority 1000 \ | |
--source-address-prefixes $(cat ~/.azure_allowed_ips) \ | |
--destination-port-ranges '*' \ | |
--access Allow \ | |
--description "Allow CorpNet (and ExpressRoute) traffic" >/dev/null | |
fi | |
vmcreateopts="$vmcreateopts --nsg default-$location" | |
fi | |
while copy=$(az storage blob show -n $blobname -c img --query properties.copy); [[ $(jq <<<$copy .status -r) == pending ]]; do | |
echo -en "Waiting for copy to complete: $(( 100 * $(jq <<<$copy .progress -r))) % ... \r" | |
done | |
echo "Copying: done " | |
echo "Creating managed image $imagename" | |
az image create -n $imagename --source $(az storage blob url -n $blobname -c img -o tsv) --os-type Linux -g $rgname -l $location >/dev/null | |
echo "Creating vm $vmname from managed image $imagename" | |
az vm create -n $vmname -l $location --image $imagename -g $rgname $vmcreateopts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment