summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/github_hosts_sync/index
blob: 31cbc7630715824dcebbe5ec707dfe87cf3075ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 "$@"