-
Notifications
You must be signed in to change notification settings - Fork 3
/
provision
executable file
·383 lines (335 loc) · 11.8 KB
/
provision
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/sh
# This script parses following arguments from /proc/cmdline:
# - bk_url=https://mgmt.koodur.com/api
# - bk_template=your-template
# - bk_snapshot=snap9
# - bk_domain=32kk.lan
# TODO: Make sure fdisk from busybox is NOT used, it's counting sectors incorrectly (?!)
# Dependencies: fdisk btrfs find dialog ntfs-3g ntfsprogs parted
# TODO: Check connectivity with API server
POOL_MOUNTPOINT=/mnt/pool
POOL_TEMPLATES=$POOL_MOUNTPOINT/templates
POOL_DEPLOYMENTS=$POOL_MOUNTPOINT/deployments
TARGET_MOUNTPOINT=/mnt/target
# Clean up mess from interrupted snapshot retrieval
if [ -f $POOL_TEMPLATES/dirty ]; then
echo "Interrupted snapshot cleanup not implemented yet!"
#for subvol in $(cat $POOL_TEMPLATES/dirty); do
# TODO: Handle these situations
#done
fi
#######################################
### Check for presence of utilities ###
#######################################
for util in btrfs jq fdisk find ntfsresize; do
if [ -z "$(which $util)" ]; then
dialog --msgbox "Butterknife was unable to locate $util,\
are you sure the provisioning image was compiled properly?" 0 0
exit 253
else
echo "Found $util..."
fi
done
# These Bashisms are insane
for chunk in $(cat /proc/cmdline); do
case "$chunk" in
bk_url=*)
URL="${chunk#bk_url=}"
;;
bk_domain=*)
DOMAIN="${chunk#bk_domain=}"
;;
bk_debug=*)
DEBUG="${chunk#bk_debug=}"
;;
bk_template=*)
TEMPLATE="${chunk#bk_template=}"
;;
bk_snapshot=*)
SNAPSHOT="${chunk#bk_snapshot=}"
;;
esac
done
set -x # Don't echo
set -e # Bail on error
#############################
### Target disk selection ###
#############################
if [ -z "$(ls /sys/class/block/" ]; then
dialog --msgbox "Butterknife was unable to detect any harddisks,\
are you sure harddisk is connected and we have drivers for it?" 0 0
exit 255
fi
for disk in /sys/class/block/*; do
if [ -d $disk/device ]; then
slug=$(basename $disk)
sector_count=$(cat $disk/size)
sector_size=$(cat $disk/queue/hw_sector_size)
# Hack around these dumbass cardreaders
if [ "$sector_count" == "0" ]; then
continue
fi
size=$(expr $sector_count / 1000000 \* $sector_size / 1000)G
echo "$slug \"$(cat $disk/device/model | xargs) ($size)\"";
fi
done > /tmp/disks
dialog \
--menu "Target disk" 0 0 0 \
--file /tmp/disks \
2> /tmp/selected_disk
DISK_SLUG=$(cat /tmp/selected_disk)
DISK=/dev/$DISK_SLUG
dialog --menu "Partitioning $DISK" 0 0 0 \
purge "Overwrite whole disk" \
reformat "Reformat partition" \
unpartitioned "Use unpartitioned area" \
receive "Receive into existing btrfs filesystem" \
2> /tmp/partitioning_method
# TODO: deploy "Deploy received template" \
# TODO: postinstall "Run postinstall scripts (reinstall GRUB)" \
# TODO: EFI way is not currently covered!
case $(cat /tmp/partitioning_method) in
unpartitioned)
clear
echo "Attempting to create new partition in unpartitioned space"
echo -e "n\np\n\n\n\nw" | fdisk $DISK
;;
purge)
clear
echo "Purging whole disk"
echo -e "o\nn\np\n\n\n\nw" | fdisk $DISK
;;
esac
########################################
### Target partition selection phase ###
########################################
for partition in $DISK?; do
partition_slug=$(echo $partition | cut -d "/" -f 3)
sector_count=$(cat /sys/block/$DISK_SLUG/$partition_slug/size)
sector_size=$(cat /sys/block/$DISK_SLUG/queue/hw_sector_size)
size=$(expr $sector_count / 1000000 \* $sector_size / 1000000)G
if [ $size == "0G" ]; then
size=$(expr $sector_count / 1000 \* $sector_size / 1000)M
fi
echo "$partition \"$size\"";
done > /tmp/partitions
dialog \
--menu "Target partition" 0 0 0 \
--file /tmp/partitions \
2> /tmp/selected_partition
PARTITION=$(cat /tmp/selected_partition)
PARTITION_SLUG=$(echo $PARTITION | cut -d "/" -f 3)
case $(cat /tmp/partitioning_method) in
purge|reformat|unpartitioned)
clear
echo "Creating clean btrfs filesystem on $PARTITON"
mkfs.btrfs -f $PARTITION
;;
esac
# Attempt to mount target directory
mkdir -p $POOL_MOUNTPOINT
mount $PARTITION $POOL_MOUNTPOINT -o subvol=/
mkdir -p $POOL_TEMPLATES
mkdir -p $POOL_DEPLOYMENTS
if [ $? -ne 0 ]; then
dialog --msgbox "Mounting $PARTITION at $POOL_MOUNTPOINT failed, are you sure kernel has btrfs support built-in?" 0 0
exit 255
fi
#######################################
### Transfer method selection phase ###
#######################################
case $(cat /tmp/partitioning_method) in
purge|reformat|unpartitioned|receive)
# Determine transfer method
if [ -z $TEMPLATE ] || [ -z $SNAPSHOT ]; then
echo "http" > /tmp/transfer_method
else
dialog --menu "Select transfer method" 0 0 0 \
http "HTTP-only" \
multicast "Multicast receive" \
tee "Multicast via HTTP and write" \
proxy "Only proxy HTTP to multicast" 2>/tmp/transfer_method
fi
;;
deploy)
echo "local" > /tmp/transfer_method
;;
*)
echo "none" > /tmp/transfer_method
;;
esac
##############################
### Server selection phase ###
##############################
if [ -z $URL ]; then
case $(cat /tmp/transfer_method) in
http|tee|proxy)
dialog --menu "Select server" 0 0 0 \
http://butterknife "Local server" \
https://mgmt.koodur.com "Koodur LLC" 2>/tmp/server_hostname
;;
esac
fi
URL="$(cat /tmp/server_hostname)/api"
################################
### Template selection phase ###
################################
if [ -z $TEMPLATE ]; then
case $(cat /tmp/transfer_method) in
local)
echo "" > /tmp/available_templates
for i in $(ls $POOL_TEMPLATES); do
echo "$i \"$i\"" >> /tmp/available_templates
done
;;
*)
# Fetch template list
curl -s $URL/container/ \
| jq '.containers[] | .name + " \"" + .description + "\""' -r \
> /tmp/available_templates
;;
esac
dialog \
--menu "Select template to deploy" 0 0 0 \
--file /tmp/available_templates \
2>/tmp/selected_template
TEMPLATE=$(cat /tmp/selected_template)
fi
################################
### Snapshot selection phase ###
################################
if [ -z $SNAPSHOT ]; then
case $(cat /tmp/transfer_method) in
http|tee|proxy|multicast)
# Fetch snapshot list
curl -s $URL/container/$TEMPLATE/snapshot/ \
> /tmp/available_snapshots.json
cat /tmp/available_snapshots.json \
| jq '.snapshots[] | .name+" \""+.comment + "\""' -r \
| head -n 100 \
> /tmp/available_snapshots
;;
local)
# List local snapshots
echo "" > /tmp/available_snapshots
for i in $(ls $POOL_TEMPLATES/$TEMPLATE); do
echo "$i \"$i\"" >> /tmp/available_snapshots
done
;;
esac
case $(cat /tmp/transfer_method) in
http|tee|proxy|local|multicast)
dialog \
--menu "Select snapshot to deploy" 0 0 0 \
--file /tmp/available_snapshots \
2>/tmp/selected_snapshot
;;
esac
SNAPSHOT=$(cat /tmp/selected_snapshot)
fi
TEMPLATE_SUBVOL=templates/$TEMPLATE/$SNAPSHOT/
clear
######################
### Transfer phase ###
######################
case $(cat /tmp/partitioning_method) in
purge|reformat|unpartitioned|receive)
# Build btrfs-stream URL
STREAM="$URL/container/$TEMPLATE/snapshot/$SNAPSHOT/stream?src="
# Determine differential snapshot parents
if [ -d $POOL_TEMPLATES/$TEMPLATE ]; then
cat /tmp/available_snapshots.json | jq -r '.snapshots[] .name' > /tmp/available_snapshot_names
ls $POOL_TEMPLATES/$TEMPLATE > /tmp/local_snapshot_names
cat /tmp/local_snapshot_names /tmp/available_snapshot_names > /tmp/merged_snapshot_names
PARENTS=$(cat /tmp/merged_snapshot_names | sort | grep -v $SNAPSHOT | uniq -d)
echo "Snapshot parents are: $PARENTS"
for parent in $PARENTS; do
STREAM="$STREAM$parent,"
done
else
echo "Differential update not possible, because no snapshot parents available locally. Falling back to full snapshot"
fi
echo "Final URL is $STREAM"
mkdir -p $POOL_MOUNTPOINT/$TEMPLATE_SUBVOL
# Mark dirty subvolumes
echo $TEMPLATE_SUBVOL > $POOL_TEMPLATES/dirty
sync
case $(cat /tmp/transfer_method) in
multicast)
udp-receiver --nokbd \
| zcat \
| btrfs receive $POOL_MOUNTPOINT/$TEMPLATE_SUBVOL
;;
http)
curl $STREAM \
| zcat \
| btrfs receive $POOL_MOUNTPOINT/$TEMPLATE_SUBVOL
;;
tee)
dialog --msgbox "Press enter once all the other machines are ready to receive" 0 0
mkfifo /tmp/multicast_stream
mkfifo /tmp/local_stream
zcat /tmp/local_stream | btrfs receive $POOL_MOUNTPOINT/$TEMPLATE_SUBVOL &
sleep 1
udp-sender --nokbd --no-progress --min-receivers 1 --min-wait 5 /tmp/multicast_stream &
sleep 1
curl -s $STREAM | tee /tmp/multicast_stream > /tmp/local_stream
sleep 2
# TODO: Ensure btrfs receive has finished
;;
proxy)
dialog --msgbox "Press enter once all the other machines are ready to receive" 0 0
curl -s $STREAM \
| udp-sender --nokbd --min-receivers 1 --min-wait 5
;;
esac
# Remove dirty flag
rm $POOL_TEMPLATES/dirty
sync
;;
esac
#################################
### Filesystem creation phase ###
#################################
case $(cat /tmp/partitioning_method) in
purge|reformat|unpartitioned|receive|deploy)
DEPLOY_SUBVOL="deployments/$(date '+%Y-%m-%d_%H:%M:%S')"
btrfs subvolume snapshot $POOL_MOUNTPOINT/$TEMPLATE_SUBVOL/* "$POOL_MOUNTPOINT/$DEPLOY_SUBVOL"
;;
fixgrub)
echo "DEPLOYED SNAPSHOT SELECTION NOT IMPLEMENTED YET"
exit 255
;;
esac
# Mount deployment subvolume at target directory
mkdir -p $TARGET_MOUNTPOINT
mount $PARTITION $TARGET_MOUNTPOINT -o subvol=$DEPLOY_SUBVOL
# Mount pool also for chroot
mkdir -p $TARGET_MOUNTPOINT$POOL_MOUNTPOINT
mount $PARTITION $TARGET_MOUNTPOINT$POOL_MOUNTPOINT -o subvol=/
# Mount stuff for chroot
mount --bind /dev/ $TARGET_MOUNTPOINT/dev/
mount --bind /sys/ $TARGET_MOUNTPOINT/sys/
mount --bind /proc/ $TARGET_MOUNTPOINT/proc/
# Export variables for postinstall scripts
export BUTTERKNIFE_DOMAIN=$DOMAIN
export BUTTERKNIFE_TEMPLATE_SUBVOL=$TEMPLATE_SUBVOL
export BUTTERKNIFE_DEPLOY_SUBVOL=$DEPLOY_SUBVOL
export BUTTERKNIFE_PARTITION=$PARTITION
export BUTTERKNIFE_DISK=$DISK
export BUTTERKNIFE_POOL_MOUNTPOINT=$POOL_MOUNTPOINT
export BUTTERKNIFE_POOL_UUID=$(blkid -s UUID -o value $PARTITION)
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Run postinstall scripts, presumably in sorted order
chroot $TARGET_MOUNTPOINT butterknife-postdeploy
# Unmount directories
umount $TARGET_MOUNTPOINT/mnt/pool
umount $TARGET_MOUNTPOINT/dev
umount $TARGET_MOUNTPOINT/sys
umount $TARGET_MOUNTPOINT/proc
umount $TARGET_MOUNTPOINT
echo "Flushing buffers"
sync
sleep 1
echo "Rebooting machine"
reboot -f