]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
admin: improve warnings and errors for missing modules
[public-inbox.git] / script / public-inbox-init
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014-2018 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 REPO_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::Inbox;
14 use File::Temp qw/tempfile/;
15 use File::Basename qw/dirname/;
16 use File::Path qw/mkpath/;
17 use Cwd qw/abs_path/;
18
19 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
20 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
21 my $version = undef;
22 my $indexlevel = undef;
23 my $skip;
24 my %opts = ( 'V|version=i' => \$version,
25              'L|indexlevel=s' => \$indexlevel,
26              'S|skip=i' => \$skip,
27 );
28 GetOptions(%opts) or usage();
29 PublicInbox::Admin::indexlevel_ok_or_die($indexlevel) if defined $indexlevel;
30 my $name = shift @ARGV or usage();
31 my $mainrepo = shift @ARGV or usage();
32 my $http_url = shift @ARGV or usage();
33 my (@address) = @ARGV;
34 @address or usage();
35 my %seen;
36
37 my $pi_config = PublicInbox::Config->default_file;
38 my $dir = dirname($pi_config);
39 mkpath($dir); # will croak on fatal errors
40 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
41 if (-e $pi_config) {
42         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
43         my @st = stat($oh);
44         my $perm = $st[2];
45         defined $perm or die "(f)stat failed on $pi_config: $!\n";
46         chmod($perm & 07777, $fh) or
47                 die "(f)chmod failed on future $pi_config: $!\n";
48         my $old;
49         {
50                 local $/;
51                 $old = <$oh>;
52         }
53         print $fh $old or die "failed to write: $!\n";
54         close $oh or die "failed to close $pi_config: $!\n";
55
56         # yes, this conflict checking is racy if multiple instances of this
57         # script are run by the same $PI_DIR
58         my $cfg = PublicInbox::Config->new;
59         my $conflict;
60         foreach my $addr (@address) {
61                 my $found = $cfg->lookup($addr);
62                 if ($found) {
63                         if ($found->{name} ne $name) {
64                                 print STDERR
65                                         "`$addr' already defined for ",
66                                         "`$found->{name}',\n",
67                                         "does not match intend `$name'\n";
68                                 $conflict = 1;
69                         } else {
70                                 $seen{lc($addr)} = 1;
71                         }
72                 }
73         }
74
75         exit(1) if $conflict;
76
77         my $ibx = $cfg->lookup_name($name);
78         if ($ibx) {
79                 if (!defined($indexlevel) && $ibx->{indexlevel}) {
80                         $indexlevel = $ibx->{indexlevel};
81                 }
82         }
83 }
84 close $fh or die "failed to close $pi_config_tmp: $!\n";
85
86 my $pfx = "publicinbox.$name";
87 my @x = (qw/git config/, "--file=$pi_config_tmp");
88
89 $mainrepo = abs_path($mainrepo);
90 if (-f "$mainrepo/inbox.lock") {
91         if (!defined $version) {
92                 $version = 2;
93         } elsif ($version != 2) {
94                 die "$mainrepo is a -V2 repo, -V$version specified\n"
95         }
96 } elsif (-d "$mainrepo/objects") {
97         if (!defined $version) {
98                 $version = 1;
99         } elsif ($version != 1) {
100                 die "$mainrepo is a -V1 repo, -V$version specified\n"
101         }
102 }
103
104 $version = 1 unless defined $version;
105
106 if ($version == 1 && defined $skip) {
107         die "--skip is only supported for -V2 repos\n";
108 }
109
110 my $ibx = PublicInbox::Inbox->new({
111         mainrepo => $mainrepo,
112         name => $name,
113         version => $version,
114         -primary_address => $address[0],
115         indexlevel => $indexlevel,
116 });
117
118 if ($version >= 2) {
119         require PublicInbox::V2Writable;
120         PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0, $skip);
121 } elsif ($version == 1) {
122         require PublicInbox::V1Writable;
123         PublicInbox::V1Writable->new($ibx, 1)->init_inbox(0, $skip);
124 } else {
125         die "Unsupported -V/--version: $version\n";
126 }
127
128 foreach my $addr (@address) {
129         next if $seen{lc($addr)};
130         x(@x, "--add", "$pfx.address", $addr);
131 }
132 x(@x, "$pfx.url", $http_url);
133 x(@x, "$pfx.mainrepo", $mainrepo);
134
135 if (defined($indexlevel)) {
136         x(@x, "$pfx.indexlevel", $indexlevel);
137 }
138
139 rename $pi_config_tmp, $pi_config or
140         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";