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;
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 //= 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 my $path = abs_path($ibx->{inboxdir});
89 $dir2ibx{$path} = $ibx;
92 W: $ibx->{name} $ibx->{inboxdir}: $!
98 my @all = values %dir2ibx;
99 @all = grep { $_->version >= $min_ver } @all;
101 } else { # directories specified on the command-line
104 push @dirs, '.' unless @dirs;
107 my $dir = resolve_repo_dir($_, \$v);
112 my $ibx = $dir2ibx{$dir} ||= unconfigured_ibx($dir, $i);
118 die "inboxes $min_ver inboxes not supported by $0\n\t",
119 join("\n\t", @old), "\n";
124 # TODO: make Devel::Peek optional, only used for daemon
125 my @base_mod = qw(Email::MIME Devel::Peek);
126 my @over_mod = qw(DBD::SQLite DBI);
128 -index => [ @base_mod, @over_mod ],
130 -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
133 sub scan_ibx_modules ($$) {
134 my ($mods, $ibx) = @_;
135 if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
136 $mods->{'Search::Xapian'} = 1;
138 $mods->{$_} = 1 foreach @over_mod;
145 while (my $mod = shift @mods) {
146 if (my $groups = $mod_groups{$mod}) {
147 push @mods, @$groups;
148 } elsif ($mod eq 'Search::Xapian') {
149 require PublicInbox::Search;
150 PublicInbox::Search::load_xapian() or
151 $err->{'Search::Xapian || Xapian'} = $@;
154 $err->{$mod} = $@ if $@;
157 scalar keys %$err ? $err : undef;
160 sub missing_mod_msg {
162 my @mods = map { "`$_'" } sort keys %$err;
163 my $last = pop @mods;
164 @mods ? (join(', ', @mods)."' and $last") : $last
168 my $err = check_require(@_) or return;
169 die missing_mod_msg($err)." required for $0\n";
172 sub indexlevel_ok_or_die ($) {
173 my ($indexlevel) = @_;
175 if ($indexlevel eq 'basic') {
177 } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
181 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
184 my $err = check_require($req) or return;
185 die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
189 my ($ibx, $im, $opt) = @_;
190 my $jobs = delete $opt->{jobs} if $opt;
191 if (ref($ibx) && $ibx->version == 2) {
192 eval { require PublicInbox::V2Writable };
193 die "v2 requirements not met: $@\n" if $@;
194 my $v2w = $im // eval { $ibx->importer(0) } || eval {
195 PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
199 $v2w->{parallel} = 0;
201 my $n = $v2w->{shards};
202 if ($jobs != ($n + 1) && !$opt->{reshard}) {
204 "Unable to respect --jobs=$jobs, inbox was created with $n shards\n";
208 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
209 local $SIG{__WARN__} = sub {
210 $warn_cb->($v2w->{current_info}, ': ', @_);
212 $v2w->index_sync($opt);
214 require PublicInbox::SearchIdx;
215 my $s = PublicInbox::SearchIdx->new($ibx, 1);
216 $s->index_sync($opt);
220 sub progress_prepare ($) {
223 # public-inbox-index defaults to quiet, -xcpdb and -compact do not
224 if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
225 $opt->{quiet} = !$opt->{verbose};
228 open my $null, '>', '/dev/null' or
229 die "failed to open /dev/null: $!\n";
230 $opt->{1} = $null; # suitable for spawn() redirect
232 $opt->{verbose} ||= 1;
233 $opt->{-progress} = sub { print STDERR @_ };
237 # same unit factors as git:
238 sub parse_unsigned ($) {
241 $$max_size =~ /\A([0-9]+)([kmg])?\z/i or return;
242 my ($n, $unit_factor) = ($1, $2 // '');
243 my %u = ( k => 1024, m => 1024**2, g => 1024**3 );
244 $$max_size = $n * ($u{lc($unit_factor)} // 1);