1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # common stuff for administrative command-line tools
5 # Unstable internal API
6 package PublicInbox::Admin;
8 use parent qw(Exporter);
11 our @EXPORT_OK = qw(setup_signals);
12 use PublicInbox::Config;
13 use PublicInbox::Inbox;
14 use PublicInbox::Spawn qw(popen_rd);
17 my ($cb, $arg) = @_; # optional
19 # we call exit() here instead of _exit() so DESTROY methods
20 # get called (e.g. File::Temp::Dir and PublicInbox::Msgmap)
21 $SIG{INT} = $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = sub {
23 # https://www.tldp.org/LDP/abs/html/exitcodes.html
24 eval { $cb->($sig, $arg) } if $cb;
26 exit(128 + POSIX->$sig);
30 sub resolve_inboxdir {
32 my $prefix = defined $cd ? $cd : './';
33 if (-d $prefix && -f "$prefix/inbox.lock") { # v2
35 return abs_path($prefix);
37 my $cmd = [ qw(git rev-parse --git-dir) ];
38 my $fh = popen_rd($cmd, undef, {-C => $cd});
39 my $dir = do { local $/; <$fh> };
40 close $fh or die "error in ".join(' ', @$cmd)." (cwd:$cd): $!\n";
43 return abs_path($cd) if ($dir eq '.' && defined $cd);
47 # for unconfigured inboxes
48 sub detect_indexlevel ($) {
51 my $over = $ibx->over;
52 my $srch = $ibx->search;
53 delete @$ibx{qw(over search)}; # don't leave open FDs lying around
55 # brand new or never before indexed inboxes default to full
56 return 'full' unless $over;
58 return $l unless $srch;
59 if (my $xdb = $srch->xdb) {
61 my $m = $xdb->get_metadata('indexlevel');
66 $ibx->{inboxdir} has unexpected indexlevel in Xapian: $m
69 $ibx->{-skip_docdata} = 1 if $xdb->get_metadata('skip_docdata');
74 sub unconfigured_ibx ($$) {
76 my $name = "unconfigured-$i";
77 PublicInbox::Inbox->new({
79 address => [ "$name\@example.com" ],
81 # TODO: consumers may want to warn on this:
86 sub resolve_inboxes ($;$$) {
87 my ($argv, $opt, $cfg) = @_;
90 $cfg //= PublicInbox::Config->new;
92 my $cfgfile = PublicInbox::Config::default_file();
93 $cfg or die "--all specified, but $cfgfile not readable\n";
94 @$argv and die "--all specified, but directories specified\n";
97 my $min_ver = $opt->{-min_inbox_version} || 0;
100 my $all = $opt->{all} ? [] : undef;
102 $cfg->each_inbox(sub {
104 my $path = abs_path($ibx->{inboxdir});
105 if (defined($path)) {
106 $dir2ibx{$path} = $ibx;
107 push @$all, $ibx if $all;
110 W: $ibx->{name} $ibx->{inboxdir}: $!
116 @$all = grep { $_->version >= $min_ver } @$all;
118 } else { # directories specified on the command-line
121 push @dirs, '.' if !@dirs && $opt->{-use_cwd};
124 my $dir = resolve_inboxdir($_, \$v);
129 my $ibx = $dir2ibx{$dir} ||= unconfigured_ibx($dir, $i);
135 die "-V$min_ver inboxes not supported by $0\n\t",
136 join("\n\t", @old), "\n";
141 # TODO: make Devel::Peek optional, only used for daemon
142 my @base_mod = qw(Devel::Peek);
143 my @over_mod = qw(DBD::SQLite DBI);
145 -index => [ @base_mod, @over_mod ],
147 -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
150 sub scan_ibx_modules ($$) {
151 my ($mods, $ibx) = @_;
152 if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
153 $mods->{'Search::Xapian'} = 1;
155 $mods->{$_} = 1 foreach @over_mod;
162 while (my $mod = shift @mods) {
163 if (my $groups = $mod_groups{$mod}) {
164 push @mods, @$groups;
165 } elsif ($mod eq 'Search::Xapian') {
166 require PublicInbox::Search;
167 PublicInbox::Search::load_xapian() or
168 $err->{'Search::Xapian || Xapian'} = $@;
171 $err->{$mod} = $@ if $@;
174 scalar keys %$err ? $err : undef;
177 sub missing_mod_msg {
179 my @mods = map { "`$_'" } sort keys %$err;
180 my $last = pop @mods;
181 @mods ? (join(', ', @mods)."' and $last") : $last
185 my $err = check_require(@_) or return;
186 die missing_mod_msg($err)." required for $0\n";
189 sub indexlevel_ok_or_die ($) {
190 my ($indexlevel) = @_;
192 if ($indexlevel eq 'basic') {
194 } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
198 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
201 my $err = check_require($req) or return;
202 die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
205 sub index_terminate {
206 my (undef, $ibx) = @_; # $_[0] = signal name
211 my ($ibx, $im, $opt) = @_;
212 require PublicInbox::InboxWritable;
213 my $jobs = delete $opt->{jobs} if $opt;
214 if (my $pr = $opt->{-progress}) {
215 $pr->("indexing $ibx->{inboxdir} ...\n");
218 setup_signals(\&index_terminate, $ibx);
219 my $warn_cb = $SIG{__WARN__} // sub { print STDERR @_ };
220 my $idx = { current_info => $ibx->{inboxdir} };
221 my $warn_ignore = PublicInbox::InboxWritable->can('warn_ignore');
222 local $SIG{__WARN__} = sub {
223 return if $warn_ignore->(@_);
224 $warn_cb->($idx->{current_info}, ': ', @_);
226 if (ref($ibx) && $ibx->version == 2) {
227 eval { require PublicInbox::V2Writable };
228 die "v2 requirements not met: $@\n" if $@;
229 $ibx->{-creat_opt}->{nproc} = $jobs;
230 my $v2w = $im // $ibx->importer($opt->{reindex} // $jobs);
233 $v2w->{parallel} = 0;
235 my $n = $v2w->{shards};
236 if ($jobs < ($n + 1) && !$opt->{reshard}) {
238 Unable to respect --jobs=$jobs on index, inbox was created with $n shards
245 require PublicInbox::SearchIdx;
246 $idx = PublicInbox::SearchIdx->new($ibx, 1);
248 $idx->index_sync($opt);
251 sub progress_prepare ($) {
254 # public-inbox-index defaults to quiet, -xcpdb and -compact do not
255 if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
256 $opt->{quiet} = !$opt->{verbose};
259 open my $null, '>', '/dev/null' or
260 die "failed to open /dev/null: $!\n";
261 $opt->{1} = $null; # suitable for spawn() redirect
263 $opt->{verbose} ||= 1;
264 $opt->{-progress} = sub { print STDERR @_ };
268 # same unit factors as git:
269 sub parse_unsigned ($) {
272 $$val =~ /\A([0-9]+)([kmg])?\z/i or return;
273 my ($n, $unit_factor) = ($1, $2 // '');
274 my %u = ( k => 1024, m => 1024**2, g => 1024**3 );
275 $$val = $n * ($u{lc($unit_factor)} // 1);
279 sub index_prepare ($$) {
280 my ($opt, $cfg) = @_;
282 if ($opt->{compact}) {
283 require PublicInbox::Xapcmd;
284 PublicInbox::Xapcmd::check_compact();
285 $opt->{compact_opt} = { -coarse_lock => 1, compact => 1 };
286 if (defined(my $jobs = $opt->{jobs})) {
287 $opt->{compact_opt}->{jobs} = $jobs;
290 for my $k (qw(max_size batch_size)) {
291 my $git_key = "publicInbox.index".ucfirst($k);
292 $git_key =~ s/_([a-z])/\U$1/g;
293 defined(my $v = $opt->{$k} // $cfg->{lc($git_key)}) or next;
294 parse_unsigned(\$v) or die "`$git_key=$v' not parsed\n";
295 $v > 0 or die "`$git_key=$v' must be positive\n";
299 # out-of-the-box builds of Xapian 1.4.x are still limited to 32-bit
300 # https://getting-started-with-xapian.readthedocs.io/en/latest/concepts/indexing/limitations.html
301 $opt->{batch_size} and
302 $env = { XAPIAN_FLUSH_THRESHOLD => '4294967295' };
304 for my $k (qw(sequential_shard)) {
305 my $git_key = "publicInbox.index".ucfirst($k);
306 $git_key =~ s/_([a-z])/\U$1/g;
307 defined(my $s = $opt->{$k} // $cfg->{lc($git_key)}) or next;
308 defined(my $v = $cfg->git_bool($s))
309 or die "`$git_key=$s' not boolean\n";