[BACK]Return to update_trango.pl CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Diff for /trango/Net-Telnet-Trango/scripts/update_trango.pl between version 1.12 and 1.33

version 1.12, 2005/12/29 18:41:17 version 1.33, 2007/02/07 22:07:35
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.11 2005/12/21 02:03:30 andrew Exp $  # $RedRiver: update_trango.pl,v 1.32 2007/02/07 19:25:05 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango hosts with a new firmware
 #  #
 # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>  # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
 ########################################################################  ########################################################################
 use strict;  # Copyright (C) 2005, 2006, 2007 by Andrew Fresh
 use warnings;  #
   # This program is free software; you can redistribute it and/or modify
 use Net::TFTP;  # it under the same terms as Perl itself.
 use YAML;  ########################################################################
 use lib '.';  use strict;
 use Net::Telnet::Trango;  use warnings;
   
 my $config_file = shift || 'update_trango.conf';  use YAML qw/ LoadFile /;
 my $max_tries = 3;  use Net::TFTP;
   use Net::Telnet::Trango;
   
   my $config_file = shift || 'update_trango.yaml';
 my $l = Mylogger->new( { log_prefix => 'UT' } );  my $max_tries = 3;
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
 $l->sp("Reading config file '$config_file'");  
 my $conf = read_conf($config_file);  $l->sp("Reading config file '$config_file'");
   my $conf = LoadFile($config_file);
 $l->sp("  Hardware Type: $conf->{'type'}");  
 $l->sp("  File Name:     $conf->{'file_name'}");  my $hosts;
 $l->sp("  File Size:     $conf->{'file_size'}");  if (@ARGV) {
 $l->sp("  File Checksum: $conf->{'file_cksum'}");      @{$hosts} = map { { name => $_, group => 'Trango-Client' } } @ARGV;
 $l->sp("  FW Version:    $conf->{'ver'}");  }
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  else {
 $l->sp("");      $hosts = parse_hosts( $conf->{hosts} );
   }
   
   #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   
   my $global_tries = $max_tries * 2;
   while ( $global_tries > 0 ) {
 foreach my $fox (@{ $conf->{'ips'} }) {      $global_tries--;
   $l->sp("Updating: $fox");      my $processed = 0;
   
   ## Connect and login.      foreach my $host ( @{$hosts} ) {
   my $t = new Net::Telnet::Trango ({  
     Host    => $fox,          if ( !exists $host->{retry} ) {
     Timeout => 5,              $host->{tries} = 0;
   });              $host->{retry} = 1;
           }
   $l->p("Connecting to $fox");  
   my ($type, $version) = $t->connect();          if ( $host->{tries} >= $max_tries ) {
               $host->{retry} = 0;
   unless (defined $type && defined $version) {          }
     $l->sp("Error connecting!");  
     next;          if ( $host->{retry} <= 0 ) {
   }              next;
           }
   if ($type ne $conf->{'type'}) {  
     $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");          $host->{tries}++;
     $t->close;          $processed++;
     next;  
   }          $l->sp("");
           $l->sp("Checking: $host->{name} (try $host->{tries})");
   if ($version eq $conf->{'ver'}) {          my $needs_reboot = 0;
     $l->sp("Already up to date with firmware version '$version'");  
     $t->close;          ## Connect and login.
     next;          my $t = new Net::Telnet::Trango(
   }              Timeout => 5,
               Errmode => 'return',
   $l->p("Logging in");          ) or die "Couldn't make new connection: $!";
   $t->login($conf->{'password'}) || die "Couldn't login: $!";          $l->p("Connecting to $host->{name}");
           unless ( $t->open( $host->{name} ) ) {
   $l->p("Sending commands");              $l->sp("Error connecting: $!");
   ## Send commands              next;
   #print Dump $t->ver();          }
   #print Dump $t->tftpd();  
   if ( upload($t, $conf->{'file_name'}) ) {          my $password = $host->{Telnet_Password} || $conf->{general}->{password};
     $l->p("Rebooting");  
     $t->reboot;          $l->p("Logging in");
   } else {          $t->login($password);
     $l->p("Exiting");          unless ($t->logged_in) {
     $t->exit;              $l->p('Failed!');
   }              $t->close;
                 next;
   $t->close;          }
   $l->sp("");  
 }          $l->sp("Getting sudb");
           my $sudb = $t->sudb_view;
 sub upload          if ($sudb) {
 {              foreach my $su (@{ $sudb }) {
   my $t    = shift;                  $l->p("Getting su info $su->{suid}");
   my $file = shift;                  my $su_info = $t->su_info( $su->{suid} );
                   if ($su_info->{ip}) {
   my $ver = $t->ver;                      if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
   $l->p("Current version '$ver->{'Firmware Version'}'");                          $l->p("Already have $su_info->{ip}");
                           next;
   if (                      }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&                      $l->sp("Adding host $su_info->{ip}");
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}                      my $new_host = {
   ) {                          password => $host->{password},
     $l->sp("Already updated!");                          name     => $su_info->{ip},
     return 1;                          remarks  => $su_info->{remarks},
   }                      };
                       push @{ $hosts }, $new_host;
   my $try = 0;                  } else {
   while (1) {                      $l->sp("Couldn't get su info for $su->{suid}");
     if ($try >= $max_tries) {                      $l->sp("ERR: " . $t->last_error);
       $l->sp("Couldn't update in $max_tries tries!");                  }
       return undef;              }
     }          }
     $try++;  
           foreach my $firmware_type ( 'Firmware', 'FPGA' ) {
     #sysinfo($self->{'_host'});  
                   if ( !exists $conf->{$firmware_type} ) {
     $l->p("Enabling TFTPd");                  $l->s("No configs for '$firmware_type'");
     $t->enable_tftpd || die "Couldn't enable tftpd";                  next;
               }
     $l->p("Uploading file ($file)");  
     # use tftp to push the file up              my $host_type = $t->host_type;
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');              if ( $firmware_type eq 'FPGA' ) {
                   $host_type =~ s/\s.*$//;
     $tftp->put($file, $file)              }
       or die "Error uploading: " . $tftp->error;  
               if ( !exists $conf->{$firmware_type}->{$host_type} ) {
     # waitfor some sort of output                  $l->sp("No '$firmware_type' config for type $host_type");
     # make sure it says 'Success.' otherwise error                  next;
     #print "LAST: " . $self->lastline;              }
     #my @lines = $self->getlines;  
     #print Dump \@lines;              if (   $firmware_type eq 'Firmware'
                       && $t->firmware_version eq
     $l->p("Checking upload ($conf->{'file_cksum'})");                  $conf->{$firmware_type}->{$host_type}->{ver} )
     my $results = $t->tftpd;              {
     # check the 'File Length' against ???                  $l->sp("Firmware already up to date");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {                  next;
       $l->sp(              }
         "File checksum '" . $results->{'File Checksum'} .  
         "does not match config file '" . $conf->{'file_cksum'} . "'!"              if ( !$t->logged_in ) {
       );                  $l->p("Logging in");
       next;                  $t->login($password);
     }                  unless ($t->logged_in) {
     $l->p("File checksum matches . . . ");                      $l->p('Failed!');
                       $t->close;
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {                      last;
       $l->sp(                  }
         "File length '" . $results->{'File Length'} .              }
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"  
       );              foreach my $k ( keys %{ $conf->{general} } ) {
       next;                  $conf->{$firmware_type}->{$host_type}->{$k} ||=
     }                    $conf->{general}->{$k};
     $l->p("File length matches . . . ");              }
               $conf->{$firmware_type}->{$host_type}->{firmware_type} ||=
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {                $firmware_type;
       $l->sp(              $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
         "File name '" . $results->{'File Name'} .  
         "' does not match config file '" . $conf->{'file_name'} . "'!"              $l->sp("$host_type $firmware_type");
       );              ## Send commands
       next;              my $rc = upload( $t, $conf->{$firmware_type}->{$host_type} );
     }              if ($rc) {
     $l->p("File name  matches . . . ");                  $l->sp("Successfull!");
                   $host->{retry}--;
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");                  $needs_reboot = 1;
     unless ($results = $t->updateflash(              }
       $ver->{'Firmware Checksum'}, $conf->{'cksum'}              elsif ( defined $rc ) {
     ) ) {                  $l->sp("Already up to date");
       $l->sp("Couldn't update flash: $!");                  $host->{retry}--;
       next;              }
     }              else {
                   $l->sp("Failed");
     unless (                  $t->bye;
       defined $results->{'Checksum'} &&                  next;
       $results->{'Checksum'} eq $conf->{'cksum'}              }
     ) {  
       $l->sp("Saved checksum does not match config file!");          }
       next;  
     }          if ($needs_reboot) {
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .              $l->sp("Rebooting $host->{name}");
           "matches ($conf->{'cksum'})");              $t->reboot;
               }
     $l->sp("Successfully updated!");          else {
     return 1;              $l->sp("Bye $host->{name}");
   }              $t->bye();
 }          }
       }
 sub read_conf  
 {      if ( !$processed ) {
   my $file = shift;          $l->sp("");
   my %conf;          $l->sp("Finished.  No more hosts.");
   my $in_ip_list = 0;          last;
   open my $fh, $file or die "Couldn't open file $file: $!";      }
   while (<$fh>) {  }
     chomp;  
     next if /^#/;  sub upload {
     next if /^$/;      my $t    = shift;
     if ($in_ip_list) {      my $conf = shift;
       s/\s+//g; # Whitespace is a no no  
       my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {  
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);      my $fw_type = $conf->{firmware_type};
       } else {  
         push @{ $conf{'ips'} }, $_;      my $ver = $t->ver;
       }  
     } else {      if (
       my ($key, $val) = split /\s+/, $_, 2;          !(
               $ver->{ $fw_type . ' Version' } && $ver->{ $fw_type . ' Checksum' }
       if (lc($key) eq 'ips') {          )
         $in_ip_list = 1;        )
         next;      {
       }          $l->sp("Error getting current version numbers");
           return;
       $key =~ s/^\s+//;      }
       $key =~ s/\s+$//;  
       $val =~ s/^\s+//;      if (   $ver->{ $fw_type . ' Version' } eq $conf->{'ver'}
       $val =~ s/\s+$//;          && $ver->{ $fw_type . ' Checksum' } eq $conf->{'cksum'} )
       {
       $conf{ lc($key) } = $val;          return 0;
     }      }
   }  
   close $fh;      $l->sp("Updating $fw_type");
       $l->sp("Config information:");
   #print Dump \%conf;      $l->sp("  Hardware Type: $conf->{'type'}");
   foreach (      $l->sp("  File Name:     $conf->{'file_name'}");
     'password',      $l->sp("  File Size:     $conf->{'file_size'}");
     'file_name', 'file_size', 'file_cksum',      $l->sp("  File Checksum: $conf->{'file_cksum'}");
     'ver', 'cksum', 'ips',      $l->sp("  Conf Version:  $conf->{'ver'}");
   ) {      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     die "No $_ specified in config file!"      $l->sp("  Conf Checksum: $conf->{'cksum'}");
       if (not exists $conf{$_});      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   }  
       my $try = 0;
   #print Dump \%conf;      while (1) {
   #exit;          if ( $try >= $max_tries ) {
   return \%conf;              $l->sp("Couldn't update in $max_tries tries!");
 }              return;
           }
 package Mylogger;          $try++;
   
 use Fcntl ':flock'; # import LOCK_* constants          $l->p("Enabling TFTPd");
 #use YAML;          unless ( $t->enable_tftpd ) {
 use constant LOG_PRINT => 128;              $l->sp("Couldn't enable tftpd");
 use constant LOG_SAVE  =>  64;              next;
           }
 DESTROY {  
   my $self = shift;          $l->p("Uploading file ($conf->{file_name})");
   if ($self->{'MYLOG'}) {  
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");          # use tftp to push the file up
     close $self->{'MYLOG'};          my $tftp = Net::TFTP->new( $t->Host, Mode => 'octet' );
   }  
 }          unless ( $tftp->put( $file, $file ) ) {
               $l->sp( "Error uploading: " . $tftp->error );
 sub new {              next;
   my $package = shift;          }
   my $self = shift || {};  
           $l->p("Checking upload ($conf->{'file_cksum'})");
   $self->{'base_path'}  ||= '.';          my $results = $t->tftpd;
   $self->{'log_path'}   ||= $self->{'base_path'};  
   $self->{'log_prefix'} ||= 'LOG';          # check the 'File Length' against ???
   $self->{'log_file'}   ||= GetLogName(          if (
     $self->{'log_prefix'},              !(
     $self->{'log_path'}                     $results->{'File Checksum'}
   );                  && $results->{'File Length'}
   bless $self, $package;                  && $results->{'File Name'}
 }              )
             )
 sub s          {
 {              $l->sp("Unable to get results of upload");
   my $self = shift;              next;
   my $m = shift;          }
   return $self->mylog($m, LOG_SAVE);          if ( $results->{'File Checksum'} ne $conf->{'file_cksum'} ) {
 }              $l->sp( "File checksum ("
                     . $results->{'File Checksum'}
 sub p                    . ") does not match config file ("
 {                    . $conf->{'file_cksum'}
   my $self = shift;                    . ")!" );
   my $m = shift;              next;
   return $self->mylog($m, LOG_PRINT);          }
 }          $l->p("File checksum matches . . . ");
   
 sub sp          if ( $results->{'File Length'} !~ /^$conf->{'file_size'} bytes/ ) {
 {              $l->sp( "File length ("
   my $self = shift;                    . $results->{'File Length'}
   my $m = shift;                    . ") does not match config file ("
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);                    . $conf->{'file_size'}
 }                    . " bytes)!" );
               next;
 sub mylog          }
 {          $l->p("File length matches . . . ");
   my $self = shift;  
           if ( uc( $results->{'File Name'} ) ne uc($file) ) {
   my $thing = shift;              $l->sp( "File name ("
   chomp $thing;                    . $results->{'File Name'}
                     . ") does not match config file ("
   my $which = shift;                    . $file
                     . ")!" );
   my $MYLOG;              next;
   if ($which & LOG_PRINT) {          }
     print $thing, "\n";          $l->p("File name matches . . . ");
   }  
           my $image_type = 'mainimage';
   if ($which & LOG_SAVE) {          if ( $fw_type eq 'FPGA' ) {
     if ($self->{'MYLOG'}) {              $image_type = 'fpgaimage';
       $MYLOG = $self->{'MYLOG'};          }
     } else {          $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
       unless ($MYLOG) {          unless (
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})              $results = $t->updateflash(
           or die "Couldn't open logfile!\n";                  args => $image_type . ' '
         my $ofh = select $MYLOG;                    . $ver->{ $fw_type . ' Checksum' } . ' '
         $|=1;                    . $conf->{'cksum'},
         select $ofh;                  Timeout => 90,
         $self->{'MYLOG'} = $MYLOG;              )
             )
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");          {
       }              $l->sp("Couldn't update flash: $!");
     }              next;
     flock($MYLOG, LOCK_EX);          }
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  
       or die "Couldn't print to MYLOG: $!";          unless ( defined $results->{'Checksum'}
     flock($MYLOG, LOCK_UN);              && $results->{'Checksum'} eq $conf->{'cksum'} )
   }          {
 }              $l->sp( "Saved checksum "
                     . $results->{'Checksum'}
 sub GetLogName                    . " does not match config file "
 {                    . $conf->{'cksum'}
   my $prefix  = shift || die "Invalid prefix passed for log";                    . "!" );
               next;
   my $logdate = GetLogDate();          }
   my $logver  = 0;  
   my $logname;          $l->p("$fw_type saved checksum matches . . . ");
   
   do {          return 1;
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';      }
     $logver++;  }
   } until (not -e $logname);  
   sub parse_hosts {
   return $logname;      my $src = shift;
 }  
       my @hosts;
 sub GetLogDate      foreach my $h ( @{$src} ) {
 {          if ( $h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/ )
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();          {
               for ( $2 .. $3 ) {
   $mon++;                  my %cur_host;
   $year += 1900;                  foreach my $k ( keys %{$h} ) {
                       $cur_host{$k} = $h->{$k};
   if ($min  < 10) { $min  = "0$min"  }                  }
   if ($sec  < 10) { $sec  = "0$sec"  }                  $cur_host{name} = $1 . $_;
   if ($hour < 10) { $hour = "0$hour" }                  if ( !grep { $cur_host{name} eq $h->{name} } @hosts ) {
   if ($mday < 10) { $mday = "0$mday" }                      push @hosts, \%cur_host;
   if ($mon  < 10) { $mon  = "0$mon"  }                  }
               }
   my $time = $year . $mon . $mday;          }
           else {
   return $time;              push @hosts, $h;
 }          }
       }
   
       return \@hosts;
   }
   
   package Mylogger;
   
   use Fcntl ':flock';    # import LOCK_* constants
   
   #use YAML;
   use constant LOG_PRINT => 128;
   use constant LOG_SAVE  => 64;
   
   DESTROY {
       my $self = shift;
       if ( $self->{'MYLOG'} ) {
           $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
           close $self->{'MYLOG'};
       }
   }
   
   sub new {
       my $package = shift;
       my $self = shift || {};
   
       $self->{'base_path'}  ||= '.';
       $self->{'log_path'}   ||= $self->{'base_path'};
       $self->{'log_prefix'} ||= 'LOG';
       $self->{'log_file'} ||=
         GetLogName( $self->{'log_prefix'}, $self->{'log_path'} );
       bless $self, $package;
   }
   
   sub s {
       my $self = shift;
       my $m    = shift;
       return $self->mylog( $m, LOG_SAVE );
   }
   
   sub p {
       my $self = shift;
       my $m    = shift;
       return $self->mylog( $m, LOG_PRINT );
   }
   
   sub sp {
       my $self = shift;
       my $m    = shift;
       return $self->mylog( $m, LOG_SAVE | LOG_PRINT );
   }
   
   sub mylog {
       my $self = shift;
   
       my $thing = shift;
       chomp $thing;
   
       my $which = shift;
   
       my $MYLOG;
       if ( $which & LOG_PRINT ) {
           print $thing, "\n";
       }
   
       if ( $which & LOG_SAVE ) {
           if ( $self->{'MYLOG'} ) {
               $MYLOG = $self->{'MYLOG'};
           }
           else {
               unless ($MYLOG) {
                   open( $MYLOG, '>>',
                       $self->{'log_path'} . '/' . $self->{'log_file'} )
                     or die "Couldn't open logfile!\n";
                   my $ofh = select $MYLOG;
                   $| = 1;
                   select $ofh;
                   $self->{'MYLOG'} = $MYLOG;
   
                   $self->p(
                       "Opened log ($self->{'log_path'}/$self->{'log_file'})");
               }
           }
           flock( $MYLOG, LOCK_EX );
           print $MYLOG ( scalar gmtime ), "\t", $thing, "\n"
             or die "Couldn't print to MYLOG: $!";
           flock( $MYLOG, LOCK_UN );
       }
   }
   
   sub GetLogName {
       my $prefix = shift || die "Invalid prefix passed for log";
   
       my $logdate = GetLogDate();
       my $logver  = 0;
       my $logname;
   
       do {
           $logname = $prefix . $logdate . sprintf( "%02d", $logver ) . '.log';
           $logver++;
       } until ( not -e $logname );
   
       return $logname;
   }
   
   sub GetLogDate {
       my ( $sec, $min, $hour, $mday, $mon, $year,,, ) = localtime();
   
       $mon++;
       $year += 1900;
   
       if ( $min  < 10 ) { $min  = "0$min"  }
       if ( $sec  < 10 ) { $sec  = "0$sec"  }
       if ( $hour < 10 ) { $hour = "0$hour" }
       if ( $mday < 10 ) { $mday = "0$mday" }
       if ( $mon  < 10 ) { $mon  = "0$mon"  }
   
       my $time = $year . $mon . $mday;
   
       return $time;
   }

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.33

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>