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