diff options
Diffstat (limited to 'herculeswrapper')
-rwxr-xr-x | herculeswrapper/char.sh | 7 | ||||
-rw-r--r-- | herculeswrapper/herc-map-wrapper-config.example | 77 | ||||
-rw-r--r-- | herculeswrapper/include.sh | 287 | ||||
-rwxr-xr-x | herculeswrapper/login.sh | 7 | ||||
-rwxr-xr-x | herculeswrapper/map.sh | 102 |
5 files changed, 480 insertions, 0 deletions
diff --git a/herculeswrapper/char.sh b/herculeswrapper/char.sh new file mode 100755 index 0000000..2b794d9 --- /dev/null +++ b/herculeswrapper/char.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +export PIPE=./log/charpipe.tmp + +source ${dir}/include.sh + +server_logic ./char-server 2s diff --git a/herculeswrapper/herc-map-wrapper-config.example b/herculeswrapper/herc-map-wrapper-config.example new file mode 100644 index 0000000..00bfe8b --- /dev/null +++ b/herculeswrapper/herc-map-wrapper-config.example @@ -0,0 +1,77 @@ +## config for herc-map-wrapper + +echo "Copy this file to herc-map-wrapper-config" >&2 +echo "Then You need to either set the oauth secret" >&2 +echo "or use a hard-coded list instead of list_issues" >&2 +echo "Both github and gitlab methods are included.">&2 +echo "Make sure to comment out the method that isn't used" >&2 +echo "Update the <repo ids> with the ID number for gitlab" >&2 +echo "or the repo name if github i.e. themanaworld/server-data" >&2 +echo "Then, delete these lines" >&2 +exit 1 + +server_data=../../server-data +client_data=../client-data +evol_music=../music +tmw_tools=../tools + +#gitlab +list_issues() +{ +python -c ' + +# replace this with one of your oauth keys from github (no permissions needed) +oauth = "01234567890123456789" + +import requests +issues = requests.get("https://gitlab.com/api/v4/projects/%d/merge_requests" % '$1', params={"state": "opened"}, headers={"PRIVATE-TOKEN": "%s" % oauth, "Accept": "application/vnd.gitlab.v4+json"}) +issues.raise_for_status() +for issue in issues.json(): + if "test" in issue["labels"]: + print(issue["iid"]) +' | sort -n +} + +#github +list_issues() +{ +python -c ' + +# replace this with one of your oauth keys from github (no permissions needed) +oauth = "0123456789abcdef0123456789abcdef01234567" + +import requests +issues = requests.get("https://api.github.com/search/issues", params={"q": "repo:'$1' type:pr state:open label:test"}, headers={"Authorization": "token %s" % oauth, "Accept": "application/vnd.github.v3+json"}) +issues.raise_for_status() +for issue in issues.json["items"]: + print(issue["number"]) +' | sort -n +} + + +server_main_branch=origin/master + +server_extra_branches=( + $(list_issues <repo id> | sed 's|^|origin/merge-requests/|;') +) + +client_main_branch=origin/master + +client_extra_branches=( + $(list_issues <repo id> | sed 's|^|origin/merge-requests/|;') +) + +music_main_branch=origin/master + +music_extra_branches=( + $(list_issues <repo id> | sed 's|^|origin/merge-requests/|;') +) +unset list_issues + +motd=../server-data/npc/commands/motd-debug-text.txt + +expected_life=60 +trouble_sleep=60 +normal_sleep=5 + +ulimit -c unlimited diff --git a/herculeswrapper/include.sh b/herculeswrapper/include.sh new file mode 100644 index 0000000..17c8c0a --- /dev/null +++ b/herculeswrapper/include.sh @@ -0,0 +1,287 @@ +#!/usr/bin/env bash + +function create_pipe { + trap "rm -f $PIPE" EXIT + if [[ ! -p $PIPE ]]; then + echo "Making pipe $PIPE" + rm -f $PIPE + mkfifo $PIPE + fi +} + +# $1 - text +function send_all_pipes { + echo $1 >$LOGIN_PIPE + echo $1 >$CHAR_PIPE +} + +# $1 - text +function send_char_pipe { + echo $1 >$CHAR_PIPE +} + +# $1 - server binary name +# $2 - sleep time +function server_logic { + create_pipe + $1 + + while true + do + if read line <$PIPE; then + echo pipe: $line + if [[ "$line" == 'exit' ]]; then + exit + fi + if [[ "$line" == 'restart' ]]; then + sleep $2 + $1 + fi + fi + done +} + +function hard_reset { + echo ${1}: hard reset + git fetch upstream + git reset --hard upstream/master + git clean -fd + # the following should be unnecessary, but just in case: + git checkout --detach + git branch -D master + git checkout upstream/master -b master +} + +function pull_all { + hard_reset "data" + cd ../client-data + hard_reset "client" + cd ../music + hard_reset "music" + cd ../server-code + hard_reset "code" + cd src/evol + hard_reset "plugin" + cd ../../../tools + hard_reset "tools" + cd .. + ./pull.sh + cd server-data + git_merge + cd ../ + ./status.sh + cd server-data + make maps +} + +function build_all { + cd ../server-code + ./build.sh $BUILD_MAP 2>err.txt + cat err.txt + cd ../server-data + make updatedb +} + +function build_clean { + cd ../tools/localserver + ./clean.sh + cd ../../server-data +} + +function build_plugin { + cd ../server-plugin + ./build.sh 2>err.txt + cat err.txt + cd ../server-data +} + +function git_merge { + if [ -f ../tools/herculeswrapper/herc-map-wrapper-config ] + then + # prevent interaction + export GIT_EDITOR=: + # force commits to have predictable hashes + export GIT_AUTHOR_DATE='1970-01-01 00:00 +0000' + export GIT_COMMITTER_DATE='1970-01-01 00:00 +0000' + + reset=$'\e[m' + black_fg=$'\e[30m' + red_fg=$'\e[31m' + green_fg=$'\e[32m' + yellow_fg=$'\e[33m' + blue_fg=$'\e[34m' + magenta_fg=$'\e[35m' + cyan_fg=$'\e[36m' + gray_fg=$'\e[37m' + + reset2='##0' + black_fg2='##0' + red_fg2='##1' + green_fg2='##2' + blue_fg2='##3' + orange_fg2='##4' + yellow_fg2='##5' + pink_fg2='##6' + purple_fg2='##7' + gray_fg2='##8' + brown_fg2='##9' + + + now() + { + date -u +'%F %T' + } + + good() + { + echo "$(now)$green_fg" "$@" "$reset" >&2 + } + + info() + { + echo "$(now)$cyan_fg" "$@" "$reset" >&2 + } + + warning() + { + echo "$(now)$yellow_fg" "$@" "$reset" >&2 + } + + error() + { + echo "$(now)$red_fg" "$@" "$reset" >&2 + } + + good2() + { + echo \""$green_fg2" "$@" "$reset2"\", + } + + info2() + { + echo \""$brown_fg2" "$@" "$reset2"\", + } + + warning2() + { + echo \""$yellow_fg2" "$@" "$reset2"\", + } + + error2() + { + echo \""$red_fg2" "$@" "$reset2"\", + } + + run() + { + info '$' "$@" + "$@" + } + + wipe_logs() + { + echo -e "$(now)\n===\n" > $merge_log + chmod +r $merge_log + } + + do_git() + { + info '$' "git $@" + echo -e "${PWD##*/} $ git $@" >> $merge_log + git "$@" 2> >(tee -a $merge_log >&2) + ec=$? + + # print a newline to logs on error + if [[ $ec != 0 ]]; then echo "" >> $merge_log; fi + return $ec + } + + try_merge() + { + test -n "$1" + local branch=$1 commit + info 'commit=$(' git rev-parse --verify -q $branch ')' + if ! commit=$(git rev-parse --verify -q $branch) + then + error2 bogus $branch >> $motd + error bogus $branch + return + fi + info commit=$commit + if run bash -c 'set -o pipefail; git branch --contains '$commit' | grep -qw master' + then + warning2 already $branch $commit >> $motd + warning already $branch + return + fi + if git merge-base --is-ancestor $branch $commit && do_git merge --ff-only $branch $commit + then + good2 fast $branch $commit >> $motd + good fast $branch $commit + return + fi + if do_git merge $branch $commit + then + good2 merge $branch $commit >> $motd + good merge $branch $commit + return + fi + git merge --abort + error2 abort $branch $commit >> $motd + error abort $branch $commit + return + } + + start_time=$(date +%s) + source ../tools/herculeswrapper/herc-map-wrapper-config + + run test -f $motd + echo -e 'function\tscript\tMOTD_debug_text\t{\n setarray $@Debug_Messages$[0],' > $motd + merge_log=${tmw_tools}/herculeswrapper/stderr.log + wipe_logs + do_git fetch --all + do_git reset --hard $server_main_branch + info 'commit=$(' git rev-parse --verify -q $server_main_branch ')' + commit=$(git rev-parse --verify -q $server_main_branch) + info2 server base $server_main_branch $commit >> $motd + for branch in ${server_extra_branches[@]} + do + try_merge $branch + done + + run cd $client_data + + # force new master and back up for future restoration + do_git fetch --all + do_git reset --hard $client_main_branch + + info 'commit=$(' git rev-parse --verify -q $client_main_branch ')' + commit=$(git rev-parse --verify -q $client_main_branch) + info2 client base $client_main_branch $commit >> $motd + for branch in ${client_extra_branches[@]} + do + try_merge $branch + done + + run cd $evol_music + + do_git fetch --all + do_git reset --hard $music_main_branch + info 'commit=$(' git rev-parse --verify -q $music_main_branch ')' + commit=$(git rev-parse --verify -q $music_main_branch) + info2 music base $music_main_branch $commit >> $motd + for branch in ${music_extra_branches[@]} + do + try_merge $branch + done + + echo '"End of Commits";' >> $motd + echo -e ' return;\n}' >> $motd + + # generate the transient updates + run cd $tmw_tools/update/ + run ./createnew.sh + run ./create_music.sh + run cd ../../server-data/ + fi +} diff --git a/herculeswrapper/login.sh b/herculeswrapper/login.sh new file mode 100755 index 0000000..32085dd --- /dev/null +++ b/herculeswrapper/login.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +export PIPE=./log/loginpipe.tmp + +source ${dir}/include.sh + +server_logic ./login-server 0s diff --git a/herculeswrapper/map.sh b/herculeswrapper/map.sh new file mode 100755 index 0000000..d0cd929 --- /dev/null +++ b/herculeswrapper/map.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +export PIPE=./log/mappipe.tmp +export LOGIN_PIPE=./log/loginpipe.tmp +export CHAR_PIPE=./log/charpipe.tmp + +source ${dir}/include.sh + +create_pipe + +while [ 1 ] ; do + $DEBUG_MAP ./map-server + export ret=$? + case "${ret}" in + 0) + echo "Returned 0. Probably ctrl+c" + break + ;; + 1) + echo "Returned 1. Probably error in server" + [ ! -z "$DEBUG_MAP" ] || break + ;; + 100) + echo "Terminating server" + send_all_pipes "exit" + break + ;; + 101) + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + 102) + echo "Restarting chat and map servers..." + send_char_pipe "restart" + sleep 3s + ;; + 103) + echo "Restarting map server..." + ;; + 104) + echo "git pull..." + pull_all + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + 105) + echo "Build all servers" + sleep 5s + build_all + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + 106) + echo "Rebuild all servers" + sleep 5s + build_clean + build_all + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + 107) + echo "git pull..." + pull_all + echo "Build all servers" + build_all + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + 108) + echo "git pull..." + pull_all + echo "Rebuild all servers" + build_clean + build_all + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + 109) + echo "Build plugin" + sleep 5s + build_plugin + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + 110) + echo "git pull..." + pull_all + echo "Build plugin" + build_plugin + echo "Restarting all servers..." + send_all_pipes "restart" + sleep 5s + ;; + esac +done |