Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

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