This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.sh
executable file
·87 lines (68 loc) · 2 KB
/
vm.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
#!/bin/bash
set -eu
readonly WORK_DIR=$(cd "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P)
if [ $# -eq 0 ]; then
"${WORK_DIR}/vm.sh" help
exit 1
fi
readonly HOSTNAME=$(vagrant status | awk 'NR==3,NR==3 {print $1}')
readonly IS_INITIALIZED=$(if [ -e "${WORK_DIR}/.vagrant/machines/${HOSTNAME}/virtualbox/id" ]; then echo true; else echo false; fi)
readonly CMD=$1
readonly MUTAGEN_FILE=mutagen.yml
readonly MUTAGEN_LOCK_FILE=mutagen.yml.lock
readonly VM_NAME=ps.vagrant
start() {
echo "Starting up virtual machine..."
if vagrant status ${VM_NAME} | grep "running (virtualbox)" > /dev/null 2>&1; then
echo "Virtual machine is already running."
exit 1
fi
vagrant up
if ! "${IS_INITIALIZED}"; then
echo "Restarting virtual machine..."
vagrant reload
fi
test -e "${MUTAGEN_LOCK_FILE}" && mutagen project terminate
mutagen project start -f "${MUTAGEN_FILE}"
echo "Virtual machine has started. ✨"
}
stop() {
echo "Shutting down virtual machine..."
test -e "${MUTAGEN_LOCK_FILE}" && mutagen project terminate
if vagrant status ${VM_NAME} | grep "poweroff (virtualbox)" > /dev/null 2>&1; then
echo "Virtual machine is already stopped."
exit 1
fi
vagrant halt
echo "Virtual machine has stopped. 😪"
}
restart() {
echo "Restarting virtual machine..."
if ! vagrant status ${VM_NAME} | grep "running (virtualbox)" > /dev/null 2>&1; then
echo "Virtual machine is not running."
exit 1
fi
vagrant reload
mutagen project reset -f "${MUTAGEN_FILE}"
echo "Virtual machine has restarted. 👍"
}
case $CMD in
"start") start;;
"stop") stop;;
"restart") restart;;
"help") {
echo "Usage:"
echo ""
echo " ./vm.sh <command>"
echo ""
echo "The commands are:"
echo ""
echo " start start a VM and create a Mutagen session"
echo " stop stop the VM and terminate the Mutagen session"
echo " restart restart the VM and recreate a Mutagen session"
};;
*) {
echo "Command not supported."
exit 1
}
esac