]> Sergey Matveev's repositories - public-inbox.git/blob - examples/grok-pull.post_update_hook.sh
77489472123192ca1df6522e6a1bbaa8fe6845b5
[public-inbox.git] / examples / grok-pull.post_update_hook.sh
1 #!/bin/sh
2
3 # use flock(1) from util-linux to avoid seek contention on slow HDDs
4 # when using multiple `pull_threads' with grok-pull:
5 # [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock "$0" "$0" "$@" || :
6
7 # post_update_hook for repos.conf as used by grok-pull, takes a full
8 # git repo path as it's first and only arg.
9 full_git_dir="$1"
10
11 url_base=http://127.0.0.1:8080/
12
13 # same default as other public-inbox-* tools
14 PI_CONFIG=${PI_CONFIG-~/.public-inbox/config}
15
16 # FreeBSD expr(1) only supports BRE, so no '+'
17 EPOCH2MAIN='\(..*\)/git/[0-9][0-9]*\.git'
18
19 # see if it's v2 or v1 based on tree contents, since somebody could
20 # theoretically name a v1 inbox with a path that looks like a v2 epoch
21 if git --git-dir="$full_git_dir" ls-tree --name-only HEAD | \
22         grep -E '^(m|d)$' >/dev/null
23 then
24         inbox_fmt=2
25         inbox_dir=$(expr "$full_git_dir" : "$EPOCH2MAIN")
26         inbox_name=$(basename "$inbox_dir")
27         msgmap="$inbox_dir"/msgmap.sqlite3
28         inbox_lock="$inbox_dir"/inbox.lock
29 else
30         inbox_fmt=1
31         inbox_dir="$full_git_dir"
32         inbox_name=$(basename "$inbox_dir" .git)
33         msgmap="$inbox_dir"/public-inbox/msgmap.sqlite3
34         inbox_lock="$inbox_dir"/ssoma.lock
35 fi
36
37 # run public-inbox-init iff unconfigured
38 cfg_dir=$(git config -f "$PI_CONFIG" publicinbox."$inbox_name".inboxdir)
39
40 # check legacy name for "inboxdir"
41 case $cfg_dir in
42 '') cfg_dir=$(git config -f "$PI_CONFIG" publicinbox."$inbox_name".mainrepo) ;;
43 esac
44
45 case $cfg_dir in
46 '')
47         remote_git_url=$(git --git-dir="$full_git_dir" config remote.origin.url)
48         case $remote_git_url in
49         '')
50                 echo >&2 "remote.origin.url unset in $full_git_dir/config"
51                 exit 1
52                 ;;
53         esac
54
55         case $inbox_fmt in
56         1)
57                 remote_inbox_url="$remote_git_url"
58                 ;;
59         2)
60                 remote_inbox_url=$(expr "$remote_git_url" : "$EPOCH2MAIN")
61                 ;;
62         esac
63
64         config_url="$remote_inbox_url"/_/text/config/raw
65         remote_config="$inbox_dir"/remote.config.$$
66         infourls=
67         trap 'rm -f "$remote_config"' EXIT
68         if curl --compressed -sSf -v "$config_url" >"$remote_config"
69         then
70                 # n.b. inbox_name on the remote may not match our local
71                 # inbox_name, so we match all addresses in the remote config
72                 addresses=$(git config -f "$remote_config" -l | \
73                         sed -ne 's/^publicinbox\..\+\.address=//p')
74                 case $addresses in
75                 '')
76                         echo >&2 'unable to extract address(es) from ' \
77                                 "$remote_config"
78                         exit 1
79                         ;;
80                 esac
81                 newsgroups=$(git config -f "$remote_config" -l | \
82                         sed -ne 's/^publicinbox\..\+\.newsgroup=//p')
83                 infourls=$(git config -f "$remote_config" -l | \
84                         sed -ne 's/^publicinbox\..\+.infourl=//p')
85         else
86                 newsgroups=
87                 addresses="$inbox_name@$$.$(hostname).example.com"
88                 echo >&2 "E: curl $config_url failed"
89                 echo >&2 "E: using bogus <$addresses> for $inbox_dir"
90         fi
91         local_url="$url_base$inbox_name"
92         public-inbox-init -V$inbox_fmt "$inbox_name" \
93                 "$inbox_dir" "$local_url" $addresses
94
95         if test $? -ne 0
96         then
97                 echo >&2 "E: public-inbox-init failed on $inbox_dir"
98                 exit 1
99         fi
100
101         for ng in $newsgroups
102         do
103                 git config -f "$PI_CONFIG" \
104                         "publicinbox.$inbox_name.newsgroup" "$ng"
105                 # only one newsgroup per inbox
106                 break
107         done
108         for url in $infourls
109         do
110                 git config -f "$PI_CONFIG" \
111                         "publicinbox.$inbox_name.infourl" "$url"
112         done
113         curl -sSfv "$remote_inbox_url"/description >"$inbox_dir"/description
114         echo "I: $inbox_name at $inbox_dir ($addresses) $local_url"
115         ;;
116 esac
117
118 # only run public-inbox-index if an index exists and has messages,
119 # since epochs may be cloned out-of-order by grokmirror and we also
120 # don't know what indexlevel a user wants
121 if test -f "$msgmap"
122 then
123         # We need to use flock(1) (from util-linux) to avoid timeouts
124         # and SQLite locking problems.
125         # FreeBSD has a similar lockf(1) utility, but it unlinks by
126         # default so we use `-k' to keep the lock on the FS.
127         FLOCK=flock
128         case $(uname -s) in
129         FreeBSD) FLOCK='lockf -k' ;;
130         # ... other OSes here
131         esac
132
133         n=$(echo 'SELECT COUNT(*) FROM msgmap' | \
134                 $FLOCK $inbox_lock sqlite3 -readonly "$msgmap")
135         case $n in
136         0|'')
137                 : v2 inboxes may be init-ed with an empty msgmap
138                 ;;
139         *)
140                 # if on HDD and limited RAM, add `--sequential-shard'
141                 # and possibly a large `--batch-size' if you have much
142                 # memory in public-inbox 1.6.0+
143                 $EATMYDATA public-inbox-index -v "$inbox_dir"
144                 ;;
145         esac
146 fi