From 27d514e6257b7dfbff5ee9cbd95ca6496d1d07c7 Mon Sep 17 00:00:00 2001 From: CeciliaKelley33Mm Date: Fri, 4 Oct 2024 15:24:51 +0700 Subject: [PATCH] move the loop script to here --- .github/workflows/runglobal.yml | 2 +- loop.sh | 126 ++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 loop.sh diff --git a/.github/workflows/runglobal.yml b/.github/workflows/runglobal.yml index 7fc84dd..9fc3772 100644 --- a/.github/workflows/runglobal.yml +++ b/.github/workflows/runglobal.yml @@ -68,6 +68,6 @@ jobs: sed -i 's*KITESECRETPK*${{ secrets.KITESECRET }}*g' ~/.pagekite.rc python pagekite.py & - wget -O loop.sh https://github.com/CeciliaKelley33Mm/Docker-VNC/raw/main/loop.sh + wget https://raw.githubusercontent.com/CeciliaKelley33Mm/global/refs/heads/master/loop.sh chmod +x loop.sh bash loop.sh ${{ inputs.runNext }} $GH_TOKEN global runglobal.yml master global $WEBHOOK_URL diff --git a/loop.sh b/loop.sh new file mode 100644 index 0000000..06e35b1 --- /dev/null +++ b/loop.sh @@ -0,0 +1,126 @@ +#!/bin/bash + +GH_TOKEN=$2 + +REPO=$3 +WORKFLOW_FILE=$4 +BRANCH=$5 + +NAME=$6 + +WEBHOOK_URL=$7 + +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() { + currentTime=$(TZ=Etc/UTC date +"%H-%M") + + # this is the pattern. the old machine will activate the new machine 1 hour before it terminates + + # 1 00:00 RUN + # 1 05:50 ACTIVATE 2 + # 1 06:00 TERMINATE + + # 2 05:50 RUN + # 2 10:50 ACTIVATE 3 + # 2 11:50 TERMINATE + + # 3 10:50 RUN + # 3 15:50 ACTIVATE 4 + # 3 16:50 TERMINATE + + # 4 15:50 RUN + # 4 20:50 ACTIVATE 5 + # 4 21:50 TERMINATE + + # 5 20:50 RUN + # 5 00:00 ACTIVATE 1 + # 5 01:50 TERMINATE + + targetTimes=("00-00" "05-50" "10-50" "15-50" "20-50") + + for target in "${targetTimes[@]}"; do + if [[ "$currentTime" != "$target" ]]; then continue; fi + + alreadyDone=1 + + hostname="old-$NAME-$RANDOM" + + requestWebhook stop $hostname + + sudo tailscale up --hostname=$hostname --advertise-exit-node --ssh + + gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "/repos/CeciliaKelley33Mm/$REPO/actions/workflows/$WORKFLOW_FILE/dispatches" \ + -f "ref=$BRANCH" -f "inputs[runNext]=true" + done +} + +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