1#!/usr/bin/env bash
2set -euo pipefail
3set -x
4
5image_nix="${1:-"./examples/basic/image.nix"}"
6
7nix-build "${image_nix}" --out-link "azure"
8
9group="nixos-images"
10location="westus2"
11img_name="nixos-image"
12img_file="$(readlink -f ./azure/disk.vhd)"
13
14if ! az group show -n "${group}" &>/dev/null; then
15 az group create --name "${group}" --location "${location}"
16fi
17
18# note: the disk access token song/dance is tedious
19# but allows us to upload direct to a disk image
20# thereby avoid storage accounts (and naming them) entirely!
21if ! az disk show -g "${group}" -n "${img_name}" &>/dev/null; then
22 bytes="$(stat -c %s ${img_file})"
23 size="30"
24 az disk create \
25 --resource-group "${group}" \
26 --name "${img_name}" \
27 --for-upload true --upload-size-bytes "${bytes}"
28
29 timeout=$(( 60 * 60 )) # disk access token timeout
30 sasurl="$(\
31 az disk grant-access \
32 --access-level Write \
33 --resource-group "${group}" \
34 --name "${img_name}" \
35 --duration-in-seconds ${timeout} \
36 | jq -r '.accessSas'
37 )"
38
39 azcopy copy "${img_file}" "${sasurl}" \
40 --blob-type PageBlob
41
42 az disk revoke-access \
43 --resource-group "${group}" \
44 --name "${img_name}"
45fi
46
47if ! az image show -g "${group}" -n "${img_name}" &>/dev/null; then
48 diskid="$(az disk show -g "${group}" -n "${img_name}" -o json | jq -r .id)"
49
50 az image create \
51 --resource-group "${group}" \
52 --name "${img_name}" \
53 --source "${diskid}" \
54 --os-type "linux" >/dev/null
55fi
56
57imageid="$(az image show -g "${group}" -n "${img_name}" -o json | jq -r .id)"
58echo "${imageid}"