lilalo

annotate lm @ 41:6452bfe549d1

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