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