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