Annotation of trango/Net-Telnet-Trango/scripts/update_trango.pl, Revision 1.28
1.16 mike 1: #!/usr/bin/perl
1.28 ! andrew 2: # $RedRiver: update_trango.pl,v 1.27 2007/02/02 21:36:00 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: }
113: }
114: }
115: }
116:
1.22 andrew 117: foreach my $firmware_type ('Firmware', 'FPGA') {
1.21 mike 118:
1.22 andrew 119: if (! exists $conf->{$firmware_type}) {
1.27 andrew 120: $l->s("No configs for '$firmware_type'");
121: $t->close;
122: next;
123: }
1.22 andrew 124:
125: my $host_type = $t->host_type;
1.23 andrew 126: if ($firmware_type eq 'FPGA') {
1.22 andrew 127: $host_type =~ s/\s.*$//;
128: }
129:
130: if (! exists $conf->{$firmware_type}->{$host_type}) {
1.27 andrew 131: $l->sp("No '$firmware_type' config for type $host_type");
132: $t->close;
1.28 ! andrew 133: next;
! 134: }
! 135:
! 136: if ($firmware_type eq 'Firmware' &&
! 137: $t->firmware_version eq
! 138: $conf->{$firmware_type}->{$host_type}->{ver}
! 139: ) {
! 140: $l->sp("Firmware already up to date");
1.27 andrew 141: next;
142: }
1.22 andrew 143:
1.23 andrew 144: if (! $t->logged_in) {
1.22 andrew 145: $l->p("Logging in");
1.25 andrew 146: $t->login($password);
1.23 andrew 147: unless ($t->logged_in) {
148: $l->p('Failed!');
1.25 andrew 149: $t->close;
1.23 andrew 150: next;
151: }
1.22 andrew 152: }
153:
1.27 andrew 154: foreach my $k (keys %{ $conf->{general} }) {
155: $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
1.22 andrew 156: }
1.27 andrew 157: $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
158: $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
1.22 andrew 159:
1.23 andrew 160: $l->sp("$host_type $firmware_type");
1.22 andrew 161: ## Send commands
1.23 andrew 162: my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
1.27 andrew 163: if ($rc) {
1.22 andrew 164: $l->sp("Successfull!");
1.27 andrew 165: $host->{retry}--;
166: $needs_reboot = 1;
1.23 andrew 167: } elsif (defined $rc) {
1.27 andrew 168: $l->sp("Already up to date");
169: $host->{retry}--;
1.22 andrew 170: } else {
1.27 andrew 171: $l->sp("Failed");
172: $t->bye;
173: next;
1.22 andrew 174: }
175:
1.21 mike 176: }
177:
1.22 andrew 178: if ($needs_reboot) {
1.25 andrew 179: $l->sp("Rebooting $host->{name}");
1.17 mike 180: $t->reboot;
181: } else {
1.25 andrew 182: $l->sp("Bye $host->{name}");
1.23 andrew 183: $t->bye();
1.17 mike 184: }
1.16 mike 185: }
186:
1.27 andrew 187: if (! $processed) {
188: $l->sp("");
189: $l->sp("Finished. No more hosts.");
190: last;
191: }
192: }
193:
1.16 mike 194: sub upload
195: {
196: my $t = shift;
1.22 andrew 197: my $conf = shift;
1.16 mike 198:
1.22 andrew 199: my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
200:
201: my $fw_type = $conf->{firmware_type};
1.19 andrew 202:
1.16 mike 203: my $ver = $t->ver;
1.26 andrew 204:
205: if (! (
206: $ver->{$fw_type . ' Version'} &&
207: $ver->{$fw_type . ' Checksum'}
208: )) {
209: $l->sp("Error getting current version numbers");
1.27 andrew 210: return;
1.26 andrew 211: }
1.16 mike 212:
213: if (
1.19 andrew 214: $ver->{$fw_type . ' Version'} eq $conf->{'ver'} &&
215: $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
1.16 mike 216: ) {
1.21 mike 217: return 0;
1.16 mike 218: }
219:
1.27 andrew 220: $l->sp("Updating $fw_type");
1.23 andrew 221: $l->sp("Config information:");
222: $l->sp(" Hardware Type: $conf->{'type'}");
223: $l->sp(" File Name: $conf->{'file_name'}");
224: $l->sp(" File Size: $conf->{'file_size'}");
225: $l->sp(" File Checksum: $conf->{'file_cksum'}");
226: $l->sp(" Conf Version: $conf->{'ver'}");
227: $l->sp(" Cur Version: $ver->{$fw_type . ' Version'}");
228: $l->sp(" Conf Checksum: $conf->{'cksum'}");
229: $l->sp(" Cur Checksum: $ver->{$fw_type . ' Checksum'}");
230:
1.16 mike 231: my $try = 0;
232: while (1) {
233: if ($try >= $max_tries) {
234: $l->sp("Couldn't update in $max_tries tries!");
1.23 andrew 235: return;
1.16 mike 236: }
237: $try++;
238:
239: $l->p("Enabling TFTPd");
1.22 andrew 240: unless ($t->enable_tftpd) {
241: $l->sp("Couldn't enable tftpd");
242: next;
243: }
1.16 mike 244:
1.27 andrew 245: $l->p("Uploading file ($conf->{file_name})");
1.16 mike 246: # use tftp to push the file up
247: my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
248:
1.21 mike 249: unless ($tftp->put($file, $file)) {
1.22 andrew 250: $l->sp("Error uploading: " . $tftp->error);
1.21 mike 251: next;
252: }
1.16 mike 253:
254: $l->p("Checking upload ($conf->{'file_cksum'})");
255: my $results = $t->tftpd;
256: # check the 'File Length' against ???
1.27 andrew 257: if (! (
258: $results->{'File Checksum'} &&
259: $results->{'File Length'} &&
260: $results->{'File Name'}
261: )) {
262: $l->sp("Unable to get results of upload");
263: next;
264: }
1.16 mike 265: if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
266: $l->sp(
1.27 andrew 267: "File checksum (" . $results->{'File Checksum'} .
268: ") does not match config file (" . $conf->{'file_cksum'} . ")!"
1.16 mike 269: );
270: next;
271: }
272: $l->p("File checksum matches . . . ");
273:
274: if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) {
275: $l->sp(
1.27 andrew 276: "File length (" . $results->{'File Length'} .
277: ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
1.16 mike 278: );
279: next;
280: }
281: $l->p("File length matches . . . ");
282:
1.24 mike 283: if ( uc($results->{'File Name'}) ne uc($file) ) {
1.16 mike 284: $l->sp(
1.27 andrew 285: "File name (" . $results->{'File Name'} .
286: ") does not match config file (" . $file . ")!"
1.16 mike 287: );
288: next;
289: }
1.27 andrew 290: $l->p("File name matches . . . ");
1.16 mike 291:
1.19 andrew 292: my $image_type = 'mainimage';
293: if ($fw_type eq 'FPGA') {
294: $image_type = 'fpgaimage';
295: }
296: $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
1.16 mike 297: unless ($results = $t->updateflash(
1.19 andrew 298: args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
1.16 mike 299: ' ' . $conf->{'cksum'},
300: Timeout => 90,
301: ) ) {
302: $l->sp("Couldn't update flash: $!");
303: next;
304: }
305:
306: unless (
307: defined $results->{'Checksum'} &&
308: $results->{'Checksum'} eq $conf->{'cksum'}
309: ) {
1.17 mike 310: $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " . $conf->{'cksum'} . "!");
1.16 mike 311: next;
312: }
1.27 andrew 313:
314: $l->p("$fw_type saved checksum matches . . . ");
1.16 mike 315:
316: return 1;
317: }
318: }
319:
320: package Mylogger;
321:
322: use Fcntl ':flock'; # import LOCK_* constants
323: #use YAML;
324: use constant LOG_PRINT => 128;
325: use constant LOG_SAVE => 64;
326:
327: DESTROY {
328: my $self = shift;
329: if ($self->{'MYLOG'}) {
330: $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
331: close $self->{'MYLOG'};
332: }
333: }
334:
335: sub new {
336: my $package = shift;
337: my $self = shift || {};
338:
339: $self->{'base_path'} ||= '.';
340: $self->{'log_path'} ||= $self->{'base_path'};
341: $self->{'log_prefix'} ||= 'LOG';
342: $self->{'log_file'} ||= GetLogName(
343: $self->{'log_prefix'},
344: $self->{'log_path'}
345: );
346: bless $self, $package;
347: }
348:
349: sub s
350: {
351: my $self = shift;
352: my $m = shift;
353: return $self->mylog($m, LOG_SAVE);
354: }
355:
356: sub p
357: {
358: my $self = shift;
359: my $m = shift;
360: return $self->mylog($m, LOG_PRINT);
361: }
362:
363: sub sp
364: {
365: my $self = shift;
366: my $m = shift;
367: return $self->mylog($m, LOG_SAVE | LOG_PRINT);
368: }
369:
370: sub mylog
371: {
372: my $self = shift;
373:
374: my $thing = shift;
375: chomp $thing;
376:
377: my $which = shift;
378:
379: my $MYLOG;
380: if ($which & LOG_PRINT) {
381: print $thing, "\n";
382: }
383:
384: if ($which & LOG_SAVE) {
385: if ($self->{'MYLOG'}) {
386: $MYLOG = $self->{'MYLOG'};
387: } else {
388: unless ($MYLOG) {
1.27 andrew 389: open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
1.16 mike 390: or die "Couldn't open logfile!\n";
391: my $ofh = select $MYLOG;
392: $|=1;
393: select $ofh;
394: $self->{'MYLOG'} = $MYLOG;
395:
396: $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
397: }
398: }
399: flock($MYLOG, LOCK_EX);
400: print $MYLOG (scalar gmtime), "\t", $thing, "\n"
401: or die "Couldn't print to MYLOG: $!";
402: flock($MYLOG, LOCK_UN);
403: }
404: }
405:
406: sub GetLogName
407: {
408: my $prefix = shift || die "Invalid prefix passed for log";
409:
410: my $logdate = GetLogDate();
411: my $logver = 0;
412: my $logname;
413:
414: do {
415: $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
416: $logver++;
417: } until (not -e $logname);
418:
419: return $logname;
420: }
421:
422: sub GetLogDate
423: {
424: my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
425:
426: $mon++;
427: $year += 1900;
428:
429: if ($min < 10) { $min = "0$min" }
430: if ($sec < 10) { $sec = "0$sec" }
431: if ($hour < 10) { $hour = "0$hour" }
432: if ($mday < 10) { $mday = "0$mday" }
433: if ($mon < 10) { $mon = "0$mon" }
434:
435: my $time = $year . $mon . $mday;
436:
437: return $time;
438: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>