forked from nullpo-head/wsl-distrod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·138 lines (122 loc) · 3.21 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
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
set -e
LATEST_RELEASE_URL="https://github.com/nullpo-head/wsl-distrod/releases/latest/download/opt_distrod.tar.gz"
help () {
cat <<-eof
Usage: $0 [flags] <command>
flags:
-r, --release-file <filename>
Use <filename> as opt_distrod.tar.gz instead of downloading from:
$LATEST_RELEASE_URL
-h, --help
Displays this help, the same as the help command
command:
- install
- update
- uninstall
- help (this)
eof
}
error_help () {
error "$(help)"
}
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
help
error_help
exit 1
fi
unset NEEDS_ROOT
COMMAND=
while [ -n "$1" ]; do
case "$1" in
-h|--help|help)
COMMAND=help
break
;;
install)
COMMAND=install
NEEDS_ROOT=1
shift
;;
uninstall)
COMMAND=uninstall
NEEDS_ROOT=1
shift
;;
update)
COMMAND=update
NEEDS_ROOT=1
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
if [ -n "$NEEDS_ROOT" ] && [ "$(whoami)" != "root" ]; then
printf "You must be root to use the '${COMMAND}' command, please use 'sudo ${0} ${COMMAND}'\n"
exit 1
fi
"$COMMAND"