lilalo

view lm @ 41:6452bfe549d1

Наброски для отправки письма после курса
author devi
date Tue Nov 22 23:27:51 2005 +0200 (2005-11-22)
parents 5e5878bd1b20
children 5499496e2462
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 print join " ", %Config;
133 print "\n";
135 open (SCRIPTS, "$Config{l3scripts}")
136 or die "Cant open l3scripts file: ".$Config{l3scripts}.": $!\n";
137 binmode SCRIPTS, ":utf8";
138 local $/;
139 $_=<SCRIPTS>;
140 close(SCRIPTS);
142 %Scripts = ("empty-element", split (/###(.*)\n/));
143 delete($Scripts{"empty-element"});
145 }
147 sub load_config
148 {
149 my %file_config;
150 my %argv_config;
151 #read_config_file(\%file_config, $Config_File);
152 GetOptions(\%argv_config, map "$_=s", keys %Config);
153 %Config = (%Config, %file_config, %argv_config);
154 }
156 sub load_course
157 {
158 $XMLCourse = XMLin($Config{"courses_path"}.$XMLClass->{"course"}.".xml", ForceArray => 1 )
159 or die "Can't open file of the course ",$XMLClass->{"course"}," [with .xml extension]\n";
160 # print Dumper($XMLCourse);
161 for my $lab (@{$XMLCourse->{"module"}}) {
162 push @Labs, $lab->{"code"};
163 }
164 }
166 sub load_class
167 {
168 my $classfile =
169 $Config{"classfile"} ||
170 $Config{"path_classes"}."/".$Config{"class"}.$Config{"class_suffix"};
171 $XMLClass = XMLin($classfile , ForceArray => [ 'student' ] )
172 or die "Can't open file of the class ",$classfile,"\n";
174 for my $student (@{$XMLClass->{"student"}}) {
175 $Machines{$student->{"host"}} = {
176 "name" => "$student->{firstname} $student->{surname}",
177 "firstname" => "$student->{firstname}",
178 "user" => "$student->{user}",
179 "student" => $student,
180 }
181 }
182 # print Dumper($XMLClass);
183 # print Dumper(\%Machines);
184 }
187 sub lm_next
188 {
189 for(my $i=0; $i<=$#Labs; $i++){
190 if ( $Labs[$i] eq $Run{"lab"} ) {
191 if ($i < $#Labs) {
192 lm_set($Labs[$i+1]);
193 return ;
194 } else {
195 die "Lab ", $Run{"lab"}, " is the last. Switch to next lab is impossible"
196 }
197 }
199 }
200 die "Lab ", $Run{"lab"}, " not found. Don't know which is next"
201 }
203 sub lm_prev
204 # Switch to previous lab
205 {
206 for(my $i=0; $i<=$#Labs; $i++){
207 if ( $Labs[$i] eq $Run{"lab"} ) {
208 if ($i > 0) {
209 lm_set($Labs[$i-1]);
210 return ;
211 } else {
212 die "Lab ", $Run{"lab"}, " is the first. Switch to previous lab is impossible"
213 }
214 }
216 }
217 die "Lab ", $Run{"lab"}, " not found. Don't know which is previous"
218 }
220 sub lm_set
221 # Switch to $_[0] lab
222 # FIXME
223 {
224 my $lab = shift;
225 print "Current lab is $lab\n";
226 $Run{"lab"} = "$lab";
227 lm_do "setlab", $lab;
228 }
231 sub lm_start
232 # Start new training day
233 {
234 print_log(`date`." STARTED\n");
235 if ($Run{"lab"}) {
236 lm_next;
237 }
238 else
239 {
240 # First lab in the course
241 lm_set($Labs[0]);
242 }
243 }
245 sub lm_stop
246 # Stop this training day
247 {
248 print_log(`date`." STOPPED\n");
249 }
252 sub lm_show_hosts
253 # Show hosts used to run a commands
254 {
255 my $i=1;
256 for my $m (sort keys %Machines) {
257 if (!@SelectedMachines || grep /^$i$/, @SelectedMachines) {
258 print "($i)","\t",$m,"\t",$Machines{$m}->{"name"},"\n";
259 }
260 $i++;
261 }
262 }
264 sub lm_show_labs
265 # Show hosts used to run a commands
266 {
267 my $i=1;
268 for my $lab (@Labs) {
269 print $lab;
270 print "*" if $lab eq $Run{"lab"};
271 print "\n";
272 }
273 }
275 sub lm_do
276 # Do the $_[0] command on all of the hosts
277 {
278 my $command = shift;
279 my $arg = join " ", @_;
280 my $i=1;
282 my %myenv = ( %Config,
283 lab => $arg,
284 center => $XMLClass->{"center"},
285 course => $XMLClass->{"course"},
286 date => $XMLClass->{"date"},
287 coursepath => $XMLCourse->{"path"},
288 );
290 if (grep { $_ eq "PRE-$command"} keys %Scripts) {
291 $_=$Scripts{"PRE-$command"};
292 s/\$(\w+)/$myenv{$1}/ge;
293 open(SHELL, "|/bin/sh -s");
294 binmode SHELL, ":utf8";
295 print SHELL $_;
296 close (SHELL);
297 }
300 for my $m (sort keys %Machines) {
301 if (!@SelectedMachines || grep $_ eq $i, @SelectedMachines) {
302 print "$m:\n" if $Config{"show_host"} =~ /y/i;
304 %myenv = ( %myenv,
305 host => $m,
306 dirs => "/root /home/".$Machines{$m}->{"user"},
307 lablogs => $Config{"path_lablogs"}."/".
308 $XMLClass->{"course"}."/".
309 $XMLClass->{"date"}."/".
310 "$m",
311 email => $Machines{$m}->{"student"}->{"email"},
312 company => $Machines{$m}->{"student"}->{"company"},
313 name => $Machines{$m}->{"name"},
314 firstname => $Machines{$m}->{"firstname"},
315 );
316 if (grep { $_ eq $command} keys %Scripts) {
317 $_=$Scripts{"$command"};
318 s/\$(\w+)/$myenv{$1}/ge;
319 open(SHELL, "|/bin/sh -s");
320 binmode SHELL, ":utf8";
321 print SHELL $_;
322 close (SHELL);
323 }
324 else {
325 my $res = `ssh $Config{"ssh_user"}\@$m $command`;
326 if ($res) {
327 my $count = ($res =~ s/(^)/$m: /mg);
328 print $res;
329 print "\n" if ($count > 1);
330 }
331 }
332 }
333 $i++;
334 }
336 if (grep { $_ eq "POST-$command"} keys %Scripts) {
337 $_=$Scripts{"POST-$command"};
338 s/\$(\w+)/$myenv{$1}/ge;
339 open(SHELL, "|/bin/sh -s");
340 binmode SHELL, ":utf8";
341 print SHELL $_;
342 close (SHELL);
343 }
344 }
348 =cut comment
350 lm report
352 Построить html представление для журналов текущего класса.
353 Для построения используется скрипт l3-report.
355 =cut
357 sub lm_report
358 {
360 my $webdir = $Config{"path_web"};
361 my $course=$XMLClass->{"course"};
362 my $date=$XMLClass->{"date"};
363 my $encoding=$XMLClass->{"charset"};
365 my $center = $XMLClass->{"center"};
366 my $instructor = $XMLClass->{"instructor"}->{"firstname"}." ".$XMLClass->{"instructor"}->{"surname"};
367 my $course_name = $XMLCourse->{"fullname"}[0];
370 # Собственно журналы
372 for my $student (@{$XMLClass->{"student"}}) {
373 my $user = $student->{"user"};
374 my $hostname = $student->{"host"};
375 my $encoding = $student->{"charset"};
376 my $student_name = $student->{"firstname"}." ".$student->{"surname"};
378 system("mkdir -p $webdir/$date/$hostname");
379 system("cp ".$Config{"path_share"}."/*.{ico,css} $webdir/$date/$hostname");
380 system($Config{"l3-report"}.
381 " --input ".$Config{"path_lablogs"}."/$course/$date/$hostname/$user".
382 " --diffs ".$Config{"path_lablogs"}."/$course/$date/$hostname/$user ".
383 $Config{"path_lablogs"}."/$course/$date/$hostname/root".
384 " --output $webdir/$date/$hostname/$user.html".
385 " --course-name '$course_name'".
386 " --course-code '$course'".
387 " --course-date '$date'".
388 " --course-center '$center'".
389 " --course-student '$student_name'".
390 " --course-trainer '$instructor'".
391 " --encoding $encoding"
392 );
393 system($Config{"l3-report"}.
394 " --input ".$Config{"path_lablogs"}."/$course/$date/$hostname/root".
395 " --diffs ".$Config{"path_lablogs"}."/$course/$date/$hostname/root ".
396 " --output $webdir/$date/$hostname/root.html".
397 " --course-name '$course_name'".
398 " --course-code '$course'".
399 " --course-date '$date'".
400 " --course-center '$center'".
401 " --course-student '$student_name'".
402 " --course-trainer '$instructor'".
403 " --encoding $encoding"
404 );
405 }
407 # Индекс для данного класса
409 my $head;
411 $head="Журналы лабораторных работ";
412 open(HTML, ">$webdir/$date/index.html")
413 or die "Can't open $webdir/$date/index.html for writing";
414 binmode HTML, ":utf8";
415 print HTML <<HEAD;
416 <html>
417 <head>
418 <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
419 <title>$head</title>
420 </head>
421 <body>
422 <h1>$head</h1>
423 <p>
424 Курс: $course_name ($course)<br/>
425 Начало: $date<br/>
426 Учебный центр: $center <br/>
427 Инструктор: $instructor <br/>
428 </p>
429 <table>
430 HEAD
431 for my $student (@{$XMLClass->{"student"}}) {
432 my $user = $student->{"user"};
433 my $hostname = $student->{"host"};
434 print HTML "<tr>\n";
435 print HTML "<td>",$student->{"firstname"}," ",$student->{"surname"},"</td>\n";
436 print HTML "<td>",$hostname,"</td>\n";
437 print HTML "<td><a href=\"$hostname/$user.html\">",$user,"</td>\n";
438 print HTML "<td><a href=\"$hostname/root.html\">","root","</td>\n";
439 print HTML "</tr>\n";
440 }
441 print HTML <<TAIL;
442 </table>
443 </html>
444 TAIL
445 close (HTML);
449 }
451 sub load_run
452 {
453 my $runfile = $Config{"path_labmaker"}."/".$Config{"path_runfile"};
454 open (RUN, $runfile)
455 or return;
456 while (<RUN>) {
457 chomp;
458 my ($var, $val) = split /\s+/,$_,2;
459 $Run{$var}=$val;
460 }
461 close (RUN);
462 }
464 sub save_run
465 {
466 my $runfile = $Config{"path_labmaker"}."/".$Config{"path_runfile"};
467 open (RN, "$runfile")
468 or die "Can't save running state to $runfile";
469 for my $var (keys %Run) {
470 print RN $var,"\t",$Run{$var},"\n";
471 }
472 close (RN);
473 }
475 sub print_log
476 {
477 my $logfile = $Config{"path_labmaker"}."/".$Config{"path_logfile"};
478 open (LOG, ">>$logfile")
479 or die "Can't open logfile $logfile for writing";
480 print LOG @_;
481 close (LOG);
482 }
485 sub print_usage_info
486 {
487 print "Usage:\n\n\t$0 [host-list] command\n";
488 print <<'USAGE';
490 Commands:
492 next -- next lab
493 prev -- prev lab
494 set LAB -- set current lab to LAB
495 start -- start this day training
496 stop -- stop this day training
497 show hosts -- show available hosts in the class
498 show labs -- show available labs in the course
499 do COMMAND -- do specified command on the hosts of hostlist
500 report -- generate XML/HTML reports
503 do commands:
505 install [PROFILE] -- install profile
507 Host list:
509 @N -- machine N
510 @N1-N2 -- all of the machines from N1 to N2
511 @N1,N2,N3 -- machine N1, N2 and N3
513 N* is numbers or domain names of the machines.
515 If host list is not specified,
516 command is executed on all of the machines
518 USAGE
519 }