2024-10-04 04:24:51 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-10-11 01:56:56 -04:00
|
|
|
START_TIME=$(date +"%H:%M") # the checkTime.js script uses local time
|
2024-10-10 22:26:05 -04:00
|
|
|
|
2024-10-04 04:24:51 -04:00
|
|
|
GH_TOKEN=$2
|
|
|
|
|
|
|
|
REPO=$3
|
|
|
|
WORKFLOW_FILE=$4
|
|
|
|
BRANCH=$5
|
|
|
|
|
|
|
|
NAME=$6
|
|
|
|
|
|
|
|
WEBHOOK_URL=$7
|
|
|
|
|
2024-10-05 21:11:19 -04:00
|
|
|
IS_GLOBAL=$8
|
|
|
|
OLD_TAILSCALE_HOSTNAME=$9
|
|
|
|
|
2024-10-04 04:24:51 -04:00
|
|
|
firstTime=1
|
|
|
|
|
|
|
|
alreadyDone=0
|
|
|
|
|
|
|
|
sudo pkill provjobd
|
|
|
|
|
|
|
|
getWebhookData() {
|
|
|
|
output="{\"username\": \"$NAME\", "
|
|
|
|
output+="\"embeds\": [ "
|
|
|
|
|
|
|
|
if [ "$1" == "start" ]; then
|
|
|
|
output+="{\"title\": \"Loop Script is running!\", "
|
|
|
|
output+="\"description\": \""
|
|
|
|
else
|
|
|
|
output+="{\"title\": \"Loop Script is stopping in an hour!\", "
|
|
|
|
output+="\"description\": \""
|
|
|
|
output+="Hostname has been renamed to: $2\\n\\n"
|
|
|
|
fi
|
|
|
|
|
|
|
|
output+="$REPO on $BRANCH, workflow file $WORKFLOW_FILE\\n\\n"
|
|
|
|
|
|
|
|
output+="Sent at:\\n$(date) (server time)\\n"
|
|
|
|
output+="$(TZ=Etc/UTC date)"
|
|
|
|
output+="\\n$(TZ=Asia/Tokyo date)"
|
|
|
|
output+="\\n$(TZ=Asia/Bangkok date)\", "
|
|
|
|
|
|
|
|
if [ "$1" == "start" ]; then
|
|
|
|
output+="\"color\": 5763719} ]}"
|
|
|
|
else
|
|
|
|
output+="\"color\": 15105570} ]}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo $output
|
|
|
|
}
|
|
|
|
|
|
|
|
requestWebhook() {
|
|
|
|
curl \
|
|
|
|
-X POST \
|
|
|
|
-H "Accept: application/json" \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
--data "$(getWebhookData $1 $2)" \
|
|
|
|
$WEBHOOK_URL
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
2024-10-11 06:54:19 -04:00
|
|
|
# checkTime.js adds the start time by 5 hours
|
2024-10-04 04:24:51 -04:00
|
|
|
|
2024-10-10 22:26:05 -04:00
|
|
|
node checkTime.js $START_TIME
|
|
|
|
exitCode=$?
|
2024-10-04 04:24:51 -04:00
|
|
|
|
2024-10-10 22:26:05 -04:00
|
|
|
if [ $exitCode -eq 0 ]; then
|
2024-10-04 04:24:51 -04:00
|
|
|
alreadyDone=1
|
|
|
|
|
|
|
|
hostname="old-$NAME-$RANDOM"
|
|
|
|
|
|
|
|
requestWebhook stop $hostname
|
|
|
|
|
|
|
|
sudo tailscale up --hostname=$hostname --advertise-exit-node --ssh
|
|
|
|
|
2024-10-05 21:11:19 -04:00
|
|
|
command="gh api "
|
|
|
|
command+="--method POST "
|
|
|
|
command+='-H "Accept: application/vnd.github+json" '
|
|
|
|
command+='-H "X-GitHub-Api-Version: 2022-11-28" '
|
|
|
|
command+="\"/repos/CeciliaKelley33Mm/$REPO/actions/workflows/$WORKFLOW_FILE/dispatches\" "
|
|
|
|
command+="-f \"ref=$BRANCH\" -f \"inputs[runNext]=true\" "
|
|
|
|
|
2024-10-06 02:02:45 -04:00
|
|
|
if [[ "$IS_GLOBAL" == "true" ]]; then
|
|
|
|
command+="-f \"inputs[oldTailscaleHostname]=$hostname\""
|
2024-10-05 21:11:19 -04:00
|
|
|
|
2024-10-09 21:42:12 -04:00
|
|
|
cd /mnt/globalData/toBackup/minecraft
|
2024-10-07 02:03:25 -04:00
|
|
|
|
2024-10-08 03:52:35 -04:00
|
|
|
timeLeft=30
|
2024-10-07 02:03:25 -04:00
|
|
|
|
2024-10-08 03:52:35 -04:00
|
|
|
while [ $timeLeft -gt 0 ]; do
|
|
|
|
if [ $(($timeLeft % 10)) -eq 0 ]; then
|
2024-10-09 02:28:32 -04:00
|
|
|
docker compose exec minecraft rcon-cli broadcast The server will stop and move to a new host in $timeLeft seconds. Please leave the server.
|
2024-10-08 03:52:35 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
timeLeft=$((timeLeft - 1))
|
|
|
|
done
|
2024-10-07 02:03:25 -04:00
|
|
|
|
|
|
|
# not stopping the server would cause problems
|
2024-10-09 21:42:12 -04:00
|
|
|
# like new world files not getting written
|
|
|
|
# or even file corruptions
|
2024-10-07 02:03:25 -04:00
|
|
|
docker compose down
|
|
|
|
|
2024-10-07 07:24:46 -04:00
|
|
|
cd /mnt/globalData
|
2024-10-05 21:11:19 -04:00
|
|
|
|
|
|
|
# sudo is necessary because we run minecraft in a docker container
|
2024-10-09 21:42:12 -04:00
|
|
|
sudo tar cf archive.tar.gz toBackup/
|
2024-10-05 21:11:19 -04:00
|
|
|
|
|
|
|
serve -p 5000 &
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval "$command"
|
2024-10-10 22:26:05 -04:00
|
|
|
fi
|
2024-10-04 04:24:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$1" == "true" ]; then
|
|
|
|
requestWebhook start
|
|
|
|
fi
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
if [ "$firstTime" != 1 ] && [ "$alreadyDone" != 1 ] && [ "$1" == "true" ]; then
|
|
|
|
check
|
|
|
|
fi
|
|
|
|
|
|
|
|
sudo sync
|
|
|
|
echo 3 | sudo tee /proc/sys/vm/drop_caches
|
|
|
|
|
|
|
|
ping -c 1 google.com
|
|
|
|
curl google.com
|
|
|
|
|
|
|
|
if [ "$firstTime" != 1 ]; then
|
|
|
|
sleep 10
|
|
|
|
else
|
|
|
|
sleep 120
|
|
|
|
|
|
|
|
firstTime=0
|
|
|
|
fi
|
|
|
|
done
|