-
Notifications
You must be signed in to change notification settings - Fork 5
/
upload-image.sh
executable file
·220 lines (193 loc) · 6.6 KB
/
upload-image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env bash
####################################################
# AZ LOGIN CHECK #
####################################################
# Making sure that one is logged in (to avoid
# surprises down the line).
if [ $(az account list | jq -r 'length') -eq 0 ]
then
echo
echo '********************************************************'
echo '* Please log in to Azure by typing "az login", and *'
echo '* repeat the "./upload-image.sh" command. *'
echo '********************************************************'
exit 1
fi
####################################################
# HELPERS #
####################################################
show_id() {
az $1 show \
--resource-group "${resource_group}" \
--name "${img_name}" \
--query "[id]" \
--output tsv
}
# make_boot_sh_opts <image-id> "<opt1>=<val1>;...;<optn>=<valn>"
make_boot_sh_opts() {
# Add `./upload-image.sh`'s resource group if not given
# https://stackoverflow.com/a/8811800/1498178 (contains string?)
if [ "${2#*g=}" != "$2" ] || [ "${2#*resource-group}" != "$2" ]
then
opt_string=$2
else
opt_string="resource-group=${resource_group};$2"
fi
acc=""
# https://stackoverflow.com/a/918931/1498178 (parse and loop opt-string)
while IFS=';' read -ra opts; do
for i in "${opts[@]}"; do
# https://stackoverflow.com/a/10520718/1498178 (separate opts and vals)
opt=${i%=*}
val=${i#*=}
# https://stackoverflow.com/a/17750975/1498178 (string length)
# https://stackoverflow.com/a/3953712/1498178 (ternary)
sep=$([ ${#opt} == 1 ] && echo "-" || echo "--")
acc="${acc}${sep}${opt} ${val} "
done
done <<< "image=$1;$opt_string"
echo $acc
}
usage() {
echo ''
echo 'USAGE: (Every switch requires an argument)'
echo ''
echo '-g --resource-group REQUIRED Created if does not exist. Will'
echo ' house a new disk and the created'
echo ' image.'
echo ''
echo '-n --image-name REQUIRED The name of the image created'
echo ' (and also of the new disk).'
echo ''
echo '-i --image-nix Nix expression to build the'
echo ' image. Default value:'
echo ' "./examples/basic/image.nix".'
echo ''
echo '-l --location Values from `az account list-locations`.'
echo ' Default value: "westus2".'
echo ''
echo '-b --boot-sh-opts Run `./boot-vm.sh` once the image is'
echo ' created and uploaded; takes arguments in'
echo ' the format of "opt1=val1;...;optn=valn".'
echo ' (See the avialable `boot-vm.sh` options'
echo ' at section 2.3 below.)'
echo ''
echo ' + "vm-name=..." (or "n=...") is mandatory'
echo ''
echo ' + if "--image" (i.e., "image=..") is'
echo ' omitted, it will be pre-populated with the'
echo ' name of the image just created'
echo ''
echo ' + if resource group is omitted, the one'
echo ' for `./upload-image.sh` is used'
}
####################################################
# SWITCHES #
####################################################
# https://unix.stackexchange.com/a/204927/85131
while [ $# -gt 0 ]; do
case "$1" in
-i|--image-nix)
image_nix="$2"
;;
-l|--location)
location="$2"
;;
-g|--resource-group)
resource_group="$2"
;;
-n|--image-name)
img_name="$2"
;;
-b|--boot-sh-opts)
boot_opts="$2"
;;
-h|--help)
usage
exit 1
;;
*)
printf "***************************\n"
printf "* Error: Invalid argument *\n"
printf "***************************\n"
usage
exit 1
esac
shift
shift
done
if [ -z "${img_name}" ] || [ -z "${resource_group}" ]
then
printf "************************************\n"
printf "* Error: Missing required argument *\n"
printf "************************************\n"
usage
exit 1
fi
####################################################
# DEFAULTS #
####################################################
image_nix_d="${image_nix:-"./examples/basic/image.nix"}"
location_d="${location:-"westus2"}"
boot_opts_d="${boot_opts:-"none"}"
####################################################
# PUT IMAGE INTO AZURE CLOUD #
####################################################
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -euxo pipefail
nix-build \
--out-link "azure" \
"${image_nix_d}"
# Make resource group exists
if ! az group show --resource-group "${resource_group}" &>/dev/null
then
az group create \
--name "${resource_group}" \
--location "${location_d}"
fi
# NOTE: The disk access token song/dance is
# tedious but allows us to upload direct
# to a disk image thereby avoid storage
# accounts (and naming them) entirely!
if ! show_id "disk" &>/dev/null
then
img_file="$(readlink -f ./azure/disk.vhd)"
bytes="$(stat -c %s ${img_file})"
az disk create \
--resource-group "${resource_group}" \
--name "${img_name}" \
--for-upload true \
--upload-size-bytes "${bytes}"
timeout=$(( 60 * 60 )) # disk access token timeout
sasurl="$(\
az disk grant-access \
--access-level Write \
--resource-group "${resource_group}" \
--name "${img_name}" \
--duration-in-seconds ${timeout} \
--query "[accessSas]" \
--output tsv
)"
azcopy copy "${img_file}" "${sasurl}" \
--blob-type PageBlob
# https://docs.microsoft.com/en-us/cli/azure/disk?view=azure-cli-latest#az-disk-revoke-access
# > Revoking the SAS will change the state of
# > the managed disk and allow you to attach
# > the disk to a VM.
az disk revoke-access \
--resource-group "${resource_group}" \
--name "${img_name}"
fi
if ! show_id "image" &>/dev/null
then
az image create \
--resource-group "${resource_group}" \
--name "${img_name}" \
--source "$(show_id "disk")" \
--os-type "linux" >/dev/null
fi
if [ "${boot_opts_d}" != "none" ]
then
img_id="$(show_id "image")"
./boot-vm.sh $(make_boot_sh_opts $img_id $boot_opts_d)
fi