33 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
|
|
5 |
|
|
|
6 |
our $VERSION = '1.1.3';
|
|
|
7 |
|
|
|
8 |
# find our location and use it for searching for libraries
|
|
|
9 |
BEGIN {
|
|
|
10 |
use FindBin;
|
|
|
11 |
use File::Spec;
|
|
|
12 |
use lib File::Spec->catdir($FindBin::Bin);
|
|
|
13 |
}
|
|
|
14 |
|
|
|
15 |
use sysinfoconf;
|
|
|
16 |
use File::Basename;
|
35 |
rodolico |
17 |
use Getopt::Long;
|
|
|
18 |
Getopt::Long::Configure ("bundling"); # allow -vd --os='debian'
|
33 |
rodolico |
19 |
|
|
|
20 |
use Data::Dumper;
|
|
|
21 |
|
35 |
rodolico |
22 |
# $verbose can have the following values
|
33 |
rodolico |
23 |
# 0 - do everything
|
|
|
24 |
# 1 - Do everything except the install, display what would be done
|
|
|
25 |
# 2 - Be verbose to STDERR
|
|
|
26 |
# 3 - Be very verbose
|
35 |
rodolico |
27 |
my $verbose = 0; # if test mode, simply show what would be done
|
|
|
28 |
my $dryRun = 0;
|
|
|
29 |
my $os;
|
|
|
30 |
my $help = 0;
|
|
|
31 |
my $version = 0;
|
33 |
rodolico |
32 |
|
|
|
33 |
my $status; # exit status of the processes
|
|
|
34 |
my $sourceDir = File::Spec->catdir($FindBin::Bin);
|
34 |
rodolico |
35 |
my $installType;
|
33 |
rodolico |
36 |
|
|
|
37 |
my %install = ( 'bindir' => '/opt/camp/sysinfo-client',
|
|
|
38 |
'confdir' => '/etc/camp/sysinfo-client',
|
34 |
rodolico |
39 |
'application name' => 'sysinfo client',
|
35 |
rodolico |
40 |
'configuration' => {
|
|
|
41 |
'configurator' => '<bindir>/configure.pl',
|
|
|
42 |
'configuration file' => '<confdir>/sysinfo-client.conf',
|
|
|
43 |
'configuration seed file' => 'sysinfo-client.seed',
|
|
|
44 |
'permission' => '700',
|
|
|
45 |
'owner' => 'root',
|
|
|
46 |
'target' => '<confdir>'
|
|
|
47 |
},
|
33 |
rodolico |
48 |
'files' => {
|
|
|
49 |
'configure.pl' => {
|
|
|
50 |
'type' => 'file',
|
|
|
51 |
'permission' => '700',
|
|
|
52 |
'owner' => 'root:root',
|
|
|
53 |
'target' => '<bindir>'
|
|
|
54 |
},
|
|
|
55 |
'sysinfo-client' => {
|
|
|
56 |
'type' => 'file',
|
|
|
57 |
'permission' => '700',
|
|
|
58 |
'owner' => 'root:root',
|
|
|
59 |
'target' => '<bindir>'
|
|
|
60 |
},
|
|
|
61 |
'sysinfoconf.pm' => {
|
|
|
62 |
'type' => 'file',
|
|
|
63 |
'permission' => '600',
|
|
|
64 |
'owner' => 'root:root',
|
|
|
65 |
'target' => '<bindir>'
|
|
|
66 |
},
|
|
|
67 |
'debian.pm' => {
|
|
|
68 |
'type' => 'file',
|
|
|
69 |
'permission' => '600',
|
|
|
70 |
'owner' => 'root:root',
|
|
|
71 |
'target' => '<bindir>'
|
|
|
72 |
},
|
|
|
73 |
'ipfire.pm' => {
|
|
|
74 |
'type' => 'file',
|
|
|
75 |
'permission' => '600',
|
|
|
76 |
'owner' => 'root:root',
|
|
|
77 |
'target' => '<bindir>'
|
|
|
78 |
},
|
|
|
79 |
'notes' => {
|
|
|
80 |
'type' => 'file',
|
|
|
81 |
'permission' => '600',
|
|
|
82 |
'owner' => 'root:root',
|
|
|
83 |
'target' => '<bindir>'
|
|
|
84 |
},
|
|
|
85 |
'sysinfo-client.conf.template' => {
|
|
|
86 |
'type' => 'file',
|
|
|
87 |
'permission' => '600',
|
|
|
88 |
'owner' => 'root:root',
|
|
|
89 |
'target' => '<bindir>'
|
|
|
90 |
},
|
|
|
91 |
'uninstall.pl' => {
|
|
|
92 |
'type' => 'file',
|
|
|
93 |
'permission' => '700',
|
|
|
94 |
'owner' => 'root:root',
|
|
|
95 |
'target' => '<bindir>'
|
|
|
96 |
},
|
|
|
97 |
'getSendEmail.pl' => {
|
|
|
98 |
'type' => 'file',
|
|
|
99 |
'permission' => '700',
|
|
|
100 |
'owner' => 'root:root',
|
|
|
101 |
'target' => '<bindir>'
|
|
|
102 |
},
|
|
|
103 |
'upgrade.pl' => {
|
|
|
104 |
'type' => 'file',
|
|
|
105 |
'permission' => '700',
|
|
|
106 |
'owner' => 'root:root',
|
|
|
107 |
'target' => '<bindir>'
|
|
|
108 |
},
|
|
|
109 |
'install.pl' => {
|
|
|
110 |
'type' => 'file',
|
|
|
111 |
'permission' => '700',
|
|
|
112 |
'owner' => 'root:root',
|
|
|
113 |
'target' => '<bindir>'
|
|
|
114 |
},
|
|
|
115 |
'MANIFEST' => {
|
|
|
116 |
'type' => 'file',
|
|
|
117 |
'permission' => '600',
|
|
|
118 |
'owner' => 'root:root',
|
|
|
119 |
'target' => '<bindir>'
|
|
|
120 |
},
|
|
|
121 |
'sysinfo-client.seed.example' => {
|
|
|
122 |
'type' => 'file',
|
|
|
123 |
'permission' => '600',
|
|
|
124 |
'owner' => 'root:root',
|
|
|
125 |
'target' => '<bindir>'
|
|
|
126 |
},
|
|
|
127 |
'VERSION' => {
|
|
|
128 |
'type' => 'file',
|
|
|
129 |
'permission' => '600',
|
|
|
130 |
'owner' => 'root:root',
|
|
|
131 |
'target' => '<bindir>'
|
|
|
132 |
},
|
|
|
133 |
'modules' => {
|
|
|
134 |
'type' => 'directory',
|
|
|
135 |
'permission' => '700',
|
|
|
136 |
'owner' => 'root:root',
|
|
|
137 |
'target' => '<bindir>',
|
|
|
138 |
'action' => 'chmod 700 *'
|
|
|
139 |
},
|
|
|
140 |
'scripts' => {
|
|
|
141 |
'type' => 'directory',
|
|
|
142 |
'permission' => '700',
|
|
|
143 |
'owner' => 'root:root',
|
|
|
144 |
'target' => '<bindir>',
|
|
|
145 |
'action' => 'chmod 700 *'
|
|
|
146 |
},
|
|
|
147 |
}
|
|
|
148 |
);
|
|
|
149 |
|
|
|
150 |
# hash to set up os specific rules
|
|
|
151 |
my %operatingSystems = (
|
|
|
152 |
'debian' => {
|
|
|
153 |
'bindir' => '/opt/camp/sysinfo-client',
|
|
|
154 |
'confdir' => '/etc/camp/sysinfo-client',
|
35 |
rodolico |
155 |
'crontab' => 'ln -s <bindir>/sysinfo-client /etc/cron.daily/sysinfo-client',
|
33 |
rodolico |
156 |
'modules' => '/^((dpkg)|(unix)|(ipmi))/',
|
|
|
157 |
},
|
|
|
158 |
'ipfire' => {
|
|
|
159 |
'bindir' => '/opt/camp/sysinfo-client',
|
|
|
160 |
'confdir' => '/etc/camp/sysinfo-client',
|
35 |
rodolico |
161 |
'crontab' => 'ln -s <bindir>sysinfo-client /etc/fcron.daily/sysinfo-client.fcron',
|
33 |
rodolico |
162 |
'modules' => '/^((ipfire)|(unix))/',
|
|
|
163 |
}
|
|
|
164 |
);
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
# attempt to locate the operating system.
|
|
|
168 |
# if found, will set some defaults for it.
|
|
|
169 |
sub getOS {
|
35 |
rodolico |
170 |
my ( $install, $operatingSystems, $os ) = @_;
|
|
|
171 |
if ( ! $os ) { # we don't know, so we must try to figure it out
|
|
|
172 |
my $osString = `uname -a`;
|
|
|
173 |
foreach my $osType ( keys %$operatingSystems ) {
|
|
|
174 |
print "Checking if OS is $osType in $osString\n" if $verbose > 2;
|
|
|
175 |
next unless $osString =~ m/$osType/i;
|
|
|
176 |
print "Yes, it is $osType\n" if $verbose > 2;
|
|
|
177 |
# We found the OS, set up some defaults
|
|
|
178 |
$os = $osType;
|
|
|
179 |
} # foreach
|
|
|
180 |
}
|
|
|
181 |
if ( $os ) {
|
|
|
182 |
$$install{'os'} = $os;
|
|
|
183 |
print "Setting keys for operating system\n" if $verbose > 2;
|
|
|
184 |
foreach my $key ( keys $$operatingSystems{ $os } ) {
|
|
|
185 |
$$install{$key} = $operatingSystems{ $os }{$key};
|
33 |
rodolico |
186 |
} # if it is a known OS
|
35 |
rodolico |
187 |
} # if
|
|
|
188 |
return $os;
|
33 |
rodolico |
189 |
} # getOperatingSystem
|
|
|
190 |
|
34 |
rodolico |
191 |
# get some input from the user and decide how to install/upgrade/remove/whatever
|
|
|
192 |
sub getInstallActions {
|
|
|
193 |
my $install = shift;
|
|
|
194 |
if ( ! &yesno( "This looks like a $$install{'os'} machine, correct?" ) ) {
|
|
|
195 |
die "User Aborted\n" if &yesno( "If we continue, I will set this up like a $$install{'os'} system. Abort?" );
|
|
|
196 |
}
|
|
|
197 |
if ( -d $$install{'confdir'} ) {
|
|
|
198 |
$$install{'action'} = &getAnswer( "It looks like $$install{'application name'} is already installed, what do you want to do?",
|
|
|
199 |
( "upgrade","remove", "overwrite" )
|
|
|
200 |
);
|
|
|
201 |
} else {
|
|
|
202 |
if ( &yesno( "This looks like a fresh install, correct?" ) ) {
|
|
|
203 |
$$install{'action'} = 'install';
|
|
|
204 |
$$install{'preseed config'} = &yesno( "Preseed the configuration file?" );
|
|
|
205 |
} else {
|
|
|
206 |
die "Can not continue at this time: Do not understand your system\n";
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
$$install{'build config'} = &yesno( "Edit config file when done?" );
|
|
|
210 |
$$install{'setup cron'} = &yesno( "Set up for automatic running via crontab?" );
|
|
|
211 |
}
|
33 |
rodolico |
212 |
|
34 |
rodolico |
213 |
sub showWork {
|
|
|
214 |
my $install = shift;
|
|
|
215 |
print Dumper( \%install );
|
|
|
216 |
}
|
35 |
rodolico |
217 |
|
|
|
218 |
|
|
|
219 |
sub doPlaceholderSubstitution {
|
|
|
220 |
my ($hash, $placeholder) = @_;
|
|
|
221 |
return if ref $hash ne 'HASH';
|
|
|
222 |
foreach my $key ( keys %$hash ) {
|
|
|
223 |
if ( ref( $$hash{$key} ) ) {
|
|
|
224 |
&doPlaceholderSubstitution( $$hash{$key}, $placeholder );
|
|
|
225 |
} else {
|
|
|
226 |
foreach my $place ( keys %$placeholder ) {
|
|
|
227 |
$$hash{$key} =~ s/$place/$$placeholder{$place}/;
|
|
|
228 |
} # foreach
|
|
|
229 |
} # if..else
|
|
|
230 |
} # foreach
|
|
|
231 |
return;
|
|
|
232 |
}
|
34 |
rodolico |
233 |
|
|
|
234 |
# This will go through and first, see if anything is a directory, in
|
|
|
235 |
# which case, we'll create new entries for all files in there.
|
|
|
236 |
# then, it will do keyword substitution of <bindir> and <confdir>
|
|
|
237 |
# to populate the target.
|
|
|
238 |
# When this is done, each file should have a source and target that is
|
|
|
239 |
# a fully qualified path and filename
|
33 |
rodolico |
240 |
sub populateSourceDir {
|
|
|
241 |
my ( $install, $sourceDir ) = @_;
|
35 |
rodolico |
242 |
my %placeHolders =
|
|
|
243 |
(
|
|
|
244 |
'<bindir>' => $$install{'bindir'},
|
|
|
245 |
'<confdir>' => $$install{'confdir'}
|
|
|
246 |
);
|
33 |
rodolico |
247 |
|
|
|
248 |
my $allFiles = $$install{'files'};
|
|
|
249 |
|
35 |
rodolico |
250 |
# find all directory entries and load files in that directory into $$install{'files'}
|
33 |
rodolico |
251 |
foreach my $dir ( keys %$allFiles ) {
|
|
|
252 |
if ( defined( $$allFiles{$dir}{'type'} ) && $$allFiles{$dir}{'type'} eq 'directory' ) {
|
35 |
rodolico |
253 |
print "Found directory $dir\n" if $verbose > 2;
|
33 |
rodolico |
254 |
if ( opendir( my $dh, "$sourceDir/$dir" ) ) {
|
|
|
255 |
my @files = map{ $dir . '/' . $_ } grep { ! /^\./ && -f "$sourceDir/$dir/$_" } readdir( $dh );
|
35 |
rodolico |
256 |
print "\tFound files " . join( ' ', @files ) . "\n" if $verbose > 2;
|
33 |
rodolico |
257 |
foreach my $file ( @files ) {
|
|
|
258 |
$$allFiles{ $file }{'type'} = 'file';
|
|
|
259 |
$$allFiles{ $file }{'permission'} = $$allFiles{ $dir }{'permission'};
|
|
|
260 |
$$allFiles{ $file }{'owner'} = $$allFiles{ $dir }{'owner'};
|
|
|
261 |
$$allFiles{ $file }{'target'} = $$allFiles{ $dir }{'target'};
|
|
|
262 |
} # foreach
|
|
|
263 |
closedir $dh;
|
|
|
264 |
} # if opendir
|
|
|
265 |
} # if it is a directory
|
|
|
266 |
} # foreach
|
35 |
rodolico |
267 |
|
|
|
268 |
# find all files, and set the source directory, and add the filename to
|
|
|
269 |
# the target
|
33 |
rodolico |
270 |
foreach my $file ( keys %$allFiles ) {
|
|
|
271 |
$$allFiles{$file}{'source'} = "$sourceDir/$file";
|
|
|
272 |
$$allFiles{$file}{'target'} .= "/$file";
|
|
|
273 |
} # foreach
|
35 |
rodolico |
274 |
|
|
|
275 |
# finally, do place holder substitution. This recursively replaces all keys
|
|
|
276 |
# in %placeHolders with the values.
|
|
|
277 |
&doPlaceholderSubstitution( $install, \%placeHolders );
|
|
|
278 |
|
|
|
279 |
print Dumper( $install ) if $verbose > 2;
|
|
|
280 |
|
33 |
rodolico |
281 |
return 1;
|
|
|
282 |
} # populateSourceDir
|
|
|
283 |
|
34 |
rodolico |
284 |
# there is a file named VERSIONS. We get the values out of the install
|
|
|
285 |
# directory and (if it exists) the target so we can decide what needs
|
|
|
286 |
# to be updated.
|
33 |
rodolico |
287 |
sub getVersions {
|
|
|
288 |
my $install = shift;
|
|
|
289 |
my $currentVersionFile = $$install{'files'}{'VERSION'}{'target'};
|
|
|
290 |
my $newVersionFile = $$install{'files'}{'VERSION'}{'source'};
|
|
|
291 |
if ( open FILE,"<$currentVersionFile" ) {
|
|
|
292 |
while ( my $line = <FILE> ) {
|
|
|
293 |
chomp $line;
|
|
|
294 |
my ( $filename, $version, $checksum ) = split( "\t", $line );
|
|
|
295 |
$$install{'files'}{$filename}{'installed version'} = $version ? $version : '';
|
|
|
296 |
}
|
|
|
297 |
close FILE;
|
|
|
298 |
}
|
|
|
299 |
if ( open FILE,"<$newVersionFile" ) {
|
|
|
300 |
while ( my $line = <FILE> ) {
|
|
|
301 |
chomp $line;
|
|
|
302 |
my ( $filename, $version, $checksum ) = split( "\t", $line );
|
|
|
303 |
$$install{'files'}{$filename}{'new version'} = $version ? $version : '';
|
|
|
304 |
}
|
|
|
305 |
close FILE;
|
|
|
306 |
}
|
|
|
307 |
return 1;
|
|
|
308 |
} # getVersions
|
|
|
309 |
|
34 |
rodolico |
310 |
# this actually does the installation, except for the configuration
|
33 |
rodolico |
311 |
sub doInstall {
|
|
|
312 |
my $install = shift;
|
|
|
313 |
my $fileList = $$install{'files'};
|
|
|
314 |
|
|
|
315 |
&checkDirectoryExists( $$install{'bindir'} . '/' );
|
|
|
316 |
foreach my $file ( keys %$fileList ) {
|
|
|
317 |
next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
|
34 |
rodolico |
318 |
next if $$install{'action'} eq 'upgrade' &&
|
|
|
319 |
$$fileList{$file}{'installed version'} &&
|
33 |
rodolico |
320 |
$$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'};
|
|
|
321 |
&checkDirectoryExists( $$fileList{$file}{'target'} );
|
|
|
322 |
&runCommand(
|
|
|
323 |
"cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
|
|
|
324 |
"chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
|
|
|
325 |
"chown $$fileList{$file}{'owner'} $$fileList{$file}{'target'}"
|
|
|
326 |
);
|
|
|
327 |
} # foreach file
|
|
|
328 |
return 1;
|
|
|
329 |
}
|
|
|
330 |
|
35 |
rodolico |
331 |
sub postInstall {
|
|
|
332 |
my $install = shift;
|
|
|
333 |
# set up crontab, if necessary
|
|
|
334 |
# run configurator, if needed
|
|
|
335 |
my %config;
|
|
|
336 |
my $seedFile = $install{'configuration'}{'configuration seed file'};
|
|
|
337 |
my $confFile = $install{'configuration'}{'configuration file'};
|
|
|
338 |
my $content;
|
|
|
339 |
if ( -f $seedFile && &yesno( 'Add installation seed file? ' ) ) {
|
|
|
340 |
my $clientName;
|
|
|
341 |
my $serialNumber;
|
|
|
342 |
my $hostname;
|
|
|
343 |
my @moduleDirs;
|
|
|
344 |
my @scriptDirs;
|
|
|
345 |
my $transports = {};
|
|
|
346 |
|
|
|
347 |
print "Loading seed file $seedFile\n";
|
|
|
348 |
open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
|
|
|
349 |
$content = join( '', <SEED> );
|
|
|
350 |
close SEED;
|
|
|
351 |
# now, eval the information we just read.
|
|
|
352 |
# NOTE: we must turn off strict while doing this, and we die if something breaks.
|
|
|
353 |
no strict "vars"; eval( $content ); use strict "vars"; die "Error during eval: $@\n" if $@;
|
34 |
rodolico |
354 |
|
35 |
rodolico |
355 |
if ( -f $confFile ) {
|
|
|
356 |
print "Loading configuration file $confFile\n";
|
|
|
357 |
open SEED, "<$confFile" or die "Could not open $confFile: $!\n";
|
|
|
358 |
$content = join( '', <SEED> );
|
|
|
359 |
close SEED;
|
|
|
360 |
# now, eval the information we just read.
|
|
|
361 |
# NOTE: we must turn off strict while doing this, and we die if something breaks.
|
|
|
362 |
no strict "vars"; eval( $content ); use strict "vars"; die "Error during eval: $@\n" if $@;
|
|
|
363 |
}
|
|
|
364 |
$config{'clientName'} = $clientName;
|
|
|
365 |
$config{'serialNumber'} = $serialNumber;
|
|
|
366 |
$config{'hostname'} = $hostname;
|
|
|
367 |
$config{'moduleDirs'} = [ @moduleDirs ];
|
|
|
368 |
$config{'scriptDirs'} = [ @scriptDirs ];
|
|
|
369 |
$config{'transports'} = $transports;
|
34 |
rodolico |
370 |
|
35 |
rodolico |
371 |
$content = &showConf( \%config );
|
|
|
372 |
print $content;
|
|
|
373 |
print &writeConfig( $install{'configuration'}{'configuration file'} , $content ) . "\n"
|
|
|
374 |
if ( &yesno( "Write the above configuration to $install{'configuration'}{'configuration file'}?" ) );
|
|
|
375 |
}
|
|
|
376 |
|
|
|
377 |
if ( ( -x $install{'configuration'}{'configurator'} ) && $install{'build config'} ) {
|
|
|
378 |
exec( $install{'configuration'}{'configurator'} );
|
|
|
379 |
}
|
|
|
380 |
}
|
34 |
rodolico |
381 |
|
35 |
rodolico |
382 |
sub help {
|
|
|
383 |
my $oses = join( ' ', keys %operatingSystems );
|
|
|
384 |
print <<END
|
|
|
385 |
$0 --verbose=x --os="osname" --dryrun --help --version
|
34 |
rodolico |
386 |
|
35 |
rodolico |
387 |
--os - osname is one of [$oses]
|
|
|
388 |
--dryrun - do not actually do anything, just tell you what I'd do
|
|
|
389 |
--verbose - x is 0 (normal) to 3 (horrible)
|
|
|
390 |
END
|
|
|
391 |
}
|
34 |
rodolico |
392 |
|
35 |
rodolico |
393 |
|
|
|
394 |
|
|
|
395 |
##########################################
|
|
|
396 |
# Main Loop
|
|
|
397 |
##########################################
|
|
|
398 |
|
|
|
399 |
# handle any command line parameters that may have been passed in
|
|
|
400 |
|
|
|
401 |
GetOptions (
|
|
|
402 |
"verbose|v=i" => \$verbose, # verbosity level, 0-9
|
|
|
403 |
"os|o=s" => \$os, # pass in the operating system
|
|
|
404 |
"dryrun|n" => \$dryRun, # do NOT actually do anything
|
|
|
405 |
'help|h' => \$help,
|
|
|
406 |
'version|V' => \$version
|
|
|
407 |
) or die "Error parsing command line\n";
|
|
|
408 |
|
|
|
409 |
if ( $help ) { &help() ; exit; }
|
|
|
410 |
if ( $version ) { print "$0 version $VERSION\n"; exit; }
|
|
|
411 |
&setDryRun( $dryRun ); # tell the library whether this is a dry run or not
|
|
|
412 |
|
33 |
rodolico |
413 |
# figure out if we know our operating system
|
35 |
rodolico |
414 |
$os = &getOS( \%install, \%operatingSystems, $os );
|
33 |
rodolico |
415 |
|
35 |
rodolico |
416 |
$installType = &getInstallActions( \%install );
|
34 |
rodolico |
417 |
|
33 |
rodolico |
418 |
# based on the defaults, flesh out the install hash
|
|
|
419 |
$status = &populateSourceDir( \%install, $sourceDir );
|
|
|
420 |
|
35 |
rodolico |
421 |
|
33 |
rodolico |
422 |
$status = &getVersions( \%install );
|
|
|
423 |
|
35 |
rodolico |
424 |
&showWork( \%install );
|
|
|
425 |
die unless &yesno( "Ready to run? Select No to abort." );
|
|
|
426 |
|
33 |
rodolico |
427 |
$status = &doInstall( \%install );
|
|
|
428 |
|
35 |
rodolico |
429 |
$status = &postInstall( \%install );
|
34 |
rodolico |
430 |
|
35 |
rodolico |
431 |
1;
|