1 # Copyright (C) 2019 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;
10 use base qw(Exporter);
11 our @EXPORT_OK = qw(resolve_repo_dir);
12 use PublicInbox::Config;
13 use PublicInbox::Inbox;
14 use PublicInbox::Spawn qw(popen_rd);
16 sub resolve_repo_dir {
18 my $prefix = defined $cd ? $cd : './';
19 if (-d $prefix && -f "$prefix/inbox.lock") { # v2
21 return abs_path($prefix);
23 my $cmd = [ qw(git rev-parse --git-dir) ];
24 my $fh = popen_rd($cmd, undef, {-C => $cd});
25 my $dir = do { local $/; <$fh> };
26 close $fh or die "error in ".join(' ', @$cmd)." (cwd:$cd): $!\n";
29 return abs_path($cd) if ($dir eq '.' && defined $cd);
33 # for unconfigured inboxes
34 sub detect_indexlevel ($) {
37 # brand new or never before indexed inboxes default to full
38 return 'full' unless $ibx->over;
39 delete $ibx->{over}; # don't leave open FD lying around
42 my $srch = $ibx->search or return $l;
43 delete $ibx->{search}; # don't leave open FD lying around
44 if (my $xdb = $srch->xdb) {
46 my $m = $xdb->get_metadata('indexlevel');
51 $ibx->{inboxdir} has unexpected indexlevel in Xapian: $m
58 sub unconfigured_ibx ($$) {
60 my $name = "unconfigured-$i";
61 PublicInbox::Inbox->new({
63 address => [ "$name\@example.com" ],
65 # TODO: consumers may want to warn on this:
70 sub resolve_inboxes ($;$$) {
71 my ($argv, $opt, $cfg) = @_;
74 $cfg //= eval { PublicInbox::Config->new };
76 my $cfgfile = PublicInbox::Config::default_file();
77 $cfg or die "--all specified, but $cfgfile not readable\n";
78 @$argv and die "--all specified, but directories specified\n";
81 my $min_ver = $opt->{-min_inbox_version} || 0;
85 $cfg->each_inbox(sub {
87 $ibx->{version} ||= 1;
88 my $path = abs_path($ibx->{inboxdir});
90 $dir2ibx{$path} = $ibx;
93 W: $ibx->{name} $ibx->{inboxdir}: $!
99 my @all = values %dir2ibx;
100 @all = grep { $_->{version} >= $min_ver } @all;
102 } else { # directories specified on the command-line
105 push @dirs, '.' unless @dirs;
108 my $dir = resolve_repo_dir($_, \$v);
113 my $ibx = $dir2ibx{$dir} ||= unconfigured_ibx($dir, $i);
119 die "inboxes $min_ver inboxes not supported by $0\n\t",
120 join("\n\t", @old), "\n";
125 # TODO: make Devel::Peek optional, only used for daemon
126 my @base_mod = qw(Email::MIME Devel::Peek);
127 my @over_mod = qw(DBD::SQLite DBI);
129 -index => [ @base_mod, @over_mod ],
131 -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
134 sub scan_ibx_modules ($$) {
135 my ($mods, $ibx) = @_;
136 if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
137 $mods->{'Search::Xapian'} = 1;
139 $mods->{$_} = 1 foreach @over_mod;
146 while (my $mod = shift @mods) {
147 if (my $groups = $mod_groups{$mod}) {
148 push @mods, @$groups;
149 } elsif ($mod eq 'Search::Xapian') {
150 require PublicInbox::Search;
151 PublicInbox::Search::load_xapian() or
152 $err->{'Search::Xapian || Xapian'} = $@;
155 $err->{$mod} = $@ if $@;
158 scalar keys %$err ? $err : undef;
161 sub missing_mod_msg {
163 my @mods = map { "`$_'" } sort keys %$err;
164 my $last = pop @mods;
165 @mods ? (join(', ', @mods)."' and $last") : $last
169 my $err = check_require(@_) or return;
170 die missing_mod_msg($err)." required for $0\n";
173 sub indexlevel_ok_or_die ($) {
174 my ($indexlevel) = @_;
176 if ($indexlevel eq 'basic') {
178 } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
182 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
185 my $err = check_require($req) or return;
186 die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
190 my ($ibx, $im, $opt) = @_;
191 my $jobs = delete $opt->{jobs} if $opt;
192 if (ref($ibx) && ($ibx->{version} || 1) == 2) {
193 eval { require PublicInbox::V2Writable };
194 die "v2 requirements not met: $@\n" if $@;
195 my $v2w = $im // eval { $ibx->importer(0) } || eval {
196 PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
200 $v2w->{parallel} = 0;
202 my $n = $v2w->{shards};
203 if ($jobs != ($n + 1) && !$opt->{reshard}) {
205 "Unable to respect --jobs=$jobs, inbox was created with $n shards\n";
209 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
210 local $SIG{__WARN__} = sub {
211 $warn_cb->($v2w->{current_info}, ': ', @_);
213 $v2w->index_sync($opt);
215 require PublicInbox::SearchIdx;
216 my $s = PublicInbox::SearchIdx->new($ibx, 1);
217 $s->index_sync($opt);
221 sub progress_prepare ($) {
224 # public-inbox-index defaults to quiet, -xcpdb and -compact do not
225 if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
226 $opt->{quiet} = !$opt->{verbose};
229 open my $null, '>', '/dev/null' or
230 die "failed to open /dev/null: $!\n";
231 $opt->{1} = $null; # suitable for spawn() redirect
233 $opt->{verbose} ||= 1;
234 $opt->{-progress} = sub { print STDERR @_ };