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