]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Admin.pm
inbox: add ->version method
[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                         my $path = abs_path($ibx->{inboxdir});
88                         if (defined($path)) {
89                                 $dir2ibx{$path} = $ibx;
90                         } else {
91                                 warn <<EOF;
92 W: $ibx->{name} $ibx->{inboxdir}: $!
93 EOF
94                         }
95                 });
96         }
97         if ($opt->{all}) {
98                 my @all = values %dir2ibx;
99                 @all = grep { $_->version >= $min_ver } @all;
100                 push @ibxs, @all;
101         } else { # directories specified on the command-line
102                 my $i = 0;
103                 my @dirs = @$argv;
104                 push @dirs, '.' unless @dirs;
105                 foreach (@dirs) {
106                         my $v;
107                         my $dir = resolve_repo_dir($_, \$v);
108                         if ($v < $min_ver) {
109                                 push @old, $dir;
110                                 next;
111                         }
112                         my $ibx = $dir2ibx{$dir} ||= unconfigured_ibx($dir, $i);
113                         $i++;
114                         push @ibxs, $ibx;
115                 }
116         }
117         if (@old) {
118                 die "inboxes $min_ver inboxes not supported by $0\n\t",
119                     join("\n\t", @old), "\n";
120         }
121         @ibxs;
122 }
123
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);
127 my %mod_groups = (
128         -index => [ @base_mod, @over_mod ],
129         -base => \@base_mod,
130         -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
131 );
132
133 sub scan_ibx_modules ($$) {
134         my ($mods, $ibx) = @_;
135         if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
136                 $mods->{'Search::Xapian'} = 1;
137         } else {
138                 $mods->{$_} = 1 foreach @over_mod;
139         }
140 }
141
142 sub check_require {
143         my (@mods) = @_;
144         my $err = {};
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'} = $@;
152                 } else {
153                         eval "require $mod";
154                         $err->{$mod} = $@ if $@;
155                 }
156         }
157         scalar keys %$err ? $err : undef;
158 }
159
160 sub missing_mod_msg {
161         my ($err) = @_;
162         my @mods = map { "`$_'" } sort keys %$err;
163         my $last = pop @mods;
164         @mods ? (join(', ', @mods)."' and $last") : $last
165 }
166
167 sub require_or_die {
168         my $err = check_require(@_) or return;
169         die missing_mod_msg($err)." required for $0\n";
170 }
171
172 sub indexlevel_ok_or_die ($) {
173         my ($indexlevel) = @_;
174         my $req;
175         if ($indexlevel eq 'basic') {
176                 $req = '-index';
177         } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
178                 $req = '-search';
179         } else {
180                 die <<"";
181 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
182
183         }
184         my $err = check_require($req) or return;
185         die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
186 }
187
188 sub index_inbox {
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});
196                 };
197                 if (defined $jobs) {
198                         if ($jobs == 0) {
199                                 $v2w->{parallel} = 0;
200                         } else {
201                                 my $n = $v2w->{shards};
202                                 if ($jobs != ($n + 1) && !$opt->{reshard}) {
203                                         warn
204 "Unable to respect --jobs=$jobs, inbox was created with $n shards\n";
205                                 }
206                         }
207                 }
208                 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
209                 local $SIG{__WARN__} = sub {
210                         $warn_cb->($v2w->{current_info}, ': ', @_);
211                 };
212                 $v2w->index_sync($opt);
213         } else {
214                 require PublicInbox::SearchIdx;
215                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
216                 $s->index_sync($opt);
217         }
218 }
219
220 sub progress_prepare ($) {
221         my ($opt) = @_;
222
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};
226         }
227         if ($opt->{quiet}) {
228                 open my $null, '>', '/dev/null' or
229                         die "failed to open /dev/null: $!\n";
230                 $opt->{1} = $null; # suitable for spawn() redirect
231         } else {
232                 $opt->{verbose} ||= 1;
233                 $opt->{-progress} = sub { print STDERR @_ };
234         }
235 }
236
237 1;