[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.20

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

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