summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/github_hosts_sync
diff options
context:
space:
mode:
authortv <tv@nomic.retiolum>2014-02-19 13:41:11 +0100
committertv <tv@nomic.retiolum>2014-02-19 13:41:11 +0100
commitbcc7aefa1c6fb999992c07327e1df9456814a283 (patch)
tree13650c1701a5995e833bce2539664e75cb282677 /retiolum/scripts/github_hosts_sync
parentca650da57a46a1768258bfb88002d9080f0d165c (diff)
retiolum github hosts sync: initial commit
Diffstat (limited to 'retiolum/scripts/github_hosts_sync')
-rwxr-xr-xretiolum/scripts/github_hosts_sync/index62
1 files changed, 62 insertions, 0 deletions
diff --git a/retiolum/scripts/github_hosts_sync/index b/retiolum/scripts/github_hosts_sync/index
new file mode 100755
index 00000000..31cbc763
--- /dev/null
+++ b/retiolum/scripts/github_hosts_sync/index
@@ -0,0 +1,62 @@
+#! /bin/sh
+# TODO do_work should retry until success
+set -euf
+
+port=1028
+local_painload=/krebs; remote_painload="git@github.com:krebscode/painload.git"
+local_hosts=/tmp/hosts; remote_hosts="git@github.com:krebscode/hosts.git"
+
+main() {
+ ensure_local_painload
+ ensure_local_hosts
+ is_worker && do_work || become_server
+}
+
+ensure_local_painload() {
+ test -d "$local_painload" || git clone "$remote_painload" "$local_painload"
+}
+
+ensure_local_hosts() {
+ test -d "$local_hosts" || git clone "$remote_hosts" "$local_hosts"
+}
+
+become_server() {
+ exec socat "TCP-LISTEN:$port,reuseaddr,fork" "EXEC:$0"
+}
+
+is_worker() {
+ test "${SOCAT_SOCKPORT-}" = "$port"
+}
+
+do_work() {
+ # read request
+ req_line="$(read line && echo "$line")"
+ req_hdrs="$(sed -n '/^\r$/q;p')"
+
+ set -x
+
+ cd "$local_hosts"
+ git pull >&2
+
+ cd "$local_hosts"
+ find . -name .git -prune -o -type f -exec git rm \{\} \; >/dev/null
+
+ cd "$local_painload"
+ git pull >&2
+
+ find "$local_painload/retiolum/hosts" -type f -exec cp \{\} "$local_hosts" \;
+
+ cd "$local_hosts"
+ find . -name .git -prune -o -type f -exec git add \{\} \; >&2
+ if git status --porcelain | grep -q .; then
+ git commit -m bump >&2
+ git push >&2
+ fi
+
+ echo "HTTP/1.1 200 OK"
+ echo
+ echo "https://github.com/krebscode/hosts/archive/master.tar.gz"
+ echo "https://github.com/krebscode/hosts/archive/master.zip"
+}
+
+main "$@"