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