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
 
43 rodolico 17
our $VERSION = '1.1.7';
42 rodolico 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))',
43 rodolico 150
                  },
151
                  'freebsd' => {
152
                     'bindir' => '/usr/local/opt/camp/sysinfo-client',
153
                     'confdir' => '/usr/local/etc/camp/sysinfo-client',
154
                     'crontab' => 'ln -s <bindir>sysinfo-client /etc/periodic/daily/sysinfo-client',
155
                     'modules' => '((bsd)|(unix))',
156
                  },
157
 
158
                );
42 rodolico 159
 
160
# list of libraries used by the system. We will offer to install them if
161
# we know how. NOTE: I have chosen to put the full installation command
162
# for each library. This allows us to use the package selector OR a different
163
# piece of code on a per-library basis, but results in something like
164
#      apt-get -y install perl-modules
165
#      apt-get -y install libwww-perl
166
# instead of
167
#      apt-get -y install libwww-perl perl-modules
168
# flexibility vs efficiency in this case.
169
my %libraries = ( 
170
                  'File::Basename' => { 'debian' => 'apt-get -y install perl-modules' },
171
                  'Exporter' => { 'debian' => 'apt-get -y install perl-base' },
43 rodolico 172
                  'LWP' => { 'debian' => 'apt-get -y install libwww-perl',
173
                             'freebsd' => 'pkg install p5-libwww' },
42 rodolico 174
                );
175
 
43 rodolico 176
# utilities md5sum
177
# freebsd isomd5sum
178
 
42 rodolico 179
# validates the libraries needed exist
180
# simply eval's each library. If it doesn't exist, creates a list of
181
# commands to be executed to install it, and displays missing libraries
182
# offering to install them if possible.
183
sub validateLibraries {
184
   my ( $libraries, $os ) = @_;
185
   my @command;
186
   my @missingLibs;
187
   foreach my $lib ( keys %$libraries ) {
188
      eval( "use $lib;" );
189
      if ( $@ ) {
190
         push @command,$$libraries{$lib}{$os} if $$libraries{$lib}{$os};
191
         push @missingLibs, $lib;
192
      }
193
   }
194
   if ( @missingLibs ) { # we have missing libraries
195
      if ( @command ) {
196
         &runCommand( join( "\n", @command ) )
197
            if &yesno(
198
                        'Following libraries need to be installed: ' . 
199
                        join( ' ', @missingLibs ) . "\n" .
200
                        "I can install them with the following command(s)\n" . 
201
                        join( "\n", @command ) . "\nDo you want me to do this?\n"
202
                     );
203
      } else {
204
         die unless &yesno( 'Following libraries need to be installed: ' . 
205
                            join( ' ', @missingLibs ) . 
206
                            "\nand I don't know how to do this. Abort?" );
207
      }
208
   } # if
209
} # validateLibraries
33 rodolico 210
 
211
 
212
# attempt to locate the operating system.
213
# if found, will set some defaults for it.
214
sub getOS {
35 rodolico 215
   my ( $install, $operatingSystems, $os ) = @_;
216
   if ( ! $os ) { # we don't know, so we must try to figure it out
217
      my $osString = `uname -a`;
218
      foreach my $osType ( keys %$operatingSystems ) {
219
         print "Checking if OS is $osType in $osString\n" if $verbose > 2;
220
         next unless $osString =~ m/$osType/i;
221
         print "Yes, it is $osType\n" if $verbose > 2;
222
         # We found the OS, set up some defaults
223
         $os = $osType;
224
      } # foreach
225
   }
226
   if ( $os ) {
227
      $$install{'os'} = $os;
228
      print "Setting keys for operating system\n" if $verbose > 2;
42 rodolico 229
      foreach my $key ( keys %{$$operatingSystems{ $os }} ) {
35 rodolico 230
         $$install{$key} = $operatingSystems{ $os }{$key};
33 rodolico 231
      } # if it is a known OS
35 rodolico 232
   } # if
233
   return $os;
33 rodolico 234
} # getOperatingSystem
235
 
37 rodolico 236
 
34 rodolico 237
# get some input from the user and decide how to install/upgrade/remove/whatever
238
sub getInstallActions {
239
   my $install = shift;
240
   if ( ! &yesno( "This looks like a $$install{'os'} machine, correct?" ) ) {
241
      die "User Aborted\n" if &yesno( "If we continue, I will set this up like a $$install{'os'} system. Abort?" );
242
   }
243
   if ( -d $$install{'confdir'} ) {
244
      $$install{'action'} = &getAnswer( "It looks like $$install{'application name'} is already installed, what do you want to do?", 
245
                            ( "upgrade","remove", "overwrite" )
246
                          );
247
   } else {
248
      if ( &yesno( "This looks like a fresh install, correct?" ) ) {
249
         $$install{'action'} = 'install';
250
         $$install{'preseed config'} = &yesno( "Preseed the configuration file?" );
251
      } else {
252
         die "Can not continue at this time: Do not understand your system\n";
253
      }
254
   }
255
   $$install{'build config'} = &yesno( "Edit config file when done?" );
256
   $$install{'setup cron'} = &yesno( "Set up for automatic running via crontab?" );
257
}
33 rodolico 258
 
34 rodolico 259
sub showWork { 
260
   my $install = shift;
261
   print Dumper( \%install );
262
}
35 rodolico 263
 
264
 
265
sub doPlaceholderSubstitution {
266
   my ($hash, $placeholder) = @_;
267
   return if ref $hash ne 'HASH';
268
   foreach my $key ( keys %$hash ) {
269
      if ( ref( $$hash{$key} ) ) {
270
         &doPlaceholderSubstitution( $$hash{$key}, $placeholder );
271
      } else {
272
         foreach my $place ( keys %$placeholder ) {
273
            $$hash{$key} =~ s/$place/$$placeholder{$place}/;
274
         } # foreach
275
      } # if..else
276
   } # foreach
277
   return;
278
}
34 rodolico 279
 
280
# This will go through and first, see if anything is a directory, in
281
# which case, we'll create new entries for all files in there.
282
# then, it will do keyword substitution of <bindir> and <confdir>
283
# to populate the target.
284
# When this is done, each file should have a source and target that is
285
# a fully qualified path and filename
33 rodolico 286
sub populateSourceDir {
287
   my ( $install, $sourceDir ) = @_;
35 rodolico 288
   my %placeHolders = 
289
      ( 
290
        '<bindir>' => $$install{'bindir'},
291
        '<confdir>' => $$install{'confdir'}
292
      );
33 rodolico 293
 
294
   my $allFiles = $$install{'files'};
295
 
35 rodolico 296
   # find all directory entries and load files in that directory into $$install{'files'}
33 rodolico 297
   foreach my $dir ( keys %$allFiles ) {
298
      if ( defined( $$allFiles{$dir}{'type'} ) && $$allFiles{$dir}{'type'} eq 'directory' ) {
35 rodolico 299
         print "Found directory $dir\n" if $verbose > 2;
33 rodolico 300
         if ( opendir( my $dh, "$sourceDir/$dir" ) ) {
301
            my @files = map{ $dir . '/' . $_ } grep { ! /^\./ && -f "$sourceDir/$dir/$_" } readdir( $dh );
35 rodolico 302
            print "\tFound files " . join( ' ', @files ) . "\n" if $verbose > 2;
33 rodolico 303
            foreach my $file ( @files ) {
304
               $$allFiles{ $file }{'type'} = 'file';
37 rodolico 305
               if ( $dir eq 'modules' ) {
306
                  $$allFiles{ $file }{'permission'} = ( $file =~ m/$$install{'modules'}/ ) ? '0700' : '0600';
307
               } else {
308
                  $$allFiles{ $file }{'permission'} = $$allFiles{ $dir }{'permission'};
309
               }
33 rodolico 310
               $$allFiles{ $file }{'owner'} = $$allFiles{ $dir }{'owner'};
311
               $$allFiles{ $file }{'target'} = $$allFiles{ $dir }{'target'};
312
            } # foreach
313
            closedir $dh;
314
         } # if opendir
315
      } # if it is a directory
316
   } # foreach
35 rodolico 317
   # find all files, and set the source directory, and add the filename to
318
   # the target
33 rodolico 319
   foreach my $file ( keys %$allFiles ) {
320
      $$allFiles{$file}{'source'} = "$sourceDir/$file";
321
      $$allFiles{$file}{'target'} .= "/$file";
322
   } # foreach
35 rodolico 323
 
324
   # finally, do place holder substitution. This recursively replaces all keys
325
   # in  %placeHolders with the values.
326
   &doPlaceholderSubstitution( $install, \%placeHolders );
327
 
328
   print Dumper( $install ) if $verbose > 2;
329
 
33 rodolico 330
   return 1;
331
} # populateSourceDir
332
 
34 rodolico 333
# there is a file named VERSIONS. We get the values out of the install
334
# directory and (if it exists) the target so we can decide what needs
335
# to be updated.
33 rodolico 336
sub getVersions {
337
   my $install = shift;
338
   my $currentVersionFile = $$install{'files'}{'VERSION'}{'target'};
339
   my $newVersionFile = $$install{'files'}{'VERSION'}{'source'};
340
   if ( open FILE,"<$currentVersionFile" ) {
341
      while ( my $line = <FILE> ) {
342
         chomp $line;
343
         my ( $filename, $version, $checksum ) = split( "\t", $line );
344
         $$install{'files'}{$filename}{'installed version'} = $version ? $version : '';
345
      }
346
      close FILE;
347
   }
348
   if ( open FILE,"<$newVersionFile" ) {
349
      while ( my $line = <FILE> ) {
350
         chomp $line;
351
         my ( $filename, $version, $checksum ) = split( "\t", $line );
352
         $$install{'files'}{$filename}{'new version'} = $version ? $version : '';
353
      }
354
      close FILE;
355
   }
40 rodolico 356
   foreach my $file ( keys %{$$install{'files'}} ) {
357
      $$install{'files'}{$file}{'installed version'} = -2 unless defined $$install{'files'}{$file}{'installed version'};
358
      $$install{'files'}{$file}{'new version'} = -1 unless defined $$install{'files'}{$file}{'new version'};
359
   }
33 rodolico 360
   return 1;
361
} # getVersions
362
 
34 rodolico 363
# this actually does the installation, except for the configuration
33 rodolico 364
sub doInstall {
365
   my $install = shift;
366
   my $fileList = $$install{'files'};
367
 
368
   &checkDirectoryExists( $$install{'bindir'} . '/' );
369
   foreach my $file ( keys %$fileList ) {
370
      next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
40 rodolico 371
      next if $$install{'action'} eq 'upgrade' && ! defined( $$fileList{$file}{'installed version'} )
372
              ||
373
              ( $$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'} );
33 rodolico 374
      &checkDirectoryExists( $$fileList{$file}{'target'} );
375
      &runCommand( 
376
            "cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
377
            "chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
378
            "chown  $$fileList{$file}{'owner'} $$fileList{$file}{'target'}"
379
            );
380
   } # foreach file
381
   return 1;
382
}
383
 
35 rodolico 384
sub postInstall {
385
   my $install = shift;
37 rodolico 386
 
35 rodolico 387
   # set up crontab, if necessary
37 rodolico 388
   &runCommand( $$install{'crontab'} ) if defined ( $$install{'crontab'} );
389
 
390
   # seed configuration, if needed
391
   if ( $$install{'build config'} ) {
43 rodolico 392
      my $config;
393
      my @fileList;
37 rodolico 394
      my $seedFile = $$install{'configuration'}{'configuration seed file'};
395
      my $confFile = $$install{'configuration'}{'configuration file'};
396
 
397
      if ( -f $seedFile  && &yesno( 'Add installation seed file? ' ) ) {
43 rodolico 398
         push @fileList, $seedFile;
37 rodolico 399
      } # if preload seed file
400
 
35 rodolico 401
      if (  -f $confFile  ) {
43 rodolico 402
         push @fileList, $confFile;
35 rodolico 403
      }
43 rodolico 404
      $config = makeConfig( @fileList );
405
      my $content = &showConf( $config );
35 rodolico 406
      print $content;
37 rodolico 407
      print &writeConfig( $$install{'configuration'}{'configuration file'} , $content ) . "\n"
408
         if ( &yesno( "Write the above configuration to $$install{'configuration'}{'configuration file'}?" ) );
409
   } # if we are building/merging configuration
35 rodolico 410
 
411
}
34 rodolico 412
 
35 rodolico 413
sub help {
414
   my $oses = join( ' ', keys %operatingSystems );
415
   print <<END
416
$0 --verbose=x --os="osname" --dryrun --help --version
34 rodolico 417
 
35 rodolico 418
--os      - osname is one of [$oses]
419
--dryrun  - do not actually do anything, just tell you what I'd do
420
--verbose - x is 0 (normal) to 3 (horrible)
421
END
422
}
34 rodolico 423
 
35 rodolico 424
 
425
 
426
##########################################
427
# Main Loop
428
##########################################
429
 
430
# handle any command line parameters that may have been passed in
431
 
432
GetOptions (
433
            "verbose|v=i" => \$verbose, # verbosity level, 0-9
434
            "os|o=s"      => \$os,      # pass in the operating system
435
            "dryrun|n"    => \$dryRun,  # do NOT actually do anything
436
            'help|h'      => \$help,
437
            'version|V'   => \$version
438
            ) or die "Error parsing command line\n";
439
 
440
if ( $help ) { &help() ; exit; }
441
if ( $version ) { print "$0 version $VERSION\n"; exit; }
442
&setDryRun( $dryRun ); # tell the library whether this is a dry run or not
443
 
33 rodolico 444
# figure out if we know our operating system
35 rodolico 445
$os = &getOS( \%install, \%operatingSystems, $os );
33 rodolico 446
 
42 rodolico 447
&validateLibraries( \%libraries, $os );
448
 
35 rodolico 449
$installType = &getInstallActions( \%install );
34 rodolico 450
 
33 rodolico 451
# based on the defaults, flesh out the install hash
452
$status = &populateSourceDir( \%install, $sourceDir );
453
 
35 rodolico 454
 
33 rodolico 455
$status = &getVersions( \%install );
456
 
35 rodolico 457
&showWork( \%install );
458
die unless &yesno( "Ready to run? Select No to abort." );
459
 
33 rodolico 460
$status = &doInstall( \%install );
461
 
35 rodolico 462
$status = &postInstall( \%install );
34 rodolico 463
 
37 rodolico 464
if ( ( -x $install{'configuration'}{'configurator'} ) && $install{'build config'} ) {
465
   exec( $install{'configuration'}{'configurator'} );
466
} else {
467
   print "Done, you should check the files in $install{'bindir'} and $install{'confdir'} before running\n";
468
   print "If you need help configuring, the helper app at\n$install{'configuration'}{'configurator'}\ncan be used.\n";
469
}
470
 
35 rodolico 471
1;   
40 rodolico 472
 
473
 
474
# add uninstall, clean to install.pl
475
# clean will look for any file in bindir which is NOT in the list of available files and remove them
476
# if files already exist in install, preserve their permissions