Subversion Repositories camp_sysinfo_client_3

Rev

Rev 33 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33 Rev 34
Line 24... Line 24...
24
# 3 - Be very verbose
24
# 3 - Be very verbose
25
my $TESTING = 1; # if test mode, simply show what would be done
25
my $TESTING = 1; # if test mode, simply show what would be done
26
 
26
 
27
my $status; # exit status of the processes
27
my $status; # exit status of the processes
28
my $sourceDir = File::Spec->catdir($FindBin::Bin);
28
my $sourceDir = File::Spec->catdir($FindBin::Bin);
29
 
-
 
-
 
29
my $installType;
30
 
30
 
31
my %install = (  'bindir' => '/opt/camp/sysinfo-client',
31
my %install = (  'bindir' => '/opt/camp/sysinfo-client',
32
                 'confdir' => '/etc/camp/sysinfo-client',
32
                 'confdir' => '/etc/camp/sysinfo-client',
-
 
33
                 'application name' => 'sysinfo client',
33
                 'files' => {
34
                 'files' => {
34
                           'configure.pl' => { 
35
                           'configure.pl' => { 
35
                                 'type' => 'file',
36
                                 'type' => 'file',
36
                                 'permission' => '700', 
37
                                 'permission' => '700', 
37
                                 'owner' => 'root:root', 
38
                                 'owner' => 'root:root', 
Line 157... Line 158...
157
                  
158
                  
158
 
159
 
159
# attempt to locate the operating system.
160
# attempt to locate the operating system.
160
# if found, will set some defaults for it.
161
# if found, will set some defaults for it.
161
sub getOS {
162
sub getOS {
162
   my ( $installs, $operatingSystems ) = @_;
163
   my ( $install, $operatingSystems ) = @_;
163
   my $osString = `uname -a`;
164
   my $osString = `uname -a`;
164
   foreach my $osType ( keys %$operatingSystems ) {
165
   foreach my $osType ( keys %$operatingSystems ) {
-
 
166
      print "Checking if OS is $osType in $osString\n" if $TESTING > 2;
165
      next unless $osString =~ m/$osType/i;
167
      next unless $osString =~ m/$osType/i;
-
 
168
      print "Yes, it is $osType\n" if $TESTING > 2;
166
      # We found the OS, set up some defaults
169
      # We found the OS, set up some defaults
167
      $$installs{'os'} = $osType;
170
      $$install{'os'} = $osType;
-
 
171
      print "Setting keys for operating system\n" if $TESTING > 2;
168
      for my $key ( keys $$operatingSystems{ $osType } ) {
172
      for my $key ( keys $$operatingSystems{ $osType } ) {
169
         $$installs{$key} = $operatingSystems{ $osType }{$key};
173
         $$install{$key} = $operatingSystems{ $osType }{$key};
170
      } # if it is a known OS
174
      } # if it is a known OS
171
      return 1;
175
      return 1;
172
   }
176
   }
173
   return 0;
177
   return 0;
174
} # getOperatingSystem
178
} # getOperatingSystem
175
 
179
 
-
 
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
   }
-
 
187
   if ( -d $$install{'confdir'} ) {
-
 
188
      $$install{'action'} = &getAnswer( "It looks like $$install{'application name'} is already installed, what do you want to do?", 
-
 
189
                            ( "upgrade","remove", "overwrite" )
-
 
190
                          );
-
 
191
   } else {
-
 
192
      if ( &yesno( "This looks like a fresh install, correct?" ) ) {
-
 
193
         $$install{'action'} = 'install';
-
 
194
         $$install{'preseed config'} = &yesno( "Preseed the configuration file?" );
-
 
195
      } else {
-
 
196
         die "Can not continue at this time: Do not understand your system\n";
-
 
197
      }
-
 
198
   }
-
 
199
   $$install{'build config'} = &yesno( "Edit config file when done?" );
-
 
200
   $$install{'setup cron'} = &yesno( "Set up for automatic running via crontab?" );
-
 
201
   &showWork( $install );
-
 
202
   die unless &yesno( "Ready to run? Select No to abort." );
-
 
203
}
176
 
204
 
-
 
205
sub showWork { 
-
 
206
   my $install = shift;
-
 
207
   print Dumper( \%install );
-
 
208
}
-
 
209
   
-
 
210
# This will go through and first, see if anything is a directory, in
-
 
211
# which case, we'll create new entries for all files in there.
-
 
212
# then, it will do keyword substitution of <bindir> and <confdir>
-
 
213
# to populate the target.
-
 
214
# When this is done, each file should have a source and target that is
-
 
215
# a fully qualified path and filename
177
sub populateSourceDir {
216
sub populateSourceDir {
178
   my ( $install, $sourceDir ) = @_;
217
   my ( $install, $sourceDir ) = @_;
179
   my $bindir = $$install{'bindir'};
218
   my $bindir = $$install{'bindir'};
180
   my $confdir = $$install{'confdir'};
219
   my $confdir = $$install{'confdir'};
181
 
220
 
Line 213... Line 252...
213
   } # foreach
252
   } # foreach
214
   
253
   
215
   return 1;
254
   return 1;
216
} # populateSourceDir
255
} # populateSourceDir
217
 
256
 
218
# find versions of all files so we can do an upgrade vs a full installation
257
# there is a file named VERSIONS. We get the values out of the install
-
 
258
# directory and (if it exists) the target so we can decide what needs
-
 
259
# to be updated.
219
sub getVersions {
260
sub getVersions {
220
   my $install = shift;
261
   my $install = shift;
221
   my $currentVersionFile = $$install{'files'}{'VERSION'}{'target'};
262
   my $currentVersionFile = $$install{'files'}{'VERSION'}{'target'};
222
   my $newVersionFile = $$install{'files'}{'VERSION'}{'source'};
263
   my $newVersionFile = $$install{'files'}{'VERSION'}{'source'};
223
   if ( open FILE,"<$currentVersionFile" ) {
264
   if ( open FILE,"<$currentVersionFile" ) {
Line 239... Line 280...
239
      close FILE;
280
      close FILE;
240
   }
281
   }
241
   return 1;
282
   return 1;
242
} # getVersions
283
} # getVersions
243
 
284
 
-
 
285
# runs a system command. Also, if in testing mode, simply shows what
-
 
286
# would have been done.
244
sub runCommand {
287
sub runCommand {
245
   while ( my $command = shift ) {
288
   while ( my $command = shift ) {
246
      if ( $TESTING ) {
289
      if ( $TESTING ) {
247
         print "$command\n";
290
         print "$command\n";
248
      } else {
291
      } else {
Line 250... Line 293...
250
      }
293
      }
251
   }
294
   }
252
   return 1;
295
   return 1;
253
} # runCommand
296
} # runCommand
254
 
297
 
-
 
298
# checks if a directory exists and, if not, creates it
255
my %directories; # holds list of directories already created
299
my %directories; # holds list of directories already created so no need to do an I/O
256
 
300
 
257
sub checkDirectoryExists {
301
sub checkDirectoryExists {
258
   my ( $filename,$mod,$owner ) = @_;
302
   my ( $filename,$mod,$owner ) = @_;
259
   $mod = "0700" unless $mod;
303
   $mod = "0700" unless $mod;
260
   $owner = "root:root" unless $owner;
304
   $owner = "root:root" unless $owner;
Line 270... Line 314...
270
      $directories{$dirname} = 1;
314
      $directories{$dirname} = 1;
271
   }
315
   }
272
   return '';   
316
   return '';   
273
}
317
}
274
   
318
   
275
 
-
 
-
 
319
# this actually does the installation, except for the configuration
276
sub doInstall {
320
sub doInstall {
277
   my $install = shift;
321
   my $install = shift;
278
   my $fileList = $$install{'files'};
322
   my $fileList = $$install{'files'};
279
   
323
   
280
   &checkDirectoryExists( $$install{'bindir'} . '/' );
324
   &checkDirectoryExists( $$install{'bindir'} . '/' );
281
   foreach my $file ( keys %$fileList ) {
325
   foreach my $file ( keys %$fileList ) {
282
      next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
326
      next unless ( $$fileList{$file}{'type'} && $$fileList{$file}{'type'} eq 'file' );
-
 
327
      next if $$install{'action'} eq 'upgrade' &&
283
      next if $$fileList{$file}{'installed version'} &&
328
              $$fileList{$file}{'installed version'} &&
284
              $$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'};
329
              $$fileList{$file}{'new version'} eq $$fileList{$file}{'installed version'};
285
      &checkDirectoryExists( $$fileList{$file}{'target'} );
330
      &checkDirectoryExists( $$fileList{$file}{'target'} );
286
      &runCommand( 
331
      &runCommand( 
287
            "cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
332
            "cp $$fileList{$file}{'source'} $$fileList{$file}{'target'}",
288
            "chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
333
            "chmod $$fileList{$file}{'permission'} $$fileList{$file}{'target'}",
Line 290... Line 335...
290
            );
335
            );
291
   } # foreach file
336
   } # foreach file
292
   return 1;
337
   return 1;
293
}
338
}
294
 
339
 
-
 
340
# parse the flags to decide the actions:
-
 
341
# install - assumes is a new install, overwrites everything
-
 
342
# upgrade - Checks for new files and overwrites only them
-
 
343
# uninstall - Removes everything
-
 
344
# reinstall - overwrites everything with fresh copy
-
 
345
 
-
 
346
################
-
 
347
# check for the configuration file!!!
-
 
348
################
-
 
349
 
-
 
350
 
-
 
351
 
-
 
352
 
295
# figure out if we know our operating system
353
# figure out if we know our operating system
296
$status = &getOS( \%install, \%operatingSystems );
354
$status = &getOS( \%install, \%operatingSystems );
297
 
355
 
-
 
356
$installType = getInstallActions( \%install );
-
 
357
 
298
# based on the defaults, flesh out the install hash
358
# based on the defaults, flesh out the install hash
299
$status = &populateSourceDir( \%install, $sourceDir );
359
$status = &populateSourceDir( \%install, $sourceDir );
300
 
360
 
301
$status = &getVersions( \%install );
361
$status = &getVersions( \%install );
302
 
362
 
303
$status = &doInstall( \%install );
363
$status = &doInstall( \%install );
304
 
364
 
-
 
365
 
305
print Dumper( \%install ) if $TESTING > 2;
366
print Dumper( \%install ) if $TESTING > 2;