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