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