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