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