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