]> Sergey Matveev's repositories - public-inbox.git/blob - examples/grok-pull.post_update_hook.sh
examples: add imapd systemd examples
[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 else
29         inbox_fmt=1
30         inbox_dir="$full_git_dir"
31         inbox_name=$(basename "$inbox_dir" .git)
32         msgmap="$inbox_dir"/public-inbox/msgmap.sqlite3
33 fi
34
35 # run public-inbox-init iff unconfigured
36 cfg_dir=$(git config -f "$PI_CONFIG" publicinbox."$inbox_name".inboxdir)
37
38 # check legacy name for "inboxdir"
39 case $cfg_dir in
40 '') cfg_dir=$(git config -f "$PI_CONFIG" publicinbox."$inbox_name".mainrepo) ;;
41 esac
42
43 case $cfg_dir in
44 '')
45         remote_git_url=$(git --git-dir="$full_git_dir" config remote.origin.url)
46         case $remote_git_url in
47         '')
48                 echo >&2 "remote.origin.url unset in $full_git_dir/config"
49                 exit 1
50                 ;;
51         esac
52
53         case $inbox_fmt in
54         1)
55                 remote_inbox_url="$remote_git_url"
56                 ;;
57         2)
58                 remote_inbox_url=$(expr "$remote_git_url" : "$EPOCH2MAIN")
59                 ;;
60         esac
61
62         config_url="$remote_inbox_url"/_/text/config/raw
63         remote_config="$inbox_dir"/remote.config.$$
64         infourls=
65         trap 'rm -f "$remote_config"' EXIT
66         if curl --compressed -sSf -v "$config_url" >"$remote_config"
67         then
68                 # n.b. inbox_name on the remote may not match our local
69                 # inbox_name, so we match all addresses in the remote config
70                 addresses=$(git config -f "$remote_config" -l | \
71                         sed -ne 's/^publicinbox\..\+\.address=//p')
72                 case $addresses in
73                 '')
74                         echo >&2 'unable to extract address(es) from ' \
75                                 "$remote_config"
76                         exit 1
77                         ;;
78                 esac
79                 newsgroups=$(git config -f "$remote_config" -l | \
80                         sed -ne 's/^publicinbox\..\+\.newsgroup=//p')
81                 infourls=$(git config -f "$remote_config" -l | \
82                         sed -ne 's/^publicinbox\..\+.infourl=//p')
83         else
84                 newsgroups=
85                 addresses="$inbox_name@$$.$(hostname).example.com"
86                 echo >&2 "E: curl $config_url failed"
87                 echo >&2 "E: using bogus <$addresses> for $inbox_dir"
88         fi
89         local_url="$url_base$inbox_name"
90         public-inbox-init -V$inbox_fmt "$inbox_name" \
91                 "$inbox_dir" "$local_url" $addresses
92
93         if test $? -ne 0
94         then
95                 echo >&2 "E: public-inbox-init failed on $inbox_dir"
96                 exit 1
97         fi
98
99         for ng in $newsgroups
100         do
101                 git config -f "$PI_CONFIG" \
102                         "publicinbox.$inbox_name.newsgroup" "$ng"
103                 # only one newsgroup per inbox
104                 break
105         done
106         for url in $infourls
107         do
108                 git config -f "$PI_CONFIG" \
109                         "publicinbox.$inbox_name.infourl" "$url"
110         done
111         curl -sSfv "$remote_inbox_url"/description >"$inbox_dir"/description
112         echo "I: $inbox_name at $inbox_dir ($addresses) $local_url"
113         ;;
114 esac
115
116 # only run public-inbox-index if an index exists and has messages,
117 # since epochs may be cloned out-of-order by grokmirror and we also
118 # don't know what indexlevel a user wants
119 if test -f "$msgmap"
120 then
121         n=$(echo 'SELECT COUNT(*) FROM msgmap' | sqlite3 -readonly "$msgmap")
122         case $n in
123         0|'')
124                 : v2 inboxes may be init-ed with an empty msgmap
125                 ;;
126         *)
127                 # if on HDD and limited RAM, add `--sequential-shard'
128                 # and possibly a large `--batch-size' if you have much
129                 # memory in public-inbox 1.6.0+
130                 $EATMYDATA public-inbox-index -v "$inbox_dir"
131                 ;;
132         esac
133 fi