Subversion Repositories camp_sysinfo_client_3

Rev

Rev 8 | Rev 10 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/perl -w

my $TESTING = 1;

my %installationTypes = (
                           'ipfire' => {
                               'post process' => \&ipfire
                            }
                      );
                      
my %installation = (
                     '1. Configuration Directory' => '/etc/camp',
                     '2. Modules Directory' => 'modules',
                     '3. Scripts Directory' => 'scripts',
                     '4. Executable Directory' => '/usr/bin'
                   );

my %configuration;

my %packageActions = (
                  'modules' => {
                     'type'       => 'directory',
                     'target'     => '2. Modules Directory',
                     'owner'      => 'root:root',
                     'dir perms'  => '0755',
                     'file perms' => '0755'
                  },
                  'scripts' => {
                     'type'       => 'directory',
                     'target'     => '3. Scripts Directory',
                     'owner'      => 'root:root',
                     'dir perms'  => '0755',
                     'file perms' => '0755'
                  },
                  'notes' => {
                     'type'       => 'file',
                     'target'     => '1. Configuration Directory',
                     'owner'      => 'root:root',
                     'dir perms'  => '0755',
                     'file perms' => '0644'
                  },
                  'sysinfo_client' => {
                     'type'       => 'file',
                     'target'     => '4. Executable Directory',
                     'owner'      => 'root:root',
                     'dir perms'  => '0755',
                     'file perms' => '0755',
                     'cron'       => 1
                  },
                  'sysinfo.conf.template' => {
                     'type'       => 'file',
                     'target'     => '1. Configuration Directory',
                     'owner'      => 'root:root',
                     'dir perms'  => '0755',
                     'file perms' => '0644'
                  }
               );
my $installationType;
my $cronLocation; 


sub getInstallationType {
   my @OSTypes = ( 'ipfire','debian' );
   my $OS = `uname -a`;
   foreach $osType ( @OSTypes ) {
      return $osType if $OS =~ m/$osType/i;
   }
   return '';
} # getInstallationType

sub findCron {
   while ( $location = shift ) {
      return $location if -d $location;
   }
   return '';
}

sub customizeInstall {
   my ( $installation, $cronLocation ) = @_;
   # Allow user to customize the installation paths
   foreach my $answer ( sort keys %$installation ) {
      if ( $answer eq '2. Modules Directory' or $answer eq '3. Scripts Directory' ) {
         $$installation{$answer} = $$installation{'1. Configuration Directory'} . '/' . $$installation{$answer};
      }
      print "$answer [$$installation{$answer}]: ";
      my $temp = <>;
      chomp $temp;
      $$installation{$answer} = $temp if $temp;
   }
   print "Link executable into which location [$$cronLocation]: ";
   my $temp = <>;
   chomp $temp;
   $$cronLocation = $temp if $temp;
}   

sub makeDirectory {
   my ( $directory, $owner, $permissions ) = @_;
   return if -d $directory;
   # make_path is very good and efficient, if File is installed, so we try it
   eval { use File::Path 'make_path'; };
   if ( $@ ) {
      if ( $TESTING ) {
         print "Using OS to create $directory\n";
         print "mkdir -p $directory\n";
         print "chown $owner $directory\n";
         print "chmod $permissions $directory\n";
         return;
      }
      `mkdir -p $directory`;
      `chown $owner $directory`;
      `chmod $permissions $directory`;
      die "Could not create directory [$directory]\n" unless -d $directory;
   } else { # we could not load library, so do it via the OS
      if ( $TESTING ) {
         print "Using make_path to create $directory\n";
         print "make_path( $directory, { mode => $permissions, owner=>$owner, error => my $err } )\n";
         return;
      }
      make_path( $directory, { mode => $permissions, owner=>$owner, error => \my $err } );
      die "Could not create directory [$directory]\n" if @$err;
   }
}

sub doInstall {
   my ( $installation, $cronLocation, $packageActions ) = @_;
   foreach my $item ( keys %$packageActions ) {
      &makeDirectory( 
         $$installation{$$packageActions{$item}{'target'}}, 
         $$packageActions{$item}{'owner'}, 
         $$packageActions{$item}{'dir perms'} 
         );
      my $targetDir = $$installation{$$packageActions{$item}{'target'}};
      my $perms = $$packageActions{$item}{'file perms'};
      my $owner = $$packageActions{$item}{'owner'};
      $item .= '/*' if  $$packageActions{$item}{'type'} eq 'directory';
      if ( $TESTING ) {
         print "mv $item $targetDir\n";
         print "chmod $perms \"$targetDir/$item\"\n";
         print "chown $owner \"$targetDir/$item\"\n";
         # is this one supposed to be run as cron?
         if ( $$packageActions{$item}{'cron'} && $cronLocation ) {
            print "ln -s \"$targetDir/$item\" \"$cronLocation/$item\"\n";
         }
      } else {
         `mv $item $targetDir`;
         `chmod $perms "$targetDir/$item"`;
         `chown $owner "$targetDir/$item"`;
         # is this one supposed to be run as cron?
         if ( $$packageActions{$item}{'cron'} && $cronLocation ) {
            `ln -s "$targetDir/$item" "$cronLocation/$item"`;
         }
      }
   } # foreach
}

sub readConfiguration {
   my ( $configuration,$confFileName ) = @_;
}   

sub ipFire {
   my @BACKUP_DIRS = ('/opt/sysinfo', '/etc/sysinfo');
   my @INSTALL_COMMANDS = (
              'cp -av opt /',
              'cp -av etc /',
              'ln -s /opt/sysinfo/sysinfo /etc/fcron.daily/sysinfo.cron'
              );
   # now, check for backup directories not in include.user
   open BACKUP, '</var/ipfire/backup/include.user';
   while ( $line = <BACKUP> ) {
      chomp $line;
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
         if ( $BACKUP_DIRS[$i] eq $line ) {
            $BACKUP_DIRS[$i] = '';
            last;
         } # if
      }# for
   } # while
   close BACKUP;

   # if any remain, append them to include.user
   open BACKUP, '>>/var/ipfire/backup/include.user';
   foreach my $backupDir ( @BACKUP_DIRS ) {
      print BACKUP "$backupDir\n" if $backupDir;
   }
   close BACKUP;

   # set all modules with ipfire in the name to run
   opendir $moduleDir, $MODULES_DIR;
   my @modules = grep { /ipfire/ } readdir $moduleDir;
   closedir $moduleDir;
   foreach my $module (@modules) {
      `chmod 755 $MODULES_DIR/$module`;
   }
}

#####################################################################
# Main Program
#####################################################################

# if installationType or cronLocation not set, find them
$installationType = &getInstallationType() unless $installationType;
$cronLocation = &findCron( '/etc/cron.daily', '/etc/fcron.daily' ) unless $cronLocation;
# allow use to change installation paths
&customizeInstall( \%installation,\$cronLocation, \%packageActions );

print "Script will be run from: $cronLocation\n";
print "The operaitng system is: $installationType\n";
foreach my $answer ( sort keys %installation ) {
   print "$answer = $installation{$answer}\n";
}
print 'Continue? [Y/n]: ';
$answer = <>;
chomp $answer;
$answer = $answer ? uc $answer : 'Y';
exit unless $answer eq 'Y';
print "Continuing the installation\n";

&doInstall( \%installation, $cronLocation, \%packageActions );

1;