33 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
|
|
5 |
|
42 |
rodolico |
6 |
# install.pl
|
|
|
7 |
#
|
|
|
8 |
# installer for perl script, in this case, sysinfo
|
|
|
9 |
#
|
|
|
10 |
# Revision history
|
|
|
11 |
#
|
|
|
12 |
# Version 1.1.7 20161010 RWR
|
|
|
13 |
# Added ability to validate required libraries are installed
|
|
|
14 |
#
|
50 |
rodolico |
15 |
# version 1.2 20170327 RWR
|
|
|
16 |
# did some major modifications to correctly work on BSD systems also
|
94 |
rodolico |
17 |
#
|
|
|
18 |
# version 2.0 20190330 RWR
|
|
|
19 |
# changed it so all configs are YAML
|
33 |
rodolico |
20 |
|
94 |
rodolico |
21 |
our $VERSION = '2.0';
|
42 |
rodolico |
22 |
|
33 |
rodolico |
23 |
# find our location and use it for searching for libraries
|
|
|
24 |
BEGIN {
|
|
|
25 |
use FindBin;
|
|
|
26 |
use File::Spec;
|
|
|
27 |
use lib File::Spec->catdir($FindBin::Bin);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
use sysinfoconf;
|
101 |
rodolico |
31 |
use YAML::Tiny;
|
94 |
rodolico |
32 |
use Data::Dumper;
|
33 |
rodolico |
33 |
use File::Basename;
|
35 |
rodolico |
34 |
use Getopt::Long;
|
|
|
35 |
Getopt::Long::Configure ("bundling"); # allow -vd --os='debian'
|
33 |
rodolico |
36 |
|
35 |
rodolico |
37 |
# $verbose can have the following values
|
33 |
rodolico |
38 |
# 0 - do everything
|
|
|
39 |
# 1 - Do everything except the install, display what would be done
|
|
|
40 |
# 2 - Be verbose to STDERR
|
|
|
41 |
# 3 - Be very verbose
|
35 |
rodolico |
42 |
my $verbose = 0; # if test mode, simply show what would be done
|
|
|
43 |
my $dryRun = 0;
|
|
|
44 |
my $os;
|
|
|
45 |
my $help = 0;
|
|
|
46 |
my $version = 0;
|
33 |
rodolico |
47 |
|
|
|
48 |
my $status; # exit status of the processes
|
|
|
49 |
my $sourceDir = File::Spec->catdir($FindBin::Bin);
|
34 |
rodolico |
50 |
my $installType;
|
33 |
rodolico |
51 |
|
57 |
rodolico |
52 |
|
33 |
rodolico |
53 |
my %install = ( 'bindir' => '/opt/camp/sysinfo-client',
|
|
|
54 |
'confdir' => '/etc/camp/sysinfo-client',
|
34 |
rodolico |
55 |
'application name' => 'sysinfo client',
|
50 |
rodolico |
56 |
'default group' => 'root',
|
|
|
57 |
'default owner' => 'root',
|
|
|
58 |
'default permission' => '0700',
|
35 |
rodolico |
59 |
'configuration' => {
|
|
|
60 |
'configurator' => '<bindir>/configure.pl',
|
94 |
rodolico |
61 |
'configuration file' => '<confdir>/sysinfo-client.yaml',
|
|
|
62 |
'configuration seed file' => 'sysinfo-client.seed.yaml',
|
|
|
63 |
'old configuration file' => '<confdir>/sysinfo-client.conf',
|
35 |
rodolico |
64 |
'permission' => '700',
|
50 |
rodolico |
65 |
'owner' => '<default owner>',
|
35 |
rodolico |
66 |
'target' => '<confdir>'
|
|
|
67 |
},
|
33 |
rodolico |
68 |
'files' => {
|
|
|
69 |
'configure.pl' => {
|
|
|
70 |
'type' => 'file',
|
|
|
71 |
'permission' => '700',
|
50 |
rodolico |
72 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
73 |
'target' => '<bindir>'
|
|
|
74 |
},
|
|
|
75 |
'sysinfo-client' => {
|
|
|
76 |
'type' => 'file',
|
|
|
77 |
'permission' => '700',
|
50 |
rodolico |
78 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
79 |
'target' => '<bindir>'
|
|
|
80 |
},
|
|
|
81 |
'sysinfoconf.pm' => {
|
|
|
82 |
'type' => 'file',
|
|
|
83 |
'permission' => '600',
|
50 |
rodolico |
84 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
85 |
'target' => '<bindir>'
|
|
|
86 |
},
|
|
|
87 |
'notes' => {
|
|
|
88 |
'type' => 'file',
|
|
|
89 |
'permission' => '600',
|
50 |
rodolico |
90 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
91 |
'target' => '<bindir>'
|
|
|
92 |
},
|
94 |
rodolico |
93 |
'sysinfo-client.conf.template.yaml' => {
|
33 |
rodolico |
94 |
'type' => 'file',
|
|
|
95 |
'permission' => '600',
|
50 |
rodolico |
96 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
97 |
'target' => '<bindir>'
|
|
|
98 |
},
|
|
|
99 |
'getSendEmail.pl' => {
|
|
|
100 |
'type' => 'file',
|
|
|
101 |
'permission' => '700',
|
50 |
rodolico |
102 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
103 |
'target' => '<bindir>'
|
|
|
104 |
},
|
|
|
105 |
'install.pl' => {
|
|
|
106 |
'type' => 'file',
|
|
|
107 |
'permission' => '700',
|
50 |
rodolico |
108 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
109 |
'target' => '<bindir>'
|
|
|
110 |
},
|
|
|
111 |
'MANIFEST' => {
|
|
|
112 |
'type' => 'file',
|
|
|
113 |
'permission' => '600',
|
50 |
rodolico |
114 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
115 |
'target' => '<bindir>'
|
|
|
116 |
},
|
94 |
rodolico |
117 |
'sysinfo-client.seed.example.yaml' => {
|
33 |
rodolico |
118 |
'type' => 'file',
|
|
|
119 |
'permission' => '600',
|
50 |
rodolico |
120 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
121 |
'target' => '<bindir>'
|
|
|
122 |
},
|
|
|
123 |
'VERSION' => {
|
|
|
124 |
'type' => 'file',
|
|
|
125 |
'permission' => '600',
|
50 |
rodolico |
126 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
127 |
'target' => '<bindir>'
|
|
|
128 |
},
|
|
|
129 |
'modules' => {
|
|
|
130 |
'type' => 'directory',
|
|
|
131 |
'permission' => '700',
|
50 |
rodolico |
132 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
133 |
'target' => '<bindir>',
|
|
|
134 |
'action' => 'chmod 700 *'
|
|
|
135 |
},
|
|
|
136 |
'scripts' => {
|
|
|
137 |
'type' => 'directory',
|
|
|
138 |
'permission' => '700',
|
50 |
rodolico |
139 |
'owner' => '<default owner>:<default group>',
|
33 |
rodolico |
140 |
'target' => '<bindir>',
|
|
|
141 |
'action' => 'chmod 700 *'
|
|
|
142 |
},
|
|
|
143 |
}
|
|
|
144 |
);
|
|
|
145 |
|
|
|
146 |
# hash to set up os specific rules
|
|
|
147 |
my %operatingSystems = (
|
|
|
148 |
'debian' => {
|
76 |
rodolico |
149 |
'regex' => '(debian|mx|devuan)',
|
33 |
rodolico |
150 |
'bindir' => '/opt/camp/sysinfo-client',
|
|
|
151 |
'confdir' => '/etc/camp/sysinfo-client',
|
35 |
rodolico |
152 |
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/cron.daily/sysinfo-client',
|
57 |
rodolico |
153 |
'modules' => '((linux)|(dpkg)|(unix)|(all))',
|
33 |
rodolico |
154 |
},
|
|
|
155 |
'ipfire' => {
|
60 |
rodolico |
156 |
'regex' => 'ipfire',
|
33 |
rodolico |
157 |
'bindir' => '/opt/camp/sysinfo-client',
|
|
|
158 |
'confdir' => '/etc/camp/sysinfo-client',
|
50 |
rodolico |
159 |
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/fcron.daily/sysinfo-client.fcron',
|
57 |
rodolico |
160 |
'modules' => '((ipfire)|(unix)|(all))',
|
43 |
rodolico |
161 |
},
|
|
|
162 |
'freebsd' => {
|
60 |
rodolico |
163 |
'regex' => 'freebsd',
|
43 |
rodolico |
164 |
'bindir' => '/usr/local/opt/camp/sysinfo-client',
|
|
|
165 |
'confdir' => '/usr/local/etc/camp/sysinfo-client',
|
50 |
rodolico |
166 |
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/periodic/daily/sysinfo-client',
|
57 |
rodolico |
167 |
'modules' => '((bsd)|(unix)|(all))',
|
50 |
rodolico |
168 |
'default group' => 'wheel',
|
|
|
169 |
'default owner' => 'root',
|
99 |
rodolico |
170 |
'files' => {
|
|
|
171 |
'YAML' => {
|
|
|
172 |
'type' => 'directory',
|
|
|
173 |
'permission' => '777',
|
|
|
174 |
'owner' => '<default owner>:<default group>',
|
|
|
175 |
'target' => '<bindir>'
|
|
|
176 |
},
|
|
|
177 |
'Tiny.pm' => {
|
|
|
178 |
'type' => 'file',
|
|
|
179 |
'permission' => '444',
|
|
|
180 |
'owner' => '<default owner>:<default group>',
|
|
|
181 |
'target' => '<bindir>/YAML'
|
|
|
182 |
},
|
|
|
183 |
},
|
43 |
rodolico |
184 |
},
|
94 |
rodolico |
185 |
'opnsense' => {
|
|
|
186 |
'fileexists' => '/conf/config.xml',
|
|
|
187 |
'bindir' => '/usr/local/opt/camp/sysinfo-client',
|
|
|
188 |
'confdir' => '/usr/local/etc/camp/sysinfo-client',
|
|
|
189 |
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/periodic/daily/sysinfo-client',
|
|
|
190 |
'modules' => '((bsd)|(unix)|(all))',
|
|
|
191 |
'default group' => 'wheel',
|
|
|
192 |
'default owner' => 'root',
|
99 |
rodolico |
193 |
'files' => {
|
|
|
194 |
'YAML' => {
|
|
|
195 |
'type' => 'directory',
|
|
|
196 |
'permission' => '777',
|
|
|
197 |
'owner' => '<default owner>:<default group>',
|
|
|
198 |
'target' => '<bindir>'
|
|
|
199 |
},
|
|
|
200 |
'Tiny.pm' => {
|
|
|
201 |
'type' => 'file',
|
|
|
202 |
'permission' => '444',
|
|
|
203 |
'owner' => '<default owner>:<default group>',
|
|
|
204 |
'target' => '<bindir>/YAML'
|
|
|
205 |
},
|
|
|
206 |
},
|
94 |
rodolico |
207 |
},
|
43 |
rodolico |
208 |
|
|
|
209 |
);
|
42 |
rodolico |
210 |
|
81 |
rodolico |
211 |
my %configuration;
|
|
|
212 |
|
42 |
rodolico |
213 |
# list of libraries used by the system. We will offer to install them if
|
|
|
214 |
# we know how. NOTE: I have chosen to put the full installation command
|
|
|
215 |
# for each library. This allows us to use the package selector OR a different
|
|
|
216 |
# piece of code on a per-library basis, but results in something like
|
|
|
217 |
# apt-get -y install perl-modules
|
|
|
218 |
# apt-get -y install libwww-perl
|
|
|
219 |
# instead of
|
|
|
220 |
# apt-get -y install libwww-perl perl-modules
|
|
|
221 |
# flexibility vs efficiency in this case.
|
|
|
222 |
my %libraries = (
|
|
|
223 |
'File::Basename' => { 'debian' => 'apt-get -y install perl-modules' },
|
|
|
224 |
'Exporter' => { 'debian' => 'apt-get -y install perl-base' },
|
50 |
rodolico |
225 |
'LWP' => {
|
|
|
226 |
'debian' => 'apt-get -y install libwww-perl',
|
55 |
rodolico |
227 |
'freebsd' => 'pkg install -y p5-libwww'
|
50 |
rodolico |
228 |
},
|
42 |
rodolico |
229 |
);
|
|
|
230 |
|
43 |
rodolico |
231 |
# utilities md5sum
|
|
|
232 |
# freebsd isomd5sum
|
|
|
233 |
|
42 |
rodolico |
234 |
# validates the libraries needed exist
|
|
|
235 |
# simply eval's each library. If it doesn't exist, creates a list of
|
|
|
236 |
# commands to be executed to install it, and displays missing libraries
|
|
|
237 |
# offering to install them if possible.
|
|
|
238 |
sub validateLibraries {
|
|
|
239 |
my ( $libraries, $os ) = @_;
|
|
|
240 |
my @command;
|
|
|
241 |
my @missingLibs;
|
|
|
242 |
foreach my $lib ( keys %$libraries ) {
|
98 |
rodolico |
243 |
print "Checking on libarary $lib\n";
|
42 |
rodolico |
244 |
eval( "use $lib;" );
|
|
|
245 |
if ( $@ ) {
|
98 |
rodolico |
246 |
print "\tNot found, adding\n";
|
42 |
rodolico |
247 |
push @command,$$libraries{$lib}{$os} if $$libraries{$lib}{$os};
|
|
|
248 |
push @missingLibs, $lib;
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
if ( @missingLibs ) { # we have missing libraries
|
|
|
252 |
if ( @command ) {
|
|
|
253 |
&runCommand( join( "\n", @command ) )
|
|
|
254 |
if &yesno(
|
|
|
255 |
'Following libraries need to be installed: ' .
|
|
|
256 |
join( ' ', @missingLibs ) . "\n" .
|
|
|
257 |
"I can install them with the following command(s)\n" .
|
|
|
258 |
join( "\n", @command ) . "\nDo you want me to do this?\n"
|
|
|
259 |
);
|
|
|
260 |
} else {
|
|
|
261 |
die unless &yesno( 'Following libraries need to be installed: ' .
|
|
|
262 |
join( ' ', @missingLibs ) .
|
|
|
263 |
"\nand I don't know how to do this. Abort?" );
|
|
|
264 |
}
|
|
|
265 |
} # if
|
|
|
266 |
} # validateLibraries
|
33 |
rodolico |
267 |
|
94 |
rodolico |
268 |
|
33 |
rodolico |
269 |
# attempt to locate the operating system.
|
|
|
270 |
# if found, will set some defaults for it.
|
94 |
rodolico |
271 |
sub setUpOperatingSystemSpecific {
|
35 |
rodolico |
272 |
my ( $install, $operatingSystems, $os ) = @_;
|
107 |
rodolico |
273 |
print "They passed $os in as the \$os\n" if $verbose > 2;
|
35 |
rodolico |
274 |
if ( $os ) {
|
94 |
rodolico |
275 |
# We found the OS, set up some defaults
|
35 |
rodolico |
276 |
$$install{'os'} = $os;
|
|
|
277 |
print "Setting keys for operating system\n" if $verbose > 2;
|
42 |
rodolico |
278 |
foreach my $key ( keys %{$$operatingSystems{ $os }} ) {
|
35 |
rodolico |
279 |
$$install{$key} = $operatingSystems{ $os }{$key};
|
33 |
rodolico |
280 |
} # if it is a known OS
|
35 |
rodolico |
281 |
} # if
|
|
|
282 |
return $os;
|
33 |
rodolico |
283 |
} # getOperatingSystem
|
|
|
284 |
|
37 |
rodolico |
285 |
|
34 |
rodolico |
286 |
# get some input from the user and decide how to install/upgrade/remove/whatever
|
|
|
287 |
sub getInstallActions {
|
|
|
288 |
my $install = shift;
|
|
|
289 |
if ( ! &yesno( "This looks like a $$install{'os'} machine, correct?" ) ) {
|
|
|
290 |
die "User Aborted\n" if &yesno( "If we continue, I will set this up like a $$install{'os'} system. Abort?" );
|
|
|
291 |
}
|
|
|
292 |
if ( -d $$install{'confdir'} ) {
|
|
|
293 |
$$install{'action'} = &getAnswer( "It looks like $$install{'application name'} is already installed, what do you want to do?",
|
|
|
294 |
( "upgrade","remove", "overwrite" )
|
|
|
295 |
);
|
|
|
296 |
} else {
|
|
|
297 |
if ( &yesno( "This looks like a fresh install, correct?" ) ) {
|
|
|
298 |
$$install{'action'} = 'install';
|
|
|
299 |
$$install{'preseed config'} = &yesno( "Preseed the configuration file?" );
|
|
|
300 |
} else {
|
|
|
301 |
die "Can not continue at this time: Do not understand your system\n";
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
$$install{'build config'} = &yesno( "Edit config file when done?" );
|
|
|
305 |
$$install{'setup cron'} = &yesno( "Set up for automatic running via crontab?" );
|
|
|
306 |
}
|
33 |
rodolico |
307 |
|
34 |
rodolico |
308 |
sub showWork {
|
|
|
309 |
my $install = shift;
|
91 |
rodolico |
310 |
print Dump( \%install );
|
34 |
rodolico |
311 |
}
|
35 |
rodolico |
312 |
|
|
|
313 |
|
|
|
314 |
sub doPlaceholderSubstitution {
|
|
|
315 |
my ($hash, $placeholder) = @_;
|
|
|
316 |
return if ref $hash ne 'HASH';
|
|
|
317 |
foreach my $key ( keys %$hash ) {
|
|
|
318 |
if ( ref( $$hash{$key} ) ) {
|
|
|
319 |
&doPlaceholderSubstitution( $$hash{$key}, $placeholder );
|
|
|
320 |
} else {
|
|
|
321 |
foreach my $place ( keys %$placeholder ) {
|
|
|
322 |
$$hash{$key} =~ s/$place/$$placeholder{$place}/;
|
|
|
323 |
} # foreach
|
|
|
324 |
} # if..else
|
|
|
325 |
} # foreach
|
|
|
326 |
return;
|
|
|
327 |
}
|
34 |
rodolico |
328 |
|
|
|
329 |
# This will go through and first, see if anything is a directory, in
|
|
|
330 |
# which case, we'll create new entries for all files in there.
|
|
|
331 |
# then, it will do keyword substitution of <bindir> and <confdir>
|
|
|
332 |
# to populate the target.
|
|
|
333 |
# When this is done, each file should have a source and target that is
|
|
|
334 |
# a fully qualified path and filename
|
33 |
rodolico |
335 |
sub populateSourceDir {
|
|
|
336 |
my ( $install, $sourceDir ) = @_;
|
35 |
rodolico |
337 |
my %placeHolders =
|
|
|
338 |
(
|
|
|
339 |
'<bindir>' => $$install{'bindir'},
|
50 |
rodolico |
340 |
'<confdir>' => $$install{'confdir'},
|
|
|
341 |
'<default owner>' => $$install{'default owner'},
|
|
|
342 |
'<default group>' => $$install{'default group'},
|
|
|
343 |
'<default permission>' => $$install{'default permission'}
|
35 |
rodolico |
344 |
);
|
33 |
rodolico |
345 |
|
|
|
346 |
my $allFiles = $$install{'files'};
|
|
|
347 |
|
35 |
rodolico |
348 |
# find all directory entries and load files in that directory into $$install{'files'}
|
33 |
rodolico |
349 |
foreach my $dir ( keys %$allFiles ) {
|
|
|
350 |
if ( defined( $$allFiles{$dir}{'type'} ) && $$allFiles{$dir}{'type'} eq 'directory' ) {
|
35 |
rodolico |
351 |
print "Found directory $dir\n" if $verbose > 2;
|
33 |
rodolico |
352 |
if ( opendir( my $dh, "$sourceDir/$dir" ) ) {
|
|
|
353 |
my @files = map{ $dir . '/' . $_ } grep { ! /^\./ && -f "$sourceDir/$dir/$_" } readdir( $dh );
|
35 |
rodolico |
354 |
print "\tFound files " . join( ' ', @files ) . "\n" if $verbose > 2;
|
33 |
rodolico |
355 |
foreach my $file ( @files ) {
|
|
|
356 |
$$allFiles{ $file }{'type'} = 'file';
|
37 |
rodolico |
357 |
if ( $dir eq 'modules' ) {
|
|
|
358 |
$$allFiles{ $file }{'permission'} = ( $file =~ m/$$install{'modules'}/ ) ? '0700' : '0600';
|
|
|
359 |
} else {
|
|
|
360 |
$$allFiles{ $file }{'permission'} = $$allFiles{ $dir }{'permission'};
|
|
|
361 |
}
|
33 |
rodolico |
362 |
$$allFiles{ $file }{'owner'} = $$allFiles{ $dir }{'owner'};
|
|
|
363 |
$$allFiles{ $file }{'target'} = $$allFiles{ $dir }{'target'};
|
|
|
364 |
} # foreach
|
|
|
365 |
closedir $dh;
|
|
|
366 |
} # if opendir
|
|
|
367 |
} # if it is a directory
|
|
|
368 |
} # foreach
|
35 |
rodolico |
369 |
# find all files, and set the source directory, and add the filename to
|
|
|
370 |
# the target
|
33 |
rodolico |
371 |
foreach my $file ( keys %$allFiles ) {
|
|
|
372 |
$$allFiles{$file}{'source'} = "$sourceDir/$file";
|
|
|
373 |
$$allFiles{$file}{'target'} .= "/$file";
|
|
|
374 |
} # foreach
|
35 |
rodolico |
375 |
|
|
|
376 |
# finally, do place holder substitution. This recursively replaces all keys
|
|
|
377 |
# in %placeHolders with the values.
|
|
|
378 |
&doPlaceholderSubstitution( $install, \%placeHolders );
|
|
|
379 |
|
91 |
rodolico |
380 |
print Dump( $install ) if $verbose > 2;
|
35 |
rodolico |
381 |
|
33 |
rodolico |
382 |
return 1;
|
|
|
383 |
} # populateSourceDir
|
|
|
384 |
|
34 |
rodolico |
385 |
# there is a file named VERSIONS. We get the values out of the install
|
|
|
386 |
# directory and (if it exists) the target so we can decide what needs
|
|
|
387 |
# to be updated.
|
33 |
rodolico |
388 |
sub getVersions {
|
|
|
389 |
my $install = shift;
|
|
|
390 |
my $currentVersionFile = $$install{'files'}{'VERSION'}{'target'};
|
|
|
391 |
my $newVersionFile = $$install{'files'}{'VERSION'}{'source'};
|
|
|
392 |
if ( open FILE,"<$currentVersionFile" ) {
|
|
|
393 |
while ( my $line = <FILE> ) {
|
|
|
394 |
chomp $line;
|
|
|
395 |
my ( $filename, $version, $checksum ) = split( "\t", $line );
|
|
|
396 |
$$install{'files'}{$filename}{'installed version'} = $version ? $version : '';
|
|
|
397 |
}
|
|
|
398 |
close FILE;
|
|
|
399 |
}
|
|
|
400 |
if ( open FILE,"<$newVersionFile" ) {
|
|
|
401 |
while ( my $line = <FILE> ) {
|
|
|
402 |
chomp $line;
|
|
|
403 |
my ( $filename, $version, $checksum ) = split( "\t", $line );
|
|
|
404 |
$$install{'files'}{$filename}{'new version'} = $version ? $version : '';
|
|
|
405 |
}
|
|
|
406 |
close FILE;
|
|
|
407 |
}
|
40 |
rodolico |
408 |
foreach my $file ( keys %{$$install{'files'}} ) {
|
|
|
409 |
$$install{'files'}{$file}{'installed version'} = -2 unless defined $$install{'files'}{$file}{'installed version'};
|
|
|
410 |
$$install{'files'}{$file}{'new version'} = -1 unless defined $$install{'files'}{$file}{'new version'};
|
|
|
411 |
}
|
33 |
rodolico |
412 |
return 1;
|
|
|
413 |
} # getVersions
|
|
|
414 |
|
34 |
rodolico |
415 |
# this actually does the installation, except for the configuration
|
33 |
rodolico |
416 |
sub doInstall {
|
|
|
417 |
my $install = shift;
|
|
|
418 |
my $fileList = $$install{'files'};
|
|
|
419 |
|
50 |
rodolico |
420 |
&checkDirectoryExists( $$install{'bindir'} . '/', $$install{'default permission'}, "$$install{'default owner'}:$$install{'default group'}" );
|
33 |
rodolico |
421 |
foreach my $file ( keys %$fileList ) {
|
|
|
422 |
next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
|
40 |
rodolico |
423 |
next if $$install{'action'} eq 'upgrade' && ! defined( $$fileList{$file}{'installed version'} )
|
|
|
424 |
||
|
|
|
425 |
( $$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'} );
|
50 |
rodolico |
426 |
&checkDirectoryExists( $$fileList{$file}{'target'}, $$install{'default permission'}, "$$install{'default owner'}:$$install{'default group'}" );
|
33 |
rodolico |
427 |
&runCommand(
|
|
|
428 |
"cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
|
|
|
429 |
"chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
|
|
|
430 |
"chown $$fileList{$file}{'owner'} $$fileList{$file}{'target'}"
|
|
|
431 |
);
|
|
|
432 |
} # foreach file
|
|
|
433 |
return 1;
|
|
|
434 |
}
|
|
|
435 |
|
35 |
rodolico |
436 |
sub postInstall {
|
|
|
437 |
my $install = shift;
|
37 |
rodolico |
438 |
|
35 |
rodolico |
439 |
# set up crontab, if necessary
|
37 |
rodolico |
440 |
&runCommand( $$install{'crontab'} ) if defined ( $$install{'crontab'} );
|
|
|
441 |
|
|
|
442 |
# seed configuration, if needed
|
|
|
443 |
if ( $$install{'build config'} ) {
|
43 |
rodolico |
444 |
my $config;
|
|
|
445 |
my @fileList;
|
37 |
rodolico |
446 |
|
94 |
rodolico |
447 |
# the order is important here as, if multiple files exist, latter ones can
|
|
|
448 |
# overwrite values in the previous. We do a push so the first value pushed
|
|
|
449 |
# is processed last, ie has higher priority.
|
|
|
450 |
push @fileList, $install->{'configuration'}->{'old configuration file'};
|
|
|
451 |
push @fileList, $install->{'configuration'}->{'configuration file'};
|
|
|
452 |
|
|
|
453 |
my $seedFile = $install->{'configuration'}->{'configuration seed file'};
|
|
|
454 |
if ( -e $seedFile && &yesno( 'Add installation seed file? ' ) ) {
|
43 |
rodolico |
455 |
push @fileList, $seedFile;
|
37 |
rodolico |
456 |
} # if preload seed file
|
|
|
457 |
|
92 |
rodolico |
458 |
$config = &makeConfig( @fileList );
|
54 |
rodolico |
459 |
# if ScriptDirs and moduleDirs not populated, do so from our configuration
|
88 |
rodolico |
460 |
if ( ! $$config{'scriptDirs'} || ! scalar @{ $$config{'scriptDirs'} } ) {
|
90 |
rodolico |
461 |
# my @temp = ( $$install{'files'}{'scripts'}{'target'} );
|
|
|
462 |
# $$config{'scriptDirs'} = \@temp;
|
|
|
463 |
$config->{'scriptDirs'} = [ $install->{'files'}->{'scripts'}->{'target'} ];
|
|
|
464 |
|
54 |
rodolico |
465 |
}
|
88 |
rodolico |
466 |
if ( ! $$config{'moduleDirs'} || ! @{ $$config{'moduleDirs'} } ) {
|
90 |
rodolico |
467 |
$config->{'moduleDirs'} = [ $install->{'files'}->{'modules'}->{'target'} ];
|
|
|
468 |
# $$config{'moduleDirs'} = [ $$install{'files'}{'modules'}{'target'} ];
|
54 |
rodolico |
469 |
}
|
|
|
470 |
# print Dumper ($config ) ; die;
|
43 |
rodolico |
471 |
my $content = &showConf( $config );
|
35 |
rodolico |
472 |
print $content;
|
37 |
rodolico |
473 |
print &writeConfig( $$install{'configuration'}{'configuration file'} , $content ) . "\n"
|
|
|
474 |
if ( &yesno( "Write the above configuration to $$install{'configuration'}{'configuration file'}?" ) );
|
|
|
475 |
} # if we are building/merging configuration
|
35 |
rodolico |
476 |
|
|
|
477 |
}
|
34 |
rodolico |
478 |
|
35 |
rodolico |
479 |
sub help {
|
|
|
480 |
my $oses = join( ' ', keys %operatingSystems );
|
|
|
481 |
print <<END
|
|
|
482 |
$0 --verbose=x --os="osname" --dryrun --help --version
|
34 |
rodolico |
483 |
|
35 |
rodolico |
484 |
--os - osname is one of [$oses]
|
|
|
485 |
--dryrun - do not actually do anything, just tell you what I'd do
|
|
|
486 |
--verbose - x is 0 (normal) to 3 (horrible)
|
|
|
487 |
END
|
|
|
488 |
}
|
34 |
rodolico |
489 |
|
35 |
rodolico |
490 |
|
|
|
491 |
|
|
|
492 |
##########################################
|
|
|
493 |
# Main Loop
|
|
|
494 |
##########################################
|
|
|
495 |
|
|
|
496 |
# handle any command line parameters that may have been passed in
|
|
|
497 |
|
|
|
498 |
GetOptions (
|
|
|
499 |
"verbose|v=i" => \$verbose, # verbosity level, 0-9
|
|
|
500 |
"os|o=s" => \$os, # pass in the operating system
|
|
|
501 |
"dryrun|n" => \$dryRun, # do NOT actually do anything
|
|
|
502 |
'help|h' => \$help,
|
|
|
503 |
'version|V' => \$version
|
|
|
504 |
) or die "Error parsing command line\n";
|
|
|
505 |
|
|
|
506 |
if ( $help ) { &help() ; exit; }
|
|
|
507 |
if ( $version ) { print "$0 version $VERSION\n"; exit; }
|
|
|
508 |
&setDryRun( $dryRun ); # tell the library whether this is a dry run or not
|
|
|
509 |
|
107 |
rodolico |
510 |
$os = `$sourceDir/determineOS` unless $os;
|
94 |
rodolico |
511 |
# figure out if we know our operating system and set up special stuff for it
|
|
|
512 |
$os = &setUpOperatingSystemSpecific( \%install, \%operatingSystems, $os );
|
33 |
rodolico |
513 |
|
42 |
rodolico |
514 |
&validateLibraries( \%libraries, $os );
|
|
|
515 |
|
35 |
rodolico |
516 |
$installType = &getInstallActions( \%install );
|
34 |
rodolico |
517 |
|
33 |
rodolico |
518 |
# based on the defaults, flesh out the install hash
|
|
|
519 |
$status = &populateSourceDir( \%install, $sourceDir );
|
|
|
520 |
|
35 |
rodolico |
521 |
|
33 |
rodolico |
522 |
$status = &getVersions( \%install );
|
|
|
523 |
|
35 |
rodolico |
524 |
&showWork( \%install );
|
|
|
525 |
die unless &yesno( "Ready to run? Select No to abort." );
|
|
|
526 |
|
94 |
rodolico |
527 |
#my $config = &makeConfig( $install{'configuration'}{'configuration seed file'}, $install{'configuration'}{'configuration file'}, $install{'configuration'}{'old configuration file'} );
|
|
|
528 |
#print Dumper( $config );
|
|
|
529 |
#die;
|
|
|
530 |
|
|
|
531 |
|
33 |
rodolico |
532 |
$status = &doInstall( \%install );
|
|
|
533 |
|
35 |
rodolico |
534 |
$status = &postInstall( \%install );
|
34 |
rodolico |
535 |
|
57 |
rodolico |
536 |
# create uninstaller script
|
|
|
537 |
# find the last non-space string in the crontab value. This is the target of the link
|
|
|
538 |
$install{'crontab'} =~ m/([^ ]+)$/;
|
94 |
rodolico |
539 |
my $uninstall = "#! /usr/bin/env sh\n\n# Uninstall syinfo\nrm -fR $install{'bindir'} $1\n";
|
57 |
rodolico |
540 |
my $outFileName = $install{'bindir'} . '/uninstall';
|
|
|
541 |
open UNINSTALL, ">$outFileName" or die "could not write to $outFileName: $!\n";
|
|
|
542 |
print UNINSTALL $uninstall;
|
|
|
543 |
close UNINSTALL;
|
|
|
544 |
qx ( chmod 700 $outFileName );
|
|
|
545 |
|
|
|
546 |
print "Uninstall script has been created at $outFileName\n";
|
|
|
547 |
|
37 |
rodolico |
548 |
if ( ( -x $install{'configuration'}{'configurator'} ) && $install{'build config'} ) {
|
|
|
549 |
exec( $install{'configuration'}{'configurator'} );
|
|
|
550 |
} else {
|
|
|
551 |
print "Done, you should check the files in $install{'bindir'} and $install{'confdir'} before running\n";
|
|
|
552 |
print "If you need help configuring, the helper app at\n$install{'configuration'}{'configurator'}\ncan be used.\n";
|
|
|
553 |
}
|
|
|
554 |
|
35 |
rodolico |
555 |
1;
|
40 |
rodolico |
556 |
|
|
|
557 |
|
|
|
558 |
# clean will look for any file in bindir which is NOT in the list of available files and remove them
|
|
|
559 |
# if files already exist in install, preserve their permissions
|