forked from nullpo-head/wsl-distrod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·125 lines (109 loc) · 2.82 KB
/
install.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
#!/bin/sh
set -e
LATEST_RELEASE_URL="https://github.com/nullpo-head/wsl-distrod/releases/latest/download/opt_distrod.tar.gz"
HELP_STR="Usage: $0 <command>
command:
- install
- update
- uninstall"
help () {
echo "$HELP_STR"
}
error_help () {
error "$HELP_STR"
}
install () {
mkdir /opt/distrod || error "Failed to create /opt/distrod."
cd /opt/distrod || error "Could not change directory to /opt/distrod"
get_release_file
tar xvf opt_distrod.tar.gz
rm opt_distrod.tar.gz
echo "Installation is complete!"
}
uninstall () {
if grep -o systemd /proc/1/status > /dev/null; then
error "This uninstall command cannot run inside a running Distrod distro."
error "To uninstall it, do the following first."
error "1. /opt/distrod/bin/distrod disable # Stop systemd from starting as init"
error "2. wsl.exe --shutdown # Terminate WSL2"
error "After that, Systemd will not run as the init and you can run uninstall."
exit 1
fi
rm -rf /opt/distrod
echo "Distrod has been uninstalled!"
}
update () {
cd /opt/distrod || error "Could not change directory to /opt/distrod"
get_release_file
EXCLUDE=""
for FILE in /opt/distrod/conf/*; do
FILE=${FILE#/opt/distrod/}
if printf "%s" "$FILE" | grep -E ' '; then
error "Found a file with a name containing spaces. Please remove it. Aborting update command."
exit 1
fi
EXCLUDE="$EXCLUDE --exclude $FILE"
done
# shellcheck disable=SC2086
tar xvf opt_distrod.tar.gz $EXCLUDE
echo "Ruuning post-update actions..."
POST_UPDATE="/opt/distrod/misc/distrod-post-update"
if [ -e "${POST_UPDATE}" ]; then
"${POST_UPDATE}"
fi
echo "Distrod has been updated!"
}
get_release_file() {
if [ -n "$RELEASE_FILE" ]; then
if [ "$(realpath "$RELEASE_FILE")" != "$(realpath opt_distrod.tar.gz)" ]; then
cp "$RELEASE_FILE" opt_distrod.tar.gz
fi
else
curl -L -O "${LATEST_RELEASE_URL}"
fi
}
error () {
echo "$@" >&2
}
if [ -z "$1" ]; then
error_help
exit 1
fi
if [ "$(whoami)" != "root" ]; then
error "You must be root to run this script, please use sudo ./install.sh"
exit 1
fi
COMMAND=
while [ -n "$1" ]; do
case "$1" in
-h|--help)
echo "$HELP_STR"
exit 0
;;
install)
COMMAND=install
shift
;;
uninstall)
COMMAND=uninstall
shift
;;
update)
COMMAND=update
shift
;;
-r|--release-file)
RELEASE_FILE="$(realpath "$2")"
shift 2
;;
-*)
error "Error: Unknown flag $1"
exit 1
;;
*) # preserve positional arguments
error "Error: Unknown command $1"
exit 1
;;
esac
done
"$COMMAND"