Annotation of trango/Net-Telnet-Trango/scripts/update_trango.pl, Revision 1.27
1.16 mike 1: #!/usr/bin/perl
1.26 andrew 2: # $RedRiver: update_trango.pl,v 1.25 2007/02/02 17:50:09 andrew Exp $
1.16 mike 3: ########################################################################
1.25 andrew 4: # update_trango.pl *** Updates trango hosts with a new firmware
1.16 mike 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;
1.25 andrew 15: use RedRiver::Wireless;
1.16 mike 16:
1.22 andrew 17: my $config_file = shift || 'update_trango.yaml';
1.16 mike 18: my $max_tries = 3;
19:
20: my $l = Mylogger->new( { log_prefix => 'UT' } );
21:
22: $l->sp("Reading config file '$config_file'");
1.22 andrew 23: my $conf = LoadFile($config_file);
1.16 mike 24:
1.27 ! andrew 25: my $hosts;
! 26: if (@ARGV) {
! 27: @{ $hosts } = map { { name => $_, group => 'Trango-Client' } } @ARGV
! 28: } else {
! 29: $l->sp("Reading hosts");
! 30: $hosts = RedRiver::Wireless::Read_Hosts();
! 31: }
! 32:
! 33: #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
1.22 andrew 34:
1.27 ! andrew 35: my $global_tries = $max_tries * 2;
! 36: while ($global_tries > 0) {
! 37: $global_tries--;
! 38: my $processed = 0;
1.25 andrew 39:
40:
41: foreach my $host (@{ $hosts }) {
42: next if $host->{group} !~ /^Trango/;
1.27 ! andrew 43:
! 44: if (! exists $host->{retry}) {
! 45: $host->{tries} = 0;
! 46: $host->{retry} = 1;
! 47: }
! 48:
! 49: if ($host->{tries} >= $max_tries) {
! 50: $host->{retry} = 0;
! 51: }
! 52:
! 53: if ($host->{retry} <= 0) {
! 54: next;
! 55: }
! 56:
! 57: $host->{tries}++;
! 58: $processed++;
! 59:
1.25 andrew 60: $l->sp("");
1.27 ! andrew 61: $l->sp("Checking: $host->{name} (try $host->{tries})");
1.22 andrew 62: my $needs_reboot = 0;
1.16 mike 63:
64: ## Connect and login.
65: my $t = new Net::Telnet::Trango (
66: Timeout => 5,
67: Errmode => 'return',
68: ) or die "Couldn't make new connection: $!";
1.25 andrew 69: $l->p("Connecting to $host->{name}");
70: unless ( $t->open($host->{name}) ) {
1.16 mike 71: $l->sp("Error connecting: $!");
72: next;
73: }
74:
1.25 andrew 75: my $password = $host->{Telnet_Password} || $conf->{general}->{password};
76:
1.27 ! andrew 77: # XXX I am not sure this is the best way to check if we should look for
! 78: # XXX associated stations.
1.25 andrew 79: if ($host->{group} !~ /Client$/) {
80: $l->p("Logging in");
81: $t->login($password);
82: unless ($t->logged_in) {
83: $l->p('Failed!');
84: $t->close;
85: next;
86: }
87:
88: $l->sp("Getting sudb");
89: my $sudb = $t->sudb_view;
90: if ($sudb) {
91: foreach my $su (@{ $sudb }) {
92: $l->p("Getting su info $su->{suid}");
93: my $su_info = $t->su_info( $su->{suid} );
94: if ($su_info->{ip}) {
1.27 ! andrew 95: if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
! 96: $l->p("Already have $su_info->{ip}");
! 97: next;
! 98: }
1.25 andrew 99: $l->sp("Adding host $su_info->{ip}");
100: my $new_host = {
101: Telnet_Password => $host->{Telnet_Password},
102: group => $host->{group} . '-Client',
103: name => $su_info->{ip},
104: remarks => $su_info->{remarks},
105: };
106: push @{ $hosts }, $new_host;
107: } else {
108: $l->sp("Couldn't get su info for $su->{suid}");
109: if ($su_info->{ERR}) {
110: $l->sp("ERR: $su_info->{ERR}");
111: }
112: next;
113: }
114: }
115: }
116: }
117:
1.22 andrew 118: foreach my $firmware_type ('Firmware', 'FPGA') {
1.21 mike 119:
1.22 andrew 120: if (! exists $conf->{$firmware_type}) {
1.27 ! andrew 121: $l->s("No configs for '$firmware_type'");
! 122: $t->close;
! 123: next;
! 124: }
1.22 andrew 125:
126: my $host_type = $t->host_type;
1.23 andrew 127: if ($firmware_type eq 'FPGA') {
1.22 andrew 128: $host_type =~ s/\s.*$//;
129: }
130:
131: if (! exists $conf->{$firmware_type}->{$host_type}) {
1.27 ! andrew 132: $l->sp("No '$firmware_type' config for type $host_type");
! 133: $t->close;
! 134: next;
! 135: }
1.22 andrew 136:
1.23 andrew 137: if (! $t->logged_in) {
1.22 andrew 138: $l->p("Logging in");
1.25 andrew 139: $t->login($password);
1.23 andrew 140: unless ($t->logged_in) {
141: $l->p('Failed!');
1.25 andrew 142: $t->close;
1.23 andrew 143: next;
144: }
1.22 andrew 145: }
146:
1.27 ! andrew 147: foreach my $k (keys %{ $conf->{general} }) {
! 148: $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
1.22 andrew 149: }
1.27 ! andrew 150: $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
! 151: $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
1.22 andrew 152:
1.23 andrew 153: $l->sp("$host_type $firmware_type");
1.22 andrew 154: ## Send commands
1.23 andrew 155: my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
1.27 ! andrew 156: if ($rc) {
1.22 andrew 157: $l->sp("Successfull!");
1.27 ! andrew 158: $host->{retry}--;
! 159: $needs_reboot = 1;
1.23 andrew 160: } elsif (defined $rc) {
1.27 ! andrew 161: $l->sp("Already up to date");
! 162: $host->{retry}--;
1.22 andrew 163: } else {
1.27 ! andrew 164: $l->sp("Failed");
! 165: $t->bye;
! 166: next;
1.22 andrew 167: }
168:
1.21 mike 169: }
170:
1.22 andrew 171: if ($needs_reboot) {
1.25 andrew 172: $l->sp("Rebooting $host->{name}");
1.17 mike 173: $t->reboot;
174: } else {
1.25 andrew 175: $l->sp("Bye $host->{name}");
1.23 andrew 176: $t->bye();
1.17 mike 177: }
1.16 mike 178: }
179:
1.27 ! andrew 180: if (! $processed) {
! 181: $l->sp("");
! 182: $l->sp("Finished. No more hosts.");
! 183: last;
! 184: }
! 185: }
! 186:
1.16 mike 187: sub upload
188: {
189: my $t = shift;
1.22 andrew 190: my $conf = shift;
1.16 mike 191:
1.22 andrew 192: my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
193:
194: my $fw_type = $conf->{firmware_type};
1.19 andrew 195:
1.16 mike 196: my $ver = $t->ver;
1.26 andrew 197:
198: if (! (
199: $ver->{$fw_type . ' Version'} &&
200: $ver->{$fw_type . ' Checksum'}
201: )) {
202: $l->sp("Error getting current version numbers");
1.27 ! andrew 203: return;
1.26 andrew 204: }
1.16 mike 205:
206: if (
1.19 andrew 207: $ver->{$fw_type . ' Version'} eq $conf->{'ver'} &&
208: $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
1.16 mike 209: ) {
1.21 mike 210: return 0;
1.16 mike 211: }
212:
1.27 ! andrew 213: $l->sp("Updating $fw_type");
1.23 andrew 214: $l->sp("Config information:");
215: $l->sp(" Hardware Type: $conf->{'type'}");
216: $l->sp(" File Name: $conf->{'file_name'}");
217: $l->sp(" File Size: $conf->{'file_size'}");
218: $l->sp(" File Checksum: $conf->{'file_cksum'}");
219: $l->sp(" Conf Version: $conf->{'ver'}");
220: $l->sp(" Cur Version: $ver->{$fw_type . ' Version'}");
221: $l->sp(" Conf Checksum: $conf->{'cksum'}");
222: $l->sp(" Cur Checksum: $ver->{$fw_type . ' Checksum'}");
223:
1.16 mike 224: my $try = 0;
225: while (1) {
226: if ($try >= $max_tries) {
227: $l->sp("Couldn't update in $max_tries tries!");
1.23 andrew 228: return;
1.16 mike 229: }
230: $try++;
231:
232: $l->p("Enabling TFTPd");
1.22 andrew 233: unless ($t->enable_tftpd) {
234: $l->sp("Couldn't enable tftpd");
235: next;
236: }
1.16 mike 237:
1.27 ! andrew 238: $l->p("Uploading file ($conf->{file_name})");
1.16 mike 239: # use tftp to push the file up
240: my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
241:
1.21 mike 242: unless ($tftp->put($file, $file)) {
1.22 andrew 243: $l->sp("Error uploading: " . $tftp->error);
1.21 mike 244: next;
245: }
1.16 mike 246:
247: $l->p("Checking upload ($conf->{'file_cksum'})");
248: my $results = $t->tftpd;
249: # check the 'File Length' against ???
1.27 ! andrew 250: if (! (
! 251: $results->{'File Checksum'} &&
! 252: $results->{'File Length'} &&
! 253: $results->{'File Name'}
! 254: )) {
! 255: $l->sp("Unable to get results of upload");
! 256: next;
! 257: }
1.16 mike 258: if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
259: $l->sp(
1.27 ! andrew 260: "File checksum (" . $results->{'File Checksum'} .
! 261: ") does not match config file (" . $conf->{'file_cksum'} . ")!"
1.16 mike 262: );
263: next;
264: }
265: $l->p("File checksum matches . . . ");
266:
267: if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) {
268: $l->sp(
1.27 ! andrew 269: "File length (" . $results->{'File Length'} .
! 270: ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
1.16 mike 271: );
272: next;
273: }
274: $l->p("File length matches . . . ");
275:
1.24 mike 276: if ( uc($results->{'File Name'}) ne uc($file) ) {
1.16 mike 277: $l->sp(
1.27 ! andrew 278: "File name (" . $results->{'File Name'} .
! 279: ") does not match config file (" . $file . ")!"
1.16 mike 280: );
281: next;
282: }
1.27 ! andrew 283: $l->p("File name matches . . . ");
1.16 mike 284:
1.19 andrew 285: my $image_type = 'mainimage';
286: if ($fw_type eq 'FPGA') {
287: $image_type = 'fpgaimage';
288: }
289: $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
1.16 mike 290: unless ($results = $t->updateflash(
1.19 andrew 291: args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
1.16 mike 292: ' ' . $conf->{'cksum'},
293: Timeout => 90,
294: ) ) {
295: $l->sp("Couldn't update flash: $!");
296: next;
297: }
298:
299: unless (
300: defined $results->{'Checksum'} &&
301: $results->{'Checksum'} eq $conf->{'cksum'}
302: ) {
1.17 mike 303: $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " . $conf->{'cksum'} . "!");
1.16 mike 304: next;
305: }
1.27 ! andrew 306:
! 307: $l->p("$fw_type saved checksum matches . . . ");
1.16 mike 308:
309: return 1;
310: }
311: }
312:
313: package Mylogger;
314:
315: use Fcntl ':flock'; # import LOCK_* constants
316: #use YAML;
317: use constant LOG_PRINT => 128;
318: use constant LOG_SAVE => 64;
319:
320: DESTROY {
321: my $self = shift;
322: if ($self->{'MYLOG'}) {
323: $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
324: close $self->{'MYLOG'};
325: }
326: }
327:
328: sub new {
329: my $package = shift;
330: my $self = shift || {};
331:
332: $self->{'base_path'} ||= '.';
333: $self->{'log_path'} ||= $self->{'base_path'};
334: $self->{'log_prefix'} ||= 'LOG';
335: $self->{'log_file'} ||= GetLogName(
336: $self->{'log_prefix'},
337: $self->{'log_path'}
338: );
339: bless $self, $package;
340: }
341:
342: sub s
343: {
344: my $self = shift;
345: my $m = shift;
346: return $self->mylog($m, LOG_SAVE);
347: }
348:
349: sub p
350: {
351: my $self = shift;
352: my $m = shift;
353: return $self->mylog($m, LOG_PRINT);
354: }
355:
356: sub sp
357: {
358: my $self = shift;
359: my $m = shift;
360: return $self->mylog($m, LOG_SAVE | LOG_PRINT);
361: }
362:
363: sub mylog
364: {
365: my $self = shift;
366:
367: my $thing = shift;
368: chomp $thing;
369:
370: my $which = shift;
371:
372: my $MYLOG;
373: if ($which & LOG_PRINT) {
374: print $thing, "\n";
375: }
376:
377: if ($which & LOG_SAVE) {
378: if ($self->{'MYLOG'}) {
379: $MYLOG = $self->{'MYLOG'};
380: } else {
381: unless ($MYLOG) {
1.27 ! andrew 382: open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
1.16 mike 383: or die "Couldn't open logfile!\n";
384: my $ofh = select $MYLOG;
385: $|=1;
386: select $ofh;
387: $self->{'MYLOG'} = $MYLOG;
388:
389: $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
390: }
391: }
392: flock($MYLOG, LOCK_EX);
393: print $MYLOG (scalar gmtime), "\t", $thing, "\n"
394: or die "Couldn't print to MYLOG: $!";
395: flock($MYLOG, LOCK_UN);
396: }
397: }
398:
399: sub GetLogName
400: {
401: my $prefix = shift || die "Invalid prefix passed for log";
402:
403: my $logdate = GetLogDate();
404: my $logver = 0;
405: my $logname;
406:
407: do {
408: $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
409: $logver++;
410: } until (not -e $logname);
411:
412: return $logname;
413: }
414:
415: sub GetLogDate
416: {
417: my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
418:
419: $mon++;
420: $year += 1900;
421:
422: if ($min < 10) { $min = "0$min" }
423: if ($sec < 10) { $sec = "0$sec" }
424: if ($hour < 10) { $hour = "0$hour" }
425: if ($mday < 10) { $mday = "0$mday" }
426: if ($mon < 10) { $mon = "0$mon" }
427:
428: my $time = $year . $mon . $mday;
429:
430: return $time;
431: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>