]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
script/*: fold $usage into $help, support `-h' instead of -?
[public-inbox.git] / script / public-inbox-init
1 #!perl -w
2 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
7 use Fcntl qw(:DEFAULT);
8 my $help = <<EOF; # the following should fit w/o scrolling in 80x24 term:
9 usage: public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]
10
11   Initialize a public-inbox
12
13 required arguments:
14
15   NAME                the name of the inbox
16   INBOX_DIR           pathname the inbox
17   HTTP_URL            HTTP (or HTTPS) URL
18   ADDRESS             email address(es), may be specified multiple times
19
20 options:
21
22   -V2                 use scalable public-inbox-v2-format(5)
23   -L LEVEL            index level `basic', `medium', or `full' (default: full)
24   --ng NEWSGROUP      set NNTP newsgroup name
25   --skip-artnum=NUM   NNTP article numbers to skip
26   --skip-epoch=NUM    epochs to skip (-V2 only)
27   -J JOBS             number of indexing jobs (-V2 only), (default: 4)
28
29 See public-inbox-init(1) man page for full documentation.
30 EOF
31
32 require PublicInbox::Admin;
33 PublicInbox::Admin::require_or_die('-base');
34
35 my ($version, $indexlevel, $skip_epoch, $skip_artnum, $jobs, $show_help);
36 my $skip_docdata;
37 my $ng = '';
38 my %opts = (
39         'V|version=i' => \$version,
40         'L|index-level|indexlevel=s' => \$indexlevel,
41         'S|skip|skip-epoch=i' => \$skip_epoch,
42         'skip-artnum=i' => \$skip_artnum,
43         'j|jobs=i' => \$jobs,
44         'ng|newsgroup=s' => \$ng,
45         'skip-docdata' => \$skip_docdata,
46         'help|h' => \$show_help,
47 );
48 my $usage_cb = sub {
49         print STDERR $help;
50         exit 1;
51 };
52 GetOptions(%opts) or $usage_cb->();
53 if ($show_help) { print $help; exit 0 };
54 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
55 my $name = shift @ARGV or $usage_cb->();
56 my $inboxdir = shift @ARGV or $usage_cb->();
57 my $http_url = shift @ARGV or $usage_cb->();
58 my (@address) = @ARGV;
59 @address or $usage_cb->();
60
61 $ng =~ m![^A-Za-z0-9/_\.\-\~\@\+\=:]! and
62         die "--newsgroup `$ng' is not valid\n";
63 ($ng =~ m!\A\.! || $ng =~ m!\.\z!) and
64         die "--newsgroup `$ng' must not start or end with `.'\n";
65
66 require PublicInbox::Config;
67 my $pi_config = PublicInbox::Config->default_file;
68 require File::Basename;
69 my $dir = File::Basename::dirname($pi_config);
70 require File::Path;
71 File::Path::mkpath($dir); # will croak on fatal errors
72
73 # first, we grab a flock to prevent simultaneous public-inbox-init
74 # processes from trampling over each other, or exiting with 255 on
75 # O_EXCL failure below.  This gets unlocked automatically on exit:
76 require PublicInbox::Lock;
77 my $lock_obj = { lock_path => "$pi_config.flock" };
78 PublicInbox::Lock::lock_acquire($lock_obj);
79
80 # git-config will operate on this (and rename on success):
81 require File::Temp;
82 my $fh = File::Temp->new(TEMPLATE => 'pi-init-XXXXXXXX', DIR => $dir);
83
84 # Now, we grab another lock to use git-config(1) locking, so it won't
85 # wait on the lock, unlike some of our internal flock()-based locks.
86 # This is to prevent direct git-config(1) usage from clobbering our
87 # changes.
88 my $lockfile = "$pi_config.lock";
89 my $lockfh;
90 sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do {
91         warn "could not open config file: $lockfile: $!\n";
92         exit(255);
93 };
94 my $auto_unlink = UnlinkMe->new($lockfile);
95 my ($perm, %seen);
96 if (-e $pi_config) {
97         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
98         my @st = stat($oh);
99         $perm = $st[2];
100         defined $perm or die "(f)stat failed on $pi_config: $!\n";
101         chmod($perm & 07777, $fh) or
102                 die "(f)chmod failed on future $pi_config: $!\n";
103         my $old;
104         {
105                 local $/;
106                 $old = <$oh>;
107         }
108         print $fh $old or die "failed to write: $!\n";
109         close $oh or die "failed to close $pi_config: $!\n";
110
111         # yes, this conflict checking is racy if multiple instances of this
112         # script are run by the same $PI_DIR
113         my $cfg = PublicInbox::Config->new;
114         my $conflict;
115         foreach my $addr (@address) {
116                 my $found = $cfg->lookup($addr);
117                 if ($found) {
118                         if ($found->{name} ne $name) {
119                                 print STDERR
120                                         "`$addr' already defined for ",
121                                         "`$found->{name}',\n",
122                                         "does not match intend `$name'\n";
123                                 $conflict = 1;
124                         } else {
125                                 $seen{lc($addr)} = 1;
126                         }
127                 }
128         }
129
130         exit(1) if $conflict;
131
132         my $ibx = $cfg->lookup_name($name);
133         $indexlevel //= $ibx->{indexlevel} if $ibx;
134 }
135 my $pi_config_tmp = $fh->filename;
136 close($fh) or die "failed to close $pi_config_tmp: $!\n";
137
138 my $pfx = "publicinbox.$name";
139 my @x = (qw/git config/, "--file=$pi_config_tmp");
140
141 require Cwd;
142 $inboxdir = Cwd::abs_path($inboxdir);
143 die "`\\n' not allowed in `$inboxdir'\n" if $inboxdir =~ /\n/s;
144 if (-f "$inboxdir/inbox.lock") {
145         if (!defined $version) {
146                 $version = 2;
147         } elsif ($version != 2) {
148                 die "$inboxdir is a -V2 inbox, -V$version specified\n"
149         }
150 } elsif (-d "$inboxdir/objects") {
151         if (!defined $version) {
152                 $version = 1;
153         } elsif ($version != 1) {
154                 die "$inboxdir is a -V1 inbox, -V$version specified\n"
155         }
156 }
157
158 $version = 1 unless defined $version;
159
160 if ($version == 1 && defined $skip_epoch) {
161         die "--skip-epoch is only supported for -V2 inboxes\n";
162 }
163
164 my $ibx = PublicInbox::Inbox->new({
165         inboxdir => $inboxdir,
166         name => $name,
167         version => $version,
168         -primary_address => $address[0],
169         indexlevel => $indexlevel,
170 });
171
172 my $creat_opt = {};
173 if (defined $jobs) {
174         die "--jobs is only supported for -V2 inboxes\n" if $version == 1;
175         die "--jobs=$jobs must be >= 1\n" if $jobs <= 0;
176         $creat_opt->{nproc} = $jobs;
177 }
178
179 require PublicInbox::InboxWritable;
180 $ibx = PublicInbox::InboxWritable->new($ibx, $creat_opt);
181 if ($skip_docdata) {
182         $ibx->{indexlevel} //= 'full'; # ensure init_inbox writes xdb
183         $ibx->{indexlevel} eq 'basic' and
184                 die "--skip-docdata ignored with --indexlevel=basic\n";
185         $ibx->{-skip_docdata} = $skip_docdata;
186 }
187 $ibx->init_inbox(0, $skip_epoch, $skip_artnum);
188
189 # needed for git prior to v2.1.0
190 umask(0077) if defined $perm;
191
192 foreach my $addr (@address) {
193         next if $seen{lc($addr)};
194         PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]);
195 }
196 PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]);
197 PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]);
198
199 if (defined($indexlevel)) {
200         PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]);
201 }
202 PublicInbox::Import::run_die([@x, "$pfx.newsgroup", $ng]) if $ng ne '';
203
204 # needed for git prior to v2.1.0
205 if (defined $perm) {
206         chmod($perm & 07777, $pi_config_tmp) or
207                         die "(f)chmod failed on future $pi_config: $!\n";
208 }
209
210 rename $pi_config_tmp, $pi_config or
211         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";
212 $auto_unlink->DESTROY;
213
214 package UnlinkMe;
215 use strict;
216
217 sub new {
218         my ($klass, $file) = @_;
219         bless { file => $file }, $klass;
220 }
221
222 sub DESTROY {
223         my $f = delete($_[0]->{file});
224         unlink($f) if defined($f);
225 }
226 1;