-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud
More file actions
executable file
·457 lines (371 loc) · 12.1 KB
/
cloud
File metadata and controls
executable file
·457 lines (371 loc) · 12.1 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env bash
#
# Bash settings
#
set -e -o pipefail
#
# Constants
#
readonly MY_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
readonly VM_NAME="minecraft"
readonly COMPOSE_ROOT="${MY_DIR}/minecraft-fig"
readonly ALL_VOLUMES="${COMPOSE_ROOT}/volumes"
readonly GAME_VOLUMES="${ALL_VOLUMES}/game"
readonly SPIGOT_JAR="${GAME_VOLUMES}/spigot/spigot.jar"
readonly DROPBOX_UPLOADER_CONFIG="${ALL_VOLUMES}/dropbox/.dropbox_uploader"
readonly RUN_LOCAL_BACKUP_IN="minecraft-fig"
readonly RUN_LOCAL_BACKUP_ON=("volumes")
readonly SSH_OPTS=(
-i "${HOME}/.docker/machine/machines/${VM_NAME}/id_rsa"
-o IdentitiesOnly=yes
-o StrictHostKeyChecking=no
-o UserKnownHostsFile=/dev/null
-o LogLevel=quiet
)
#
# Imports
#
source "${MY_DIR}/bash-concurrent/concurrent.lib.sh"
#
# VM interaction functions
#
print_ip() {
docker-machine ip "${VM_NAME}"
}
print_ssh_user() {
docker-machine inspect -f '{{.Driver.SSHUser}}' "${VM_NAME}"
}
while_connected() (
cd "${COMPOSE_ROOT}"
docker-machine env "${VM_NAME}" > /dev/null
eval "$(docker-machine env "${VM_NAME}")"
"$@"
)
while_sshed() {
ssh -l "$(print_ssh_user)" "${SSH_OPTS[@]}" "$(print_ip)" "$@"
}
interactive_ssh() {
while_sshed -t "$@"
}
use_dropbox() {
[ -f "${DROPBOX_UPLOADER_CONFIG}" ]
}
compose() {
while_connected docker-compose -f "docker-compose.images.yml" "${@}"
}
stop_services() {
echo -e "\n>>> stopping services..."
compose stop
}
container_id() {
local name=$1
compose ps -q "${name}"
}
mark2_attach() {
while_connected docker exec -it $(container_id game) mark2 attach
}
up() (
if [[ "${1}" == "--local" ]]; then
shift
use_dropbox() { false; }
elif [[ "${1}" == "--delete-world" ]]; then
shift
rm -rf -- "${GAME_VOLUMES}"/world*
use_dropbox() { false; }
fi
if [[ "${1}" == "--build-spigot" ]]; then
shift
rm -f -- "${SPIGOT_JAR}"
fi
local driver=${1-digitalocean}
source "${driver}.auth"
source "${driver}.spec"
create_machine() {
if docker-machine env "${VM_NAME}" &> /dev/null; then
echo "(already created: $(print_ip))" >&3
print_ip
else
echo "(${driver})" >&3
docker-machine create --driver "${driver}" "${VM_NAME}"
echo "(${driver}: $(print_ip))" >&3
fi
}
enable_swap() {
local size=$1
if [ "${size}" == "0" ]; then
echo "(skipping)" >&3
echo -e "\n>>> not enabling swapfile"
return
fi
echo -en "\n>>> enabling swapfile..."
if ! while_sshed "which fallocate" &> /dev/null; then
echo "(skipping)" >&3
echo "skipping (no fallocate)"
return
fi
echo "(${size})" >&3
local cmd=""
cmd+='if [ $(swapon -s | wc -l) -eq 1 ]; then'
cmd+=" echo"
cmd+=" && fallocate -l ${size} /swapfile"
cmd+=" && chmod 600 /swapfile"
cmd+=" && mkswap /swapfile"
cmd+=" && swapon /swapfile"
cmd+=" && sysctl vm.swappiness=1"
cmd+="; else"
cmd+=" echo 'already enabled'"
cmd+="; fi"
while_sshed "sudo bash -c \"${cmd}\""
}
make_ramdisk() {
local path=$1
local size=$2
if [ "${size}" == "0" ]; then
echo "(skipping)" >&3
echo -e "\n>>> not enabling ramdisk"
return
fi
echo "(${size})" >&3
echo -en "\n>>> enabling ${size} ramdisk..."
local cmd=""
cmd+="if ! grep -sq '^tmpfs ${path}' /etc/fstab; then "
cmd+="echo -e 'tmpfs ${path} tmpfs rw,size=${size} 0 0' >> /etc/fstab"
cmd+=" && echo"
cmd+=" && mkdir -p ${path}"
cmd+=" && mount ${path}"
cmd+="; else "
cmd+="echo done"
cmd+="; fi"
cmd+=" && (mount | grep -F '${path}')"
while_sshed "sudo bash -c \"${cmd}\""
}
prepare_save_restore_containers() {
set -e
compose build \
load_world_data_from_tar \
save_world_data_to_tar
if use_dropbox; then
compose pull \
load_world_data_from_dropbox \
save_world_data_to_dropbox
fi
}
should_build_spigot() {
[[ ! -e "${SPIGOT_JAR}" ]]
}
spigot_prepare_container() {
if should_build_spigot; then
compose pull build_spigot
else
echo "(already build)" >&3
fi
}
spigot_building_jar() {
if should_build_spigot; then
compose run --rm build_spigot
else
echo "(already built)" >&3
fi
}
restore_data_to_vm() {
if [[ "${1}" == "--overwrite=true" ]] || remote_data_missing; then
if use_dropbox; then
echo "(from dropbox)" >&3
load_dropbox_world_data
else
echo "(from local)" >&3
load_local_world_data
fi
fi
}
remote_data_missing() {
while_sshed "bash -c '[ ! -e \"${GAME_VOLUMES}/world/level.dat\" ]'"
}
load_local_world_data() {
while_sshed "mkdir -p '${ALL_VOLUMES}'"
tar_volumes_to_stdout . | load_world_data_from_tar
}
load_dropbox_world_data() {
while_sshed "mkdir -p '${ALL_VOLUMES}'"
# Copy dropbox config (if present) and default settings to the VM.
tar_volumes_to_stdout "dropbox" "game/settings-default" | load_world_data_from_tar
# Copy the world data and custom settings from Dropbox.
compose run --rm load_world_data_from_dropbox
}
tar_volumes_to_stdout() {
tar -cz --exclude='*.lock' --exclude='.git' --exclude='.gitignore' -C "${ALL_VOLUMES}" "$@"
}
load_world_data_from_tar() {
# TODO: This command should work but there seems to be a bug in compose:
# TODO: compose run --rm load_world_data_from_tar
# TODO: Instead, we will defer to docker directly (until the bug is fixed).
# TODO: <https://github.com/docker/compose/issues/3829>
while_connected docker run --rm -i -v "${ALL_VOLUMES}:/volumes" \
minecraftfig_load_world_data_from_tar
}
local args=(
- "Creating VM" create_machine
- "Enabling swap" enable_swap "${SWAP_SIZE:-0}"
- "Creating ramdisk" make_ramdisk "${GAME_VOLUMES}" "${RAMDISK_SIZE:-512M}"
- "Preparing save/restore containers" prepare_save_restore_containers
- "Uploading world data" restore_data_to_vm
- "Preparing map containers" compose pull map mapcrafter
- "Starting map render" compose up -d map mapcrafter
- "Spigot: preparing container" spigot_prepare_container
- "Spigot: building JAR" spigot_building_jar
- "Preparing game container" compose pull game
- "Starting game server" compose up -d game
--require "Creating VM"
--before "Enabling swap"
--before "Creating ramdisk"
--before "Preparing save/restore containers"
--require "Creating ramdisk"
--require "Preparing save/restore containers"
--before "Uploading world data"
--require "Preparing save/restore containers"
--before "Preparing map containers"
--require "Preparing map containers"
--before "Spigot: preparing container"
--before "Starting map render"
--require "Uploading world data"
--before "Starting map render"
--require "Spigot: preparing container"
--before "Spigot: building JAR"
--before "Preparing game container"
--require "Enabling swap"
--require "Uploading world data"
--require "Spigot: building JAR"
--require "Preparing game container"
--before "Starting game server"
)
printf '\n[CREATING MINECRAFT SERVER]\n'
concurrent "${args[@]}"
printf '\n>>> up: %s\n' "$(print_ip)"
)
down() (
stop_mark2_and_ignore_errors() {
mark2_stop || :
}
delete_vm() {
echo -e "\n>>> deleting vm..."
docker-machine rm -y "${VM_NAME}"
}
mark2_stop() {
while_connected docker exec "$(container_id game)" mark2 stop
}
backup_items() {
local i
for i in "${@}"; do
if [ -e "${i}" ]; then
mv -n "${i}" "${i}.bak"
fi
done
}
delete_backups() {
local i
for i in "${@}"; do
rm -rf "${i}.bak"
done
}
save_dropbox_world_data() {
compose run --rm save_world_data_to_dropbox
}
save_local_world_data() {
local replace_items=(
"${GAME_VOLUMES}/world"
"${GAME_VOLUMES}/world_nether"
"${GAME_VOLUMES}/world_the_end"
)
# Complete replace existing world data.
backup_items "${replace_items[@]}"
function save_world_data_to_tar {
# TODO: This command should work but there seems to be a bug in compose:
# TODO: compose run --rm save_world_data_to_tar
# TODO: Instead, we will defer to docker directly (until the bug is fixed).
while_connected docker run --rm -v "${ALL_VOLUMES}:/volumes:ro" \
minecraftfig_save_world_data_to_tar
}
save_world_data_to_tar | untar_volumes_from_stdin
# Remove old world data if downloads were successful.
delete_backups "${replace_items[@]}"
}
untar_volumes_from_stdin() {
tar --no-same-owner -x --warning=no-timestamp -C "${ALL_VOLUMES}"
}
archive_local_data() {
for backup_script in backup_world_*; do
if [ -x "${backup_script}" ]; then
echo "(${backup_script})" >&3
echo -e "\n>>> archiving up local data with '${backup_script}'..."
"./${backup_script}" "${RUN_LOCAL_BACKUP_IN}" "${RUN_LOCAL_BACKUP_ON[@]}"
fi
done
}
local args=()
if [ "$1" != "--no-preserve" ]; then
args+=(
- "Stopping mark2" stop_mark2_and_ignore_errors
- "Stopping services" stop_services
- "Backing up world data to local" save_local_world_data
--sequential
)
fi
if [[ "$1" != "--no-preserve" ]] && use_dropbox; then
args+=(
- "Backing up world data to dropbox" save_dropbox_world_data
--require "Stopping services"
--before "Backing up world data to dropbox"
)
fi
args+=(
- "Deleting VM" delete_vm
--require-all
--before "Deleting VM"
)
if [ "$1" != "--no-preserve" ]; then
args+=(
- "Archiving local data" archive_local_data
--require "Backing up world data to local"
--before "Archiving local data"
)
fi
printf '\n[DELETING MINECRAFT SERVER]\n'
concurrent "${args[@]}"
)
proxy() {
printf 'Forwarding local port 25565 to %s. CTRL+C to stop.\n' "$(print_ip)"
socat TCP-LISTEN:25565,fork TCP:$(print_ip):25565
}
#
# Entrypoint
#
main() {
cd "${MY_DIR}"
if [ "$1" == "up" ]; then
shift
up "$@"
elif [ "$1" == "proxy" ]; then
proxy
elif [ "$1" == "rm" ]; then
shift
down "$@"
elif [ "$1" == "admin" ]; then
mark2_attach
elif [ "$1" == "map" ]; then
compose up mapcrafter
elif [ "$1" == "ssh" ]; then
shift
interactive_ssh "$@"
elif [ "$1" == "compose" ]; then
shift
compose "$@"
elif [ "$1" == "docker" ]; then
shift
while_connected docker "$@"
else
echo "Usage...... $0 (up [--local|--delete-world] [--build-spigot] [driver]|proxy|map|admin|rm [--no-preserve])"
echo "Advanced... $0 (ssh [args...]|compose [args...]|docker [args...])"
exit 1
fi
}
[ "${BASH_SOURCE[0]}" != "${0}" ] || main "$@"