lilalo

view lm @ 42:5499496e2462

Печать сначала в переменную, а потом всё выводим сразу
author devi
date Tue Nov 22 23:49:32 2005 +0200 (2005-11-22)
parents 6452bfe549d1
children e0db0f8bb482
line source
1 #!/usr/bin/perl
4 use strict;
5 use Data::Dumper;
6 use Switch;
7 use XML::Simple;
8 use Getopt::Long;
9 use utf8;
11 use lib "/usr/local/bin";
12 use l3config;
14 our $XMLClass;
15 our $XMLCourse;
16 our @Labs;
18 our %Machines; # Machines list from class.xml
19 our @SelectedMachines; # Machines list given as the command line argument
21 our $Config_File = "labmaker.conf";
22 our %Config_ = (
23 "show_host" => "no",
25 # Вспомогательные программы
26 #"l3-report" => "./lm-report",
27 "l3-report" => "./l3-report",
29 # Каталоги
30 "path_lilalo" => "/var/lilalo/",
31 "path_classes" => "/var/lilalo/classes/",
32 "path_lablogs" => "/var/lilalo/lablogs/",
33 "courses_path" => "/var/lilalo/courses/",
34 "outpath" => "/var/lilalo/out/",
35 "path_web" => "/var/www/l3", # Путь к web-отчётам
36 "path_share" => "./share/", # Путь к web-отчётам
38 # Файлы
39 "runfile" => "lm.run",
40 "logfile" => "lm.log",
42 "class" => "class", # Имя файла класса
43 "class_suffix" => ".xml", # Cуффикс файла класса
44 "classfile" => "",
46 "sshkey" => "$ENV{HOME}/.ssh/id_dsa.pub",
47 "lmssh" => "./lm-ssh",
48 "lminstall" => "./lm-install",
49 "ssh_user" => "root",
50 );
52 our %Run = (
53 "lab" => ""
54 );
56 our %Scripts;
58 sub load_class;
59 sub load_config;
60 sub load_course;
61 sub load_scripts;
63 sub lm_next;
64 sub lm_prev;
65 sub lm_start;
66 sub lm_stop;
67 sub lm_set;
68 sub lm_do;
69 sub lm_report;
70 sub lm_show_hosts;
71 sub lm_show_labs;
73 sub load_run;
74 sub save_run;
75 sub print_log;
76 sub print_usage_info;
77 sub main();
79 main();
81 sub main()
82 {
83 binmode STDOUT, ":utf8";
85 if (! @ARGV) {
86 print_usage_info();
87 exit(0);
88 }
90 init_config();
91 #load_config;
92 load_run;
93 load_scripts;
94 load_class;
95 load_course;
97 my $arg = join " ", @ARGV;
99 # Getting @SelectedMachines if any
100 if ($arg =~ s/@(.*?)\s//) {
101 my $machines = $1;
102 my @list = split /,/, $machines;
103 for my $interval (@list) {
104 my ($first, $last) = split /-/, $interval;
106 push @SelectedMachines, $first;
107 while ($first < $last) {
108 push @SelectedMachines, ++$first;
109 }
110 }
111 }
113 # Choose command to do
114 switch ($arg) {
115 case "next" { lm_next }
116 case "prev" { lm_prev }
117 case /set / { $arg =~ /set (.*)/; lm_set $1 }
118 case "report" { lm_report }
119 case "start" { lm_start }
120 case "stop" { lm_stop }
121 case "show hosts" { lm_show_hosts }
122 case "show labs" { lm_show_labs }
123 case /do / { $arg =~ /do (.*)/; lm_do "$1" }
124 else { print_usage_info() }
125 }
126 save_run;
127 exit(0);
128 }
130 sub load_scripts
131 {
132 open (SCRIPTS, "$Config{l3scripts}")
133 or die "Cant open l3scripts file: ".$Config{l3scripts}.": $!\n";
134 binmode SCRIPTS, ":utf8";
135 local $/;
136 $_=<SCRIPTS>;
137 close(SCRIPTS);
139 %Scripts = ("empty-element", split (/###(.*)\n/));
140 delete($Scripts{"empty-element"});
142 }
144 sub load_config
145 {
146 my %file_config;
147 my %argv_config;
148 #read_config_file(\%file_config, $Config_File);
149 GetOptions(\%argv_config, map "$_=s", keys %Config);
150 %Config = (%Config, %file_config, %argv_config);
151 }
153 sub load_course
154 {
155 $XMLCourse = XMLin($Config{"courses_path"}.$XMLClass->{"course"}.".xml", ForceArray => 1 )
156 or die "Can't open file of the course ",$XMLClass->{"course"}," [with .xml extension]\n";
157 # print Dumper($XMLCourse);
158 for my $lab (@{$XMLCourse->{"module"}}) {
159 push @Labs, $lab->{"code"};
160 }
161 }
163 sub load_class
164 {
165 my $classfile =
166 $Config{"classfile"} ||
167 $Config{"path_classes"}."/".$Config{"class"}.$Config{"class_suffix"};
168 $XMLClass = XMLin($classfile , ForceArray => [ 'student' ] )
169 or die "Can't open file of the class ",$classfile,"\n";
171 for my $student (@{$XMLClass->{"student"}}) {
172 $Machines{$student->{"host"}} = {
173 "name" => "$student->{firstname} $student->{surname}",
174 "firstname" => "$student->{firstname}",
175 "user" => "$student->{user}",
176 "student" => $student,
177 }
178 }
179 # print Dumper($XMLClass);
180 # print Dumper(\%Machines);
181 }
184 sub lm_next
185 {
186 for(my $i=0; $i<=$#Labs; $i++){
187 if ( $Labs[$i] eq $Run{"lab"} ) {
188 if ($i < $#Labs) {
189 lm_set($Labs[$i+1]);
190 return ;
191 } else {
192 die "Lab ", $Run{"lab"}, " is the last. Switch to next lab is impossible"
193 }
194 }
196 }
197 die "Lab ", $Run{"lab"}, " not found. Don't know which is next"
198 }
200 sub lm_prev
201 # Switch to previous lab
202 {
203 for(my $i=0; $i<=$#Labs; $i++){
204 if ( $Labs[$i] eq $Run{"lab"} ) {
205 if ($i > 0) {
206 lm_set($Labs[$i-1]);
207 return ;
208 } else {
209 die "Lab ", $Run{"lab"}, " is the first. Switch to previous lab is impossible"
210 }
211 }
213 }
214 die "Lab ", $Run{"lab"}, " not found. Don't know which is previous"
215 }
217 sub lm_set
218 # Switch to $_[0] lab
219 # FIXME
220 {
221 my $lab = shift;
222 print "Current lab is $lab\n";
223 $Run{"lab"} = "$lab";
224 lm_do "setlab", $lab;
225 }
228 sub lm_start
229 # Start new training day
230 {
231 print_log(`date`." STARTED\n");
232 if ($Run{"lab"}) {
233 lm_next;
234 }
235 else
236 {
237 # First lab in the course
238 lm_set($Labs[0]);
239 }
240 }
242 sub lm_stop
243 # Stop this training day
244 {
245 print_log(`date`." STOPPED\n");
246 }
249 sub lm_show_hosts
250 # Show hosts used to run a commands
251 {
252 my $i=1;
253 for my $m (sort keys %Machines) {
254 if (!@SelectedMachines || grep /^$i$/, @SelectedMachines) {
255 print "($i)","\t",$m,"\t",$Machines{$m}->{"name"},"\n";
256 }
257 $i++;
258 }
259 }
261 sub lm_show_labs
262 # Show hosts used to run a commands
263 {
264 my $i=1;
265 for my $lab (@Labs) {
266 print $lab;
267 print "*" if $lab eq $Run{"lab"};
268 print "\n";
269 }
270 }
272 sub lm_do
273 # Do the $_[0] command on all of the hosts
274 {
275 my $command = shift;
276 my $arg = join " ", @_;
277 my $i=1;
279 my %myenv = ( %Config,
280 lab => $arg,
281 center => $XMLClass->{"center"},
282 course => $XMLClass->{"course"},
283 date => $XMLClass->{"date"},
284 coursepath => $XMLCourse->{"path"},
285 );
287 if (grep { $_ eq "PRE-$command"} keys %Scripts) {
288 $_=$Scripts{"PRE-$command"};
289 s/\$(\w+)/$myenv{$1}/ge;
290 open(SHELL, "|/bin/sh -s");
291 binmode SHELL, ":utf8";
292 print SHELL $_;
293 close (SHELL);
294 }
297 for my $m (sort keys %Machines) {
298 if (!@SelectedMachines || grep $_ eq $i, @SelectedMachines) {
299 print "$m:\n" if $Config{"show_host"} =~ /y/i;
301 %myenv = ( %myenv,
302 host => $m,
303 dirs => "/root /home/".$Machines{$m}->{"user"},
304 lablogs => $Config{"path_lablogs"}."/".
305 $XMLClass->{"course"}."/".
306 $XMLClass->{"date"}."/".
307 "$m",
308 email => $Machines{$m}->{"student"}->{"email"},
309 company => $Machines{$m}->{"student"}->{"company"},
310 name => $Machines{$m}->{"name"},
311 firstname => $Machines{$m}->{"firstname"},
312 );
313 if (grep { $_ eq $command} keys %Scripts) {
314 $_=$Scripts{"$command"};
315 s/\$(\w+)/$myenv{$1}/ge;
316 open(SHELL, "|/bin/sh -s");
317 binmode SHELL, ":utf8";
318 print SHELL $_;
319 close (SHELL);
320 }
321 else {
322 my $res = `ssh $Config{"ssh_user"}\@$m $command`;
323 if ($res) {
324 my $count = ($res =~ s/(^)/$m: /mg);
325 print $res;
326 print "\n" if ($count > 1);
327 }
328 }
329 }
330 $i++;
331 }
333 if (grep { $_ eq "POST-$command"} keys %Scripts) {
334 $_=$Scripts{"POST-$command"};
335 s/\$(\w+)/$myenv{$1}/ge;
336 open(SHELL, "|/bin/sh -s");
337 binmode SHELL, ":utf8";
338 print SHELL $_;
339 close (SHELL);
340 }
341 }
345 =cut comment
347 lm report
349 Построить html представление для журналов текущего класса.
350 Для построения используется скрипт l3-report.
352 =cut
354 sub lm_report
355 {
357 my $webdir = $Config{"path_web"};
358 my $course=$XMLClass->{"course"};
359 my $date=$XMLClass->{"date"};
360 my $encoding=$XMLClass->{"charset"};
362 my $center = $XMLClass->{"center"};
363 my $instructor = $XMLClass->{"instructor"}->{"firstname"}." ".$XMLClass->{"instructor"}->{"surname"};
364 my $course_name = $XMLCourse->{"fullname"}[0];
367 # Собственно журналы
369 for my $student (@{$XMLClass->{"student"}}) {
370 my $user = $student->{"user"};
371 my $hostname = $student->{"host"};
372 my $encoding = $student->{"charset"};
373 my $student_name = $student->{"firstname"}." ".$student->{"surname"};
375 system("mkdir -p $webdir/$date/$hostname");
376 system("cp ".$Config{"path_share"}."/*.{ico,css} $webdir/$date/$hostname");
377 system($Config{"l3-report"}.
378 " --input ".$Config{"path_lablogs"}."/$course/$date/$hostname/$user".
379 " --diffs ".$Config{"path_lablogs"}."/$course/$date/$hostname/$user ".
380 $Config{"path_lablogs"}."/$course/$date/$hostname/root".
381 " --output $webdir/$date/$hostname/$user.html".
382 " --course-name '$course_name'".
383 " --course-code '$course'".
384 " --course-date '$date'".
385 " --course-center '$center'".
386 " --course-student '$student_name'".
387 " --course-trainer '$instructor'".
388 " --encoding $encoding"
389 );
390 system($Config{"l3-report"}.
391 " --input ".$Config{"path_lablogs"}."/$course/$date/$hostname/root".
392 " --diffs ".$Config{"path_lablogs"}."/$course/$date/$hostname/root ".
393 " --output $webdir/$date/$hostname/root.html".
394 " --course-name '$course_name'".
395 " --course-code '$course'".
396 " --course-date '$date'".
397 " --course-center '$center'".
398 " --course-student '$student_name'".
399 " --course-trainer '$instructor'".
400 " --encoding $encoding"
401 );
402 }
404 # Индекс для данного класса
406 my $head;
408 $head="Журналы лабораторных работ";
409 open(HTML, ">$webdir/$date/index.html")
410 or die "Can't open $webdir/$date/index.html for writing";
411 binmode HTML, ":utf8";
412 print HTML <<HEAD;
413 <html>
414 <head>
415 <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
416 <title>$head</title>
417 </head>
418 <body>
419 <h1>$head</h1>
420 <p>
421 Курс: $course_name ($course)<br/>
422 Начало: $date<br/>
423 Учебный центр: $center <br/>
424 Инструктор: $instructor <br/>
425 </p>
426 <table>
427 HEAD
428 for my $student (@{$XMLClass->{"student"}}) {
429 my $user = $student->{"user"};
430 my $hostname = $student->{"host"};
431 print HTML "<tr>\n";
432 print HTML "<td>",$student->{"firstname"}," ",$student->{"surname"},"</td>\n";
433 print HTML "<td>",$hostname,"</td>\n";
434 print HTML "<td><a href=\"$hostname/$user.html\">",$user,"</td>\n";
435 print HTML "<td><a href=\"$hostname/root.html\">","root","</td>\n";
436 print HTML "</tr>\n";
437 }
438 print HTML <<TAIL;
439 </table>
440 </html>
441 TAIL
442 close (HTML);
446 }
448 sub load_run
449 {
450 my $runfile = $Config{"path_labmaker"}."/".$Config{"path_runfile"};
451 open (RUN, $runfile)
452 or return;
453 while (<RUN>) {
454 chomp;
455 my ($var, $val) = split /\s+/,$_,2;
456 $Run{$var}=$val;
457 }
458 close (RUN);
459 }
461 sub save_run
462 {
463 my $runfile = $Config{"path_labmaker"}."/".$Config{"path_runfile"};
464 open (RN, "$runfile")
465 or die "Can't save running state to $runfile";
466 for my $var (keys %Run) {
467 print RN $var,"\t",$Run{$var},"\n";
468 }
469 close (RN);
470 }
472 sub print_log
473 {
474 my $logfile = $Config{"path_labmaker"}."/".$Config{"path_logfile"};
475 open (LOG, ">>$logfile")
476 or die "Can't open logfile $logfile for writing";
477 print LOG @_;
478 close (LOG);
479 }
482 sub print_usage_info
483 {
484 print "Usage:\n\n\t$0 [host-list] command\n";
485 print <<'USAGE';
487 Commands:
489 next -- next lab
490 prev -- prev lab
491 set LAB -- set current lab to LAB
492 start -- start this day training
493 stop -- stop this day training
494 show hosts -- show available hosts in the class
495 show labs -- show available labs in the course
496 do COMMAND -- do specified command on the hosts of hostlist
497 report -- generate XML/HTML reports
500 do commands:
502 install [PROFILE] -- install profile
504 Host list:
506 @N -- machine N
507 @N1-N2 -- all of the machines from N1 to N2
508 @N1,N2,N3 -- machine N1, N2 and N3
510 N* is numbers or domain names of the machines.
512 If host list is not specified,
513 command is executed on all of the machines
515 USAGE
516 }