Skip to content

Instantly share code, notes, and snippets.

@colemickens
Last active April 23, 2018 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colemickens/5144c1b49aeaf99885304d130df08098 to your computer and use it in GitHub Desktop.
Save colemickens/5144c1b49aeaf99885304d130df08098 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
F="${HOME}/.secretz/azure.sh"; [[ -f "${F}" ]] && source "${F}"
set -x
set -euo pipefail
PREFIX="colemick-nixosdev"
RG="${PREFIX}-rg"
VNET="${PREFIX}-vnet"
DISK="${PREFIX}-disk"
VM="${PREFIX}-vm"
LOCATION="westus2"
STORAGE="${PREFIX//-/}"
CONTAINER="${PREFIX}"
SOURCE="https://nixos.blob.core.windows.net/images/nixos-image-16.03.847.8688c17-x86_64-linux.vhd"
TARGET="nixos-image-azure-16.03.847.8688c17-x86_64-linux.vhd"
SOURCE="https://nixos.blob.core.windows.net/images/nixos-unstable-nixops.vhd"
TARGET="nixos-unstable-nixops-20180423.vhd"
export AZURE_CONFIG_DIR="$(mktemp -d)"
trap "rm -rf ${AZURE_CONFIG_DIR}" EXIT
# LOGIN
az login --service-principal --username="${AZURE_USERNAME}" --password="${AZURE_PASSWORD}" --tenant="${AZURE_TENANT_ID}"
az account set --subscription="${AZURE_SUBSCRIPTION_ID}"
# RESOURCE GROUP
if ! az group show -n "${RG}" &>/dev/null ; then
az group create -n "${RG}" -l "${LOCATION}"
fi
# VNET
if ! az network vnet show -n "${VNET}" -g "${RG}" &>/dev/null ; then
az network vnet create -n "${VNET}" -g "${RG}"
fi
# STORAGE ACCOUNT (needed to stage the NixOS bootstrap VHD into a MD)
if ! az storage account show -n "${STORAGE}" -g "${RG}" &>/dev/null ; then
az storage account create -n "${STORAGE}" -g "${RG}" --sku "Premium_LRS"
fi
export AZURE_STORAGE_CONNECTION_STRING=$(az storage account show-connection-string -n "${STORAGE}" -g "${RG}" --query connectionString --output tsv)
# STORAGE CONTAINER
if ! az storage container show -n "${CONTAINER}" -g "${RG}" &>/dev/null ; then
az storage container create -n "${CONTAINER}"
fi
# DISK BLOB COPY
T="$(az storage blob show -c "${CONTAINER}" -n "${TARGET}")"
if [[ -z "${T}" ]] ; then
az storage blob copy start -u "${SOURCE}" -c "${CONTAINER}" -b "${TARGET}"
fi
set +x
while [ $(az storage blob show -c "${CONTAINER}" -n "${TARGET}" | jq .properties.copy.status| tr -d '"') == "pending" ]; do
progress=$(az storage blob show -c "${CONTAINER}" -n "${TARGET}" | jq -r .properties.copy.progress)
done_count=$(echo $progress | sed 's,\([0-9]*\)/\([0-9]*\),\1,g')
total_count=$(echo $progress | sed 's,\([0-9]*\)/\([0-9]*\),\2,g')
progress_percent=$((100 * $done_count / $total_count))
echo -ne "Copying: $progress ($progress_percent %)" && echo -ne "%\033[0K\r" && sleep 5
done
set -x
export BLOB="$(az storage blob url -c "${CONTAINER}" -n "${TARGET}" -o tsv)"
# MANAGED DISK
T="$(az disk show -n "${DISK}" -g "${RG}")"
if [[ -z "${T}" ]] ; then
az disk create --debug -n "${DISK}" -g "${RG}" --source "${BLOB}"
fi
# VM
if ! az vm get-instance-view -n "${VM}" -g "${RG}" &>/dev/null ; then
echo "FINISH ME"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment