]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Admin.pm
1f1b133d40b4379f27fcdc8bdc28bc845b5e6c9f
[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 use PublicInbox::Config;
13 use PublicInbox::Inbox;
14 use PublicInbox::Spawn qw(popen_rd);
15
16 sub resolve_repo_dir {
17         my ($cd, $ver) = @_;
18         my $prefix = defined $cd ? $cd : './';
19         if (-d $prefix && -f "$prefix/inbox.lock") { # v2
20                 $$ver = 2 if $ver;
21                 return abs_path($prefix);
22         }
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";
27         chomp $dir;
28         $$ver = 1 if $ver;
29         return abs_path($cd) if ($dir eq '.' && defined $cd);
30         abs_path($dir);
31 }
32
33 # for unconfigured inboxes
34 sub detect_indexlevel ($) {
35         my ($ibx) = @_;
36
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
40
41         my $l = 'basic';
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) {
45                 $l = 'full';
46                 my $m = $xdb->get_metadata('indexlevel');
47                 if ($m eq 'medium') {
48                         $l = $m;
49                 } elsif ($m ne '') {
50                         warn <<"";
51 $ibx->{inboxdir} has unexpected indexlevel in Xapian: $m
52
53                 }
54         }
55         $l;
56 }
57
58 sub unconfigured_ibx ($$) {
59         my ($dir, $i) = @_;
60         my $name = "unconfigured-$i";
61         PublicInbox::Inbox->new({
62                 name => $name,
63                 address => [ "$name\@example.com" ],
64                 inboxdir => $dir,
65                 # TODO: consumers may want to warn on this:
66                 #-unconfigured => 1,
67         });
68 }
69
70 sub resolve_inboxes ($;$$) {
71         my ($argv, $opt, $cfg) = @_;
72         $opt ||= {};
73
74         $cfg //= eval { PublicInbox::Config->new };
75         if ($opt->{all}) {
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";
79         }
80
81         my $min_ver = $opt->{-min_inbox_version} || 0;
82         my (@old, @ibxs);
83         my %dir2ibx;
84         if ($cfg) {
85                 $cfg->each_inbox(sub {
86                         my ($ibx) = @_;
87                         $ibx->{version} ||= 1;
88                         my $path = abs_path($ibx->{inboxdir});
89                         if (defined($path)) {
90                                 $dir2ibx{$path} = $ibx;
91                         } else {
92                                 warn <<EOF;
93 W: $ibx->{name} $ibx->{inboxdir}: $!
94 EOF
95                         }
96                 });
97         }
98         if ($opt->{all}) {
99                 my @all = values %dir2ibx;
100                 @all = grep { $_->{version} >= $min_ver } @all;
101                 push @ibxs, @all;
102         } else { # directories specified on the command-line
103                 my $i = 0;
104                 my @dirs = @$argv;
105                 push @dirs, '.' unless @dirs;
106                 foreach (@dirs) {
107                         my $v;
108                         my $dir = resolve_repo_dir($_, \$v);
109                         if ($v < $min_ver) {
110                                 push @old, $dir;
111                                 next;
112                         }
113                         my $ibx = $dir2ibx{$dir} ||= unconfigured_ibx($dir, $i);
114                         $i++;
115                         push @ibxs, $ibx;
116                 }
117         }
118         if (@old) {
119                 die "inboxes $min_ver inboxes not supported by $0\n\t",
120                     join("\n\t", @old), "\n";
121         }
122         @ibxs;
123 }
124
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);
128 my %mod_groups = (
129         -index => [ @base_mod, @over_mod ],
130         -base => \@base_mod,
131         -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
132 );
133
134 sub scan_ibx_modules ($$) {
135         my ($mods, $ibx) = @_;
136         if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
137                 $mods->{'Search::Xapian'} = 1;
138         } else {
139                 $mods->{$_} = 1 foreach @over_mod;
140         }
141 }
142
143 sub check_require {
144         my (@mods) = @_;
145         my $err = {};
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'} = $@;
153                 } else {
154                         eval "require $mod";
155                         $err->{$mod} = $@ if $@;
156                 }
157         }
158         scalar keys %$err ? $err : undef;
159 }
160
161 sub missing_mod_msg {
162         my ($err) = @_;
163         my @mods = map { "`$_'" } sort keys %$err;
164         my $last = pop @mods;
165         @mods ? (join(', ', @mods)."' and $last") : $last
166 }
167
168 sub require_or_die {
169         my $err = check_require(@_) or return;
170         die missing_mod_msg($err)." required for $0\n";
171 }
172
173 sub indexlevel_ok_or_die ($) {
174         my ($indexlevel) = @_;
175         my $req;
176         if ($indexlevel eq 'basic') {
177                 $req = '-index';
178         } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
179                 $req = '-search';
180         } else {
181                 die <<"";
182 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
183
184         }
185         my $err = check_require($req) or return;
186         die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
187 }
188
189 sub index_inbox {
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});
197                 };
198                 if (defined $jobs) {
199                         if ($jobs == 0) {
200                                 $v2w->{parallel} = 0;
201                         } else {
202                                 my $n = $v2w->{shards};
203                                 if ($jobs != ($n + 1) && !$opt->{reshard}) {
204                                         warn
205 "Unable to respect --jobs=$jobs, inbox was created with $n shards\n";
206                                 }
207                         }
208                 }
209                 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
210                 local $SIG{__WARN__} = sub {
211                         $warn_cb->($v2w->{current_info}, ': ', @_);
212                 };
213                 $v2w->index_sync($opt);
214         } else {
215                 require PublicInbox::SearchIdx;
216                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
217                 $s->index_sync($opt);
218         }
219 }
220
221 sub progress_prepare ($) {
222         my ($opt) = @_;
223
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};
227         }
228         if ($opt->{quiet}) {
229                 open my $null, '>', '/dev/null' or
230                         die "failed to open /dev/null: $!\n";
231                 $opt->{1} = $null; # suitable for spawn() redirect
232         } else {
233                 $opt->{verbose} ||= 1;
234                 $opt->{-progress} = sub { print STDERR @_ };
235         }
236 }
237
238 1;