]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Admin.pm
admin: remove warning arg for unconfigured inboxes
[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 resolve_inboxes {
70         my ($argv) = @_;
71         require PublicInbox::Config;
72         require PublicInbox::Inbox;
73
74         my @ibxs = map { resolve_repo_dir($_) } @$argv;
75         push(@ibxs, resolve_repo_dir()) unless @ibxs;
76
77         my %dir2ibx;
78         if (my $config = eval { PublicInbox::Config->new }) {
79                 $config->each_inbox(sub {
80                         my ($ibx) = @_;
81                         $dir2ibx{abs_path($ibx->{mainrepo})} = $ibx;
82                 });
83         }
84         for my $i (0..$#ibxs) {
85                 my $dir = $ibxs[$i];
86                 $ibxs[$i] = $dir2ibx{$dir} ||= do {
87                         my $name = "unconfigured-$i";
88                         PublicInbox::Inbox->new({
89                                 name => $name,
90                                 address => [ "$name\@example.com" ],
91                                 mainrepo => $dir,
92                                 # TODO: consumers may want to warn on this:
93                                 #-unconfigured => 1,
94                         });
95                 };
96         }
97         @ibxs;
98 }
99
100 # TODO: make Devel::Peek optional, only used for daemon
101 my @base_mod = qw(Email::MIME Date::Parse Devel::Peek);
102 my @over_mod = qw(DBD::SQLite DBI);
103 my %mod_groups = (
104         -index => [ @base_mod, @over_mod ],
105         -base => \@base_mod,
106         -search => [ @base_mod, @over_mod, 'Search::Xapian' ],
107 );
108
109 sub scan_ibx_modules ($$) {
110         my ($mods, $ibx) = @_;
111         if (!$ibx->{indexlevel} || $ibx->{indexlevel} ne 'basic') {
112                 $mods->{'Search::Xapian'} = 1;
113         } else {
114                 $mods->{$_} = 1 foreach @over_mod;
115         }
116 }
117
118 sub check_require {
119         my (@mods) = @_;
120         my $err = {};
121         while (my $mod = shift @mods) {
122                 if (my $groups = $mod_groups{$mod}) {
123                         push @mods, @$groups;
124                 } else {
125                         eval "require $mod";
126                         $err->{$mod} = $@ if $@;
127                 }
128         }
129         scalar keys %$err ? $err : undef;
130 }
131
132 sub missing_mod_msg {
133         my ($err) = @_;
134         my @mods = map { "`$_'" } sort keys %$err;
135         my $last = pop @mods;
136         @mods ? (join(', ', @mods)."' and $last") : $last
137 }
138
139 sub require_or_die {
140         my $err = check_require(@_) or return;
141         die missing_mod_msg($err)." required for $0\n";
142 }
143
144 sub indexlevel_ok_or_die ($) {
145         my ($indexlevel) = @_;
146         my $req;
147         if ($indexlevel eq 'basic') {
148                 $req = '-index';
149         } elsif ($indexlevel =~ /\A(?:medium|full)\z/) {
150                 $req = '-search';
151         } else {
152                 die <<"";
153 invalid indexlevel=$indexlevel (must be `basic', `medium', or `full')
154
155         }
156         my $err = check_require($req) or return;
157         die missing_mod_msg($err) ." required for indexlevel=$indexlevel\n";
158 }
159
160 sub index_inbox {
161         my ($ibx, $opt) = @_;
162         my $jobs = delete $opt->{jobs} if $opt;
163         if (ref($ibx) && ($ibx->{version} || 1) == 2) {
164                 eval { require PublicInbox::V2Writable };
165                 die "v2 requirements not met: $@\n" if $@;
166                 my $v2w = eval { $ibx->importer(0) } || eval {
167                         PublicInbox::V2Writable->new($ibx, {nproc=>$jobs});
168                 };
169                 if (defined $jobs) {
170                         if ($jobs == 0) {
171                                 $v2w->{parallel} = 0;
172                         } else {
173                                 my $n = $v2w->{partitions};
174                                 if ($jobs != ($n + 1)) {
175                                         warn
176 "Unable to respect --jobs=$jobs, inbox was created with $n partitions\n";
177                                 }
178                         }
179                 }
180                 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
181                 local $SIG{__WARN__} = sub {
182                         $warn_cb->($v2w->{current_info}, ': ', @_);
183                 };
184                 $v2w->index_sync($opt);
185         } else {
186                 require PublicInbox::SearchIdx;
187                 my $s = PublicInbox::SearchIdx->new($ibx, 1);
188                 $s->index_sync($opt);
189         }
190 }
191
192 sub progress_prepare ($) {
193         my ($opt) = @_;
194
195         # public-inbox-index defaults to quiet, -xcpdb and -compact do not
196         if (defined($opt->{quiet}) && $opt->{quiet} < 0) {
197                 $opt->{quiet} = !$opt->{verbose};
198         }
199         if ($opt->{quiet}) {
200                 open my $null, '>', '/dev/null' or
201                         die "failed to open /dev/null: $!\n";
202                 $opt->{1} = fileno($null); # suitable for spawn() redirect
203                 $opt->{-dev_null} = $null;
204         } else {
205                 $opt->{verbose} ||= 1;
206                 $opt->{-progress} = sub { print STDERR @_ };
207         }
208 }
209
210 1;