-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·232 lines (184 loc) · 8.25 KB
/
bootstrap
File metadata and controls
executable file
·232 lines (184 loc) · 8.25 KB
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
#!/bin/bash
set -uo pipefail
umask 077
TTY="/dev/$(ps -p $$ -o tty=)"
################################################################
# Helper functions
function die() { echo "[1;31merror:[m $*" >&2; exit 1; }
function status() { echo "${had_status+$'\n'}[1;33m$*[m"; had_status=1; }
function flush_stdin() { while read -r -t 0.01 empty_stdin < "$TTY"; do true; done; }
function ask() {
local response
while true; do
flush_stdin
read -r -p "$* (Y/n) " response < "$TTY" || die "Error in read"
case "${response,,}" in
'') return 0 ;;
y|yes) return 0 ;;
n|no) return 1 ;;
*) continue ;;
esac
done
}
# $1 = src, $2 = dest, $3 = owner, $4 = mode
function install_file() {
cp "$1" "$2" || die "Could not copy '$1' to '$2'"
chown "$3" "$2" || die "Could not chown '$2'"
chmod "$4" "$2" || die "Could not chmod '$2'"
}
################################################################
# Ensure that all required tools are installed
abort=0
for i in jq git tmux rdiff-backup java openssl; do
if ! type "$i" &>/dev/null; then
echo "[1;31mmissing: [1;33m$i[m" >&2
abort=1
fi
done
[[ "$abort" == "0" ]] \
|| die "Please install the missing tools first."
################################################################
# Ensure memory requirements are met and we have root access
total_ram_gibi=$(free -g | grep -oP '\d+' | head -n1)
if [[ $total_ram_gibi -le 4 ]]; then
echo "[31mYour system has [33m${total_ram_gibi}GiB[31m of RAM, which probably is not enough to run minecraft"
echo "without issues. The recommended minimum amount is _at least_ [33m12GiB[31m.[m"
ask "Continue anyway?" || die "Installation aborted."
echo
elif [[ $total_ram_gibi -lt 12 ]]; then
echo "[31mYour system has [33m${total_ram_gibi}GiB[31m of RAM, which less than the recommended amount of"
echo "_at least_ [33m12GiB[31m. This will hurt the server performance significantly.[m"
ask "Continue anyway?" || die "Installation aborted."
echo
fi
[[ $EUID == 0 ]] || die "Must be root for system-wide installation."
################################################################
# Ensure EULA is accepted
status "Agree to Mojang EULA"
echo "You have to agree to Mojang's EULA to use the server software."
echo "It is available here: https://account.mojang.com/documents/minecraft_eula"
ask "Do you agree to the EULA?" \
|| die "Installation aborted. EULA must be accepted to continue."
################################################################
# Create minecraft user if necessary
if ! getent passwd minecraft &>/dev/null; then
status "Creating minecraft user"
useradd --system --home-dir /var/lib/minecraft --no-create-home minecraft \
|| die "Could not create user 'minecraft'"
fi
mkdir -p /var/lib/minecraft
chmod 700 /var/lib/minecraft
chown minecraft: /var/lib/minecraft
################################################################
# Setup repository
if [[ -e /var/lib/minecraft/deploy ]]; then
echo "[33mThe deploy directory /var/lib/minecraft/deploy already exists."
echo "You can still run the installer again, but it may overwrite some"
echo "configuration files if you edited them in the meantime.[m"
ask "Are you sure you want to continue?" \
|| die "Installation aborted."
fi
cd /var/lib/minecraft \
|| die "Could not change into /var/lib/minecraft"
if [[ -e deploy ]]; then
status "Updating deploy repository"
runuser -u minecraft -- git -C deploy pull \
|| die "Could not pull repository"
else
status "Cloning deploy repository"
runuser -u minecraft -- git clone "https://github.com/oddlama/minecraft-server" deploy \
|| die "Could not clone repository"
fi
cd deploy \
|| die "Could not change into deploy directory"
status "Configuring server"
install_file <(echo "eula=true") server/eula.txt minecraft: 600
for d in $(find contrib/default_config -type d -printf '%P\n'); do
mkdir -p -m700 "$d" || die "Could not create directory '$d'"
chown minecraft: "$d" || die "Could not chown directory '$d'"
done
for f in $(find contrib/default_config -type f -printf '%P\n'); do
install_file contrib/default_config/"$f" "$f" minecraft: 600
done
VELOCITYSECRET=$(openssl rand -base64 16) \
|| die "Could not generate velocity secret"
install_file <(echo -n "$VELOCITYSECRET") proxy/forwarding.secret minecraft: 600
sed -i 's|{{VELOCITYSECRET}}|'"$VELOCITYSECRET"'|' server/config/paper-global.yml \
|| die "Could not insert velocity secret in paper-global.yml"
echo "Depending on your player base, you might want to allow certain gamplay"
echo "exploits on your server. These are fixed by default in PaperMC, but would"
echo "allow your players to build certain vanilla machines (TNT blast chambers,"
echo "bedrock removal, ...). My personal recommendation is to answer with yes"
echo "to all of these questions."
echo
if ask "Allow headless pistons and bedrock breaking?"; then
sed -i 's|allow-headless-pistons: false|allow-headless-pistons: true|' server/config/paper-global.yml \
|| die "Could not replace allow-headless-pistons config"
sed -i 's|allow-permanent-block-break-exploits: false|allow-permanent-block-break-exploits: true|' server/config/paper-global.yml \
|| die "Could not replace allow-permanent-block-break-exploits config"
fi
if ask "Allow piston TNT duping?"; then
sed -i 's|allow-piston-duplication: false|allow-piston-duplication: true|' server/config/paper-global.yml \
|| die "Could not replace allow-piston-duplication config"
fi
if ask "Enable Anti-XRAY?"; then
sed -i 's|false # ANTI_XRAY|true|' server/config/paper-world-defaults.yml \
|| die "Could not replace ANTI_XRAY config"
else
sed -i 's|false # ANTI_XRAY|false|' server/config/paper-world-defaults.yml \
|| die "Could not replace ANTI_XRAY config"
fi
if ask "Replenish loot in loot chests after 1-2 realtime days?"; then
sed -i 's|auto-replenish: false|auto-replenish: true|' server/config/paper-world-defaults.yml \
|| die "Could not replace auto-replenish config"
fi
if ask "Disable hopper item move event to reduce lag?"; then
sed -i 's|disable-move-event: false|disable-move-event: true|' server/config/paper-world-defaults.yml \
|| die "Could not replace disable-move-event config"
fi
if ask "Increase view-distance to 15 chunks?"; then
sed -i 's|view-distance=10|view-distance=15|' server/server.properties \
|| die "Could not replace view-distance config"
fi
echo
runuser -u minecraft -- ./update.sh < "$TTY" \
|| die "Could not update server files"
################################################################
# Install systemd services
status "Installing service files"
./contrib/install.sh < "$TTY" \
|| die "Error while installing service files"
################################################################
# Enable and start services
status "Starting services ..."
echo "[33mSystem services for the proxy and server will now be enabled and started."
echo "If you want to adjust any proxy related configuration, you may want to start"
echo "them manually later.[m"
echo
postponed_service_message=""
if ask "Enable and start system services now?"; then
systemctl enable --now minecraft-server minecraft-proxy < "$TTY" \
|| die "Error while enabling services"
else
postponed_service_message="
You have postponed enabling the system services. Make sure to execute the
following command when you are ready to start the server:
[1;32m systemctl enable --now minecraft-server minecraft-proxy[m
"
fi
################################################################
# Success message
cat <<EOF
[1m================================================================================[m
[1;32mInstallation successful![m
[1m================================================================================[m
The server and proxy have been installed successfully! The PaperMC minecraft
server will be started when a player connects via the proxy. Be aware that the
first start may take a while due to world generation.
Please make sure that TCP port [35m25565[m (server), [35m25566[m (multiplex 1) and [35m8100[m
(bluemap webserver) are exposed to the internet via a port-foward or similar
mechanism. If you want to change your server configuration, please do so now.
$postponed_service_message
For more information, visit [35mhttps://github.com/oddlama/minecraft-server[m
[1m================================================================================[m
EOF