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