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