summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/tmwa-map-wrapper211
-rw-r--r--bin/tmwa-map-wrapper-config51
2 files changed, 262 insertions, 0 deletions
diff --git a/bin/tmwa-map-wrapper b/bin/tmwa-map-wrapper
new file mode 100755
index 0000000..723b284
--- /dev/null
+++ b/bin/tmwa-map-wrapper
@@ -0,0 +1,211 @@
+#!/bin/bash -eu
+# autoupdate and restart a tmwa-map server
+
+## config is now in tmwa-map-wrapper-config
+
+## utilities
+trap 'echo EXIT $? >&2' EXIT
+
+# 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 '$' "$@"
+ "$@"
+}
+
+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 run git merge --ff-only $branch $commit
+ then
+ good2 fast $branch $commit >> $motd
+ good fast $branch $commit
+ return
+ fi
+ if run 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
+}
+
+
+## main script
+trouble=false
+
+while true
+do
+ start_time=$(date +%s)
+ source tmwa-map-wrapper-config
+ run test -f $motd
+
+
+
+ run cd $tmw_tools
+ run git pull
+
+
+
+ run cd $server_data
+ run git fetch --all
+ run 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
+ if $trouble
+ then
+ error "Not merging branches - we're in trouble."
+ else
+ for branch in ${server_extra_branches[@]}
+ do
+ try_merge $branch
+ done
+ fi
+ $tmw_tools/secrets-build < $magic_conf.template > $magic_conf
+ $tmw_tools/news.py $start_dir $start_dir/news.d/
+ chmod a+r $start_dir/news.txt $start_dir/news.html
+
+
+
+ run cd $client_data
+ # restore update state to old
+ run cp ~/www/updates/resources.xml{.master,}
+ run cp ~/www/updates/resources2.txt{.master,}
+ run git branch -f update-zips $client_main_branch
+
+ # force new master and back up for future restoration
+ run git fetch --all
+ run git reset --hard $client_main_branch
+ run $tmw_tools/client/make-updates
+ run cp ~/www/updates/resources.xml{,.master}
+ run cp ~/www/updates/resources2.txt{,.master}
+
+ 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
+ if $trouble
+ then
+ error "Not merging branches - we're in trouble."
+ else
+ for branch in ${client_extra_branches[@]}
+ do
+ try_merge $branch
+ done
+ fi
+ # generate the transient updates
+ run $tmw_tools/client/make-updates
+
+
+
+ run cd $start_dir
+
+ info '$' tmwa-map
+ tmwa-map || { error tmwa-map exited with status $?; }
+
+
+ end_time=$(date +%s)
+
+ if (( end_time < start_time + expected_life ))
+ then
+ trouble=true
+ error 'Something is wrong'
+ run sleep $trouble_sleep
+ else
+ trouble=false
+ warning 'Something is not wrong'
+ run sleep $normal_sleep
+ fi
+done
diff --git a/bin/tmwa-map-wrapper-config b/bin/tmwa-map-wrapper-config
new file mode 100644
index 0000000..4608df7
--- /dev/null
+++ b/bin/tmwa-map-wrapper-config
@@ -0,0 +1,51 @@
+## config for tmwa-map-wrapper
+# Must be in $PATH even though it's not executable.
+
+echo "You need to either set the oauth secret" >&2
+echo "or use a hard-coded list instead of list_issues" >&2
+echo "Then, delete these lines" >&2
+exit 1
+
+server_data=~/tmwa-server-data
+client_data=~/tmwa-client-data
+tmw_tools=~/tmw-tools
+
+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=(
+ origin/stable
+ $(list_issues themanaworld/tmwa-server-data | sed 's|^|origin/pr/|;')
+)
+
+client_main_branch=origin/master
+
+client_extra_branches=(
+ $(list_issues themanaworld/tmwa-client-data | sed 's|^|origin/pr/|;')
+)
+unset list_issues
+
+start_dir=$server_data/world/map
+motd=$start_dir/conf/motd.txt
+magic_conf=$start_dir/conf/magic-secrets.conf
+
+expected_life=60
+trouble_sleep=60
+normal_sleep=5
+
+ulimit -c unlimited