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 my $CFG; # all the admin stuff is a singleton
13 require PublicInbox::Config;
15 sub resolve_repo_dir {
17 my $prefix = defined $cd ? $cd : './';
18 if (-d $prefix && -f "$prefix/inbox.lock") { # v2
20 return abs_path($prefix);
23 my @cmd = qw(git rev-parse --git-dir);
24 my $cmd = join(' ', @cmd);
25 my $pid = open my $fh, '-|';
26 defined $pid or die "forking $cmd failed: $!\n";
29 chdir $cd or die "chdir $cd failed: $!\n";
32 die "Failed to exec $cmd: $!\n";
38 close $fh or die "error in $cmd: $!\n";
41 return abs_path($cd) if ($dir eq '.' && defined $cd);
46 # for unconfigured inboxes
47 sub detect_indexlevel ($) {
50 # brand new or never before indexed inboxes default to full
51 return 'full' unless $ibx->over;
52 delete $ibx->{over}; # don't leave open FD lying around
55 my $srch = $ibx->search or return $l;
56 delete $ibx->{search}; # don't leave open FD lying around
57 if (my $xdb = $srch->xdb) {
59 my $m = $xdb->get_metadata('indexlevel');
64 $ibx->{mainrepo} has unexpected indexlevel in Xapian: $m
71 sub unconfigured_ibx ($$) {
73 my $name = "unconfigured-$i";
74 PublicInbox::Inbox->new({
76 address => [ "$name\@example.com" ],
78 # TODO: consumers may want to warn on this:
83 sub config () { $CFG //= eval { PublicInbox::Config->new } }
85 sub resolve_inboxes ($;$) {
86 my ($argv, $opt) = @_;
87 require PublicInbox::Inbox;
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;
101 $cfg->each_inbox(sub {
103 $ibx->{version} ||= 1;
104 $dir2ibx{abs_path($ibx->{mainrepo})} = $ibx;
108 my @all = values %dir2ibx;
109 @all = grep { $_->{version} >= $min_ver } @all;
111 } else { # directories specified on the command-line
114 push @dirs, '.' unless @dirs;
117 my $dir = resolve_repo_dir($_, \$v);
122 my $ibx = $dir2ibx{$dir} ||= unconfigured_ibx($dir, $i);
128 die "inboxes $min_ver inboxes not supported by $0\n\t",
129 join("\n\t", @old), "\n";
134 # TODO: make Devel::Peek optional, only used for daemon
135 my @base_mod = qw(Email::MIME Date::Parse Devel::Peek);
136 my @over_mod = qw(DBD::SQLite DBI);
138 -index => [ @base_mod, @over_mod ],
140 -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
143 sub scan_ibx_modules ($$) {
144 my ($mods, $ibx) = @_;
145 if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
146 $mods->{'Search::Xapian'} = 1;
148 $mods->{$_} = 1 foreach @over_mod;
155 while (my $mod = shift @mods) {
156 if (my $groups = $mod_groups{$mod}) {
157 push @mods, @$groups;
160 $err->{$mod} = $@ if $@;
163 scalar keys %$err ? $err : undef;
166 sub missing_mod_msg {
168 my @mods = map { "`$_'" } sort keys %$err;
169 my $last = pop @mods;
170 @mods ? (join(', ', @mods)."' and $last") : $last
174 my $err = check_require(@_) or return;
175 die missing_mod_msg($err)." required for $0\n";
178 sub indexlevel_ok_or_die ($) {
179 my ($indexlevel) = @_;
181 if ($indexlevel eq 'basic') {
183 } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
187 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
190 my $err = check_require($req) or return;
191 die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
195 my ($ibx, $opt) = @_;
196 my $jobs = delete $opt->{jobs} if $opt;
197 if (ref($ibx) && ($ibx->{version} || 1) == 2) {
198 eval { require PublicInbox::V2Writable };
199 die "v2 requirements not met: $@\n" if $@;
200 my $v2w = eval { $ibx->importer(0) } || eval {
201 PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
205 $v2w->{parallel} = 0;
207 my $n = $v2w->{shards};
208 if ($jobs != ($n + 1) && !$opt->{reshard}) {
210 "Unable to respect --jobs=$jobs, inbox was created with $n shards\n";
214 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
215 local $SIG{__WARN__} = sub {
216 $warn_cb->($v2w->{current_info}, ': ', @_);
218 $v2w->index_sync($opt);
220 require PublicInbox::SearchIdx;
221 my $s = PublicInbox::SearchIdx->new($ibx, 1);
222 $s->index_sync($opt);
226 sub progress_prepare ($) {
229 # public-inbox-index defaults to quiet, -xcpdb and -compact do not
230 if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
231 $opt->{quiet} = !$opt->{verbose};
234 open my $null, '>', '/dev/null' or
235 die "failed to open /dev/null: $!\n";
236 $opt->{1} = fileno($null); # suitable for spawn() redirect
237 $opt->{-dev_null} = $null;
239 $opt->{verbose} ||= 1;
240 $opt->{-progress} = sub { print STDERR @_ };