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
                           '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
   }
287
   return 1;
288
} # getVersions
289
 
34 rodolico 290
# this actually does the installation, except for the configuration
33 rodolico 291
sub doInstall {
292
   my $install = shift;
293
   my $fileList = $$install{'files'};
294
 
295
   &checkDirectoryExists( $$install{'bindir'} . '/' );
296
   foreach my $file ( keys %$fileList ) {
297
      next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
34 rodolico 298
      next if $$install{'action'} eq 'upgrade' &&
299
              $$fileList{$file}{'installed version'} &&
33 rodolico 300
              $$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'};
301
      &checkDirectoryExists( $$fileList{$file}{'target'} );
302
      &runCommand( 
303
            "cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
304
            "chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
305
            "chown  $$fileList{$file}{'owner'} $$fileList{$file}{'target'}"
306
            );
307
   } # foreach file
308
   return 1;
309
}
310
 
35 rodolico 311
sub postInstall {
312
   my $install = shift;
37 rodolico 313
 
35 rodolico 314
   # set up crontab, if necessary
37 rodolico 315
   &runCommand( $$install{'crontab'} ) if defined ( $$install{'crontab'} );
316
 
317
   # seed configuration, if needed
318
   if ( $$install{'build config'} ) {
319
      my %config;
320
      my $seedFile = $$install{'configuration'}{'configuration seed file'};
321
      my $confFile = $$install{'configuration'}{'configuration file'};
322
      my $content;
323
 
35 rodolico 324
      my $clientName;
325
      my $serialNumber;
326
      my $hostname;
327
      my @moduleDirs;
328
      my @scriptDirs;
329
      my $transports = {};
34 rodolico 330
 
37 rodolico 331
 
332
      if ( -f $seedFile  && &yesno( 'Add installation seed file? ' ) ) {
333
         print "Loading seed file $seedFile\n";
334
         open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
335
         $content = join( '', <SEED> );
336
         close SEED;
337
         # now, eval the information we just read.
338
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
339
         no strict "vars"; eval( $content ); use strict "vars"; die "Error during eval: $@\n" if $@;
340
      } # if preload seed file
341
 
35 rodolico 342
      if (  -f $confFile  ) {
343
         print "Loading configuration file $confFile\n";
344
         open SEED, "<$confFile" or die "Could not open $confFile: $!\n";
345
         $content = join( '', <SEED> );
346
         close SEED;
347
         # now, eval the information we just read.
348
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
349
         no strict "vars"; eval( $content ); use strict "vars"; die "Error during eval: $@\n" if $@;
350
      }
351
      $config{'clientName'} = $clientName;
352
      $config{'serialNumber'} = $serialNumber;
353
      $config{'hostname'} = $hostname;
354
      $config{'moduleDirs'} = [ @moduleDirs ];
355
      $config{'scriptDirs'} = [ @scriptDirs ];
356
      $config{'transports'} = $transports;
34 rodolico 357
 
35 rodolico 358
      $content = &showConf( \%config );
359
      print $content;
37 rodolico 360
      print &writeConfig( $$install{'configuration'}{'configuration file'} , $content ) . "\n"
361
         if ( &yesno( "Write the above configuration to $$install{'configuration'}{'configuration file'}?" ) );
362
   } # if we are building/merging configuration
35 rodolico 363
 
364
}
34 rodolico 365
 
35 rodolico 366
sub help {
367
   my $oses = join( ' ', keys %operatingSystems );
368
   print <<END
369
$0 --verbose=x --os="osname" --dryrun --help --version
34 rodolico 370
 
35 rodolico 371
--os      - osname is one of [$oses]
372
--dryrun  - do not actually do anything, just tell you what I'd do
373
--verbose - x is 0 (normal) to 3 (horrible)
374
END
375
}
34 rodolico 376
 
35 rodolico 377
 
378
 
379
##########################################
380
# Main Loop
381
##########################################
382
 
383
# handle any command line parameters that may have been passed in
384
 
385
GetOptions (
386
            "verbose|v=i" => \$verbose, # verbosity level, 0-9
387
            "os|o=s"      => \$os,      # pass in the operating system
388
            "dryrun|n"    => \$dryRun,  # do NOT actually do anything
389
            'help|h'      => \$help,
390
            'version|V'   => \$version
391
            ) or die "Error parsing command line\n";
392
 
393
if ( $help ) { &help() ; exit; }
394
if ( $version ) { print "$0 version $VERSION\n"; exit; }
395
&setDryRun( $dryRun ); # tell the library whether this is a dry run or not
396
 
33 rodolico 397
# figure out if we know our operating system
35 rodolico 398
$os = &getOS( \%install, \%operatingSystems, $os );
33 rodolico 399
 
35 rodolico 400
$installType = &getInstallActions( \%install );
34 rodolico 401
 
33 rodolico 402
# based on the defaults, flesh out the install hash
403
$status = &populateSourceDir( \%install, $sourceDir );
404
 
35 rodolico 405
 
33 rodolico 406
$status = &getVersions( \%install );
407
 
35 rodolico 408
&showWork( \%install );
409
die unless &yesno( "Ready to run? Select No to abort." );
410
 
33 rodolico 411
$status = &doInstall( \%install );
412
 
35 rodolico 413
$status = &postInstall( \%install );
34 rodolico 414
 
37 rodolico 415
if ( ( -x $install{'configuration'}{'configurator'} ) && $install{'build config'} ) {
416
   exec( $install{'configuration'}{'configurator'} );
417
} else {
418
   print "Done, you should check the files in $install{'bindir'} and $install{'confdir'} before running\n";
419
   print "If you need help configuring, the helper app at\n$install{'configuration'}{'configurator'}\ncan be used.\n";
420
}
421
 
35 rodolico 422
1;