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