forked from colemickens/nixos-flake-example
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_vm_virtualbox.sh
executable file
·67 lines (57 loc) · 1.61 KB
/
build_vm_virtualbox.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
#!/usr/bin/env bash
usage() {
echo "Build VirtualBox VM script"
echo "Usage: $0 -c <vm-config> [-n]" 1>&2;
echo
echo "-c (Nix config) Config to build"
echo "-n (Don't notify) Don't notify on build"
echo
echo "Examples:"
echo "# Do not notify"
echo "$0 -n"
echo
exit 1;
}
arg_notify="true"
arg_config="false"
while getopts "c:n" flag
do
case "${flag}" in
n) arg_notify="false";;
c) arg_config=${OPTARG};;
esac
done
if [[ "$arg_config" == "false" ]]; then
echo "ERROR: VM config not provided"
echo
usage
fi
sudo chmod 777 /tmp/vm_virtualbox_${arg_config}_hash
GIT_CURRENT_HASH=`sudo -u user git -C /home/user/git/maxos rev-parse --short HEAD`
GIT_PREV_HASH=0
if [[ -f "/tmp/vm_virtualbox_hash" ]]; then
GIT_PREV_HASH=`cat /tmp/vm_virtualbox_${arg_config}_hash`
fi
# Check we haven't built VM for this hash
if [[ "$GIT_CURRENT_HASH" != "$GIT_PREV_HASH" ]]; then
nixos-generate --flake .#${arg_config} -f virtualbox -o maxos_${arg_config}
RETURN_VALUE=$?
# Problem?
if [[ "$RETURN_VALUE" == "0" ]]; then
if [[ "$arg_notify" == "true" ]]; then
/home/user/git/maxos/scripts/telegram_notify.sh -m "Finished building VirtualBox VM"
fi
echo $GIT_CURRENT_HASH > /tmp/vm_virtualbox_${arg_config}_hash
exit 0
else
if [[ "$arg_notify" == "true" ]]; then
/home/user/git/maxos/scripts/telegram_notify.sh -m "Error code while building VirtualBox VM: $?"
fi
exit 1
fi
else
if [[ "$arg_notify" == "true" ]]; then
/home/user/git/maxos/scripts/telegram_notify.sh -m "VirtualBox VM already built for $GIT_CURRENT_HASH"
fi
exit 1
fi