forked from eprintsug/ulcc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.pl.in
executable file
·289 lines (227 loc) · 5.61 KB
/
install.pl.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!@PERL_PATH@
our $PREFIX="@PREFIX@";
our $INSTALL_USER="@INSTALL_USER@";
our $INSTALL_GROUP="@INSTALL_GROUP@";
use strict;
use Cwd;
use Digest::MD5;
use Data::Dumper;
use File::Copy;
use File::Path;
use Getopt::Long;
my %opts;
GetOptions(\%opts,
"dist",
"uninstall",
);
#
# Set the umask so nothing goes odd on systems which
# change it.
#
umask( 0022 );
### Trim prefix if needed
$PREFIX =~ s{/+$}{};
our $DESTDIR;
# for convenience we support a --dist mode which will copy the installed files
# into $DESTDIR without $PREFIX
if ($opts{dist})
{
$DESTDIR = "$ENV{DESTDIR}";
}
else
{
$DESTDIR = "$ENV{DESTDIR}$PREFIX";
}
$DESTDIR =~ s{/+$}{};
our $SIGNATURE_PATH="$DESTDIR/SIGNATURES";
my(undef,undef,$uid) = getpwnam($INSTALL_USER);
my(undef,undef,$gid) = getgrnam($INSTALL_GROUP);
# Warn if we can't install as the requested user/group
unless( $uid )
{
warn("Warning! User $INSTALL_USER doesn't exist. You probably need to do: useradd $INSTALL_USER\n");
}
unless( $gid )
{
warn("Warning! Group $INSTALL_GROUP doesn't exist. You probably need to do: groupadd $INSTALL_GROUP\n");
}
unless( $opts{dist} or $< == 0 or (defined $uid and $uid == $<) )
{
warn("Warning! It looks like you're trying to install as user $INSTALL_USER without root. You probably need to do: sudo $0\n");
}
$uid = $< || 0 unless defined $uid;
$gid = $( || 0 unless defined $gid;
# If we are upgrading and have a digest file, read the digests into a hash
# to check them against the files in place to see if they've changed.
uninstall( $DESTDIR, $SIGNATURE_PATH );
exit if $opts{uninstall};
#
# Actual install starts here.
#
ensure_dir( $DESTDIR, $uid, $gid, 0755 );
ensure_dir( "$DESTDIR/archives", $uid, $gid, 02770 );
ensure_dir( "$DESTDIR/testdata", $uid, $gid, 0750 );
my $newsighash = {};
my @dirs = (
bin => [0750, $uid, $gid, 0750],
cgi => [0640, $uid, $gid, 0750],
cfg => [0640, $uid, $gid, 0750],
lib => [0660, $uid, $gid, 02770], # epms installed as 'apache'
licenses => [0660, $uid, $gid, 0750],
perl_lib => [0640, $uid, $gid, 0750],
"testdata/bin" => [0750, $uid, $gid, 0750],
"testdata/data" => [0640, $uid, $gid, 0750],
tests => [0640, $uid, $gid, 0750],
tools => [0750, $uid, $gid, 0750],
var => [0660, $uid, $gid, 02770], # indexer may run as 'apache'
);
foreach my $i (grep { $_ % 2 == 0 } 0 .. $#dirs)
{
install_dir($dirs[$i], @{$dirs[$i+1]}, $DESTDIR, $newsighash);
}
write_sigs( $DESTDIR, $newsighash, $uid, $gid );
print "Installed EPrints to: $DESTDIR\n";
# Post installation bits
sub uninstall
{
my( $destdir, $sig_path ) = @_;
return if !-e $sig_path;
print "Uninstalling ...\n";
open(my $fh, "<", $sig_path) or die "Error opening $sig_path: $!";
while(<$fh>)
{
chomp;
my( $filepath, $digest ) = split /\s/, $_;
$filepath = "$destdir/$filepath";
next if !-e $filepath;
if( get_digest($filepath) eq $digest )
{
unlink($filepath);
}
else
{
move_out_of_harms_way($filepath);
}
}
}
sub write_sigs
{
my( $dir, $sigs, $uid, $gid ) = @_;
my $sigfile = $dir.'/SIGNATURES';
# Dump out signature file
unless( open (SIGOUT, ">".$sigfile ) )
{
print "Unable to write $sigfile: $!\n";
exit 1;
}
binmode(SIGOUT);
foreach( sort keys %{$sigs} )
{
print SIGOUT $_." ".$sigs->{$_}."\n";
}
close(SIGOUT);
chown($uid, $gid, $sigfile );
chmod( 0660, $sigfile );
}
# Calculate the MD5 digest of a file.
sub get_digest
{
my($file) = @_;
my $ctx = Digest::MD5->new;
open(my $fh, "<", $file) or die "Error opening $file: $!";
$ctx->addfile( $fh );
return $ctx->hexdigest;
}
# Install $file from directory $dir into $dest. The permissions
# from $perms are set, and the group and user are set from $group
# and $user. The %hash is used for MD5 comparisions - currently
# not doing anything active, but checking is enabled.
sub install_file
{
my($file, $dir, $perms, $uid, $gid, $dest, $newsighash) = @_;
my $relpath = $file;
if( defined $dir ) { $relpath = "$dir/$file"; }
if( !defined $dir ) { $dir = ""; }
return unless (-e "$relpath");
$newsighash->{$relpath} = get_digest($relpath);
if( -e "$dest/$relpath" )
{
move_out_of_harms_way( "$dest/$relpath" );
}
copy($relpath, "$dest/$relpath");
chmod($perms, "$dest/$relpath");
chown($uid, $gid, "$dest/$relpath");
}
sub install_dir
{
my( $dir, $fmode, $uid, $gid, $dmode, $dest, $newsighash) = @_;
ensure_dir("$dest/$dir", $uid, $gid, $dmode);
opendir(INDIR, $dir) or return;
my @dirs = ();
my @files = ();
while(my $item = readdir(INDIR))
{
next if $item =~ /^\./;
next if $item =~ /\.in$/; # source files are copied by make dist
if( -d "$dir/$item" )
{
push(@dirs, $item);
}
else
{
push(@files, $item);
}
}
closedir(INDIR);
foreach(@files)
{
install_file($_, $dir, $fmode, $uid, $gid, $dest, $newsighash );
}
foreach(@dirs)
{
install_dir("$dir/".$_, $fmode, $uid, $gid, $dmode, $dest, $newsighash );
}
}
##############################################################
sub move_out_of_harms_way
{
my( $file ) = @_;
my @date = localtime;
my $safename = sprintf(
"%s.backup.%04d-%02d-%02d",
$file,
$date[5]+1900,
$date[4]+1,
$date[3] );
my $n = 0;
while( -e $safename )
{
++$n;
$safename = sprintf(
"%s.backup.%04d-%02d-%02d.%d",
$file,
$date[5]+1900,
$date[4]+1,
$date[3],
$n );
}
rename $file, $safename;
print "\n$file has been modified.\nSaving old version as $safename\n";
}
sub ensure_dir
{
my( $dir, $uid, $gid, $mode ) = @_;
File::Path::make_path($dir, {
# we're not installing into the dist
($opts{dist} ? () : (
user => $uid,
group => $gid,
)),
mode => $mode,
});
if( !-d $dir )
{
die "Error creating directory $dir: $!\n";
}
return;
}