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

Annotation of trango/Net-Telnet-Trango/scripts/update_trango.pl, Revision 1.23

1.16      mike        1: #!/usr/bin/perl
1.23    ! andrew      2: # $RedRiver: update_trango.pl,v 1.22 2007/02/01 17:58:33 andrew Exp $
1.16      mike        3: ########################################################################
                      4: # update_trango.pl *** Updates trango foxes with a new firmware
                      5: #
                      6: # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
                      7: ########################################################################
                      8: use strict;
                      9: use warnings;
                     10:
1.23    ! andrew     11: use YAML qw/ LoadFile /;
1.16      mike       12: use Net::TFTP;
                     13: use lib '.';
                     14: use Net::Telnet::Trango;
                     15:
1.22      andrew     16: my $config_file = shift || 'update_trango.yaml';
1.16      mike       17: my $max_tries = 3;
                     18:
                     19: my $l = Mylogger->new( { log_prefix => 'UT' } );
                     20:
                     21: $l->sp("Reading config file '$config_file'");
1.22      andrew     22: my $conf = LoadFile($config_file);
1.16      mike       23:
1.22      andrew     24: my @foxes = (
                     25: '10.100.1.2',
                     26: '10.100.2.2',
                     27: '10.100.3.2',
                     28: '10.100.4.2',
                     29: );
                     30:
                     31: foreach my $fox (@foxes) {
                     32:   $l->sp("Checking: $fox");
                     33:   my $needs_reboot = 0;
1.16      mike       34:
                     35:   ## Connect and login.
                     36:   my $t = new Net::Telnet::Trango (
                     37:     Timeout => 5,
                     38:     Errmode => 'return',
                     39:   ) or die "Couldn't make new connection: $!";
                     40:   $l->p("Connecting to $fox");
                     41:   unless ( $t->open($fox) ) {
                     42:     $l->sp("Error connecting: $!");
                     43:     next;
                     44:   }
                     45:
1.22      andrew     46:   foreach my $firmware_type ('Firmware', 'FPGA') {
1.21      mike       47:
1.22      andrew     48:     if (! exists $conf->{$firmware_type}) {
                     49:          $l->s("No configs for '$firmware_type'");
                     50:          $t->close();
                     51:          next;
                     52:        }
                     53:
                     54:     my $host_type = $t->host_type;
1.23    ! andrew     55:     if ($firmware_type eq 'FPGA') {
1.22      andrew     56:       $host_type =~ s/\s.*$//;
                     57:     }
                     58:
                     59:     if (! exists $conf->{$firmware_type}->{$host_type}) {
                     60:          $l->sp("No '$firmware_type' config for type $host_type");
                     61:          $t->close();
                     62:          next;
                     63:        }
                     64:
1.23    ! andrew     65:     if (! $t->logged_in) {
1.22      andrew     66:       $l->p("Logging in");
1.23    ! andrew     67:       $t->login($conf->{general}->{'password'});
        !            68:       unless ($t->logged_in) {
        !            69:         $l->p('Failed!');
        !            70:         $t->bye();
        !            71:         next;
        !            72:       }
1.22      andrew     73:     }
                     74:
                     75:        foreach my $k (keys %{ $conf->{general} }) {
                     76:          $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
                     77:     }
                     78:        $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
                     79:        $conf->{$firmware_type}->{$host_type}->{type} ||= $host_type;
                     80:
1.23    ! andrew     81:     $l->sp("$host_type $firmware_type");
1.22      andrew     82:     $l->p("Sending commands");
                     83:     ## Send commands
1.23    ! andrew     84:     my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
        !            85:        if ($rc) {
1.22      andrew     86:       $l->sp("Successfull!");
                     87:          $needs_reboot = 1;
1.23    ! andrew     88:     } elsif (defined $rc) {
        !            89:          $l->sp("Already up to date");
1.22      andrew     90:     } else {
                     91:          $l->sp("Failed");
1.23    ! andrew     92:          $t->bye;
        !            93:          next;
1.22      andrew     94:     }
                     95:
1.21      mike       96:   }
                     97:
1.22      andrew     98:   if ($needs_reboot) {
                     99:     $l->sp("Rebooting $fox");
1.17      mike      100:     $t->reboot;
                    101:   } else {
1.23    ! andrew    102:     $l->sp("Bye $fox");
        !           103:     $t->bye();
1.17      mike      104:   }
1.23    ! andrew    105:   $l->sp("");
1.16      mike      106: }
                    107:
                    108: sub upload
                    109: {
                    110:   my $t    = shift;
1.22      andrew    111:   my $conf = shift;
1.16      mike      112:
1.22      andrew    113:   my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
                    114:
                    115:   my $fw_type = $conf->{firmware_type};
1.19      andrew    116:
1.16      mike      117:   my $ver = $t->ver;
                    118:
                    119:   if (
1.19      andrew    120:     $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
                    121:     $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
1.16      mike      122:   ) {
1.21      mike      123:     return 0;
1.16      mike      124:   }
                    125:
1.23    ! andrew    126:   $l->sp("Config information:");
        !           127:   $l->sp("  Hardware Type: $conf->{'type'}");
        !           128:   $l->sp("  File Name:     $conf->{'file_name'}");
        !           129:   $l->sp("  File Size:     $conf->{'file_size'}");
        !           130:   $l->sp("  File Checksum: $conf->{'file_cksum'}");
        !           131:   $l->sp("  Conf Version:  $conf->{'ver'}");
        !           132:   $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
        !           133:   $l->sp("  Conf Checksum: $conf->{'cksum'}");
        !           134:   $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
        !           135:
        !           136:   $l->sp("Updating");
1.16      mike      137:   my $try = 0;
                    138:   while (1) {
                    139:     if ($try >= $max_tries) {
                    140:       $l->sp("Couldn't update in $max_tries tries!");
1.23    ! andrew    141:       return;
1.16      mike      142:     }
                    143:     $try++;
                    144:
                    145:     #sysinfo($self->{'_host'});
                    146:
                    147:     $l->p("Enabling TFTPd");
1.22      andrew    148:     unless ($t->enable_tftpd) {
                    149:       $l->sp("Couldn't enable tftpd");
                    150:       next;
                    151:     }
1.16      mike      152:
                    153:     $l->p("Uploading file ($file)");
                    154:     # use tftp to push the file up
                    155:     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
                    156:
1.21      mike      157:     unless ($tftp->put($file, $file)) {
1.22      andrew    158:       $l->sp("Error uploading: " . $tftp->error);
1.21      mike      159:       next;
                    160:     }
1.16      mike      161:
                    162:     $l->p("Checking upload ($conf->{'file_cksum'})");
                    163:     my $results = $t->tftpd;
                    164:     # check the 'File Length' against ???
                    165:     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
                    166:       $l->sp(
                    167:         "File checksum '" . $results->{'File Checksum'} .
1.20      mike      168:         " does not match config file '" . $conf->{'file_cksum'} . "'!"
1.16      mike      169:       );
                    170:       next;
                    171:     }
                    172:     $l->p("File checksum matches . . . ");
                    173:
                    174:     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
                    175:       $l->sp(
                    176:         "File length '" . $results->{'File Length'} .
                    177:         "does not match config file '" . $conf->{'file_size'} . " bytes'!"
                    178:       );
                    179:       next;
                    180:     }
                    181:     $l->p("File length matches . . . ");
                    182:
                    183:     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
                    184:       $l->sp(
                    185:         "File name '" . $results->{'File Name'} .
                    186:         "' does not match config file '" . $conf->{'file_name'} . "'!"
                    187:       );
                    188:       next;
                    189:     }
                    190:     $l->p("File name  matches . . . ");
                    191:
1.19      andrew    192:     my $image_type = 'mainimage';
                    193:     if ($fw_type eq 'FPGA') {
                    194:       $image_type = 'fpgaimage';
                    195:     }
                    196:     $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
1.16      mike      197:     unless ($results = $t->updateflash(
1.19      andrew    198:       args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
1.16      mike      199:               ' '          . $conf->{'cksum'},
                    200:       Timeout => 90,
                    201:     ) ) {
                    202:       $l->sp("Couldn't update flash: $!");
                    203:       next;
                    204:     }
                    205:
                    206:     unless (
                    207:       defined $results->{'Checksum'} &&
                    208:       $results->{'Checksum'} eq $conf->{'cksum'}
                    209:     ) {
1.17      mike      210:       $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
1.16      mike      211:       next;
                    212:     }
                    213:     $l->p("Uploaded checksum ($results->{'Checksum'}) " .
                    214:           "matches ($conf->{'cksum'})");
                    215:
                    216:     $l->sp("Successfully updated!");
                    217:     return 1;
                    218:   }
                    219: }
                    220:
                    221: package Mylogger;
                    222:
                    223: use Fcntl ':flock'; # import LOCK_* constants
                    224: #use YAML;
                    225: use constant LOG_PRINT => 128;
                    226: use constant LOG_SAVE  =>  64;
                    227:
                    228: DESTROY {
                    229:   my $self = shift;
                    230:   if ($self->{'MYLOG'}) {
                    231:     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
                    232:     close $self->{'MYLOG'};
                    233:   }
                    234: }
                    235:
                    236: sub new {
                    237:   my $package = shift;
                    238:   my $self = shift || {};
                    239:
                    240:   $self->{'base_path'}  ||= '.';
                    241:   $self->{'log_path'}   ||= $self->{'base_path'};
                    242:   $self->{'log_prefix'} ||= 'LOG';
                    243:   $self->{'log_file'}   ||= GetLogName(
                    244:     $self->{'log_prefix'},
                    245:     $self->{'log_path'}
                    246:   );
                    247:   bless $self, $package;
                    248: }
                    249:
                    250: sub s
                    251: {
                    252:   my $self = shift;
                    253:   my $m = shift;
                    254:   return $self->mylog($m, LOG_SAVE);
                    255: }
                    256:
                    257: sub p
                    258: {
                    259:   my $self = shift;
                    260:   my $m = shift;
                    261:   return $self->mylog($m, LOG_PRINT);
                    262: }
                    263:
                    264: sub sp
                    265: {
                    266:   my $self = shift;
                    267:   my $m = shift;
                    268:   return $self->mylog($m, LOG_SAVE | LOG_PRINT);
                    269: }
                    270:
                    271: sub mylog
                    272: {
                    273:   my $self = shift;
                    274:
                    275:   my $thing = shift;
                    276:   chomp $thing;
                    277:
                    278:   my $which = shift;
                    279:
                    280:   my $MYLOG;
                    281:   if ($which & LOG_PRINT) {
                    282:     print $thing, "\n";
                    283:   }
                    284:
                    285:   if ($which & LOG_SAVE) {
                    286:     if ($self->{'MYLOG'}) {
                    287:       $MYLOG = $self->{'MYLOG'};
                    288:     } else {
                    289:       unless ($MYLOG) {
                    290:                open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
                    291:           or die "Couldn't open logfile!\n";
                    292:         my $ofh = select $MYLOG;
                    293:         $|=1;
                    294:         select $ofh;
                    295:         $self->{'MYLOG'} = $MYLOG;
                    296:
                    297:         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
                    298:       }
                    299:     }
                    300:     flock($MYLOG, LOCK_EX);
                    301:     print $MYLOG (scalar gmtime), "\t", $thing, "\n"
                    302:       or die "Couldn't print to MYLOG: $!";
                    303:     flock($MYLOG, LOCK_UN);
                    304:   }
                    305: }
                    306:
                    307: sub GetLogName
                    308: {
                    309:   my $prefix  = shift || die "Invalid prefix passed for log";
                    310:
                    311:   my $logdate = GetLogDate();
                    312:   my $logver  = 0;
                    313:   my $logname;
                    314:
                    315:   do {
                    316:     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
                    317:     $logver++;
                    318:   } until (not -e $logname);
                    319:
                    320:   return $logname;
                    321: }
                    322:
                    323: sub GetLogDate
                    324: {
                    325:   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
                    326:
                    327:   $mon++;
                    328:   $year += 1900;
                    329:
                    330:   if ($min  < 10) { $min  = "0$min"  }
                    331:   if ($sec  < 10) { $sec  = "0$sec"  }
                    332:   if ($hour < 10) { $hour = "0$hour" }
                    333:   if ($mday < 10) { $mday = "0$mday" }
                    334:   if ($mon  < 10) { $mon  = "0$mon"  }
                    335:
                    336:   my $time = $year . $mon . $mday;
                    337:
                    338:   return $time;
                    339: }

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