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