]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Admin.pm
admin: show failing directory
[public-inbox.git] / lib / PublicInbox / Admin.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # common stuff for administrative command-line tools
5 # Unstable internal API
6 package PublicInbox::Admin;
7 use strict;
8 use warnings;
9 use Cwd 'abs_path';
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;
14
15 sub resolve_repo_dir {
16         my ($cd, $ver) = @_;
17         my $prefix = defined $cd ? $cd : './';
18         if (-d $prefix && -f "$prefix/inbox.lock") { # v2
19                 $$ver = 2 if $ver;
20                 return abs_path($prefix);
21         }
22
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";
27         if ($pid == 0) {
28                 if (defined $cd) {
29                         chdir $cd or die "chdir $cd failed: $!\n";
30                 }
31                 exec @cmd;
32                 die "Failed to exec $cmd: $!\n";
33         } else {
34                 my $dir = eval {
35                         local $/;
36                         <$fh>;
37                 };
38                 close $fh or die "error in $cmd (cwd:$cd): $!\n";
39                 chomp $dir;
40                 $$ver = 1 if $ver;
41                 return abs_path($cd) if ($dir eq '.' && defined $cd);
42                 abs_path($dir);
43         }
44 }
45
46 # for unconfigured inboxes
47 sub detect_indexlevel ($) {
48         my ($ibx) = @_;
49
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
53
54         my $l = 'basic';
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) {
58                 $l = 'full';
59                 my $m = $xdb->get_metadata('indexlevel');
60                 if ($m eq 'medium') {
61                         $l = $m;
62                 } elsif ($m ne '') {
63                         warn <<"";
64 $ibx->{mainrepo} has unexpected indexlevel in Xapian: $m
65
66                 }
67         }
68         $l;
69 }
70
71 sub unconfigured_ibx ($$) {
72         my ($dir, $i) = @_;
73         my $name = "unconfigured-$i";
74         PublicInbox::Inbox->new({
75                 name => $name,
76                 address => [ "$name\@example.com" ],
77                 mainrepo => $dir,
78                 # TODO: consumers may want to warn on this:
79                 #-unconfigured => 1,
80         });
81 }
82
83 sub config () { $CFG //= eval { PublicInbox::Config->new } }
84
85 sub resolve_inboxes ($;$) {
86         my ($argv, $opt) = @_;
87         require PublicInbox::Inbox;
88         $opt ||= {};
89
90         my $cfg = config();
91         if ($opt->{all}) {
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";
95         }
96
97         my $min_ver = $opt->{-min_inbox_version} || 0;
98         my (@old, @ibxs);
99         my %dir2ibx;
100         if ($cfg) {
101                 $cfg->each_inbox(sub {
102                         my ($ibx) = @_;
103                         $ibx->{version} ||= 1;
104                         my $path = abs_path($ibx->{mainrepo});
105                         if (defined($path)) {
106                                 $dir2ibx{$path} = $ibx;
107                         } else {
108                                 warn <<EOF;
109 W: $ibx->{name} $ibx->{mainrepo}: $!
110 EOF
111                         }
112                 });
113         }
114         if ($opt->{all}) {
115                 my @all = values %dir2ibx;
116                 @all = grep { $_->{version} >= $min_ver } @all;
117                 push @ibxs, @all;
118         } else { # directories specified on the command-line
119                 my $i = 0;
120                 my @dirs = @$argv;
121                 push @dirs, '.' unless @dirs;
122                 foreach (@dirs) {
123                         my $v;
124                         my $dir = resolve_repo_dir($_, \$v);
125                         if ($v < $min_ver) {
126                                 push @old, $dir;
127                                 next;
128                         }
129                         my $ibx = $dir2ibx{$dir} ||= unconfigured_ibx($dir, $i);
130                         $i++;
131                         push @ibxs, $ibx;
132                 }
133         }
134         if (@old) {
135                 die "inboxes $min_ver inboxes not supported by $0\n\t",
136                     join("\n\t", @old), "\n";
137         }
138         @ibxs;
139 }
140
141 # TODO: make Devel::Peek optional, only used for daemon
142 my @base_mod = qw(Email::MIME Date::Parse Devel::Peek);
143 my @over_mod = qw(DBD::SQLite DBI);
144 my %mod_groups = (
145         -index => [ @base_mod, @over_mod ],
146         -base => \@base_mod,
147         -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
148 );
149
150 sub scan_ibx_modules ($$) {
151         my ($mods, $ibx) = @_;
152         if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
153                 $mods->{'Search::Xapian'} = 1;
154         } else {
155                 $mods->{$_} = 1 foreach @over_mod;
156         }
157 }
158
159 sub check_require {
160         my (@mods) = @_;
161         my $err = {};
162         while (my $mod = shift @mods) {
163                 if (my $groups = $mod_groups{$mod}) {
164                         push @mods, @$groups;
165                 } else {
166                         eval "require $mod";
167                         $err->{$mod} = $@ if $@;
168                 }
169         }
170         scalar keys %$err ? $err : undef;
171 }
172
173 sub missing_mod_msg {
174         my ($err) = @_;
175         my @mods = map { "`$_'" } sort keys %$err;
176         my $last = pop @mods;
177         @mods ? (join(', ', @mods)."' and $last") : $last
178 }
179
180 sub require_or_die {
181         my $err = check_require(@_) or return;
182         die missing_mod_msg($err)." required for $0\n";
183 }
184
185 sub indexlevel_ok_or_die ($) {
186         my ($indexlevel) = @_;
187         my $req;
188         if ($indexlevel eq 'basic') {
189                 $req = '-index';
190         } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
191                 $req = '-search';
192         } else {
193                 die <<"";
194 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
195
196         }
197         my $err = check_require($req) or return;
198         die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
199 }
200
201 sub index_inbox {
202         my ($ibx, $opt) = @_;
203         my $jobs = delete $opt->{jobs} if $opt;
204         if (ref($ibx) && ($ibx->{version} || 1) == 2) {
205                 eval { require PublicInbox::V2Writable };
206                 die "v2 requirements not met: $@\n" if $@;
207                 my $v2w = eval { $ibx->importer(0) } || eval {
208                         PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
209                 };
210                 if (defined $jobs) {
211                         if ($jobs == 0) {
212                                 $v2w->{parallel} = 0;
213                         } else {
214                                 my $n = $v2w->{shards};
215                                 if ($jobs != ($n + 1) && !$opt->{reshard}) {
216                                         warn
217 "Unable to respect --jobs=$jobs, inbox was created with $n shards\n";
218                                 }
219                         }
220                 }
221                 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
222                 local $SIG{__WARN__} = sub {
223                         $warn_cb->($v2w->{current_info}, ': ', @_);
224                 };
225                 $v2w->index_sync($opt);
226         } else {
227                 require PublicInbox::SearchIdx;
228                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
229                 $s->index_sync($opt);
230         }
231 }
232
233 sub progress_prepare ($) {
234         my ($opt) = @_;
235
236         # public-inbox-index defaults to quiet, -xcpdb and -compact do not
237         if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
238                 $opt->{quiet} = !$opt->{verbose};
239         }
240         if ($opt->{quiet}) {
241                 open my $null, '>', '/dev/null' or
242                         die "failed to open /dev/null: $!\n";
243                 $opt->{1} = fileno($null); # suitable for spawn() redirect
244                 $opt->{-dev_null} = $null;
245         } else {
246                 $opt->{verbose} ||= 1;
247                 $opt->{-progress} = sub { print STDERR @_ };
248         }
249 }
250
251 1;