1 | package Bccd; |
---|
2 | |
---|
3 | # $Id: Bccd.pm 3120 2011-04-13 19:44:26Z fitz $ |
---|
4 | |
---|
5 | # This file is part of BCCD, an open-source live CD for computational science |
---|
6 | # education. |
---|
7 | # |
---|
8 | # Copyright (C) 2010 Andrew Fitz Gibbon, Paul Gray, Kevin Hunter, Dave Joiner, |
---|
9 | # Sam Leeman-Munk, Tom Murphy, Charlie Peck, Skylar Thompson, & Aaron Weeden |
---|
10 | |
---|
11 | # |
---|
12 | # This program is free software: you can redistribute it and/or modify |
---|
13 | # it under the terms of the GNU General Public License as published by |
---|
14 | # the Free Software Foundation, either version 3 of the License, or |
---|
15 | # (at your option) any later version. |
---|
16 | # |
---|
17 | # This program is distributed in the hope that it will be useful, |
---|
18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | # GNU General Public License for more details. |
---|
21 | # |
---|
22 | # You should have received a copy of the GNU General Public License |
---|
23 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | |
---|
25 | use strict; |
---|
26 | use warnings; |
---|
27 | use File::Path; |
---|
28 | use File::Temp; |
---|
29 | use File::Copy; |
---|
30 | use WWW::Mechanize; |
---|
31 | use Term::ReadKey; |
---|
32 | use POSIX; |
---|
33 | use Carp; |
---|
34 | use Readonly; |
---|
35 | use UI::Dialog; |
---|
36 | use Data::Dumper; |
---|
37 | use NetAddr::IP; |
---|
38 | use IO::Socket::INET; |
---|
39 | use Net::DHCP::Packet; |
---|
40 | use Net::DHCP::Constants; |
---|
41 | use Net::CIDR ':all'; |
---|
42 | use Errno qw(:POSIX); |
---|
43 | use Fcntl ':mode'; |
---|
44 | use YAML qw/LoadFile/; |
---|
45 | |
---|
46 | my $passed = 0; |
---|
47 | my $total = 0; |
---|
48 | Readonly my $KERNREV => '2.6.31.12-aufs'; |
---|
49 | Readonly my $DHCFILE => '/etc/dhcp3/dhclient.conf'; |
---|
50 | Readonly my $ALLOUTFILE => "allout"; |
---|
51 | Readonly my $LVMROOT => "/sbin/"; |
---|
52 | Readonly my $PROJECT => "bccd"; |
---|
53 | Readonly my $IFCONFIG => "/sbin/ifconfig -a"; |
---|
54 | Readonly my $INTFILE => "/etc/network/interfaces"; |
---|
55 | Readonly my $NATSH => "/etc/network/if-up.d/nat"; |
---|
56 | Readonly my $TEMPLATE_IPTABLES_UP => '/etc/iptables.up.rules.template'; |
---|
57 | Readonly my $IPTABLES_UP => '/etc/iptables.up.rules'; |
---|
58 | Readonly my $START_PKBFILE => "/etc/network/if-up.d/start-pkbcast"; |
---|
59 | Readonly my $CMDLINE_FILE => "/proc/cmdline"; |
---|
60 | Readonly my $BCCD_NET => { 'ipaddr' => '192.168.3.1', |
---|
61 | 'mask' => '255.255.255.0', |
---|
62 | 'bcast' => '192.168.3.255', |
---|
63 | 'dhcp' => 0, |
---|
64 | 'bccdnet' => 1, |
---|
65 | }; |
---|
66 | Readonly my $DHCP_RANGES => { 'res' => 10, |
---|
67 | 'dhcp' => 100, |
---|
68 | 'pxe' => 100 |
---|
69 | }; |
---|
70 | Readonly my $DHCP_CONF => '/etc/dhcp3/bccd_net.conf'; |
---|
71 | Readonly my $TEMPLATE_DHCP_CONF => $DHCP_CONF."_template"; |
---|
72 | Readonly my $PXELINUX => "/var/lib/tftpboot/pxelinux.cfg/default"; |
---|
73 | Readonly my $TEMPLATE_PXELINUX => $PXELINUX."_template"; |
---|
74 | Readonly my $DISKLESS_FSTAB => "/diskless/bccd/etc/fstab"; |
---|
75 | Readonly my $TEMPLATE_DISKLESS_FSTAB => $DISKLESS_FSTAB."_template"; |
---|
76 | my $hostname = `/bin/hostname`; |
---|
77 | chomp($hostname); |
---|
78 | Readonly my $HOSTNAME => $hostname; |
---|
79 | $hostname = `/bin/hostname -s`; |
---|
80 | chomp($hostname); |
---|
81 | Readonly my $SHORT_HOSTNAME => $hostname; |
---|
82 | |
---|
83 | #Boot Flag Indices |
---|
84 | Readonly my $CUSTOM_NIC => "02"; |
---|
85 | Readonly my $IS_BCCD_NETWORK_NIC => 0; |
---|
86 | |
---|
87 | undef($hostname); |
---|
88 | |
---|
89 | my $debug = 0; |
---|
90 | my $INFO = 0b1; |
---|
91 | my $DEBUG = 0b10; |
---|
92 | my $LOG = 0; |
---|
93 | |
---|
94 | sub new { |
---|
95 | my $class = shift; |
---|
96 | my $self = {}; |
---|
97 | bless($self,$class); |
---|
98 | return $self; |
---|
99 | } |
---|
100 | |
---|
101 | sub log_and_cont( $$$$ ) { |
---|
102 | my($self,$code,$func,$msg) = @_; |
---|
103 | |
---|
104 | carp "$0: $code: $func: $msg\n"; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | sub log_and_die( $$$$ ) { |
---|
109 | my($self,$code,$func,$msg) = @_; |
---|
110 | |
---|
111 | croak "$0: $code: $func: $msg\n"; |
---|
112 | } |
---|
113 | |
---|
114 | sub enter_sub( $$ ) { |
---|
115 | my($self,$sub) = @_; |
---|
116 | |
---|
117 | if($self->is_log($DEBUG)) { |
---|
118 | $self->log_and_cont("DEBUG",$sub,"Entering $sub"); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | sub leave_sub( $$ ) { |
---|
123 | my($self,$sub) = @_; |
---|
124 | |
---|
125 | if($self->is_log($DEBUG)) { |
---|
126 | $self->log_and_cont("DEBUG",$sub,"Leaving $sub") ; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | sub cmd_num_die( $@ ) { |
---|
131 | my($self,@cmds) = @_; |
---|
132 | my $sub = "cmd_num_due"; |
---|
133 | $self->enter_sub($sub); |
---|
134 | |
---|
135 | $self->log_and_die("ERROR",$sub,"Incorrect number of command line arguments: $#cmds; @cmds"); |
---|
136 | $self->leave_sub($sub); |
---|
137 | } |
---|
138 | |
---|
139 | sub print_array ( $@ ) { |
---|
140 | my($self,@array) = @_; |
---|
141 | my $sub = "print_array"; |
---|
142 | $self->enter_sub($sub); |
---|
143 | my $i; |
---|
144 | |
---|
145 | $i = 0; |
---|
146 | foreach my $row ( @array ) { |
---|
147 | print "$i: $row\n"; |
---|
148 | $i++; |
---|
149 | } |
---|
150 | $self->leave_sub($sub); |
---|
151 | } |
---|
152 | |
---|
153 | sub get_lvminfo( $$ ) { |
---|
154 | my($self,$layer) = @_; |
---|
155 | my($sub,$cmd,$rc,$out); |
---|
156 | my(@info,@splitinfo); |
---|
157 | $sub = "get_lvminfo"; |
---|
158 | $self->enter_sub($sub); |
---|
159 | |
---|
160 | if($layer !~ m/(?:pv|vg|lv)/) { |
---|
161 | $self->log_and_die("ERROR",$sub,"Layer must be one of pv, vg, or lv."); |
---|
162 | } |
---|
163 | |
---|
164 | $cmd = "$LVMROOT/".$layer."display -c"; |
---|
165 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
166 | $self->log_and_cont("INFO",$sub,"Executing $cmd"); |
---|
167 | } |
---|
168 | ($rc,$out) = $self->exec_system($cmd); |
---|
169 | if($rc == 5) { |
---|
170 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
171 | $self->log_and_cont("INFO",$sub,"Nothing to display for $cmd."); |
---|
172 | } |
---|
173 | return undef; |
---|
174 | } |
---|
175 | elsif($rc) { |
---|
176 | if($rc) { |
---|
177 | $self->log_and_cont("NOTICE", $sub,"$cmd failed with output $out and rc $rc: $!"); |
---|
178 | } |
---|
179 | return undef; |
---|
180 | } |
---|
181 | |
---|
182 | foreach my $line ( split('\n',$out) ) { |
---|
183 | $line =~ s/^\s+//g; |
---|
184 | if($line =~ m/is a new physical volume/) { # pvdisplay reports this when the PV has no VG |
---|
185 | next; |
---|
186 | } |
---|
187 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
188 | $self->log_and_cont("INFO",$sub,"Pushing line $line."); |
---|
189 | } |
---|
190 | push(@splitinfo,[ split(':',$line) ]); |
---|
191 | } |
---|
192 | |
---|
193 | return @splitinfo; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | sub rm_all_lv( $ ) { |
---|
198 | my($self) = @_; |
---|
199 | my($sub,$cmdrc,$rc,$out); |
---|
200 | my @info; |
---|
201 | my %lvs; |
---|
202 | $sub = 'rm_all_lv'; |
---|
203 | $self->enter_sub($sub); |
---|
204 | |
---|
205 | $rc = 0; |
---|
206 | @info = $self->get_lvminfo('lv'); |
---|
207 | if(@info) { |
---|
208 | for(my $i=0;$i<=$#info;$i++) { |
---|
209 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
210 | $self->log_and_cont("INFO",$sub,"Found volume group for logical volumes: $info[$i][1]."); |
---|
211 | } |
---|
212 | $lvs{$info[$i][1]} = 1; |
---|
213 | } |
---|
214 | |
---|
215 | foreach my $key ( keys %lvs ) { |
---|
216 | my $cmd = "/sbin/lvremove -f $key"; |
---|
217 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
218 | $self->log_and_cont("INFO",$sub,"Running cmd $cmd."); |
---|
219 | } |
---|
220 | ($cmdrc,$out) = $self->exec_system("$cmd"); |
---|
221 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
222 | $self->log_and_cont("INFO",$sub,"$cmd returned $cmdrc with output $out"); |
---|
223 | } |
---|
224 | if($rc) { |
---|
225 | $self->log_and_cont("ERROR", $sub,"$cmd failed with output $out and rc $rc: $!"); |
---|
226 | } |
---|
227 | $rc += $cmdrc; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
232 | $self->log_and_cont("INFO",$sub,"Returning with rc $rc."); |
---|
233 | } |
---|
234 | $self->leave_sub($sub); |
---|
235 | return $rc; |
---|
236 | } |
---|
237 | |
---|
238 | sub rm_all_vg( $ ) { |
---|
239 | my($self) = @_; |
---|
240 | my($sub,$rc,$cmdrc,$out); |
---|
241 | my @info; |
---|
242 | my %vgs; |
---|
243 | $sub = 'rm_all_vg'; |
---|
244 | $self->enter_sub($sub); |
---|
245 | |
---|
246 | $rc = 0; |
---|
247 | @info = $self->get_lvminfo('vg'); |
---|
248 | if(@info) { |
---|
249 | for(my $i=0;$i<=$#info;$i++) { |
---|
250 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
251 | $self->log_and_cont("INFO",$sub,"Found volume group: $info[$i][0]."); |
---|
252 | } |
---|
253 | $vgs{$info[$i][0]} = 1; |
---|
254 | } |
---|
255 | |
---|
256 | foreach my $key ( keys %vgs ) { |
---|
257 | my $cmd = "/sbin/vgremove -f $key"; |
---|
258 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
259 | $self->log_and_cont("INFO",$sub,"Running cmd $cmd."); |
---|
260 | } |
---|
261 | ($cmdrc,$out) = $self->exec_system("$cmd"); |
---|
262 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
263 | $self->log_and_cont("INFO",$sub,"$cmd returned $cmdrc with output $out"); |
---|
264 | } |
---|
265 | if($rc) { |
---|
266 | $self->log_and_cont("ERROR", $sub,"$cmd failed with output $out and rc $rc: $!"); |
---|
267 | } |
---|
268 | $rc += $cmdrc; |
---|
269 | } |
---|
270 | } |
---|
271 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
272 | $self->log_and_cont("INFO",$sub,"Returning with rc $rc."); |
---|
273 | } |
---|
274 | |
---|
275 | $self->leave_sub($sub); |
---|
276 | return $rc; |
---|
277 | } |
---|
278 | |
---|
279 | sub rm_all_pv( $ ) { |
---|
280 | my($self) = @_; |
---|
281 | my($sub,$cmdrc,$rc,$out); |
---|
282 | my @info; |
---|
283 | my %pvs; |
---|
284 | $sub = 'rm_all_pv'; |
---|
285 | $self->enter_sub($sub); |
---|
286 | |
---|
287 | $rc = 0; |
---|
288 | @info = $self->get_lvminfo('pv'); |
---|
289 | if(@info) { |
---|
290 | for(my $i=0;$i<=$#info;$i++) { |
---|
291 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
292 | $self->log_and_cont("INFO",$sub,"Found physical volume: $info[$i][0]."); |
---|
293 | } |
---|
294 | $pvs{$info[$i][0]} = 1; |
---|
295 | } |
---|
296 | |
---|
297 | foreach my $key ( keys %pvs ) { |
---|
298 | my $cmd = "/sbin/pvremove -f $key"; |
---|
299 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
300 | $self->log_and_cont("INFO",$sub,"Running cmd $cmd."); |
---|
301 | } |
---|
302 | |
---|
303 | ($cmdrc,$out) = $self->exec_system("$cmd"); |
---|
304 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
305 | $self->log_and_cont("INFO",$sub,"$cmd returned $cmdrc with output $out"); |
---|
306 | } |
---|
307 | |
---|
308 | if($rc) { |
---|
309 | $self->log_and_die("ERROR", $sub,"$cmd failed with output $out and rc $rc: $!"); |
---|
310 | } |
---|
311 | $rc += $cmdrc; |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
316 | $self->log_and_cont("INFO",$sub,"Returning with rc $rc."); |
---|
317 | } |
---|
318 | |
---|
319 | $self->leave_sub($sub); |
---|
320 | return $rc; |
---|
321 | } |
---|
322 | |
---|
323 | sub get_lvinfo( $ ) { |
---|
324 | my($self) = @_; |
---|
325 | my $sub = "get_lvinfo"; |
---|
326 | $self->enter_sub($sub); |
---|
327 | my($lvinfo,$cmd); |
---|
328 | |
---|
329 | $cmd = "$LVMROOT/lvdisplay -c"; |
---|
330 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
331 | $self->log_and_cont("INFO",$sub,"Executing $cmd"); |
---|
332 | } |
---|
333 | $lvinfo = `$cmd`; |
---|
334 | if(WEXITSTATUS($?)) { |
---|
335 | $self->log_and_die("ERROR", $sub,"$cmd with output $lvinfo: $!"); |
---|
336 | } |
---|
337 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
338 | $self->log_and_cont("INFO",$sub,"Ran $cmd and got output $lvinfo"); |
---|
339 | } |
---|
340 | |
---|
341 | $self->leave_sub($sub); |
---|
342 | return split(':', $lvinfo); |
---|
343 | } |
---|
344 | |
---|
345 | sub get_vginfo( $ ) { |
---|
346 | my($self) = @_; |
---|
347 | my $sub = "get_vginfo"; |
---|
348 | $self->enter_sub($sub); |
---|
349 | my($vginfo,$cmd); |
---|
350 | |
---|
351 | $cmd = "$LVMROOT/vgdisplay -c"; |
---|
352 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
353 | $self->log_and_cont("INFO",$sub,"Executing $cmd"); |
---|
354 | } |
---|
355 | $vginfo = `$cmd`; |
---|
356 | if(WEXITSTATUS($?)) { |
---|
357 | $self->log_and_die("ERROR", $sub,"$cmd with output $vginfo: $!"); |
---|
358 | } |
---|
359 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
360 | $self->log_and_cont("INFO",$sub,"Ran $cmd and got output $vginfo"); |
---|
361 | } |
---|
362 | |
---|
363 | $self->leave_sub($sub); |
---|
364 | return split(':', $vginfo); |
---|
365 | } |
---|
366 | |
---|
367 | sub get_pvinfo( $ ) { |
---|
368 | my($self) = @_; |
---|
369 | my $sub = "get_pvinfo"; |
---|
370 | $self->enter_sub($sub); |
---|
371 | my($pvinfo,$cmd); |
---|
372 | |
---|
373 | $cmd = "$LVMROOT/pvdisplay -c"; |
---|
374 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
375 | $self->log_and_cont("INFO",$sub,"Executing $cmd"); |
---|
376 | } |
---|
377 | $pvinfo = `$cmd`; |
---|
378 | if(WEXITSTATUS($?)) { |
---|
379 | $self->log_and_die("ERROR",$sub,"$cmd failed: $!"); |
---|
380 | } |
---|
381 | |
---|
382 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
383 | $self->log_and_cont("INFO",$sub,"Ran $cmd and got output $pvinfo"); |
---|
384 | } |
---|
385 | $self->leave_sub($sub); |
---|
386 | return split(':', $pvinfo); |
---|
387 | } |
---|
388 | |
---|
389 | sub get_pe_size( $ ) { |
---|
390 | my($self) = @_; |
---|
391 | my $sub = "get_pe_size"; |
---|
392 | $self->enter_sub($sub); |
---|
393 | my @vginfo = $self->get_vginfo(); |
---|
394 | if($self->is_log($DEBUG)) { |
---|
395 | $self->log_and_cont("DEBUG",$sub,"Retrieved @vginfo from get_vginfo."); |
---|
396 | } |
---|
397 | |
---|
398 | $self->leave_sub($sub); |
---|
399 | return $vginfo[12]; |
---|
400 | } |
---|
401 | |
---|
402 | sub get_free_pe_count( $ ) { |
---|
403 | my($self) = @_; |
---|
404 | my $sub = "get_free_pe_count"; |
---|
405 | $self->enter_sub($sub); |
---|
406 | |
---|
407 | my @vginfo = $self->get_vginfo(); |
---|
408 | if($self->is_log($DEBUG)) { |
---|
409 | $self->log_and_cont("DEBUG",$sub,"Retrieved @vginfo from get_vginfo."); |
---|
410 | } |
---|
411 | |
---|
412 | $self->leave_sub($sub); |
---|
413 | return $vginfo[15]; |
---|
414 | } |
---|
415 | |
---|
416 | sub snarf_file( $$ ) { |
---|
417 | my($self,$file) = @_; |
---|
418 | my($sub,$FILE); |
---|
419 | $sub = "snarf_file"; |
---|
420 | $self->enter_sub($sub); |
---|
421 | my $input; |
---|
422 | { |
---|
423 | local $/; |
---|
424 | open($FILE, "< $file") or $self->log_and_die("ERROR",$sub,"Could not open file $file for reading: $!"); |
---|
425 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
426 | $self->log_and_cont("INFO",$sub,"Opened $file for reading."); |
---|
427 | } |
---|
428 | |
---|
429 | $input = <$FILE>; |
---|
430 | } |
---|
431 | close($FILE); |
---|
432 | |
---|
433 | chomp $input; |
---|
434 | |
---|
435 | $self->leave_sub($sub); |
---|
436 | return $input; |
---|
437 | } |
---|
438 | |
---|
439 | sub test_regexsub_file( $$$$$$$ ) { |
---|
440 | my($self,$type,$okrc,$msg,$file,$regex1,$regex2) = @_; |
---|
441 | my($sub,$text,$rc); |
---|
442 | $sub = 'test_regexsub_file'; |
---|
443 | |
---|
444 | if($okrc eq '') { |
---|
445 | $okrc = 1; |
---|
446 | } |
---|
447 | |
---|
448 | if( ! -f $file ) { |
---|
449 | $self->fail_msg("$msg: $file not found for regex sub."); |
---|
450 | return 0; |
---|
451 | } |
---|
452 | |
---|
453 | $text = $self->snarf_file($file); |
---|
454 | |
---|
455 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
456 | $self->log_and_cont("INFO",$sub,"Regex1: $regex1; Regex2: $regex2; Pretext: $text"); |
---|
457 | } |
---|
458 | |
---|
459 | $text =~ s/$regex1/$regex2/g; |
---|
460 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
461 | $self->log_and_cont("INFO",$sub,"Posttext: $text"); |
---|
462 | } |
---|
463 | |
---|
464 | $rc = $self->test_fwrite($type,$okrc,"Writing $file after $regex1 -> $regex2." |
---|
465 | ,'w',$file,$text); |
---|
466 | |
---|
467 | if($rc == $okrc) { |
---|
468 | $self->ok_msg($msg); |
---|
469 | $rc = 1; |
---|
470 | } |
---|
471 | else { |
---|
472 | $self->fail_msg($msg); |
---|
473 | $rc = 0; |
---|
474 | } |
---|
475 | |
---|
476 | return $rc; |
---|
477 | } |
---|
478 | |
---|
479 | sub test_read_yaml{ |
---|
480 | my($self,$type,$okrc,$msg,$file) = @_; |
---|
481 | my $sub = 'test_read_yaml'; |
---|
482 | |
---|
483 | $self->enter_sub($sub); |
---|
484 | |
---|
485 | if(! -f $file) { |
---|
486 | $self->log_and_die("ERROR",$sub,"Cannot read in $file"); |
---|
487 | } |
---|
488 | |
---|
489 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
490 | $self->log_and_cont("INFO",$sub,"Reading in: $file"); |
---|
491 | } |
---|
492 | my $y = LoadFile($file); |
---|
493 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
494 | $self->log_and_cont("INFO",$sub,"Read in:".Dumper($y)); |
---|
495 | } |
---|
496 | |
---|
497 | $self->leave_sub($sub); |
---|
498 | return $y; |
---|
499 | } |
---|
500 | |
---|
501 | sub test_mknods{ |
---|
502 | my($self,$type,$okrc,$msg,$file,$base) = @_; |
---|
503 | my($rc,$temprc,$out); |
---|
504 | my $sub = 'test_mknods'; |
---|
505 | |
---|
506 | $self->enter_sub($sub); |
---|
507 | |
---|
508 | if($okrc eq '') { |
---|
509 | $okrc = 0; |
---|
510 | } |
---|
511 | |
---|
512 | my $y = $self->test_read_yaml($type,$okrc,"Reading mknod configuration from $file.",$file); |
---|
513 | if(!defined($y)) { |
---|
514 | $self->log_and_die("ERROR",$sub,"Can't proceeded with invalid configuration."); |
---|
515 | } |
---|
516 | |
---|
517 | $rc = 0; |
---|
518 | foreach my $d (keys(%{$y})) { |
---|
519 | my $cmd = "/bin/mknod $base/$d $y->{$d}->{type} $y->{$d}->{major} $y->{$d}->{minor}"; |
---|
520 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
521 | $self->log_and_cont("INFO",$sub,"Running $cmd"); |
---|
522 | } |
---|
523 | |
---|
524 | ($temprc,$out) = $self->exec_system($cmd); |
---|
525 | if($rc) { |
---|
526 | $self->log_and_cont("$cmd failed with $temprc, out $out"); |
---|
527 | } |
---|
528 | if($temprc > $rc) { |
---|
529 | $rc = $temprc; |
---|
530 | } |
---|
531 | } |
---|
532 | |
---|
533 | if($rc == $okrc) { |
---|
534 | $self->ok_msg($msg); |
---|
535 | $rc = 1; |
---|
536 | } |
---|
537 | else { |
---|
538 | $self->fail_msg($msg); |
---|
539 | $rc = 0; |
---|
540 | } |
---|
541 | |
---|
542 | $self->leave_sub($sub); |
---|
543 | return $rc; |
---|
544 | } |
---|
545 | |
---|
546 | sub test_rm_lvm( $$$$ ) { |
---|
547 | my($self,$type,$okrc,$msg) = @_; |
---|
548 | my($sub,$rc,$cmdrc); |
---|
549 | $sub = 'test_rm_lvm'; |
---|
550 | $self->enter_sub($sub); |
---|
551 | |
---|
552 | if($okrc eq '') { |
---|
553 | $okrc = 0; |
---|
554 | } |
---|
555 | |
---|
556 | $rc = 0; |
---|
557 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
558 | $self->log_and_cont("INFO",$sub,"Removing logical volumes."); |
---|
559 | } |
---|
560 | $cmdrc = $self->rm_all_lv(); |
---|
561 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
562 | $self->log_and_cont("INFO",$sub,"Logical volume remove exited with rc $cmdrc."); |
---|
563 | } |
---|
564 | $rc += $cmdrc; |
---|
565 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
566 | $self->log_and_cont("INFO",$sub,"Removing volume groups."); |
---|
567 | } |
---|
568 | $cmdrc = $self->rm_all_vg(); |
---|
569 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
570 | $self->log_and_cont("INFO",$sub,"Volume group remove exited with rc $cmdrc."); |
---|
571 | } |
---|
572 | $rc += $cmdrc; |
---|
573 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
574 | $self->log_and_cont("INFO",$sub,"Removing physical volumes."); |
---|
575 | } |
---|
576 | $cmdrc = $self->rm_all_pv(); |
---|
577 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
578 | $self->log_and_cont("INFO",$sub,"Physical volume remove exited with rc $cmdrc."); |
---|
579 | } |
---|
580 | $rc += $cmdrc; |
---|
581 | |
---|
582 | if($rc == $okrc) { |
---|
583 | $self->ok_msg($msg); |
---|
584 | $rc = 1; |
---|
585 | } |
---|
586 | else { |
---|
587 | $self->fail_msg($msg); |
---|
588 | $rc = 0; |
---|
589 | } |
---|
590 | |
---|
591 | $self->leave_sub($sub); |
---|
592 | return $rc; |
---|
593 | } |
---|
594 | |
---|
595 | sub test_system( $$$$$ ) { |
---|
596 | my($self,$type,$okrc,$msg,$cmd) = @_; |
---|
597 | my $sub = "test_system"; |
---|
598 | $self->enter_sub($sub); |
---|
599 | my $rc = 0; |
---|
600 | my $out; |
---|
601 | |
---|
602 | if( $okrc eq "" ) { |
---|
603 | $okrc = 0; |
---|
604 | } |
---|
605 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
606 | $self->log_and_cont("INFO",$sub,"Passing $cmd to exec_system"); |
---|
607 | } |
---|
608 | ($rc,$out) = $self->exec_system($cmd); |
---|
609 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
610 | $self->log_and_cont("INFO",$sub,"$cmd came back with rc $rc, out $out"); |
---|
611 | } |
---|
612 | |
---|
613 | if($rc == $okrc) { |
---|
614 | $self->ok_msg($msg); |
---|
615 | $rc = 1; |
---|
616 | } |
---|
617 | else { |
---|
618 | $self->fail_msg("$msg,$out"); |
---|
619 | $rc = 0; |
---|
620 | } |
---|
621 | |
---|
622 | $self->leave_sub($sub); |
---|
623 | return ($out,$rc); |
---|
624 | } |
---|
625 | |
---|
626 | sub test_chdir( $$$$$ ) { |
---|
627 | my($self,$type,$okrc,$msg,$dir) = @_; |
---|
628 | my $sub = "test_chdir"; |
---|
629 | $self->enter_sub($sub); |
---|
630 | my $rc = 0; |
---|
631 | |
---|
632 | if( $okrc eq "" ) { |
---|
633 | $okrc = 1; |
---|
634 | } |
---|
635 | $rc = chdir($dir); |
---|
636 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
637 | $self->log_and_cont("INFO",$sub,"chdir'd to $dir with rc $rc"); |
---|
638 | } |
---|
639 | |
---|
640 | if($rc == $okrc) { |
---|
641 | $self->ok_msg($msg); |
---|
642 | $rc = 1; |
---|
643 | } |
---|
644 | else { |
---|
645 | $self->fail_msg($msg); |
---|
646 | $rc = 0; |
---|
647 | } |
---|
648 | |
---|
649 | $self->leave_sub($sub); |
---|
650 | return $rc; |
---|
651 | } |
---|
652 | |
---|
653 | sub test_mkpath( $$$$$ ) { |
---|
654 | my($self,$type,$okrc,$msg,$dir) = @_; |
---|
655 | my $sub = "test_mkpath"; |
---|
656 | $self->enter_sub($sub); |
---|
657 | my $rc = 0; |
---|
658 | |
---|
659 | if( $okrc eq "" ) { |
---|
660 | $okrc = 1; |
---|
661 | } |
---|
662 | eval { mkpath($dir) }; |
---|
663 | if($@) { |
---|
664 | $rc = 0; |
---|
665 | } |
---|
666 | else { |
---|
667 | $rc = $okrc; |
---|
668 | } |
---|
669 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
670 | $self->log_and_cont("INFO",$sub,"mkpath'd $dir with rc $rc"); |
---|
671 | } |
---|
672 | |
---|
673 | if($rc == $okrc) { |
---|
674 | $self->ok_msg($msg); |
---|
675 | $rc = 1; |
---|
676 | } |
---|
677 | else { |
---|
678 | $self->fail_msg($msg); |
---|
679 | $rc = 0; |
---|
680 | } |
---|
681 | |
---|
682 | $self->leave_sub($sub); |
---|
683 | return $rc; |
---|
684 | } |
---|
685 | |
---|
686 | sub test_wwwmech( $$$$$$ ) { |
---|
687 | my($self,$type,$okrc,$msg,$srcurl,$destfile) = @_; |
---|
688 | my $sub = "test_wwwmech"; |
---|
689 | $self->enter_sub($sub); |
---|
690 | my $rc = 0; |
---|
691 | my $out; |
---|
692 | |
---|
693 | if( $okrc eq "" ) { |
---|
694 | $okrc = 1; |
---|
695 | } |
---|
696 | my $mech = WWW::Mechanize->new(); |
---|
697 | $mech->get("$srcurl", ':content_file' => "$destfile"); |
---|
698 | $rc = $mech->success(); |
---|
699 | $out = $mech->status(); |
---|
700 | |
---|
701 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
702 | $self->log_and_cont("INFO",$sub,"Fetched $srcurl to $destfile with rc $rc and output $out"); |
---|
703 | } |
---|
704 | |
---|
705 | if($rc == $okrc) { |
---|
706 | $self->ok_msg($msg); |
---|
707 | $rc = 1; |
---|
708 | } |
---|
709 | else { |
---|
710 | $self->fail_msg($msg); |
---|
711 | $rc = 0; |
---|
712 | } |
---|
713 | |
---|
714 | $self->leave_sub($sub); |
---|
715 | return $rc; |
---|
716 | } |
---|
717 | |
---|
718 | sub test_chmod( $$$$$$ ) { |
---|
719 | my($self,$type,$okrc,$msg,$mode,$file) = @_; |
---|
720 | my $sub = "test_chmod"; |
---|
721 | $self->enter_sub($sub); |
---|
722 | my $rc = 0; |
---|
723 | |
---|
724 | if( $okrc eq "" ) { |
---|
725 | $okrc = 1; |
---|
726 | } |
---|
727 | $rc = chmod($mode,"$file"); |
---|
728 | |
---|
729 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
730 | $self->log_and_cont("INFO",$sub,"chmod'd $file to $mode"); |
---|
731 | } |
---|
732 | |
---|
733 | if($rc == $okrc) { |
---|
734 | $self->ok_msg($msg); |
---|
735 | $rc = 1; |
---|
736 | } |
---|
737 | else { |
---|
738 | $self->fail_msg($msg); |
---|
739 | $rc = 0; |
---|
740 | } |
---|
741 | |
---|
742 | $self->leave_sub($sub); |
---|
743 | return $rc; |
---|
744 | } |
---|
745 | |
---|
746 | sub test_unlink( $$$$$ ) { |
---|
747 | my($self,$type,$okrc,$msg,$file) = @_; |
---|
748 | my $sub = "test_unlink"; |
---|
749 | $self->enter_sub($sub); |
---|
750 | my $rc = 0; |
---|
751 | |
---|
752 | if( $okrc eq "" ) { |
---|
753 | $okrc = 1; |
---|
754 | } |
---|
755 | $rc = unlink($file); |
---|
756 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
757 | $self->log_and_cont("INFO",$sub,"unlink'd $file with rc $rc"); |
---|
758 | } |
---|
759 | |
---|
760 | if($rc >= $okrc) { |
---|
761 | $self->ok_msg($msg); |
---|
762 | $rc = 1; |
---|
763 | } |
---|
764 | else { |
---|
765 | $self->fail_msg($msg); |
---|
766 | $rc = 0; |
---|
767 | } |
---|
768 | |
---|
769 | $self->leave_sub($sub); |
---|
770 | return $rc; |
---|
771 | } |
---|
772 | |
---|
773 | # Do we even want this function? Goes against one-test-per-action philosophy |
---|
774 | sub test_unlinkall( $$$$$ ) { |
---|
775 | my($self,$type,$okrc,$msg,$dir) = @_; |
---|
776 | my $sub = "test_unlinkall"; |
---|
777 | $self->enter_sub($sub); |
---|
778 | my $rc = 0; |
---|
779 | |
---|
780 | if( $okrc eq "" ) { |
---|
781 | $okrc = 1; |
---|
782 | } |
---|
783 | my @files = <$dir/*>; |
---|
784 | $rc = unlink(@files); |
---|
785 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
786 | $self->log_and_cont("INFO",$sub,"Unlink'd files in $dir with rc $rc"); |
---|
787 | } |
---|
788 | |
---|
789 | $msg .= " Deleted $rc files out of $#files total files."; |
---|
790 | |
---|
791 | if($rc >= $okrc && $rc == $#files) { |
---|
792 | $self->ok_msg($msg); |
---|
793 | $rc = 1; |
---|
794 | } |
---|
795 | else { |
---|
796 | $self->fail_msg($msg); |
---|
797 | $rc = 0; |
---|
798 | } |
---|
799 | |
---|
800 | $self->leave_sub($sub); |
---|
801 | return $rc; |
---|
802 | } |
---|
803 | |
---|
804 | sub test_symlink( $$$$$$ ) { |
---|
805 | my($self,$type,$okrc,$msg,$srcfile,$destfile) = @_; |
---|
806 | my $sub = "test_symlink"; |
---|
807 | $self->enter_sub($sub); |
---|
808 | my $rc = 0; |
---|
809 | |
---|
810 | if( $okrc eq "" ) { |
---|
811 | $okrc = 1; |
---|
812 | } |
---|
813 | $rc = symlink($srcfile,$destfile); |
---|
814 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
815 | $self->log_and_cont("INFO",$sub,"Symlink'd $srcfile to $destfile with rc $rc"); |
---|
816 | } |
---|
817 | |
---|
818 | if($rc == $okrc) { |
---|
819 | $self->ok_msg($msg); |
---|
820 | $rc = 1; |
---|
821 | } |
---|
822 | else { |
---|
823 | $self->fail_msg($msg); |
---|
824 | $rc = 0; |
---|
825 | } |
---|
826 | |
---|
827 | $self->leave_sub($sub); |
---|
828 | return $rc; |
---|
829 | } |
---|
830 | |
---|
831 | sub test_fcopy( $$$$$$ ) { |
---|
832 | my($self,$type,$okrc,$msg,$srcfile,$destfile) = @_; |
---|
833 | my $sub = "test_fcopy"; |
---|
834 | $self->enter_sub($sub); |
---|
835 | my $rc = 0; |
---|
836 | |
---|
837 | if( $okrc eq "" ) { |
---|
838 | $okrc = 1; |
---|
839 | } |
---|
840 | $rc = copy($srcfile,$destfile); |
---|
841 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
842 | $self->log_and_cont("INFO",$sub,"Copied $srcfile to $destfile with rc $rc"); |
---|
843 | } |
---|
844 | |
---|
845 | if($rc == $okrc) { |
---|
846 | $self->ok_msg($msg); |
---|
847 | $rc = 1; |
---|
848 | } |
---|
849 | else { |
---|
850 | $self->fail_msg($msg); |
---|
851 | $rc = 0; |
---|
852 | } |
---|
853 | |
---|
854 | $self->leave_sub($sub); |
---|
855 | return $rc; |
---|
856 | } |
---|
857 | |
---|
858 | sub test_fmove( $$$$$$ ) { |
---|
859 | my($self,$type,$okrc,$msg,$srcfile,$destfile) = @_; |
---|
860 | my $sub = "test_fmove"; |
---|
861 | $self->enter_sub($sub); |
---|
862 | my $rc = 0; |
---|
863 | |
---|
864 | if( $okrc eq "" ) { |
---|
865 | $okrc = 1; |
---|
866 | } |
---|
867 | $rc = move($srcfile,$destfile); |
---|
868 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
869 | $self->log_and_cont("INFO",$sub,"Moved $srcfile to $destfile with rc $rc"); |
---|
870 | } |
---|
871 | |
---|
872 | if($rc == $okrc) { |
---|
873 | $self->ok_msg($msg); |
---|
874 | $rc = 1; |
---|
875 | } |
---|
876 | else { |
---|
877 | $self->fail_msg($msg); |
---|
878 | $rc = 0; |
---|
879 | } |
---|
880 | |
---|
881 | $self->leave_sub($sub); |
---|
882 | return $rc; |
---|
883 | } |
---|
884 | |
---|
885 | sub test_getsvnrev( $$$$$ ) { |
---|
886 | my($self,$type,$okrc,$msg,$websvn) = @_; |
---|
887 | my $sub = "test_getsvnrev"; |
---|
888 | $self->enter_sub($sub); |
---|
889 | my $rc = 0; |
---|
890 | |
---|
891 | if( $okrc eq "" ) { |
---|
892 | $okrc = 1; |
---|
893 | } |
---|
894 | $rc = $self->get_svn_rev($websvn); |
---|
895 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
896 | $self->log_and_cont("INFO",$sub,"Got rev $rc from $websvn"); |
---|
897 | } |
---|
898 | |
---|
899 | if($rc >= $okrc) { |
---|
900 | $self->ok_msg($msg); |
---|
901 | } |
---|
902 | else { |
---|
903 | $self->fail_msg($msg); |
---|
904 | $rc = 0; |
---|
905 | } |
---|
906 | |
---|
907 | $self->leave_sub($sub); |
---|
908 | return $rc; |
---|
909 | } |
---|
910 | |
---|
911 | sub test_fwrite( $$$$$$$ ) { |
---|
912 | my($self,$type,$okrc,$msg,$mode,$file,$text) = @_; |
---|
913 | my($sub,$FILE); |
---|
914 | $sub = "test_fwrite"; |
---|
915 | $self->enter_sub($sub); |
---|
916 | my $rc = 0; |
---|
917 | my $temprc; |
---|
918 | |
---|
919 | if( $okrc eq "" ) { |
---|
920 | $okrc = 2; |
---|
921 | } |
---|
922 | |
---|
923 | if( "$mode" =~ /^w$/ ) { |
---|
924 | $rc += open($FILE, '>', $file) or $self->log_and_die("ERROR", $sub, "Opening file $file for replace&write failed with return $?: $!"); |
---|
925 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
926 | $self->log_and_cont("INFO",$sub,"Opened file $file for replace&write."); |
---|
927 | } |
---|
928 | } |
---|
929 | elsif( "$mode" =~ m/^a$/ ) { |
---|
930 | $rc += open($FILE, '>>', $file) or $self->log_and_die("ERROR",$sub, "Opening file $file for appending failed with return $?, rc $rc: $!"); |
---|
931 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
932 | $self->log_and_cont("INFO",$sub,"Opened file $file for appending."); |
---|
933 | } |
---|
934 | } |
---|
935 | else { |
---|
936 | $self->log_and_die("ERROR",$sub,"Unknown write option: $mode!"); |
---|
937 | } |
---|
938 | |
---|
939 | $temprc = print $FILE "$text\n"; |
---|
940 | $self->log_and_cont("WARN", $sub, "Writing to filehandle FILE (file $file) failed with return $?, rc $rc, errno $!.") if(!$temprc); |
---|
941 | $rc += $temprc; |
---|
942 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
943 | $self->log_and_cont("INFO",$sub,"Wrote text to filehandle FILE."); |
---|
944 | } |
---|
945 | |
---|
946 | $rc += close($FILE) or $self->log_and_die("ERROR", $sub,"Can't close file handle FILE (file $file): $!"); |
---|
947 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
948 | $self->log_and_cont("INFO",$sub,"Closed filehandle FILE (file $file)."); |
---|
949 | } |
---|
950 | |
---|
951 | if($rc >= $okrc) { |
---|
952 | $self->ok_msg($msg); |
---|
953 | $rc = 1; |
---|
954 | } |
---|
955 | else { |
---|
956 | $self->fail_msg($msg); |
---|
957 | $rc = 0; |
---|
958 | } |
---|
959 | |
---|
960 | $self->leave_sub($sub); |
---|
961 | return $rc; |
---|
962 | } |
---|
963 | |
---|
964 | sub test_revfetch( $$$$$$$ ) { |
---|
965 | my($self,$type,$okrc,$msg,$svnrev,$url,$destfile) = @_; |
---|
966 | my $sub = "test_revfetch"; |
---|
967 | $self->enter_sub($sub); |
---|
968 | my $rc = 0; |
---|
969 | my($out,$cmd); |
---|
970 | |
---|
971 | if( $okrc eq "" ) { |
---|
972 | $okrc = 0; |
---|
973 | } |
---|
974 | |
---|
975 | $cmd = "svn cat -r $svnrev $url > $destfile"; |
---|
976 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
977 | $self->log_and_cont("INFO",$sub,"Executing $cmd"); |
---|
978 | } |
---|
979 | ($rc,$out) = $self->exec_system("$cmd"); |
---|
980 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
981 | $self->log_and_cont("INFO",$sub,"$cmd returned rc $rc with output $out"); |
---|
982 | } |
---|
983 | |
---|
984 | if($rc == $okrc) { |
---|
985 | $self->ok_msg($msg); |
---|
986 | $rc = 1; |
---|
987 | } |
---|
988 | else { |
---|
989 | $self->fail_msg("$msg: $out,$rc"); |
---|
990 | $self->test_unlink($type,"","Unlinking $destfile from url $url at rev $svnrev due to failure.",$destfile); |
---|
991 | $rc = 0; |
---|
992 | } |
---|
993 | |
---|
994 | $self->leave_sub($sub); |
---|
995 | return $rc; |
---|
996 | } |
---|
997 | |
---|
998 | sub test_rename( $$$$$$ ) { |
---|
999 | my($self,$type,$okrc,$msg,$srcfile,$destfile) = @_; |
---|
1000 | my $sub = "test_rename"; |
---|
1001 | $self->enter_sub($sub); |
---|
1002 | my $rc; |
---|
1003 | |
---|
1004 | if( $okrc eq "" ) { |
---|
1005 | $okrc = 1; |
---|
1006 | } |
---|
1007 | $rc = rename("$srcfile","$destfile"); |
---|
1008 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1009 | $self->log_and_cont("INFO",$sub,"Renamed $srcfile to $destfile with rc $rc"); |
---|
1010 | } |
---|
1011 | |
---|
1012 | if($rc == $okrc) { |
---|
1013 | $self->ok_msg($msg); |
---|
1014 | $rc = 1; |
---|
1015 | } |
---|
1016 | else { |
---|
1017 | $self->fail_msg($msg); |
---|
1018 | $rc = 0; |
---|
1019 | } |
---|
1020 | |
---|
1021 | $self->leave_sub($sub); |
---|
1022 | return $rc; |
---|
1023 | } |
---|
1024 | |
---|
1025 | sub test_recrevfetch( $$$$$$ ) { |
---|
1026 | my($self,$type,$okrc,$msg,$svnrev,$svndir) = @_; |
---|
1027 | my $sub = "test_recrevfetch"; |
---|
1028 | $self->enter_sub($sub); |
---|
1029 | my($rc,$out,$cmd); |
---|
1030 | |
---|
1031 | if( $okrc eq "" ) { |
---|
1032 | $okrc = 0; |
---|
1033 | } |
---|
1034 | |
---|
1035 | $cmd = "svn --force export -r $svnrev $svndir"; |
---|
1036 | ($rc,$out) = $self->exec_system("$cmd"); |
---|
1037 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1038 | $self->log_and_cont("DEBUG",$sub,"Fetched from SVN with command $cmd and rc $rc"); |
---|
1039 | } |
---|
1040 | |
---|
1041 | if($rc == $okrc) { |
---|
1042 | $self->ok_msg($msg); |
---|
1043 | $rc = 1; |
---|
1044 | } |
---|
1045 | else { |
---|
1046 | $self->fail_msg($msg); |
---|
1047 | $rc = 0; |
---|
1048 | } |
---|
1049 | |
---|
1050 | $self->leave_sub($sub); |
---|
1051 | return $rc; |
---|
1052 | } |
---|
1053 | |
---|
1054 | sub test_rmtree( $$$$$ ) { |
---|
1055 | my($self,$type,$okrc,$msg,$dir) = @_; |
---|
1056 | my $sub = "test_rmtree"; |
---|
1057 | $self->enter_sub($sub); |
---|
1058 | my $rc; |
---|
1059 | |
---|
1060 | if( $okrc eq "" ) { |
---|
1061 | $okrc = 1; |
---|
1062 | } |
---|
1063 | $rc = rmtree("$dir",0,0); |
---|
1064 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1065 | $self->log_and_cont("INFO",$sub,"Removed $dir tree with rc $rc"); |
---|
1066 | } |
---|
1067 | |
---|
1068 | if($rc >= $okrc) { |
---|
1069 | $self->ok_msg($msg); |
---|
1070 | $rc = 1; |
---|
1071 | } |
---|
1072 | else { |
---|
1073 | $self->fail_msg($msg); |
---|
1074 | $rc = -1; |
---|
1075 | } |
---|
1076 | |
---|
1077 | $self->leave_sub($sub); |
---|
1078 | return $rc; |
---|
1079 | } |
---|
1080 | |
---|
1081 | sub test_getuseruid( $$$$$ ) { |
---|
1082 | my($self,$type,$okrc,$msg,$user) = @_; |
---|
1083 | my $sub = "test_getuseruid"; |
---|
1084 | $self->enter_sub($sub); |
---|
1085 | my $rc; |
---|
1086 | |
---|
1087 | if( $okrc eq "" ) { |
---|
1088 | $okrc = 1; |
---|
1089 | } |
---|
1090 | |
---|
1091 | (undef,undef,$rc,undef) = getpwnam("$user") or $self->log_and_die("ERROR",$sub,"Can't find $user in user database for user lookup: $!"); |
---|
1092 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1093 | $self->log_and_cont("INFO",$sub,"getpwnam $user returned with rc $rc"); |
---|
1094 | } |
---|
1095 | |
---|
1096 | if($rc >= $okrc) { |
---|
1097 | $self->ok_msg($msg); |
---|
1098 | } |
---|
1099 | else { |
---|
1100 | $self->fail_msg($msg); |
---|
1101 | $rc = -1; |
---|
1102 | } |
---|
1103 | |
---|
1104 | $self->leave_sub($sub); |
---|
1105 | return $rc; |
---|
1106 | } |
---|
1107 | |
---|
1108 | sub test_getusergid( $$$$$ ) { |
---|
1109 | my($self,$type,$okrc,$msg,$user) = @_; |
---|
1110 | my $sub = "test_getusergid"; |
---|
1111 | $self->enter_sub($sub); |
---|
1112 | my $rc; |
---|
1113 | |
---|
1114 | if( $okrc eq "" ) { |
---|
1115 | $okrc = 1; |
---|
1116 | } |
---|
1117 | |
---|
1118 | (undef,undef,undef,$rc) = getpwnam("$user") or $self->log_and_die("ERROR",$sub,"Can't find $user in user database for group lookup: $!"); |
---|
1119 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1120 | $self->log_and_cont("INFO",$sub,"getpwnam $user returned with rc $rc"); |
---|
1121 | } |
---|
1122 | |
---|
1123 | if($rc >= $okrc) { |
---|
1124 | $self->ok_msg($msg); |
---|
1125 | } |
---|
1126 | else { |
---|
1127 | $self->fail_msg($msg); |
---|
1128 | $rc = 0; |
---|
1129 | } |
---|
1130 | |
---|
1131 | $self->leave_sub($sub); |
---|
1132 | return $rc; |
---|
1133 | } |
---|
1134 | |
---|
1135 | sub test_lsofkill( $$$$$ ) { |
---|
1136 | my($self,$type,$okrc,$msg,$dirname) = @_; |
---|
1137 | my $sub = "test_lsofkill"; |
---|
1138 | $self->enter_sub($sub); |
---|
1139 | my(@pids,@pnames,@lsof); |
---|
1140 | my($ppid,$rc,$i); |
---|
1141 | if( $okrc eq "" ) { |
---|
1142 | $okrc = 2; |
---|
1143 | } |
---|
1144 | |
---|
1145 | $rc = 0; |
---|
1146 | open(my $LSOF, "lsof|") or $self->log_and_die("ERROR",$sub,"Opening lsof for piping failed with return $?, rc $rc: $!"); |
---|
1147 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1148 | $self->log_and_cont("INFO",$sub,"Running lsof|"); |
---|
1149 | } |
---|
1150 | $rc += $?; |
---|
1151 | while( @lsof = split('\s+', <$LSOF> ) ) { |
---|
1152 | if($self->is_log($DEBUG)) { |
---|
1153 | $self->log_and_cont("DEBUG",$sub,"Got @lsof from lsof"); |
---|
1154 | } |
---|
1155 | if( $lsof[8] && $lsof[8] =~ m/$dirname/ && !($lsof[1] =~ m/(?:$$|getppid())/) && !($lsof[0] =~ m/lsof/) && !$self->in_list(\@pids,$lsof[1]) ) { |
---|
1156 | $rc += kill(15,$lsof[1]); |
---|
1157 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1158 | $self->log_and_cont("INFO",$sub,"Killed $lsof[1]"); |
---|
1159 | } |
---|
1160 | push(@pnames,$lsof[0]); |
---|
1161 | push(@pids,$lsof[1]); |
---|
1162 | } |
---|
1163 | } |
---|
1164 | $rc += close($LSOF); |
---|
1165 | for($i=0;$i<$#pnames;$i++) { |
---|
1166 | $msg .= " $pnames[$i]:$pids[$i]"; |
---|
1167 | } |
---|
1168 | $msg .= "\n"; |
---|
1169 | |
---|
1170 | if($rc >= $okrc) { |
---|
1171 | $self->ok_msg($msg); |
---|
1172 | $rc = 1; |
---|
1173 | } |
---|
1174 | else { |
---|
1175 | $self->fail_msg($msg); |
---|
1176 | $rc = 0; |
---|
1177 | } |
---|
1178 | |
---|
1179 | $self->leave_sub($sub); |
---|
1180 | return $rc; |
---|
1181 | } |
---|
1182 | |
---|
1183 | sub test_chown( $$$$$$$ ) { |
---|
1184 | my($self,$type,$okrc,$msg,$user,$group,$path) = @_; |
---|
1185 | my $sub = "test_chown"; |
---|
1186 | $self->enter_sub($sub); |
---|
1187 | my $rc; |
---|
1188 | |
---|
1189 | if( $okrc eq "" ) { |
---|
1190 | $okrc = 0; |
---|
1191 | } |
---|
1192 | |
---|
1193 | $rc = chown($user,$group,$path); |
---|
1194 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1195 | $self->log_and_cont("INFO",$sub,"chown'd $path to $user:$group"); |
---|
1196 | } |
---|
1197 | |
---|
1198 | if($rc > $okrc) { |
---|
1199 | $self->ok_msg($msg); |
---|
1200 | } |
---|
1201 | else { |
---|
1202 | $self->fail_msg($msg); |
---|
1203 | $rc = -1; |
---|
1204 | } |
---|
1205 | |
---|
1206 | if($self->is_log($DEBUG)) { |
---|
1207 | $self->log_and_cont("DEBUG",$sub,"Leaving test_chown"); |
---|
1208 | } |
---|
1209 | return $rc; |
---|
1210 | } |
---|
1211 | |
---|
1212 | sub test_rsync( $$$$$$ ) { |
---|
1213 | my($self,$type,$okrc,$msg,$src,$dst) = @_; |
---|
1214 | my $sub = "test_rsync"; |
---|
1215 | $self->enter_sub($sub); |
---|
1216 | my($rc,$out,$cmd); |
---|
1217 | |
---|
1218 | if( $okrc eq "" ) { |
---|
1219 | $okrc = 0; |
---|
1220 | } |
---|
1221 | |
---|
1222 | $cmd = "rsync -av $src $dst"; |
---|
1223 | ($rc,$out) = $self->exec_system("$cmd"); |
---|
1224 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1225 | $self->log_and_cont("INFO",$sub,"Ran $cmd with rc $rc and output $out"); |
---|
1226 | } |
---|
1227 | |
---|
1228 | if($rc == $okrc) { |
---|
1229 | $self->ok_msg($msg); |
---|
1230 | $rc = 1; |
---|
1231 | } |
---|
1232 | else { |
---|
1233 | $self->fail_msg("$msg,$out"); |
---|
1234 | $rc = 0; |
---|
1235 | } |
---|
1236 | |
---|
1237 | $self->leave_sub($sub); |
---|
1238 | return $rc; |
---|
1239 | } |
---|
1240 | |
---|
1241 | # Type will define what function is run |
---|
1242 | # This function should be moved into Dc.pm once all tests are entered |
---|
1243 | sub run_test { |
---|
1244 | my $self = shift; |
---|
1245 | my @args = @_; |
---|
1246 | my $sub = "run_test"; |
---|
1247 | $self->enter_sub($sub); |
---|
1248 | my $metatests = 3; |
---|
1249 | my($rc,$out,$type,$okrc,$msg,$i); |
---|
1250 | my @cmds; |
---|
1251 | |
---|
1252 | if($#args < $metatests ) { # there must be at least one command |
---|
1253 | $self->log_and_die("ERROR",$sub,"Not enough arguments to run_test! Minimum of $metatests."); |
---|
1254 | } |
---|
1255 | |
---|
1256 | $type = $args[0]; |
---|
1257 | $okrc = $args[1]; |
---|
1258 | $msg = $args[2]; |
---|
1259 | |
---|
1260 | @cmds = splice(@args,$metatests); |
---|
1261 | |
---|
1262 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1263 | $self->log_and_cont("INFO",$sub,"Running test $type"); |
---|
1264 | } |
---|
1265 | if( $type =~ m/^system$/ ) { |
---|
1266 | if( $#cmds != 0 ) { |
---|
1267 | $self->cmd_num_die(@cmds); |
---|
1268 | } |
---|
1269 | ($out,$rc) = $self->test_system($type,$okrc,$msg,$cmds[0]); |
---|
1270 | } |
---|
1271 | elsif( $type =~ m/^chdir$/ ) { |
---|
1272 | if( $#cmds != 0 ) { |
---|
1273 | $self->cmd_num_die(@cmds); |
---|
1274 | } |
---|
1275 | $rc = $self->test_chdir($type,$okrc,$msg,$cmds[0]); |
---|
1276 | } |
---|
1277 | elsif( $type =~ m/^mkpath$/ ) { |
---|
1278 | if( $#cmds != 0 ) { |
---|
1279 | $self->cmd_num_die(@cmds); |
---|
1280 | } |
---|
1281 | $rc = $self->test_mkpath($type,$okrc,$msg,$cmds[0]); |
---|
1282 | } |
---|
1283 | elsif( $type =~ m/^wwwmech$/ ) { |
---|
1284 | if( $#cmds != 1 ) { |
---|
1285 | $self->cmd_num_die(@cmds); |
---|
1286 | } |
---|
1287 | $rc = $self->test_wwwmech($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1288 | } |
---|
1289 | elsif( $type =~ m/^chmod$/ ) { |
---|
1290 | if( $#cmds != 1 ) { |
---|
1291 | $self->cmd_num_die(@cmds); |
---|
1292 | } |
---|
1293 | $rc = $self->test_chmod($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1294 | } |
---|
1295 | elsif( $type =~ m/^unlink$/ ) { |
---|
1296 | if( $#cmds != 0 ) { |
---|
1297 | $self->cmd_num_die(@cmds); |
---|
1298 | } |
---|
1299 | $rc = $self->test_unlink($type,$okrc,$msg,$cmds[0]); |
---|
1300 | } |
---|
1301 | elsif( $type =~ m/^unlinkall$/ ) { |
---|
1302 | if( $#cmds != 0 ) { |
---|
1303 | $self->cmd_num_die(@cmds); |
---|
1304 | } |
---|
1305 | $rc = $self->test_unlinkall($type,$okrc,$msg,$cmds[0]); |
---|
1306 | } |
---|
1307 | elsif( $type =~ m/^symlink$/ ) { |
---|
1308 | if( $#cmds != 1 ) { |
---|
1309 | $self->cmd_num_die(@cmds); |
---|
1310 | } |
---|
1311 | $rc = $self->test_symlink($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1312 | } |
---|
1313 | elsif ( $type =~ m/^fcopy$/ ) { |
---|
1314 | if( $#cmds != 1 ) { |
---|
1315 | $self->cmd_num_die(@cmds); |
---|
1316 | } |
---|
1317 | $rc = $self->test_fcopy($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1318 | } |
---|
1319 | elsif( $type =~ m/^getsvnrev$/ ) { |
---|
1320 | if( $#cmds != 0 ) { |
---|
1321 | $self->cmd_num_die(@cmds); |
---|
1322 | } |
---|
1323 | $rc = $self->test_getsvnrev($type,$okrc,$msg,$cmds[0]); |
---|
1324 | } |
---|
1325 | elsif( $type =~ m/^fwrite$/ ) { |
---|
1326 | if( $#cmds != 2 ) { |
---|
1327 | $self->cmd_num_die(@cmds); |
---|
1328 | } |
---|
1329 | $rc = $self->test_fwrite($type,$okrc,$msg,$cmds[0],$cmds[1],$cmds[2]); |
---|
1330 | } |
---|
1331 | elsif( $type =~ m/^revfetch$/ ) { |
---|
1332 | if( $#cmds != 2 ) { |
---|
1333 | $self->cmd_num_die(@cmds); |
---|
1334 | } |
---|
1335 | $rc = $self->test_revfetch($type,$okrc,$msg,$cmds[0],$cmds[1],$cmds[2]); |
---|
1336 | } |
---|
1337 | elsif( $type =~ m/^recrevfetch$/ ) { |
---|
1338 | if( $#cmds != 1 ) { |
---|
1339 | $self->cmd_num_die(@cmds); |
---|
1340 | } |
---|
1341 | $rc = $self->test_recrevfetch($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1342 | } |
---|
1343 | elsif( $type =~ m/^rename$/ ) { |
---|
1344 | if( $#cmds != 1 ) { |
---|
1345 | $self->cmd_num_die(@cmds); |
---|
1346 | } |
---|
1347 | $rc = $self->test_rename($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1348 | } |
---|
1349 | elsif( $type =~ m/^rmtree$/ ) { |
---|
1350 | if($#cmds != 0) { |
---|
1351 | $self->cmd_num_die(@cmds); |
---|
1352 | } |
---|
1353 | $rc = $self->test_rmtree($type,$okrc,$msg,$cmds[0]); |
---|
1354 | } |
---|
1355 | elsif( $type =~ m/^lsofkill$/ ) { |
---|
1356 | if( $#cmds != 0 ) { |
---|
1357 | $self->cmd_num_die(@cmds); |
---|
1358 | } |
---|
1359 | $rc = $self->test_lsofkill($type,$okrc,$msg,$cmds[0]); |
---|
1360 | } |
---|
1361 | elsif( $type =~ m/^getuseruid$/ ) { |
---|
1362 | if( $#cmds != 0 ) { |
---|
1363 | $self->cmd_num_die(@cmds); |
---|
1364 | } |
---|
1365 | $rc = $self->test_getuseruid($type,$okrc,$msg,$cmds[0]); |
---|
1366 | } |
---|
1367 | elsif( $type =~ m/getusergid$/ ) { |
---|
1368 | if( $#cmds != 0 ) { |
---|
1369 | $self->cmd_num_die(@cmds); |
---|
1370 | } |
---|
1371 | $rc = $self->test_getusergid($type,$okrc,$msg,$cmds[0]); |
---|
1372 | } |
---|
1373 | elsif( $type =~ m/^chown$/ ) { |
---|
1374 | if( $#cmds != 2 ) { |
---|
1375 | $self->cmd_num_die(@cmds); |
---|
1376 | } |
---|
1377 | $rc = $self->test_chown($type,$okrc,$msg,$cmds[0],$cmds[1],$cmds[2]); |
---|
1378 | } |
---|
1379 | elsif( $type =~ m/^fmove$/ ) { |
---|
1380 | if( $#cmds != 1 ) { |
---|
1381 | $self->cmd_num_die(@cmds); |
---|
1382 | } |
---|
1383 | $rc = $self->test_fmove($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1384 | } |
---|
1385 | elsif( $type =~ m/^rsync$/ ) { |
---|
1386 | if( $#cmds != 1 ) { |
---|
1387 | $self->cmd_num_die(@cmds); |
---|
1388 | } |
---|
1389 | $rc = $self->test_rsync($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1390 | } |
---|
1391 | elsif($type =~ m/^rm_lvm$/ ) { |
---|
1392 | $rc = $self->test_rm_lvm($type,$okrc,$msg); |
---|
1393 | } |
---|
1394 | elsif($type =~ m/^regexsub_file$/) { |
---|
1395 | if( $#cmds != 2 ) { |
---|
1396 | $self->cmd_num_die(@cmds); |
---|
1397 | } |
---|
1398 | $rc = $self->test_regexsub_file($type,$okrc,$msg,$cmds[0],$cmds[1],$cmds[2]); |
---|
1399 | } |
---|
1400 | elsif($type =~ m/^read_yaml$/) { |
---|
1401 | if( $#cmds != 0 ) { |
---|
1402 | $self->cmd_num_die(@cmds); |
---|
1403 | } |
---|
1404 | $rc = $self->test_read_yaml($type,$okrc,$msg,$cmds[0]); |
---|
1405 | } |
---|
1406 | elsif($type =~ m/^mknods$/) { |
---|
1407 | if( $#cmds != 1 ) { |
---|
1408 | $self->cmd_num_die(@cmds); |
---|
1409 | } |
---|
1410 | $rc = $self->test_mknods($type,$okrc,$msg,$cmds[0],$cmds[1]); |
---|
1411 | } |
---|
1412 | else { |
---|
1413 | $self->log_and_die("ERROR",$sub,"This is an undefined test: $type!"); |
---|
1414 | } |
---|
1415 | |
---|
1416 | incr_total($self); |
---|
1417 | if($rc) { |
---|
1418 | incr_passed($self); |
---|
1419 | } |
---|
1420 | |
---|
1421 | $self->leave_sub($sub); |
---|
1422 | if(defined($out)) { |
---|
1423 | return($out,$rc); |
---|
1424 | } else { |
---|
1425 | return $rc; |
---|
1426 | } |
---|
1427 | } |
---|
1428 | |
---|
1429 | sub exec_system( $$ ) { |
---|
1430 | my($self,$cmd) = @_; |
---|
1431 | my $sub = "exec_system"; |
---|
1432 | $self->enter_sub($sub); |
---|
1433 | my($out,$rc); |
---|
1434 | |
---|
1435 | $out = `$cmd 2>&1`; |
---|
1436 | $rc = WEXITSTATUS($?); |
---|
1437 | |
---|
1438 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1439 | $self->log_and_cont("INFO",$sub,"Ran $cmd with rc $rc and output $out"); |
---|
1440 | } |
---|
1441 | |
---|
1442 | $self->leave_sub($sub); |
---|
1443 | return ($rc,$out); |
---|
1444 | } |
---|
1445 | |
---|
1446 | sub print_hash ( $% ) { |
---|
1447 | my($self,%h) = @_; |
---|
1448 | my $sub = "print_hash"; |
---|
1449 | $self->enter_sub($sub); |
---|
1450 | |
---|
1451 | foreach my $k (sort keys %h) { |
---|
1452 | print "$k => $h{$k}\n"; |
---|
1453 | } |
---|
1454 | $self->leave_sub($sub); |
---|
1455 | } |
---|
1456 | |
---|
1457 | sub mech_error( $$ ) { |
---|
1458 | my($self,$mech) = @_; |
---|
1459 | my $sub = "mech_error"; |
---|
1460 | $self->enter_sub($sub); |
---|
1461 | $self->leave_sub($sub); |
---|
1462 | return "HTTP status: ".$mech->status."\n"; |
---|
1463 | } |
---|
1464 | |
---|
1465 | sub in_list( $$$ ) { |
---|
1466 | my($self,$list_ref,$s) = @_; |
---|
1467 | my $sub = "in_list"; |
---|
1468 | $self->enter_sub($sub); |
---|
1469 | my @list = @{$list_ref}; |
---|
1470 | |
---|
1471 | if( $#list > 0 ) { |
---|
1472 | foreach my $x ( @list ) { |
---|
1473 | if( "$x" eq "$s" ) { |
---|
1474 | $self->leave_sub($sub); |
---|
1475 | return 1; |
---|
1476 | } |
---|
1477 | } |
---|
1478 | $self->leave_sub($sub); |
---|
1479 | return 0; |
---|
1480 | } |
---|
1481 | else { |
---|
1482 | $self->leave_sub($sub); |
---|
1483 | return 0; |
---|
1484 | } |
---|
1485 | } |
---|
1486 | |
---|
1487 | sub get_stage( $ ) { |
---|
1488 | my($self) = @_; |
---|
1489 | my $sub = "get_state"; |
---|
1490 | $self->enter_sub($sub); |
---|
1491 | if( !(-f "/etc/$PROJECT-stage") ) { |
---|
1492 | $self->leave_sub($sub); |
---|
1493 | return "BUILD"; # Should only true for build system |
---|
1494 | } |
---|
1495 | |
---|
1496 | $self->leave_sub($sub); |
---|
1497 | return $self->snarf_file("/etc/$PROJECT-stage"); |
---|
1498 | } |
---|
1499 | |
---|
1500 | sub get_svn_rev( $$ ) { |
---|
1501 | my($self,$svnurl) = @_; |
---|
1502 | my $sub = "get_svn_rev"; |
---|
1503 | $self->enter_sub($sub); |
---|
1504 | my $mech = WWW::Mechanize->new(); |
---|
1505 | |
---|
1506 | $mech->get($svnurl); |
---|
1507 | if( !$mech->success() ) { |
---|
1508 | $self->log_and_die("ERROR","get_svn_rev","Could not fetch $svnurl: $mech->status()!"); |
---|
1509 | } |
---|
1510 | |
---|
1511 | $self->leave_sub($sub); |
---|
1512 | if( ($mech->content( format => 'text' )) =~ m/^svn\s-\sRevision\s+(\d+):/ ) { |
---|
1513 | return $1; |
---|
1514 | } |
---|
1515 | |
---|
1516 | return 0; |
---|
1517 | } |
---|
1518 | |
---|
1519 | sub get_rev( $ ) { |
---|
1520 | my($self) = @_; |
---|
1521 | my $sub = "get_rev"; |
---|
1522 | $self->enter_sub($sub); |
---|
1523 | |
---|
1524 | if( !(-f "/etc/$PROJECT-revision") ) { |
---|
1525 | $self->leave_sub($sub); |
---|
1526 | return 0; # Invalid for build system |
---|
1527 | } |
---|
1528 | |
---|
1529 | $self->leave_sub($sub); |
---|
1530 | return $self->snarf_file("/etc/$PROJECT-revision"); |
---|
1531 | } |
---|
1532 | |
---|
1533 | sub get_project( $ ) { |
---|
1534 | my($self) = @_; |
---|
1535 | my $sub = 'get_project'; |
---|
1536 | $self->enter_sub($sub); |
---|
1537 | |
---|
1538 | $self->leave_sub($sub); |
---|
1539 | return $PROJECT; |
---|
1540 | } |
---|
1541 | |
---|
1542 | sub get_passed( $ ) { |
---|
1543 | my $self = shift; |
---|
1544 | my $sub = "get_passed"; |
---|
1545 | $self->enter_sub($sub); |
---|
1546 | $self->leave_sub($sub); |
---|
1547 | return $passed; |
---|
1548 | } |
---|
1549 | |
---|
1550 | sub get_total( $ ) { |
---|
1551 | my $self = shift; |
---|
1552 | my $sub = "get_total"; |
---|
1553 | $self->enter_sub($sub); |
---|
1554 | $self->leave_sub($sub); |
---|
1555 | return $total; |
---|
1556 | } |
---|
1557 | |
---|
1558 | sub incr_passed { |
---|
1559 | my $self = shift; |
---|
1560 | my $sub = "incr_passed"; |
---|
1561 | $self->enter_sub($sub); |
---|
1562 | $self->leave_sub($sub); |
---|
1563 | $passed++; |
---|
1564 | return $passed; |
---|
1565 | } |
---|
1566 | |
---|
1567 | sub incr_total( $ ) { |
---|
1568 | my $self = shift; |
---|
1569 | my $sub = "incr_total"; |
---|
1570 | $self->enter_sub($sub); |
---|
1571 | $self->leave_sub($sub); |
---|
1572 | $total++; |
---|
1573 | } |
---|
1574 | |
---|
1575 | sub ok_msg( $$ ) { |
---|
1576 | my($self,$msg) = @_; |
---|
1577 | my $sub = "ok_msg"; |
---|
1578 | $self->enter_sub($sub); |
---|
1579 | $self->leave_sub($sub); |
---|
1580 | print "ok ".get_total($self)." - $msg\n"; |
---|
1581 | } |
---|
1582 | |
---|
1583 | sub fail_msg( $$ ) { |
---|
1584 | my($self,$msg) = @_; |
---|
1585 | my $sub = "fail_msg"; |
---|
1586 | $self->enter_sub($sub); |
---|
1587 | $self->leave_sub($sub); |
---|
1588 | print "not ok ".get_total($self)." - $msg\n"; |
---|
1589 | } |
---|
1590 | |
---|
1591 | sub redirect_stdio( $ ) { |
---|
1592 | my $self = shift; |
---|
1593 | my $sub = "redirect_stdio"; |
---|
1594 | $self->enter_sub($sub); |
---|
1595 | my($outdir) = @_; |
---|
1596 | open(STDOUT, '>', "$outdir/$ALLOUTFILE") or |
---|
1597 | $self->log_and_die("ERROR","redirect_stdio","Can't open file $outdir/$ALLOUTFILE: $!"); |
---|
1598 | open(STDERR, ">&STDOUT"); |
---|
1599 | $self->leave_sub($sub); |
---|
1600 | } |
---|
1601 | |
---|
1602 | sub close_stdio( $ ) { |
---|
1603 | my $self = shift; |
---|
1604 | my $sub = "close_stdio"; |
---|
1605 | $self->enter_sub($sub); |
---|
1606 | close(STDERR); |
---|
1607 | close(STDOUT); |
---|
1608 | $self->leave_sub($sub); |
---|
1609 | } |
---|
1610 | |
---|
1611 | sub get_lvmroot( $ ) { |
---|
1612 | my $self = shift; |
---|
1613 | my $sub = "get_lvmroot"; |
---|
1614 | $self->enter_sub($sub); |
---|
1615 | |
---|
1616 | $self->leave_sub($sub); |
---|
1617 | return $LVMROOT; |
---|
1618 | } |
---|
1619 | |
---|
1620 | sub set_debug( $$ ) { |
---|
1621 | my($self,$log) = @_; |
---|
1622 | my $sub = "set_debug"; |
---|
1623 | $self->enter_sub($sub); |
---|
1624 | if($log eq 'INFO') { |
---|
1625 | $LOG |= $INFO; |
---|
1626 | } |
---|
1627 | elsif($log eq 'DEBUG') { |
---|
1628 | $LOG |= $DEBUG; |
---|
1629 | } |
---|
1630 | else { |
---|
1631 | $self->log_and_cont("WARN","set_debug","Unknown log setting $log"); |
---|
1632 | } |
---|
1633 | $self->leave_sub($sub); |
---|
1634 | } |
---|
1635 | |
---|
1636 | sub unset_debug( $$ ) { |
---|
1637 | my($self,$log) = @_; |
---|
1638 | my $sub = "unset_debug"; |
---|
1639 | $self->enter_sub($sub); |
---|
1640 | if($log eq 'INFO') { |
---|
1641 | $LOG &= ~$INFO; |
---|
1642 | } |
---|
1643 | elsif($log eq 'DEBUG') { |
---|
1644 | $LOG &= ~$DEBUG; |
---|
1645 | } |
---|
1646 | else { |
---|
1647 | $self->log_and_cont("WARN","unset_debug","Unknown log setting $log"); |
---|
1648 | } |
---|
1649 | $self->leave_sub($sub); |
---|
1650 | } |
---|
1651 | |
---|
1652 | # No debug statements to avoid circular references now |
---|
1653 | sub is_log( $$ ) { |
---|
1654 | my($self,$log) = @_; |
---|
1655 | return ($LOG & $log); |
---|
1656 | } |
---|
1657 | |
---|
1658 | # Fetch from /proc/cmdline |
---|
1659 | sub get_cmdline( $ ) { |
---|
1660 | my($self) = @_; |
---|
1661 | my $sub = "get_cmdline"; |
---|
1662 | $self->enter_sub($sub); |
---|
1663 | $self->leave_sub($sub); |
---|
1664 | return $self->snarf_file("$CMDLINE_FILE"); |
---|
1665 | } |
---|
1666 | |
---|
1667 | # Parse a value-key tuple out of /proc/cmdline |
---|
1668 | sub parse_cmdline( $$ ) { |
---|
1669 | my($self,$key) = @_; |
---|
1670 | my($sub,$cmdline,$value); |
---|
1671 | $sub = "parse_cmdline"; |
---|
1672 | $self->enter_sub($sub); |
---|
1673 | |
---|
1674 | foreach my $line ( split('\s+',$self->get_cmdline() ) ) { |
---|
1675 | if( $line =~ m/^$key="?(.*?)"?$/ ) { |
---|
1676 | return $1; |
---|
1677 | } |
---|
1678 | elsif($line =~ m/$key/) { |
---|
1679 | return 1; |
---|
1680 | } |
---|
1681 | } |
---|
1682 | |
---|
1683 | $self->leave_sub($sub); |
---|
1684 | return 0; |
---|
1685 | } |
---|
1686 | |
---|
1687 | sub parse_nic_conf( $$ ) { |
---|
1688 | my($self,$cmdline) = @_; |
---|
1689 | my $sub = "parse_nic_conf"; |
---|
1690 | $self->enter_sub($sub); |
---|
1691 | my @nicsconf; |
---|
1692 | |
---|
1693 | if($cmdline =~ m/nics=\"(.*)\"/) { |
---|
1694 | @nicsconf = split ':', $1; |
---|
1695 | } else { |
---|
1696 | $self->leave_sub($sub); |
---|
1697 | return @nicsconf; |
---|
1698 | } |
---|
1699 | |
---|
1700 | $self->leave_sub($sub); |
---|
1701 | return @nicsconf; |
---|
1702 | } |
---|
1703 | |
---|
1704 | sub get_eth_nics( $ ) { |
---|
1705 | my($self) = @_; |
---|
1706 | my $sub = "get_eth_nics"; |
---|
1707 | $self->enter_sub($sub); |
---|
1708 | my $line; |
---|
1709 | my @nics; |
---|
1710 | open(my $IF, "$IFCONFIG |") or $self->log_and_die("ERROR",$sub,"Can't open $IFCONFIG for piping: $!"); |
---|
1711 | |
---|
1712 | while($line = <$IF>) { |
---|
1713 | chomp $line; |
---|
1714 | if($line =~ m/^(eth\d+(\:\d+)*)\s+Link\sencap:Ethernet/) { |
---|
1715 | $self->log_and_cont("INFO",$sub,"Found NIC $1") |
---|
1716 | if($self->is_log($INFO) || $self->is_log($DEBUG)); |
---|
1717 | push(@nics,$1); |
---|
1718 | } |
---|
1719 | } |
---|
1720 | |
---|
1721 | close($IF); |
---|
1722 | |
---|
1723 | $self->leave_sub($sub); |
---|
1724 | |
---|
1725 | return @nics; |
---|
1726 | } |
---|
1727 | |
---|
1728 | sub flash_nic( $$$ ) { |
---|
1729 | my($self,$nic,$sec) = @_; |
---|
1730 | my $sub = "flash_nic"; |
---|
1731 | $self->enter_sub($sub); |
---|
1732 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
1733 | $self->log_and_cont("INFO",$sub,"Flashing NIC $nic for $sec seconds."); |
---|
1734 | } |
---|
1735 | $self->exec_system("ethtool -p $nic $sec"); |
---|
1736 | $self->leave_sub($sub); |
---|
1737 | return WEXITSTATUS($?); |
---|
1738 | } |
---|
1739 | |
---|
1740 | sub conf_nics( $$$ ) { |
---|
1741 | my($self,$nicsconf_ref,$nics_ref) = @_; |
---|
1742 | my $sub; |
---|
1743 | my @auto; |
---|
1744 | $sub = "conf_nics"; |
---|
1745 | $self->enter_sub($sub); |
---|
1746 | my @nicsconf = @{ $nicsconf_ref }; |
---|
1747 | my @nics = @{ $nics_ref }; |
---|
1748 | my($stdin,$rc,$sec); |
---|
1749 | |
---|
1750 | $sec = 10; # Flash NICs for 10 seconds |
---|
1751 | |
---|
1752 | if($#nics < $#nicsconf) { |
---|
1753 | $self->log_and_cont("WARN",$sub,"Fewer NICs than conf options; configuring all we can..."); |
---|
1754 | } |
---|
1755 | |
---|
1756 | push(@auto,'lo'); |
---|
1757 | open(my $INT, '>', $INTFILE) or $self->log_and_die("ERROR",$sub,"Can't open $INTFILE for writing: $!"); |
---|
1758 | print $INT "iface lo inet loopback\n\n"; |
---|
1759 | |
---|
1760 | for(my $i=0;$i<=$#nics;$i++) { |
---|
1761 | print STDERR "We're configuring NIC $i\n"; |
---|
1762 | print STDERR "Plug in the cable where the NIC is flashing. The NIC will flash for $sec seconds.\n"; |
---|
1763 | print STDERR "Doesn't look like you have any flashers, so just take a wild guess where to plug that cable.\n" if($self->flash_nic($nics[$i],10)); |
---|
1764 | push(@auto,$nics[$i]); # All NICs need auto at once |
---|
1765 | if($nicsconf[$i] eq 'dhcp') { |
---|
1766 | print $INT "iface $nics[$i] inet dhcp\n"; |
---|
1767 | } |
---|
1768 | elsif($nicsconf[$i] =~ m/((?:\d{1,3}\.){1,3}\d{1,3})\/((?:\d{1,3}\.){1,3}\d{1,3})/) { |
---|
1769 | print $INT "iface $nics[$i] inet static\n"; |
---|
1770 | print $INT "\taddress $1\n"; |
---|
1771 | print $INT "\tnetmask $2\n"; |
---|
1772 | } |
---|
1773 | print STDERR "Press any key to continue.\n"; |
---|
1774 | $stdin = <STDIN>; |
---|
1775 | print "\n\n"; |
---|
1776 | } |
---|
1777 | print $INT "auto ".reverse(sort(@auto))."\n"; |
---|
1778 | |
---|
1779 | close($INT); |
---|
1780 | $self->leave_sub($sub); |
---|
1781 | } |
---|
1782 | |
---|
1783 | sub nic_dialog { |
---|
1784 | my($self) = @_; |
---|
1785 | my @nics; |
---|
1786 | my $nic_conf; |
---|
1787 | my $sub='nic_dialog'; |
---|
1788 | my $d = new UI::Dialog (backtitle => "Configure NICS", |
---|
1789 | listheight => 10, height => 20); |
---|
1790 | |
---|
1791 | foreach my $nic ($self->get_eth_nics()) { |
---|
1792 | push(@nics,($nic,["",0])); |
---|
1793 | } |
---|
1794 | |
---|
1795 | my @chosen_nics = $d->checklist(text => "Pick NICs to configure.", |
---|
1796 | list => \@nics); |
---|
1797 | if(!$self->is_dialog_ok($d)) { |
---|
1798 | return undef; |
---|
1799 | } |
---|
1800 | |
---|
1801 | foreach my $nic (@chosen_nics) { |
---|
1802 | $nic_conf->{$nic} = $self->config_nic_dialog($d,$nic); |
---|
1803 | if(!defined($nic_conf->{$nic})) { |
---|
1804 | return undef; |
---|
1805 | } |
---|
1806 | } |
---|
1807 | |
---|
1808 | $self->config_interfaces($nic_conf); |
---|
1809 | |
---|
1810 | return $nic_conf; |
---|
1811 | } |
---|
1812 | |
---|
1813 | sub require_bccd_server { |
---|
1814 | my($self) = @_; |
---|
1815 | my($sub,$dhc,$replace,$rc); |
---|
1816 | $sub='require_bccd_server'; |
---|
1817 | |
---|
1818 | $rc = 0; |
---|
1819 | |
---|
1820 | $rc += $self->run_test('unlink','','Unlinking dhclient.conf for BCCD.',$DHCFILE); |
---|
1821 | $rc += $self->run_test('symlink','','Relinking dhclient.conf for BCCD.',"$DHCFILE-bccd",$DHCFILE); |
---|
1822 | |
---|
1823 | return $rc; |
---|
1824 | } |
---|
1825 | |
---|
1826 | sub unrequire_bccd_server { |
---|
1827 | my($self) = @_; |
---|
1828 | my($sub,$dhc,$replace,$rc); |
---|
1829 | $sub='unrequire_bccd_server'; |
---|
1830 | |
---|
1831 | $rc = 0; |
---|
1832 | |
---|
1833 | $rc += $self->run_test('unlink','','Unlinking dhclient.conf for BCCD.',$DHCFILE); |
---|
1834 | $rc += $self->run_test('symlink','','Relinking dhclient.conf for BCCD.',"$DHCFILE-any",$DHCFILE); |
---|
1835 | |
---|
1836 | return $rc; |
---|
1837 | } |
---|
1838 | |
---|
1839 | sub config_interfaces{ |
---|
1840 | my($self,$nic_conf) = @_; |
---|
1841 | my($sub,$rc); |
---|
1842 | my @auto; |
---|
1843 | $sub='config_interfaces'; |
---|
1844 | $self->enter_sub($sub); |
---|
1845 | |
---|
1846 | open(my $INT, '>', $INTFILE) or |
---|
1847 | $self->log_and_die("ERROR",$sub,"Couldn't open $INTFILE: $!"); |
---|
1848 | |
---|
1849 | push(@auto,'lo'); |
---|
1850 | print $INT "iface lo inet loopback\n\n"; |
---|
1851 | |
---|
1852 | foreach my $nic (keys(%{$nic_conf})) { |
---|
1853 | if($nic_conf->{$nic}->{'dhcp'}) { |
---|
1854 | push(@auto,$nic); |
---|
1855 | print $INT "iface $nic inet dhcp\n\n"; |
---|
1856 | if(defined($nic_conf->{$nic}->{'dhcp_source'}) && $nic_conf->{$nic}->{'dhcp_source'} eq 'BCCD') { |
---|
1857 | if($self->require_bccd_server() > 2) { |
---|
1858 | $self->log_and_die("ERROR",$sub,"Couldn't set BCCD server in dhclient."); |
---|
1859 | } |
---|
1860 | } |
---|
1861 | else { |
---|
1862 | if($self->unrequire_bccd_server() > 2) { |
---|
1863 | $self->log_and_die("ERROR",$sub,"Couldn't unset BCCD server in dhclient."); |
---|
1864 | } |
---|
1865 | } |
---|
1866 | } |
---|
1867 | elsif(defined($nic_conf->{$nic}->{'ipaddr'}) && defined($nic_conf->{$nic}->{'mask'})) { |
---|
1868 | push(@auto,$nic); |
---|
1869 | print $INT "iface $nic inet static\n"; |
---|
1870 | print $INT "\taddress $nic_conf->{$nic}->{'ipaddr'}\n"; |
---|
1871 | print $INT "\tnetmask $nic_conf->{$nic}->{'mask'}\n"; |
---|
1872 | if(defined($nic_conf->{$nic}->{'bcast'})) { |
---|
1873 | print $INT "\tbroadcast $nic_conf->{$nic}->{'bcast'}\n"; |
---|
1874 | } |
---|
1875 | if(defined($nic_conf->{$nic}->{'gw'})) { |
---|
1876 | print $INT "\tgateway $nic_conf->{$nic}->{'gw'}\n"; |
---|
1877 | } |
---|
1878 | } |
---|
1879 | } |
---|
1880 | @auto = sort(@auto); |
---|
1881 | print $INT "auto @auto\n"; |
---|
1882 | close($INT); |
---|
1883 | $self->leave_sub($sub); |
---|
1884 | |
---|
1885 | return $nic_conf; |
---|
1886 | } |
---|
1887 | |
---|
1888 | sub check_bccd_net{ |
---|
1889 | my($self,$nic_conf) = @_; |
---|
1890 | my $sub = 'check_bccd_net'; |
---|
1891 | |
---|
1892 | foreach my $nic (keys(%{$nic_conf})) { |
---|
1893 | if(defined($nic_conf->{$nic}->{'dhcp_source'}) && |
---|
1894 | $nic_conf->{$nic}->{dhcp_source} eq 'BCCD') { |
---|
1895 | return 1; |
---|
1896 | } |
---|
1897 | } |
---|
1898 | |
---|
1899 | return undef; |
---|
1900 | } |
---|
1901 | |
---|
1902 | sub config_dhcp{ |
---|
1903 | my($self,$nic_conf) = @_; |
---|
1904 | my($sub,$pubnetip,$j,$oneip,$file,$pubnet,$pxenet,$havedhcp, |
---|
1905 | $bcast,$mask,$i,$rc,$out,$pxenic,$pxenetip,$addr,$dhcpnic, |
---|
1906 | $destfile); |
---|
1907 | $sub = 'config_dhcp'; |
---|
1908 | |
---|
1909 | $havedhcp = 0; |
---|
1910 | FIND_PXE_NIC: |
---|
1911 | foreach my $nic (keys(%{$nic_conf})) { |
---|
1912 | if(defined($nic_conf->{$nic}->{'pxenic'})) { |
---|
1913 | $pxenic = $nic; |
---|
1914 | last FIND_PXE_NIC; |
---|
1915 | } |
---|
1916 | } |
---|
1917 | |
---|
1918 | foreach my $nic (keys(%{$nic_conf})) { |
---|
1919 | if(defined($nic_conf->{$nic}->{'dhcp_source'}) && |
---|
1920 | $nic_conf->{$nic}->{'dhcp_source'} eq 'BCCD') { |
---|
1921 | $havedhcp = 1; |
---|
1922 | } |
---|
1923 | } |
---|
1924 | |
---|
1925 | HAVE_DHCP: foreach my $nic (keys(%{$nic_conf})) { |
---|
1926 | if(defined($nic_conf->{$nic}->{'bccdnet'})) { |
---|
1927 | $dhcpnic = $nic; |
---|
1928 | last HAVE_DHCP; |
---|
1929 | } |
---|
1930 | } |
---|
1931 | |
---|
1932 | if(defined($pxenic)) { |
---|
1933 | $pxenetip = new NetAddr::IP($nic_conf->{$pxenic}->{'ipaddr'},$nic_conf->{$pxenic}->{'mask'}) || |
---|
1934 | $self->log_and_die("ERROR",$sub,"Couldn't create network IP object for $nic_conf->{$pxenic}->{'ipaddr'}: $!"); |
---|
1935 | if(!defined($nic_conf->{$pxenic}->{'gw'})) { |
---|
1936 | $nic_conf->{$pxenic}->{'gw'} = $nic_conf->{$pxenic}->{'ipaddr'}; |
---|
1937 | } |
---|
1938 | } |
---|
1939 | $pubnetip=new NetAddr::IP($BCCD_NET->{'ipaddr'},$BCCD_NET->{'mask'}) || |
---|
1940 | $self->log_and_die("ERROR",$sub,"Couldn't create network IP object for $BCCD_NET->{'ipaddr'}: $!"); |
---|
1941 | |
---|
1942 | $oneip=new NetAddr::IP('0.0.0.1') || # Addition doesn't work the way it should |
---|
1943 | $self->log_and_die("ERROR",$sub,"Couldn't create singleton IP object: $!"); |
---|
1944 | |
---|
1945 | $pubnet->{'network'} = $pubnetip->network(); |
---|
1946 | $pubnet->{'network'} =~ s/\/\d+$//g; |
---|
1947 | $pubnet->{'bcast'} = $pubnetip->broadcast(); |
---|
1948 | $pubnet->{'bcast'} =~ s/\/\d+$//g; |
---|
1949 | $pubnet->{'mask'} = $pubnetip->mask(); |
---|
1950 | if(defined($pxenic)) { |
---|
1951 | $pxenet->{'network'} = $pxenetip->network(); |
---|
1952 | $pxenet->{'network'} =~ s/\/\d+$//g; |
---|
1953 | $pxenet->{'bcast'} = $pxenetip->broadcast(); |
---|
1954 | $pxenet->{'bcast'} =~ s/\/\d+$//g; |
---|
1955 | $pxenet->{'mask'} = $pxenetip->mask(); |
---|
1956 | $pxenet->{'next'} = $pxenetip->addr(); |
---|
1957 | $pxenet->{'first'} = $pxenetip->first(); |
---|
1958 | $pxenet->{'first'} =~ s/\/\d+$//g; |
---|
1959 | $pxenet->{'last'} = $pxenetip->last(); |
---|
1960 | $pxenet->{'last'} =~ s/\/\d+$//g; |
---|
1961 | } |
---|
1962 | |
---|
1963 | open(my $HOSTS,'>','/etc/hosts') or $self->log_and_die("ERROR",$sub,"Can't open file /etc/hosts: $!"); |
---|
1964 | print $HOSTS "127.0.0.1\tlocalhost\n"; |
---|
1965 | |
---|
1966 | $j = 0; |
---|
1967 | # Increment to first DHCP address |
---|
1968 | for($i=1;$i<$DHCP_RANGES->{'res'};$i++) { |
---|
1969 | if(defined($dhcpnic) && $pubnetip->addr() eq $nic_conf->{$dhcpnic}->{'ipaddr'}) { |
---|
1970 | print $HOSTS sprintf("%s\tnode%.3d.bccd.net node%.3d %s %s\t# Reserved IP\n", $pubnetip->addr(), $j, $j, |
---|
1971 | $HOSTNAME, $SHORT_HOSTNAME); |
---|
1972 | } |
---|
1973 | else { |
---|
1974 | print $HOSTS sprintf("%s\tnode%.3d.bccd.net node%.3d\t# Reserved IP\n", $pubnetip->addr(), $j, $j); |
---|
1975 | } |
---|
1976 | $pubnetip++; |
---|
1977 | $j++; |
---|
1978 | } |
---|
1979 | |
---|
1980 | $pubnet->{'dhcprange'} = $pubnetip->addr(); |
---|
1981 | |
---|
1982 | for($i=0;$i<$DHCP_RANGES->{'dhcp'};$i++) { |
---|
1983 | print $HOSTS sprintf("%s\tnode%.3d.bccd.net node%.3d\t#DHCP IP\n", $pubnetip->addr(), $j, $j); |
---|
1984 | $pubnetip++; |
---|
1985 | $j++; |
---|
1986 | } |
---|
1987 | |
---|
1988 | $pubnet->{'dhcprange'} .= " ".$pubnetip->addr(); |
---|
1989 | |
---|
1990 | if(defined($pxenic)) { |
---|
1991 | $i = 0; |
---|
1992 | while( $pxenetip->addr() ne $pxenet->{'last'} ) { |
---|
1993 | print $HOSTS sprintf("%s\tpxenode%.3d.bccd.net pxenode%.3d\t#PXE IP\n", $pxenetip->addr(), $i, $i); |
---|
1994 | $pxenetip++; |
---|
1995 | if($i == 10) { |
---|
1996 | $pxenet->{'firstip'} = $pxenetip->addr(); |
---|
1997 | } |
---|
1998 | elsif($i == 100) { |
---|
1999 | $pxenet->{'lastip'} = $pxenetip->addr(); |
---|
2000 | last; |
---|
2001 | } |
---|
2002 | $i++; |
---|
2003 | } |
---|
2004 | if(!defined($pxenet->{'firstip'}) || !defined($pxenet->{'lastip'})) { |
---|
2005 | $self->log_and_die("ERROR",$sub,"No PXE IP range defined!"); |
---|
2006 | } |
---|
2007 | } |
---|
2008 | close($HOSTS); |
---|
2009 | open(my $DCONF,'>',$DHCP_CONF) || |
---|
2010 | $self->log_and_die("ERROR",$sub,"Can't open file $DHCP_CONF: $!"); |
---|
2011 | |
---|
2012 | print $DCONF "allow bootp;\nallow booting;\n\n"; |
---|
2013 | print $DCONF "subnet $pubnet->{'network'} netmask $pubnet->{'mask'} {\n"; |
---|
2014 | print $DCONF "\toption subnet-mask $pubnet->{'mask'};\n"; |
---|
2015 | print $DCONF "\toption broadcast-address $pubnet->{'bcast'};\n"; |
---|
2016 | print $DCONF "\toption routers $BCCD_NET->{'ipaddr'};\n"; |
---|
2017 | print $DCONF "\tpool {\n"; |
---|
2018 | print $DCONF "\t\tallow members of \"bccd-nodes\";\n"; |
---|
2019 | print $DCONF "\t\trange $pubnet->{'dhcprange'};\n"; |
---|
2020 | print $DCONF "\t}\n"; |
---|
2021 | print $DCONF "}\n"; |
---|
2022 | |
---|
2023 | if(defined($pxenic)) { |
---|
2024 | print $DCONF "subnet $pxenet->{'network'} netmask $pxenet->{'mask'} {\n"; |
---|
2025 | print $DCONF "\toption subnet-mask $pxenet->{'mask'};\n"; |
---|
2026 | print $DCONF "\toption broadcast-address $pxenet->{'bcast'};\n"; |
---|
2027 | print $DCONF "\toption routers $nic_conf->{$pxenic}->{'gw'};\n"; |
---|
2028 | print $DCONF "\tpool {\n"; |
---|
2029 | print $DCONF "\t\trange $pxenet->{'firstip'} $pxenet->{'lastip'};\n"; |
---|
2030 | print $DCONF "\t\tallow members of \"pxelinux-nodes\";\n"; |
---|
2031 | print $DCONF "\t\tfilename \"pxelinux.0\";\n"; |
---|
2032 | print $DCONF "\t\tnext-server $nic_conf->{$pxenic}->{'ipaddr'};\n"; |
---|
2033 | print $DCONF "\t\toption root-path \"$nic_conf->{$pxenic}->{'ipaddr'}:/,nfsvers=3,tcp,hard\";\n"; |
---|
2034 | print $DCONF "\t}\n"; |
---|
2035 | print $DCONF "}\n"; |
---|
2036 | open(my $PCONF, '>', $PXELINUX) || |
---|
2037 | $self->log_and_die("ERROR",$sub,"Can't open file $PXELINUX: $!"); |
---|
2038 | |
---|
2039 | print $PCONF "default bccd\n"; |
---|
2040 | print $PCONF "label bccd\n"; |
---|
2041 | print $PCONF "\tkernel vmlinuz-$KERNREV\n"; |
---|
2042 | print $PCONF "\tappend ETHERNET=eth0 initrd=initramfs-$KERNREV root=/dev/nfs nfsroot=$nic_conf->{$pxenic}->{'ipaddr'}:/ ip=dhcp init=/sbin/init vga=791 lang=us\n"; |
---|
2043 | |
---|
2044 | close($PCONF); |
---|
2045 | if(-d "/diskless/$PROJECT") { |
---|
2046 | open(my $FCONF, '>', $DISKLESS_FSTAB) || |
---|
2047 | $self->log_and_die("ERROR",$sub,"Can't open file $DISKLESS_FSTAB: $!"); |
---|
2048 | |
---|
2049 | print $FCONF "$nic_conf->{$pxenic}->{'ipaddr'}:/bccd/home /bccd/home nfs nfsvers=3,tcp,rsize=32768,wsize=32768,hard,intr 0 0\n"; |
---|
2050 | |
---|
2051 | close($FCONF); |
---|
2052 | } |
---|
2053 | } |
---|
2054 | |
---|
2055 | close($DCONF); |
---|
2056 | |
---|
2057 | if($self->parse_cmdline("recoverdhcp")) { |
---|
2058 | my($recentmach,$i,$latestts,$ft); |
---|
2059 | $ft = new File::Temp(); |
---|
2060 | Readonly my $SLEEP => 60; |
---|
2061 | Readonly my $PWD => getcwd(); |
---|
2062 | my $tempdir = $ft->tempdir("DHCP",CLEANUP => 0); |
---|
2063 | my(undef,undef,$uid,$gid) = getpwnam('bccd'); |
---|
2064 | chown($uid, $gid, $tempdir); |
---|
2065 | $rc = $self->run_test("rmtree","","Removing /etc/network/run",'/etc/network/run'); |
---|
2066 | if($rc) { |
---|
2067 | $self->log_and_cont("ERROR",$sub,"Couldn't remove /etc/network/run"); |
---|
2068 | } |
---|
2069 | $rc = $self->run_test("mkpath","","mkdir /etc/network/run",'/etc/network/run'); |
---|
2070 | if($rc) { |
---|
2071 | $self->log_and_cont("ERROR",$sub,"Couldn't remake /etc/network/run"); |
---|
2072 | } |
---|
2073 | ($out,$rc) = $self->run_test("system","","touch /etc/network/run/ifstate","touch /etc/network/run/ifstate"); |
---|
2074 | if(!$rc) { |
---|
2075 | $self->log_and_cont("ERROR",$sub,"Couldn't touch /etc/network/run/ifstate: $out"); |
---|
2076 | } |
---|
2077 | ($out,$rc) = $self->run_test("system","","Starting networking","/etc/init.d/networking start"); # No invoke-rc.d because utmp has not been updated |
---|
2078 | if(!$rc) { |
---|
2079 | $self->log_and_cont("ERROR",$sub,"Couldn't start networking: $out"); |
---|
2080 | } |
---|
2081 | ($out,$rc) = $self->run_test("system","","Starting snmpd","/usr/sbin/invoke-rc.d snmpd start"); |
---|
2082 | if(!$rc) { |
---|
2083 | $self->log_and_cont("ERROR",$sub,"Couldn't start snmpd: $out"); |
---|
2084 | } |
---|
2085 | ($rc,$out) = $self->run_test("system","","Starting DHCP server","/usr/sbin/invoke-rc.d dhcp3-server stop"); |
---|
2086 | if(!$rc) { |
---|
2087 | $self->log_and_cont("ERROR",$sub,"Couldn't stop DHCP server: $out"); |
---|
2088 | } |
---|
2089 | ($rc,$out) = $self->run_test("system","","Starting sshd","/usr/sbin/invoke-rc.d ssh start"); |
---|
2090 | if(!$rc) { |
---|
2091 | $self->log_and_cont("ERROR",$sub,"Couldn't start ssh: $out"); |
---|
2092 | } |
---|
2093 | ($rc,$out) = $self->run_test("system","","Starting BCCD autodetection",qq{su bccd -c "/bin/bccd-auto-ssh > /tmp/bccd-auto-ssh.out 2>&1" }); |
---|
2094 | |
---|
2095 | $self->log_and_cont("INFO",$sub,"Waiting for responses, sleeping $SLEEP seconds..."); |
---|
2096 | sleep($SLEEP); |
---|
2097 | |
---|
2098 | chdir($tempdir); |
---|
2099 | ($rc,$out) = $self->run_test("system","","Snarfing hosts",qq{su bccd -c "/bin/bccd-snarfhosts $tempdir/machines"}); |
---|
2100 | if($rc) { |
---|
2101 | $self->log_and_cont("ERROR",$sub,"Couldn't snarf hosts, $out"); |
---|
2102 | } |
---|
2103 | |
---|
2104 | open(my $MACHINES, "$tempdir/machines") or |
---|
2105 | $self->log_and_die("ERROR",$sub,"Can't open file $tempdir/machines: $!\n"); |
---|
2106 | $i = $latestts = 0; |
---|
2107 | while(my $line = <$MACHINES>) { |
---|
2108 | chomp $line; |
---|
2109 | my $machine = (split(/\s+/,$line))[0]; |
---|
2110 | if($self->is_log($DEBUG)) { |
---|
2111 | $self->log_and_cont("INFO",$sub,"Processing $machine for DHCP leases"); |
---|
2112 | } |
---|
2113 | if($i++ > 0) { # The head node always appears first, and should not be processed |
---|
2114 | my $leases; |
---|
2115 | $destfile = "$tempdir/$machine"."_dhcpd.leases"; |
---|
2116 | ($rc,$out) = $self->run_test("system","","Copying lease from $machine",qq{su bccd -c "scp $machine:/var/tmp/dhcpd.leases $destfile"}); |
---|
2117 | if(!$rc) { |
---|
2118 | $self->log_and_cont("WARN",$sub,"Couldn't copy lease file from $machine"); |
---|
2119 | } |
---|
2120 | else { |
---|
2121 | $leases = $self->snarf_file($destfile); |
---|
2122 | if(!defined($leases)) { |
---|
2123 | $self->log_and_cont("WARN",$sub,"Couldn't read lease file from $machine"); |
---|
2124 | } |
---|
2125 | if($leases =~ m{^#\s+BCCD TS:\s+(\d+)$}m) { |
---|
2126 | if($1 > $latestts) { |
---|
2127 | $latestts = $1; |
---|
2128 | $recentmach = $machine; |
---|
2129 | if($self->is_log($DEBUG)) { |
---|
2130 | $self->log_and_cont("INFO",$sub,"$machine is most recent"); |
---|
2131 | } |
---|
2132 | } |
---|
2133 | } |
---|
2134 | } |
---|
2135 | } |
---|
2136 | } |
---|
2137 | if(defined($recentmach)) { |
---|
2138 | if($self->is_log($DEBUG)) { |
---|
2139 | $self->log_and_cont("INFO",$sub,"Copied $tempdir/$recentmach"."_dhcpd.leases to /var/lib/dhcp3/dhcpd.leases"); |
---|
2140 | } |
---|
2141 | $rc = $self->run_test("fcopy","","Copying $tempdir/$recentmach"."_dhcpd.leases -> /var/lib/dhcp3/dhcpd.leases","$tempdir/$recentmach"."_dhcpd.leases","/var/lib/dhcp3/dhcpd.leases"); |
---|
2142 | if(!$rc) { |
---|
2143 | $self->log_and_die("ERROR",$sub,"Couldn't move lease from $recentmach into place."); |
---|
2144 | } |
---|
2145 | } |
---|
2146 | $self->run_test("system","","Killing pkbcast","killall pkbcast"); |
---|
2147 | $self->run_test("system","","Killing bccd-allow-all","killall bccd-allow-all"); |
---|
2148 | close($MACHINES); |
---|
2149 | } |
---|
2150 | |
---|
2151 | if(!$havedhcp) { |
---|
2152 | ($rc,$out) = $self->exec_system("update-rc.d dhcp3-server defaults"); |
---|
2153 | if($rc == 0) { |
---|
2154 | $self->log_and_cont("NOTICE",$sub,"Set DHCP server to start.\n"); |
---|
2155 | } |
---|
2156 | else { |
---|
2157 | $self->log_and_die("ERROR",$sub,"Couldn't set DHCP server to start: $out\n"); |
---|
2158 | } |
---|
2159 | } |
---|
2160 | else { |
---|
2161 | ($rc,$out) = $self->exec_system("update-rc.d -f dhcp3-server remove"); |
---|
2162 | if($rc == 0) { |
---|
2163 | $self->log_and_cont("NOTICE",$sub,"Set DHCP server not to start.\n"); |
---|
2164 | } |
---|
2165 | else { |
---|
2166 | $self->log_and_die("NOTICE",$sub,"Couldn't set DHCP server not to start: $out\n") |
---|
2167 | } |
---|
2168 | } |
---|
2169 | return $nic_conf; |
---|
2170 | } |
---|
2171 | |
---|
2172 | sub config_nat{ |
---|
2173 | my($self,$nic_conf) = @_; |
---|
2174 | my($natnic,$sub); |
---|
2175 | $sub = 'config_nat'; |
---|
2176 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
2177 | $self->log_and_cont('INFO',$sub,"Gathering routing information"); |
---|
2178 | } |
---|
2179 | |
---|
2180 | open(my $NETSTAT, '-|', '/bin/netstat', '-rn') or |
---|
2181 | $self->log_and_die("ERROR",$sub,"Couldn't open up netstat for piping!"); |
---|
2182 | |
---|
2183 | NETSTAT: |
---|
2184 | while(my $line = <$NETSTAT>) { |
---|
2185 | chomp $line; |
---|
2186 | my @splitline = split(/\s+/, $line); |
---|
2187 | if($splitline[0] eq '0.0.0.0') { |
---|
2188 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
2189 | $self->log_and_cont('INFO',$sub,"$splitline[7] is a default router"); |
---|
2190 | } |
---|
2191 | $natnic = $splitline[7]; |
---|
2192 | last NETSTAT; |
---|
2193 | } |
---|
2194 | } |
---|
2195 | close($NETSTAT); |
---|
2196 | |
---|
2197 | if(defined($natnic)) { |
---|
2198 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
2199 | $self->log_and_cont('INFO',$sub,"Writing out $NATSH"); |
---|
2200 | } |
---|
2201 | open(my $NAT, '>', $NATSH) or |
---|
2202 | $self->log_and_die("ERROR",$sub,"Couldn't open $NATSH for writing: $!"); |
---|
2203 | |
---|
2204 | my $natip = $self->get_nic_ip($natnic); |
---|
2205 | if(!defined($natip)) { |
---|
2206 | $self->log_and_die("ERROR",$sub,"Couldn't get IP address for $natnic!"); |
---|
2207 | } |
---|
2208 | |
---|
2209 | print $NAT qq{#!/bin/bash\n\n}; |
---|
2210 | foreach my $LINE ( |
---|
2211 | q{--flush}, |
---|
2212 | q{-t nat --flush}, |
---|
2213 | q{--delete-chain}, |
---|
2214 | q{-t nat --delete-chain}, |
---|
2215 | qq{-t nat -A POSTROUTING -s 192.168.3.0/24 -j SNAT --to $natip}, |
---|
2216 | ) { |
---|
2217 | print $NAT "/sbin/iptables $LINE\n"; |
---|
2218 | } |
---|
2219 | |
---|
2220 | close($NAT); |
---|
2221 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
2222 | $self->log_and_cont('INFO',$sub,"Making $NATSH executable"); |
---|
2223 | } |
---|
2224 | chmod(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, $NATSH) or |
---|
2225 | $self->log_and_die("ERROR",$sub,"Couldn't set $NATSH to be executable: $!"); |
---|
2226 | |
---|
2227 | return 1; |
---|
2228 | } |
---|
2229 | return undef; |
---|
2230 | } |
---|
2231 | |
---|
2232 | sub is_dialog_ok { |
---|
2233 | my($self,$d) = @_; |
---|
2234 | |
---|
2235 | if($d->state() eq 'OK') { |
---|
2236 | return 1; |
---|
2237 | } |
---|
2238 | |
---|
2239 | return undef; |
---|
2240 | } |
---|
2241 | |
---|
2242 | sub config_nic_dialog { |
---|
2243 | my($self,$d,$nic_conf) = @_; |
---|
2244 | my($temp,$bccd_nic,$gotpxe,$dhcp_source,$sub,$rc); |
---|
2245 | $sub = 'config_nic_dialog'; |
---|
2246 | |
---|
2247 | $gotpxe = 0; |
---|
2248 | |
---|
2249 | FIND_EXT_NIC: |
---|
2250 | { |
---|
2251 | my @dhcp_nics; |
---|
2252 | # See if there's a BCCD server response |
---|
2253 | foreach my $nic (keys(%{$nic_conf})) { |
---|
2254 | if(defined($nic_conf->{$nic}->{'dhcp_source'}) |
---|
2255 | && $nic_conf->{$nic}->{'dhcp_source'} eq 'BCCD') { |
---|
2256 | $dhcp_source = 'BCCD'; |
---|
2257 | } |
---|
2258 | } |
---|
2259 | foreach my $nic (sort(keys(%{$nic_conf}))) { |
---|
2260 | # Only if we didn't pick up a BCCD server |
---|
2261 | if((defined($nic_conf->{$nic}->{'dhcp_source'}) && $nic_conf->{$nic}->{'dhcp_source'} ne 'BCCD') || !defined($dhcp_source)) { |
---|
2262 | push(@dhcp_nics,($nic,$nic)); |
---|
2263 | } |
---|
2264 | } |
---|
2265 | if($#dhcp_nics == 1) { |
---|
2266 | $bccd_nic = $dhcp_nics[0]; |
---|
2267 | } |
---|
2268 | elsif($#dhcp_nics > 1 and not -e '/testmode') { |
---|
2269 | $bccd_nic = $d->menu(text=>"Choose NIC to have BCCD network.", list => \@dhcp_nics); |
---|
2270 | if(!$self->is_dialog_ok($d)) { |
---|
2271 | redo FIND_EXT_NIC; |
---|
2272 | } |
---|
2273 | }else { |
---|
2274 | open (TEST,'</bccd_boot_flags') or croak "$!"; |
---|
2275 | my @command = grep (/BCCD_NIC/, <TEST>); |
---|
2276 | if ($command[0] =~ m/export BCCD_NIC=(.*)$/){ |
---|
2277 | $bccd_nic = $1; |
---|
2278 | } |
---|
2279 | print STDERR "$bccd_nic selected for BCCD network\n" |
---|
2280 | } |
---|
2281 | # Copy iptables template regardless of NAT Status |
---|
2282 | open(my $IPT, '>', $IPTABLES_UP) or |
---|
2283 | $self->log_and_die("ERROR",$sub, |
---|
2284 | "Couldn't open $IPTABLES_UP for appending: $!"); |
---|
2285 | foreach my $LINE ( |
---|
2286 | q{*filter}, |
---|
2287 | q{:INPUT ACCEPT [6562:602865]}, |
---|
2288 | q{:FORWARD ACCEPT [100:8276]}, |
---|
2289 | q{:OUTPUT ACCEPT [5836:748341]}, |
---|
2290 | q{COMMIT}, |
---|
2291 | ) { |
---|
2292 | print $IPT "$LINE\n"; |
---|
2293 | } |
---|
2294 | close($IPT); |
---|
2295 | if(defined($bccd_nic) && !$self->check_bccd_net($nic_conf)) { |
---|
2296 | $nic_conf->{"$bccd_nic:1"} = $BCCD_NET; |
---|
2297 | open(my $IPT, '>>', $IPTABLES_UP) or |
---|
2298 | $self->log_and_die("ERROR",$sub, |
---|
2299 | "Couldn't open $IPTABLES_UP for appending: $!"); |
---|
2300 | print $IPT qq{-A POSTROUTING -o $bccd_nic -j MASQUERADE\n}; |
---|
2301 | print $IPT qq{COMMIT\n}; |
---|
2302 | print $IPT qq{# Completed on Wed Jul 15 23:51:28 2009\n}; |
---|
2303 | close($IPT); |
---|
2304 | } |
---|
2305 | |
---|
2306 | NIC_CONF: |
---|
2307 | foreach my $nic (sort keys %{$nic_conf}) { |
---|
2308 | if(defined($nic_conf->{$nic}->{'ipaddr'}) && defined($nic_conf->{$nic}->{'dhcp_source'}) && |
---|
2309 | $nic_conf->{$nic}->{'dhcp_source'} eq 'BCCD') { |
---|
2310 | if(!$self->parse_cmdline('standalone')) { |
---|
2311 | $nic_conf->{$nic}->{'dhcp'} = 1; |
---|
2312 | } |
---|
2313 | } |
---|
2314 | elsif(defined($nic_conf->{$nic}->{'dhcp_source'}) |
---|
2315 | && ((-e '/testmode') || ($d->yesno(text=>"$nic has an IP address $nic_conf->{$nic}->{'ipaddr'} from $nic_conf->{$nic}->{'dhcp_source'}. Take this address?") |
---|
2316 | ))) { |
---|
2317 | $nic_conf->{$nic}->{'dhcp'} = 1; |
---|
2318 | } |
---|
2319 | else { |
---|
2320 | $nic_conf->{$nic}->{'dhcp'} = 0; |
---|
2321 | } |
---|
2322 | if($nic_conf->{$nic}->{'dhcp'} == 0 && !defined($nic_conf->{$nic}->{'ipaddr'}) |
---|
2323 | && ! -e '/testmode' |
---|
2324 | && ! $d->yesno(text=>"No DHCP for $nic, skip?")) { |
---|
2325 | $nic_conf->{$nic}->{'dhcp'} = 0; |
---|
2326 | } |
---|
2327 | else { |
---|
2328 | next NIC_CONF; |
---|
2329 | } |
---|
2330 | if($nic_conf->{$nic}->{'dhcp'} == 0) { |
---|
2331 | FIND_CUR_NIC: |
---|
2332 | do { |
---|
2333 | if (-e '/testmode'){ |
---|
2334 | $nic_conf->{$nic}->{'ipaddr'} = '192.168.0.*'; |
---|
2335 | }else{ |
---|
2336 | $nic_conf->{$nic}->{'ipaddr'} = |
---|
2337 | ($temp = $d->inputbox(text=> |
---|
2338 | "$nic IP address (mandatory)")) ? $temp : undef; |
---|
2339 | |
---|
2340 | if(!$self->is_dialog_ok($d)) { |
---|
2341 | redo FIND_EXT_NIC; |
---|
2342 | } |
---|
2343 | elsif($nic_conf->{$nic}->{'ipaddr'} |
---|
2344 | eq $BCCD_NET->{'ipaddr'}) { |
---|
2345 | $d->msgbox(text => |
---|
2346 | "IP address cannot be the BCCD virtual IP ($BCCD_NET->{'ipaddr'})."); |
---|
2347 | goto FIND_CUR_NIC; |
---|
2348 | } |
---|
2349 | } |
---|
2350 | |
---|
2351 | if (-e '/testmode'){ |
---|
2352 | $nic_conf->{$nic}->{'mask'} = "255.255.255.0"; |
---|
2353 | } else { |
---|
2354 | $nic_conf->{$nic}->{'mask'} = |
---|
2355 | ($temp = $d->inputbox(text=> |
---|
2356 | "$nic Subnet mask (mandatory)")) ? $temp : undef; |
---|
2357 | |
---|
2358 | if(!$self->is_dialog_ok($d)) { |
---|
2359 | redo FIND_EXT_NIC; |
---|
2360 | } |
---|
2361 | } |
---|
2362 | |
---|
2363 | $nic_conf->{$nic}->{'gw'} = ($temp = $d->inputbox(text=>"$nic Gateway (optional)")) ? $temp : undef; |
---|
2364 | |
---|
2365 | if(!$gotpxe && $self->get_stage() eq 'LIBERATED' && $d->yesno(text=>"Make $nic the PXE-capable NIC?")) { |
---|
2366 | $gotpxe = 1; |
---|
2367 | $nic_conf->{$nic}->{'pxenic'} = $nic; |
---|
2368 | } |
---|
2369 | } while(!defined($nic_conf->{$nic}->{'ipaddr'}) || !defined($nic_conf->{$nic}->{'mask'})); |
---|
2370 | } |
---|
2371 | } |
---|
2372 | } |
---|
2373 | |
---|
2374 | return $nic_conf; |
---|
2375 | } |
---|
2376 | |
---|
2377 | sub get_nic_ip( $$ ) { |
---|
2378 | my($self,$nic) = @_; |
---|
2379 | my($sub,$cmd,$rc,$out,$ip); |
---|
2380 | $sub = 'get_nic_ip'; |
---|
2381 | $self->enter_sub($sub); |
---|
2382 | |
---|
2383 | if(!defined($nic)) { |
---|
2384 | return undef; |
---|
2385 | } |
---|
2386 | |
---|
2387 | $cmd = "/sbin/ifconfig $nic"; |
---|
2388 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
2389 | $self->log_and_cont("INFO",$sub,"Running $cmd."); |
---|
2390 | } |
---|
2391 | ($rc,$out) = $self->exec_system($cmd); |
---|
2392 | if($rc) { |
---|
2393 | $self->log_and_die("ERROR",$sub,"$cmd failed with rc $rc, out $out.") |
---|
2394 | } |
---|
2395 | if($out =~ m/inet\s+addr:((?:\d{0,3}\.){3}\d{0,3})/) { |
---|
2396 | $ip = $1; |
---|
2397 | } |
---|
2398 | else { |
---|
2399 | undef $ip; |
---|
2400 | } |
---|
2401 | |
---|
2402 | $self->leave_sub($sub); |
---|
2403 | return $ip; |
---|
2404 | } |
---|
2405 | |
---|
2406 | sub get_nic_mask( $$ ) { |
---|
2407 | my($self,$nic) = @_; |
---|
2408 | my($sub,$cmd,$rc,$out,$mask); |
---|
2409 | $sub = 'get_nic_mask'; |
---|
2410 | $self->enter_sub($sub); |
---|
2411 | |
---|
2412 | $cmd = "/sbin/ifconfig $nic"; |
---|
2413 | if($self->is_log($INFO) || $self->is_log($DEBUG)) { |
---|
2414 | $self->log_and_cont("INFO",$sub,"Running $cmd."); |
---|
2415 | } |
---|
2416 | ($rc,$out) = $self->exec_system($cmd); |
---|
2417 | if($rc) { |
---|
2418 | $self->log_and_die("ERROR",$sub,"$cmd failed with rc $rc, out $out."); |
---|
2419 | } |
---|
2420 | if($out =~ m/Mask:((?:\d{0,3}\.){3}\d{0,3})/) { |
---|
2421 | $mask = $1; |
---|
2422 | } |
---|
2423 | else { |
---|
2424 | undef $mask; |
---|
2425 | } |
---|
2426 | |
---|
2427 | $self->leave_sub($sub); |
---|
2428 | return $mask; |
---|
2429 | } |
---|
2430 | |
---|
2431 | sub run_nic_dhcp { |
---|
2432 | my($self,$nic,$cfg) = @_; |
---|
2433 | my($cmd,$out,$rc,$sub); |
---|
2434 | $sub = 'run_nic_dhcp'; |
---|
2435 | |
---|
2436 | $cmd = "killall dhclient3"; |
---|
2437 | ($out,$rc) = $self->exec_system($cmd); |
---|
2438 | |
---|
2439 | foreach my $lease_file ( </var/lib/dhcp3/dhclient*leases*> ) { |
---|
2440 | if(!$self->run_test('unlink','',"Removing $lease_file.",$lease_file)) { |
---|
2441 | $self->log_and_die("ERROR",$sub,"Couldn't remove $lease_file."); |
---|
2442 | } |
---|
2443 | } |
---|
2444 | |
---|
2445 | $cmd = "dhclient3 -cf $cfg -1 $nic"; |
---|
2446 | ($out,$rc) = $self->run_test('system','',"Running $cmd.",$cmd); |
---|
2447 | |
---|
2448 | if($out =~ m/^bound to ((?:\d{1,3}\.){3}\d{1,3})/m) { |
---|
2449 | return $1; |
---|
2450 | } |
---|
2451 | return undef; |
---|
2452 | } |
---|
2453 | |
---|
2454 | sub read_passwd { |
---|
2455 | my($self) = @_; |
---|
2456 | my($passwd,$confirm,$empty); |
---|
2457 | do { |
---|
2458 | print "Please enter your password: "; |
---|
2459 | ReadMode('noecho'); |
---|
2460 | $passwd = <STDIN>; |
---|
2461 | if ($passwd){ |
---|
2462 | chomp $passwd; |
---|
2463 | } |
---|
2464 | print "\n"; |
---|
2465 | ReadMode('restore'); |
---|
2466 | |
---|
2467 | print "Please confirm your password: "; |
---|
2468 | ReadMode('noecho'); |
---|
2469 | $confirm = <STDIN>; |
---|
2470 | if ($confirm){ |
---|
2471 | chomp $confirm; |
---|
2472 | } |
---|
2473 | print "\n"; |
---|
2474 | ReadMode('restore'); |
---|
2475 | if (not $passwd){ |
---|
2476 | $passwd = ""; |
---|
2477 | } |
---|
2478 | if (not $confirm){ |
---|
2479 | $confirm = ""; |
---|
2480 | } |
---|
2481 | } while($passwd ne $confirm); |
---|
2482 | return $passwd; |
---|
2483 | } |
---|
2484 | |
---|
2485 | sub get_boot_flags_from_NIC{ |
---|
2486 | my %request = @_; |
---|
2487 | my @custom_macs; |
---|
2488 | my @custom_nics; |
---|
2489 | my @boot_strings; |
---|
2490 | my $boot_string; |
---|
2491 | my $bccd_nic; |
---|
2492 | |
---|
2493 | |
---|
2494 | my @mac_addresses = |
---|
2495 | split(/\n/,`$IFCONFIG | awk '/HWaddr/ {print \$1 " " \$5}'`); |
---|
2496 | @custom_macs = grep {/ 02:/} @mac_addresses; |
---|
2497 | |
---|
2498 | for(my $i = 0; $i <= $#custom_macs; ++$i){ |
---|
2499 | if($custom_macs[$i] =~ m/^(.*?) /) { |
---|
2500 | $custom_nics[$i] = $1; |
---|
2501 | } |
---|
2502 | |
---|
2503 | my $open_hex; |
---|
2504 | if ($custom_macs[$i] =~ m/([0-9a-fA-F]):([0-9a-fA-F][0-9a-fA-F])$/){ |
---|
2505 | $open_hex = $1.$2; |
---|
2506 | } else { |
---|
2507 | die "Error in detecting last three hexes in MAC $!"; |
---|
2508 | } |
---|
2509 | |
---|
2510 | $boot_strings[$i] = sprintf "%012b", hex( $open_hex ); |
---|
2511 | |
---|
2512 | if ( |
---|
2513 | substr($boot_strings[$i], |
---|
2514 | $IS_BCCD_NETWORK_NIC,1) |
---|
2515 | eq '1' |
---|
2516 | ) |
---|
2517 | { |
---|
2518 | $bccd_nic = $custom_nics[$i]; |
---|
2519 | $boot_string = $boot_strings[$i]; |
---|
2520 | } |
---|
2521 | } |
---|
2522 | |
---|
2523 | my $boot_flags = "#!/bin/sh\nexport BCCD_NIC=$bccd_nic\n"; |
---|
2524 | |
---|
2525 | foreach my $key(keys %request){ |
---|
2526 | my $value; |
---|
2527 | my $pointer = $request{$key}; |
---|
2528 | my ($index,$length) = split(/,/,$pointer); |
---|
2529 | |
---|
2530 | unless($length){ |
---|
2531 | $length = 1; |
---|
2532 | } |
---|
2533 | |
---|
2534 | $value = substr($boot_string,$index,$length); |
---|
2535 | $value = oct ("0b$value"); |
---|
2536 | |
---|
2537 | $boot_flags .= "export $key=$value\n"; |
---|
2538 | } |
---|
2539 | |
---|
2540 | if ($boot_flags){ |
---|
2541 | open(BOOTFLAGS, ">/bccd_boot_flags") |
---|
2542 | or die "Could not open /bccd_boot_flags:$!"; |
---|
2543 | print BOOTFLAGS $boot_flags; |
---|
2544 | close(BOOTFLAGS); |
---|
2545 | chmod 0755, '/bccd_boot_flags'; |
---|
2546 | } |
---|
2547 | } |
---|
2548 | |
---|
2549 | 1; |
---|
2550 | |
---|
2551 | __END__ |
---|
2552 | |
---|
2553 | =head1 NAME |
---|
2554 | |
---|
2555 | Bccd.pm |
---|
2556 | |
---|
2557 | =head1 DESCRIPTION |
---|
2558 | |
---|
2559 | This is the Perl module common to all BCCD scripts except for the testing database. What follows |
---|
2560 | is a description of all the subroutines available in the module. The signature below includes |
---|
2561 | the reference to the module, but only extra parameters are explicitly mentioned. |
---|
2562 | |
---|
2563 | =head2 GENERAL SUBROUTINES |
---|
2564 | |
---|
2565 | These functions all take a reference to the parent module, along with whatever other |
---|
2566 | parameters that are passed in. |
---|
2567 | |
---|
2568 | =head3 cmd_num_die($@) |
---|
2569 | |
---|
2570 | This is the subroutine called when another subroutine does not have the proper number of |
---|
2571 | arguments. Takes an array. |
---|
2572 | |
---|
2573 | =head3 print_array($@) |
---|
2574 | |
---|
2575 | This prints an array with line counters. Takes an array. |
---|
2576 | |
---|
2577 | =head3 get_vginfo($) |
---|
2578 | |
---|
2579 | This subroutine returns the LVM volume group information in colon-delimited format. |
---|
2580 | |
---|
2581 | =head3 get_pvinfo($) |
---|
2582 | |
---|
2583 | Returns the LVM physical volume information in colon delimited format. |
---|
2584 | |
---|
2585 | =head3 get_free_pe_count($) |
---|
2586 | |
---|
2587 | Returns the number of available physical extents in the volume groups present. |
---|
2588 | |
---|
2589 | =head3 snarf_file($$) |
---|
2590 | |
---|
2591 | Takes a path to a file and reads it in as one string. |
---|
2592 | |
---|
2593 | =head2 TESTING SUBROUTINES |
---|
2594 | |
---|
2595 | These functions all take a refernce to the parent module, the test type, the success return |
---|
2596 | code to be expected (required but can be blank for a safe default), a message to print out, |
---|
2597 | and whatever other parameters the specific test requires. In this documentation, only extra |
---|
2598 | parameters are explicitly mentioned. Unless otherwise noted, this returns the exit code as a |
---|
2599 | Perl truth value (0 == failure, anything else is OK). |
---|
2600 | |
---|
2601 | =head3 test_system($$$$$) |
---|
2602 | |
---|
2603 | Takes a command and runs it. |
---|
2604 | |
---|
2605 | =head3 test_chdir($$$$$) |
---|
2606 | |
---|
2607 | Takes a directory and changes the present directory to it. |
---|
2608 | |
---|
2609 | =head3 test_mkpath($$$$$) |
---|
2610 | |
---|
2611 | Takes a directory and makes it. |
---|
2612 | |
---|
2613 | =head3 test_wwwmech($$$$$$) |
---|
2614 | |
---|
2615 | Takes a URL and a destination file, and fetches the URL to the file. For subversion |
---|
2616 | access, see test_revfetch and test_recrevfetch. |
---|
2617 | |
---|
2618 | =head3 test_chmod($$$$$$) |
---|
2619 | |
---|
2620 | Takes an octal permission mode and a file, and sets the permissions on the file to the given |
---|
2621 | mode. Make sure not to represent the octal permissions as text (i.e. don't use quotes). |
---|
2622 | |
---|
2623 | =head3 test_unlink($$$$$) |
---|
2624 | |
---|
2625 | Takes a directory entry and removes it. |
---|
2626 | |
---|
2627 | =head3 test_symlink($$$$$$) |
---|
2628 | |
---|
2629 | Takes a source file and destination, and symbolically links the source to the destination. |
---|
2630 | |
---|
2631 | =head3 test_fcopy($$$$$$) |
---|
2632 | |
---|
2633 | Takes a source file and destination file, and copies the source to the destination. |
---|
2634 | |
---|
2635 | =head3 test_fmove($$$$$$) |
---|
2636 | |
---|
2637 | Takes a source file and destination, and moves the source file to the destination. |
---|
2638 | |
---|
2639 | =head3 test_getsvnrev($$$$$) |
---|
2640 | |
---|
2641 | Gets the current subversion revision from the given URL. Returns the subversion revision. |
---|
2642 | |
---|
2643 | =head3 test_fwrite($$$$$$$) |
---|
2644 | |
---|
2645 | Takes a mode, file, and a text string, and writes the text to the file. Valid modes are "w" |
---|
2646 | for replacing the file, and "a" for appending to an existing file. |
---|
2647 | |
---|
2648 | =head3 test_revfetch($$$$$$$) |
---|
2649 | |
---|
2650 | Takes a subversion revision, URL in a subversion repository, and a destination file. Fetches |
---|
2651 | the file in the URL at the given revision to the destination file. |
---|
2652 | |
---|
2653 | =head3 test_rename($$$$$$) |
---|
2654 | |
---|
2655 | Takes a source file and destination file and renames the source to the destination. Functionally |
---|
2656 | equivalent to test_fmove. |
---|
2657 | |
---|
2658 | =head3 test_recrevfetch($$$$$$) |
---|
2659 | |
---|
2660 | Takes a subversion revision and URL, and fetches all files underneath the URL to the present |
---|
2661 | directory. |
---|
2662 | |
---|
2663 | =head3 test_rmtree($$$$$) |
---|
2664 | |
---|
2665 | Takes a directory tree and recursively removes it. |
---|
2666 | |
---|
2667 | =head3 test_getuseruid($$$$$) |
---|
2668 | |
---|
2669 | Takes a username and return the UID. |
---|
2670 | |
---|
2671 | =head3 test_getusergid($$$$$) |
---|
2672 | |
---|
2673 | Takes a username and returns the primary GID. |
---|
2674 | |
---|
2675 | =head3 test_lsofkill($$$$$) |
---|
2676 | |
---|
2677 | Takes a directory name and kills all processes with open files in that directory. |
---|
2678 | |
---|
2679 | =head3 test_chown($$$$$$$) |
---|
2680 | |
---|
2681 | Takes a file, user, and group and changes ownership of the file to that user and group. |
---|
2682 | |
---|
2683 | =head3 test_rsync($$$$$$) |
---|
2684 | |
---|
2685 | Takes a source and destination path and rsync's the source to the destination. |
---|
2686 | |
---|
2687 | =head2 MISCELLANEOUS SUBROUTINES |
---|
2688 | |
---|
2689 | These do not take any standardized arguments |
---|
2690 | |
---|
2691 | =head3 get_boot_flags_from_NIC(%) |
---|
2692 | |
---|
2693 | Takes data from the NIC's MAC address and populates /bccd_boot_flags |
---|
2694 | according to key-position hash. |
---|
2695 | |
---|
2696 | Splits the last three hexes of the bccd network's custom NIC |
---|
2697 | into a 12 digit binary string. Takes a hash of keys and |
---|
2698 | positions in that binary string. Key translates directly into |
---|
2699 | the name of the environment variable into which the information |
---|
2700 | is stored, and the position is a comma-separated-value list |
---|
2701 | with an index (starting at 0) and optionally a bit length (default 1). |
---|
2702 | so ('BUILD_CONTROL' => 1) takes the second bit in the last |
---|
2703 | three digits of the MAC and writes "export BUILD_CONTROL=<value>" to |
---|
2704 | /bccd_boot_flags |
---|
2705 | |
---|
2706 | The first entry in /bccd_boot_flags is always "export BCCD_NIC=<BCCD NIC>" |
---|
2707 | |
---|
2708 | For a list of the official automated test bootflags |
---|
2709 | and their corresponding locations, check the BCCD Wiki |
---|
2710 | |
---|
2711 | =cut |
---|