/usr/lib/obs/server/BSSched/BuildJob.pm is in obs-server 2.7.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 | # Copyright (c) 2015 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
package BSSched::BuildJob;
# gctx functions
# init_ourjobs
# purgejob
# killjob
# killscheduled
# killbuilding
# killunwantedjobs
# writejob
# add_crossmarker
# update_buildavg
# patchpackstatus
# addjobhist
# path2buildinfopath
#
# ectx functions
# jobfinished
#
# ctx functions
# fakejobfinished
# fakejobfinished_nouseforbuild
# nextbcnt
# create
# metacheck
#
# static functions
# jobname
# sortedmd5toreason
# diffsortedmd5
#
# gctx usage
# myjobsdir
# jobsdir
# arch
# buildavg [rw]
# changed_med [rw]
# projpacks
# reporoot
# prpsearchpath
# repounchanged [rw]
# obsname
# remoteprojs
#
# ctx usage
# gctx
# project
# repository
# gdst
# relsyncmax
# conf
# prpsearchpath
# sysbuild_$buildtype [rw]
# pool
# prp
#
# ectx usage
# gctx
# fullcache
use strict;
use warnings;
use Data::Dumper;
use Digest::MD5 ();
use BSUtil;
use BSXML;
use BSFileDB;
use BSConfiguration;
use BSSched::DoD; # for dodcheck
use BSSched::Access; # for checkaccess
use BSSched::EventSource::Directory; # for sendevent
use Build;
use BSRPC;
use BSCando;
=head1 NAME
BSSched::BuildJob
=head1 SYNOPSIS
TODO
=head1 DESCRIPTION
This library contains functions to handle jobs in openbuild-service
=cut
=head1 FUNCTIONS / METHODS
=cut
# history of succeeded builds
my $historylay = [qw{versrel bcnt srcmd5 rev time duration}];
# scheduled jobs (does not need to be exact)
my %ourjobs; # XXX: move into gctx?
my $workersrcserver = $BSConfig::workersrcserver ? $BSConfig::workersrcserver : $BSConfig::srcserver;
my $workerreposerver = $BSConfig::workerreposerver ? $BSConfig::workerreposerver : $BSConfig::reposerver;
=head2 init_ourjobs - intialize ourjobs hash on startup
This function is used to initialize the ourjobs hash on scheduler startup.
=cut
sub init_ourjobs {
my ($gctx) = @_;
my $myjobsdir = $gctx->{'myjobsdir'};
for my $job (grep {!/(?::dir|:status)$/} ls($myjobsdir)) {
$ourjobs{$1}->{$job} = 1 if $job =~ /^(:.+?|[^:].*?::.+?)::/s;
}
}
=head2 purgejob - remove a job and all of its artifacts
should hold the job lock when called
=cut
sub purgejob {
my ($gctx, $job) = @_;
my $myjobsdir = $gctx->{'myjobsdir'};
if (-d "$myjobsdir/$job:dir") {
unlink("$myjobsdir/$job:dir/$_") for ls("$myjobsdir/$job:dir");
rmdir("$myjobsdir/$job:dir");
}
unlink("$myjobsdir/$job");
unlink("$myjobsdir/$job:status");
delete (($ourjobs{$1} || {})->{$job}) if $job =~ /^(:.+?|[^:].*?::.+?)::/s;
}
=head2 killjob - kill a single build job
input: $job - job identificator
=cut
sub killjob {
my ($gctx, $job) = @_;
my $myjobsdir = $gctx->{'myjobsdir'};
local *F;
if (! -e "$myjobsdir/$job:status") {
# create locked status
my $js = {'code' => 'deleting'};
if (BSUtil::lockcreatexml(\*F, "$myjobsdir/.sched.$$", "$myjobsdir/$job:status", $js, $BSXML::jobstatus)) {
print " (job was not building)\n";
purgejob($gctx, $job);
close F;
return;
}
}
my $js = BSUtil::lockopenxml(\*F, '<', "$myjobsdir/$job:status", $BSXML::jobstatus, 1);
if (!$js) {
# can't happen actually
print " (job was not building)\n";
purgejob($gctx, $job);
return;
}
if ($js->{'code'} eq 'building') {
print " (job was building on $js->{'workerid'})\n";
my $req = {
'uri' => "$js->{'uri'}/discard",
'timeout' => 60,
};
eval {
BSRPC::rpc($req, undef, "jobid=$js->{'jobid'}");
};
warn("kill $job: $@") if $@;
}
purgejob($gctx, $job);
close(F);
}
=head2 killscheduled - kill a single build job if it is scheduled but not building
input: $job - job identificator
=cut
sub killscheduled {
my ($gctx, $job) = @_;
my $myjobsdir = $gctx->{'myjobsdir'};
return if -e "$myjobsdir/$job:status";
local *F;
my $js = {'code' => 'deleting'};
if (BSUtil::lockcreatexml(\*F, "$myjobsdir/.sched.$$", "$myjobsdir/$job:status", $js, $BSXML::jobstatus)) {
purgejob($gctx, $job);
close F;
}
}
=head2 killbuilding - kill build jobs
used if a project/package got deleted to kill all running jobs
input: $prp - prp we are working on
$packid - just kill the builds of the package
=cut
sub killbuilding {
my ($gctx, $prp, $packid) = @_;
my $myjobsdir = $gctx->{'myjobsdir'};
my @jobs;
if (defined $packid) {
my $f = jobname($prp, $packid);
@jobs = grep {$_ eq $f || /^\Q$f\E-[0-9a-f]{32}$/} ls($myjobsdir);
} else {
my $f = jobname($prp, '');
@jobs = grep {/^\Q$f\E/} ls($myjobsdir);
@jobs = grep {!/(?::dir|:status)$/} @jobs;
}
for my $job (@jobs) {
print " killing obsolete job $job\n";
killjob($gctx, $job);
}
}
=head2 killunwantedjobs - kill all jobs where the packagestatus is excluded/disabled
TODO
=cut
sub killunwantedjobs {
my ($gctx, $prp, $packstatus) = @_;
my $job1 = $prp;
$job1 =~ s/\//::/s;
my $job2 = ':'.Digest::MD5::md5_hex($prp);
return unless $ourjobs{$job1} || $ourjobs{$job2};
my $myjobsdir = $gctx->{'myjobsdir'};
for my $job (keys %{$ourjobs{$job1} || {}}, keys %{$ourjobs{$job2} || {}}) {
if ($job =~ /^(?:\Q$job1\E|\Q$job2\E)::(.*)-[0-9a-f]{32}$/s) {
my $status = $packstatus->{$1} || '';
next if $status eq 'scheduled';
if (! -e "$myjobsdir/$job") {
delete(($ourjobs{$1} || {})->{$job}) if $job =~ /^(:.+?|[^:].*?::.+?)::/s;
next;
}
if ($status eq 'disabled' || $status eq 'excluded' || $status eq 'locked') {
print " killing old job $job, now in disabled/excluded/locked state\n";
killjob($gctx, $job);
} elsif ($status eq 'blocked' || $status eq 'unresolvable' || $status eq 'broken') {
# blocked jobs get removed, if they are currently not building. building jobs
# stay since they may become valid again
killscheduled($gctx, $job);
}
}
}
delete $ourjobs{$job1} unless %{$ourjobs{$job1} || {}};
delete $ourjobs{$job2} unless %{$ourjobs{$job2} || {}};
}
=head2 writejob - write a new job to disc
After writing the dispatcher will pick up the job and dispatch it
to a free worker.
=cut
sub writejob {
my ($gctx, $job, $binfo) = @_;
$binfo->{'srcserver'} ||= $workersrcserver;
$binfo->{'reposerver'} ||= $workerreposerver;
my $myjobsdir = $gctx->{'myjobsdir'};
writexml("$myjobsdir/.$job", "$myjobsdir/$job", $binfo, $BSXML::buildinfo);
add_crossmarker($gctx, $binfo->{'hostarch'}, $job) if $binfo->{'hostarch'};
$ourjobs{$1}->{$job} = 1 if $job =~ /^(:.+?|[^:].*?::.+?)::/s;
}
=head2 add_crossmarker - add a marker into a foreign jobdir
TODO
=cut
sub add_crossmarker {
my ($gctx, $hostarch, $job) = @_;
my $myarch = $gctx->{'arch'};
return if $hostarch eq $myarch;
return unless $BSCando::knownarch{$hostarch};
my $markerdir = "$gctx->{'jobsdir'}/$hostarch";
my $marker = "$markerdir/$job:$myarch:cross";
return if -e $marker;
mkdir_p($markerdir);
BSUtil::touch($marker);
}
=head2 update_buildavg - incorporate the jobtime into the buildavg statistics
TODO
=cut
sub update_buildavg {
my ($gctx, $jobtime) = @_;
my $buildavg = $gctx->{'buildavg'} || 0;
my $weight = 0.1;
$buildavg = ($weight * $jobtime) + ((1 - $weight) * $buildavg);
$gctx->{'buildavg'} = $buildavg;
}
=head2 jobfinished - called when a build job is finished
- move artifacts into built result dir
- move built binaries into :full tree
- set changed flag
input: $job - job identification
$js - job status information (BSXML::jobstatus)
$changed - reference to changed hash, mark prp if
we changed the repository
$fullcache - store data for delayed writing of :full.solv
=cut
sub jobfinished {
my ($ectx, $job, $js) = @_;
my $gctx = $ectx->{'gctx'};
my $myjobsdir = $gctx->{'myjobsdir'};
my $info = readxml("$myjobsdir/$job", $BSXML::buildinfo, 1);
my $jobdatadir = "$myjobsdir/$job:dir";
if (!$info || ! -d $jobdatadir) {
print " - $job is bad\n";
return;
}
# dispatch to specialized versions for aggregates and deltas
if ($info->{'file'} eq '_aggregate') {
BSSched::BuildJob::Aggregate::jobfinished($ectx, $job, $js);
return ;
}
if ($info->{'file'} eq '_delta') {
BSSched::BuildJob::DeltaRpm::jobfinished($ectx, $job, $js);
return ;
}
my $myarch = $gctx->{'arch'};
my $changed = $gctx->{'changed_med'};
my $projid = $info->{'project'};
my $repoid = $info->{'repository'};
my $packid = $info->{'package'};
my $prp = "$projid/$repoid";
my $now = time(); # ensure that we use the same time in all logs
if ($info->{'arch'} ne $myarch) {
print " - $job has bad arch\n";
return;
}
my $projpacks = $gctx->{'projpacks'};
if (!$projpacks->{$projid}) {
print " - $job belongs to an unknown project\n";
return;
}
my $pdata = ($projpacks->{$projid}->{'package'} || {})->{$packid};
if (!$pdata) {
print " - $job belongs to an unknown package, discard\n";
return;
}
my $reporoot = $gctx->{'reporoot'};
my $gdst = "$reporoot/$prp/$myarch";
my $dst = "$gdst/$packid";
my $status = readxml("$dst/status", $BSXML::buildstatus, 1);
if ($status && (!$status->{'job'} || $status->{'job'} ne $job)) {
print " - $job is outdated\n";
return;
}
$status ||= {'readytime' => $info->{'readytime'} || $info->{'starttime'}};
# calculate exponential weighted average
my $myjobtime = time() - $status->{'readytime'};
update_buildavg($gctx, $myjobtime);
delete $status->{'job'}; # no longer building
delete $status->{'arch'}; # obsolete
delete $status->{'uri'}; # obsolete
my $code = $js->{'result'};
$code = 'failed' unless $code eq 'succeeded' || $code eq 'unchanged';
my @all = ls($jobdatadir);
my %all = map {$_ => 1} @all;
@all = map {"$jobdatadir/$_"} @all;
mkdir_p($dst);
mkdir_p("$gdst/:meta");
mkdir_p("$gdst/:logfiles.fail");
mkdir_p("$gdst/:logfiles.success");
unlink("$gdst/:repodone");
if (!$all{'meta'}) {
if ($code eq 'succeeded') {
print " - $job claims success but there is no meta\n";
return;
}
# severe failure, create src change fake...
my $verifymd5 = $info->{'verifymd5'} || $info->{'srcmd5'};
writestr("$jobdatadir/meta", undef, "$verifymd5 $packid\nfake to detect source changes... fake\n");
push @all, "$jobdatadir/meta";
$all{'meta'} = 1;
}
# update packstatus so that it doesn't fall back to scheduled
patchpackstatus($gctx, $prp, $packid, $code);
my $meta = $all{'meta'} ? "$jobdatadir/meta" : undef;
if ($code eq 'unchanged') {
print " - $job: build result is unchanged\n";
if ( -e "$gdst/:logfiles.success/$packid" ){
# make sure to use the last succeeded logfile matching to these binaries
link("$gdst/:logfiles.success/$packid", "$dst/logfile.dup");
rename("$dst/logfile.dup", "$dst/logfile");
unlink("$dst/logfile.dup");
}
if (open(F, '+>>', "$dst/logfile")) {
# Add a comment to logfile from last real build
print F "\nRetried build at ".localtime(time())." returned same result, skipped";
close(F);
}
unlink("$gdst/:logfiles.fail/$packid");
rename($meta, "$gdst/:meta/$packid") if $meta;
unlink($_) for @all;
rmdir($jobdatadir);
addjobhist($gctx, $prp, $info, $status, $js, 'unchanged');
$status->{'status'} = 'succeeded';
writexml("$dst/.status", "$dst/status", $status, $BSXML::buildstatus);
$changed->{$prp} ||= 1; # package is no longer blocking
return;
}
if ($code eq 'failed') {
print " - $job: build failed\n";
link("$jobdatadir/logfile", "$jobdatadir/logfile.dup");
rename("$jobdatadir/logfile", "$dst/logfile");
rename("$jobdatadir/logfile.dup", "$gdst/:logfiles.fail/$packid");
rename($meta, "$gdst/:meta/$packid") if $meta;
unlink($_) for @all;
rmdir($jobdatadir);
$status->{'status'} = 'failed';
addjobhist($gctx, $prp, $info, $status, $js, 'failed');
writexml("$dst/.status", "$dst/status", $status, $BSXML::buildstatus);
$changed->{$prp} ||= 1; # package is no longer blocking
return;
}
print " - $prp: $packid built: ".(@all). " files\n";
mkdir_p("$gdst/:logfiles.success");
mkdir_p("$gdst/:logfiles.fail");
unlink("$jobdatadir/.preinstallimage");
BSUtil::touch("$jobdatadir/.preinstallimage") if $info->{'file'} eq '_preinstallimage';
my $useforbuildenabled = 1;
$useforbuildenabled = BSUtil::enabled($repoid, $projpacks->{$projid}->{'useforbuild'}, $useforbuildenabled, $myarch);
$useforbuildenabled = BSUtil::enabled($repoid, $pdata->{'useforbuild'}, $useforbuildenabled, $myarch);
my $prpsearchpath = $gctx->{'prpsearchpath'}->{$prp};
my $fullcache = $ectx->{'fullcache'};
BSSched::BuildResult::update_dst_full($gctx, $prp, $packid, $jobdatadir, $meta, $useforbuildenabled, $prpsearchpath, $fullcache);
$changed->{$prp} = 2 if $useforbuildenabled;
my $repounchanged = $gctx->{'repounchanged'};
delete $repounchanged->{$prp} if $useforbuildenabled;
$repounchanged->{$prp} = 2 if $repounchanged->{$prp};
$changed->{$prp} ||= 1;
# save meta file
rename($meta, "$gdst/:meta/$packid") if $meta;
# write new status
$status->{'status'} = 'succeeded';
addjobhist($gctx, $prp, $info, $status, $js, 'succeeded');
writexml("$dst/.status", "$dst/status", $status, $BSXML::buildstatus);
# write history file
my $duration = 0;
$duration = $js->{'endtime'} - $js->{'starttime'} if $js->{'endtime'} && $js->{'starttime'};;
my $h = {'versrel' => $info->{'versrel'}, 'bcnt' => $info->{'bcnt'}, 'time' => $now, 'srcmd5' => $info->{'srcmd5'}, 'rev' => $info->{'rev'}, 'reason' => $info->{'reason'}, 'duration' => $duration};
BSFileDB::fdb_add("$dst/history", $historylay, $h);
# update relsync file (use relsync.merge if relsync is too big)
if (((-s "$gdst/:relsync") || 0) < 8192 && ! -e "$gdst/:relsync.merge") {
my $relsync = BSUtil::retrieve("$gdst/:relsync", 1) || {};
$relsync->{$packid} = "$info->{'versrel'}.$info->{'bcnt'}";
BSUtil::store("$gdst/.:relsync", "$gdst/:relsync", $relsync);
} else {
my $relsync = BSUtil::retrieve("$gdst/:relsync.merge", 1) || {};
$relsync->{$packid} = "$info->{'versrel'}.$info->{'bcnt'}";
BSUtil::store("$gdst/.:relsync.merge", "$gdst/:relsync.merge", $relsync);
}
# save logfile
link("$jobdatadir/logfile", "$jobdatadir/logfile.dup");
rename("$jobdatadir/logfile", "$dst/logfile");
rename("$jobdatadir/logfile.dup", "$gdst/:logfiles.success/$packid");
unlink("$gdst/:logfiles.fail/$packid");
}
=head2 fakejobfinished - fake a built job
Used for aggregates, as they do not get built on a worker, but
by the scheduler itself.
=cut
sub fakejobfinished {
my ($ctx, $packid, $job, $code, $buildinfoskel, $needsign) = @_;
my $gctx = $ctx->{'gctx'};
my $myarch = $gctx->{'arch'};
local *F;
# directly go into the finished state
my $jobstatus = {
'code' => 'finished',
'result' => $code,
};
my $myjobsdir = $gctx->{'myjobsdir'};
if (!BSUtil::lockcreatexml(\*F, "$myjobsdir/.$job", "$myjobsdir/$job:status", $jobstatus, $BSXML::jobstatus)) {
die("job lock failed\n");
}
my $binfo = {
'project' => $ctx->{'project'},
'repository' => $ctx->{'repository'},
'package' => $packid,
'arch' => $myarch,
'job' => $job,
%{$buildinfoskel || {}},
};
writejob($gctx, $job, $binfo);
close(F);
my $ev = {'type' => 'built', 'arch' => $myarch, 'job' => $job};
if ($needsign) {
BSSched::EventSource::Directory::sendevent($gctx, $ev, 'signer', "finished:$myarch:$job");
} else {
BSSched::EventSource::Directory::sendevent($gctx, $ev, $myarch, "finished:$job");
}
}
##########################################################################
=head2 fakejobfinished_nouseforbuild - fake a nouseforbuild built job
used for channel and patchinfo builds. They do not need signing and do
not go into the full tree. Thus we do not need to generate an event,
but can directly move the result into the build directory.
=cut
sub fakejobfinished_nouseforbuild {
my ($ctx, $packid, $job, $code, $bininfo, $pdata) = @_;
my $gctx = $ctx->{'gctx'};
my $myarch = $gctx->{'arch'};
my $projid = $ctx->{'project'};
my $repoid = $ctx->{'repository'};
my $prp = "$projid/$repoid";
my $gdst = "$gctx->{'reporoot'}/$prp/$myarch";
my $dst = "$gdst/$packid";
my $myjobsdir = $gctx->{'myjobsdir'};
my $jobdatadir = "$myjobsdir/$job:dir";
mkdir_p($dst);
mkdir_p("$gdst/:meta");
mkdir_p("$gdst/:logfiles.fail");
mkdir_p("$gdst/:logfiles.success");
unlink("$gdst/:repodone");
if (-e "$jobdatadir/logfile") {
link("$jobdatadir/logfile", "$jobdatadir/logfile.dup");
if ($code eq 'failed') {
rename("$jobdatadir/logfile.dup", "$gdst/:logfiles.fail/$packid");
} else {
rename("$jobdatadir/logfile.dup", "$gdst/:logfiles.success/$packid");
unlink("$gdst/:logfiles.fail/$packid");
}
rename("$jobdatadir/logfile", "$dst/logfile");
}
rename("$jobdatadir/meta", "$gdst/:meta/$packid");
if ($code eq 'succeeded') {
$bininfo ||= {};
# fixup filename entries
for (keys %$bininfo) {
die("job $job contains imported files\n") if /^::import::/;
if (defined($bininfo->{$_}->{'filename'}) && $bininfo->{$_}->{'filename'} ne $_) {
$bininfo->{$_} = { %{$bininfo->{$_}}, 'filename' => $_ };
}
}
# commit job and
BSUtil::cleandir($dst);
for my $f (ls($jobdatadir)) {
rename("$jobdatadir/$f", "$dst/$f") || die("rename $jobdatadir/$f $dst/$f: $!\n");
}
if (!BSSched::Access::checkaccess($ctx->{'gctx'}, 'sourceaccess', $projid, $packid, $repoid)) {
BSUtil::touch("$dst/.nosourceaccess");
$bininfo->{'.nosourceaccess'} = {};
}
$bininfo->{'.nouseforbuild'} = {};
BSUtil::store("$dst/.bininfo.new", "$dst/.bininfo", $bininfo);
my @bininfo_s = stat("$dst/.bininfo");
$bininfo->{'.bininfo'} = {'id' => "$bininfo_s[9]/$bininfo_s[7]/$bininfo_s[1]"} if @bininfo_s;
my $gbininfo = {};
$gbininfo = BSUtil::retrieve("$gdst/:bininfo.merge", 1) if -e "$gdst/:bininfo.merge";
if ($gbininfo) {
$gbininfo->{$packid} = $bininfo;
BSUtil::store("$gdst/.:bininfo.merge", "$gdst/:bininfo.merge", $gbininfo);
} else {
writestr("$gdst/.:bininfo.merge", "$gdst/:bininfo.merge", ''); # corrupt file, mark
}
delete $bininfo->{'.bininfo'};
# write history file
my $h = {'versrel' => $pdata->{'versrel'}, 'bcnt' => "0", 'time' => time(), 'srcmd5' => $pdata->{'srcmd5'}, 'rev' => $pdata->{'rev'}, 'reason' => "", 'duration' => "0"};
BSFileDB::fdb_add("$dst/history", $historylay, $h);
}
BSUtil::cleandir($jobdatadir);
rmdir($jobdatadir);
}
##########################################################################
=head2 patchpackstatus - TODO: add summary
patch the packstatus entry of package $packid so that it reflects the finished state
and does not revert back to scheduled
=cut
sub patchpackstatus {
my ($gctx, $prp, $packid, $code) = @_;
my $reporoot = $gctx->{'reporoot'};
my $myarch = $gctx->{'arch'};
my $gdst = "$reporoot/$prp/$myarch";
$code ||= 'unknown';
BSUtil::appendstr("$gdst/:packstatus.finished", "$code $packid\n");
# touch mtime to make watchers see a change
utime(time, time, "$gdst/:packstatus");
}
=head2 addjobhist - add a new job entry to :jobhistory file
TODO: add description
=cut
sub addjobhist {
my ($gctx, $prp, $info, $status, $js, $code) = @_;
my $jobhist = {};
$jobhist->{'code'} = $code;
$jobhist->{$_} = $js->{$_} for qw{readytime starttime endtime uri workerid hostarch};
$jobhist->{$_} = $info->{$_} for qw{package rev srcmd5 versrel bcnt reason};
$jobhist->{'verifymd5'} = $info->{'verifymd5'} if $info->{'verifymd5'};
$jobhist->{'readytime'} ||= $status->{'readytime'}; # backward compat
my $myarch = $gctx->{'arch'};
my $gdst = "$gctx->{'reporoot'}/$prp/$myarch";
mkdir_p($gdst);
BSFileDB::fdb_add("$gdst/:jobhistory", $BSXML::jobhistlay, $jobhist);
}
=head2 nextbcnt - calculate the build counter for the next build
TODO: add description
=cut
sub nextbcnt {
my ($ctx, $packid, $pdata) = @_;
my $h;
my $gdst = $ctx->{'gdst'};
my $relsyncmax = $ctx->{'relsyncmax'};
my $dst = "$gdst/$packid";
if (-e "$dst/history") {
$h = BSFileDB::fdb_getmatch("$dst/history", $historylay, 'versrel', $pdata->{'versrel'}, 1);
}
$h = {'bcnt' => 0} unless $h;
# max with sync data
my $tag = $pdata->{'bcntsynctag'} || $packid;
if ($relsyncmax && $relsyncmax->{"$tag/$pdata->{'versrel'}"}) {
if ($h->{'bcnt'} + 1 < $relsyncmax->{"$tag/$pdata->{'versrel'}"}) {
$h->{'bcnt'} = $relsyncmax->{"$tag/$pdata->{'versrel'}"} - 1;
}
}
return $h->{'bcnt'} + 1;
}
=cut
=head2 create - create a new build job
input: $ctx - prp context
$packid - package to be built
$pdata - package data
$info - file and dependency information
$subpacks - all subpackages of this package we know of
$edeps - expanded build dependencies
$reason - what triggered the build
$needed - packages blocked by this job
output: $state - scheduled, broken
$job/error - the job or the error
check if this job is already building, if yes, do nothing.
otherwise calculate and expand build dependencies, kill all
other jobs of the same prp/package, write status and job info.
not that hard, was it?
=cut
sub create {
my ($ctx, $packid, $pdata, $info, $subpacks, $edeps, $reason, $needed) = @_;
my $gctx = $ctx->{'gctx'};
my $myarch = $gctx->{'arch'};
my $projid = $ctx->{'project'};
my $repoid = $ctx->{'repository'};
my $bconf = $ctx->{'conf'};
my $gdst = $ctx->{'gdst'};
my $projpacks = $gctx->{'projpacks'};
my $proj = $projpacks->{$projid};
my $prp = "$projid/$repoid";
my $srcmd5 = $pdata->{'srcmd5'};
my $job = jobname($prp, $packid);
my $myjobsdir = $gctx->{'myjobsdir'};
if (-s "$myjobsdir/$job-$srcmd5") {
add_crossmarker($gctx, $bconf->{'hostarch'}, "$job-$srcmd5") if $bconf->{'hostarch'}; # just in case...
return ('scheduled', "$job-$srcmd5");
}
return ('scheduled', $job) if -s "$myjobsdir/$job"; # obsolete
my @otherjobs = grep {/^\Q$job\E-[0-9a-f]{32}$/} ls($myjobsdir);
$job = "$job-$srcmd5";
# a new one. expand usedforbuild. write info file.
my $buildtype = Build::recipe2buildtype($info->{'file'});
my $syspath;
my $searchpath = path2buildinfopath($gctx, $ctx->{'prpsearchpath'});
if ($buildtype eq 'kiwi') {
# switch searchpath to kiwi info path
$syspath = $searchpath if @$searchpath;
$searchpath = path2buildinfopath($gctx, [ expandkiwipath($info, $ctx->{'prpsearchpath'}) ]);
}
# calculate sysdeps (cannot cache in the kiwi case)
my @sysdeps;
if ($buildtype eq 'kiwi') {
@sysdeps = Build::get_sysbuild($bconf, 'kiwi-image', [ grep {/^kiwi-.*:/} @{$info->{'dep'} || []} ]);
} else {
$ctx->{"sysbuild_$buildtype"} ||= [ Build::get_sysbuild($bconf, $buildtype) ];
@sysdeps = @{$ctx->{"sysbuild_$buildtype"}};
}
# calculate packages needed for building
my @bdeps = grep {!/^\// || $bconf->{'fileprovides'}->{$_}} @{$info->{'prereq'} || []};
unshift @bdeps, '--directdepsend--' if @bdeps;
unshift @bdeps, @{$info->{'dep'} || []};
push @bdeps, '--ignoreignore--' if @sysdeps;
if ($buildtype eq 'kiwi') {
@bdeps = (1, @$edeps); # reuse edeps packages, no need to expand again
} else {
@bdeps = Build::get_build($bconf, $subpacks, @bdeps);
}
if (!shift(@bdeps)) {
print " unresolvable:\n";
print " $_\n" for @bdeps;
return ('unresolvable', join(', ', @bdeps));
}
if (@sysdeps && !shift(@sysdeps)) {
print " unresolvable:\n";
print " $_\n" for @sysdeps;
return ('unresolvable', join(', ', @sysdeps));
}
if ($BSConfig::enable_download_on_demand) {
my $dods = BSSched::DoD::dodcheck($ctx, $ctx->{'pool'}, $myarch, Build::get_preinstalls($bconf), Build::get_vminstalls($bconf), @bdeps, @sysdeps);
if ($dods) {
print " blocked: $dods\n";
return ('blocked', $dods);
}
}
my $dst = "$gdst/$packid";
# find the last build count we used for this version/release
mkdir_p($dst);
my $bcnt = nextbcnt($ctx, $packid, $pdata);
# kill those ancient other jobs
for my $otherjob (@otherjobs) {
print " killing old job $otherjob\n";
killjob($gctx, $otherjob);
}
# jay! ready for building, write status and job info
my $now = time();
writexml("$dst/.status", "$dst/status", { 'status' => 'scheduled', 'readytime' => $now, 'job' => $job}, $BSXML::buildstatus);
# And store reason and time
$reason->{'time'} = $now;
writexml("$dst/.reason", "$dst/reason", $reason, $BSXML::buildreason);
my @pdeps = Build::get_preinstalls($bconf);
my @vmdeps = Build::get_vminstalls($bconf);
my %runscripts = map {$_ => 1} Build::get_runscripts($bconf);
my %bdeps = map {$_ => 1} @bdeps;
my %pdeps = map {$_ => 1} @pdeps;
my %vmdeps = map {$_ => 1} @vmdeps;
my %edeps = map {$_ => 1} @$edeps;
my %sysdeps = map {$_ => 1} @sysdeps;
@bdeps = BSUtil::unify(@pdeps, @vmdeps, @$edeps, @bdeps, @sysdeps);
for (@bdeps) {
my $n = $_;
$_ = {'name' => $_};
$_->{'preinstall'} = 1 if $pdeps{$n};
$_->{'vminstall'} = 1 if $vmdeps{$n};
$_->{'runscripts'} = 1 if $runscripts{$n};
$_->{'notmeta'} = 1 unless $edeps{$n};
if (@sysdeps) {
$_->{'installonly'} = 1 if $sysdeps{$n} && !$bdeps{$n} && $buildtype ne 'kiwi';
$_->{'noinstall'} = 1 if $bdeps{$n} && !($sysdeps{$n} || $vmdeps{$n} || $pdeps{$n});
}
}
if ($info->{'extrasource'}) {
push @bdeps, map {{
'name' => $_->{'file'}, 'version' => '', 'repoarch' => 'src',
'project' => $_->{'project'}, 'package' => $_->{'package'}, 'srcmd5' => $_->{'srcmd5'},
}} @{$info->{'extrasource'}};
}
my $vmd5 = $pdata->{'verifymd5'} || $pdata->{'srcmd5'};
my $binfo = {
'project' => $projid,
'repository' => $repoid,
'package' => $packid,
'srcserver' => $workersrcserver,
'reposerver' => $workerreposerver,
'job' => $job,
'arch' => $myarch,
'reason' => $reason->{'explain'},
'readytime' => $now,
'srcmd5' => $pdata->{'srcmd5'},
'verifymd5' => $vmd5,
'rev' => $pdata->{'rev'},
'file' => $info->{'file'},
'versrel' => $pdata->{'versrel'},
'bcnt' => $bcnt,
'subpack' => ($subpacks || []),
'bdep' => \@bdeps,
'path' => $searchpath,
'needed' => $needed,
};
my $obsname = $gctx->{'obsname'};
$binfo->{'disturl'} = "obs://$obsname/$projid/$repoid/$pdata->{'srcmd5'}-$packid";
$binfo->{'syspath'} = $syspath if $syspath;
$binfo->{'hostarch'} = $bconf->{'hostarch'} if $bconf->{'hostarch'};
$binfo->{'constraintsmd5'} = $pdata->{'constraintsmd5'} if $pdata->{'constraintsmd5'};
$binfo->{'prjconfconstraint'} = $bconf->{'constraint'} if @{$bconf->{'constraint'} || []};
$binfo->{'nounchanged'} = 1 if $info->{'nounchanged'};
if ($pdata->{'revtime'}) {
$binfo->{'revtime'} = $pdata->{'revtime'};
# use max of revtime for interproject links
for (@{$pdata->{'linked'} || []}) {
last if $_->{'project'} ne $projid || !$proj->{'package'};
my $lpdata = $proj->{'package'}->{$_->{'package'}} || {};
$binfo->{'revtime'} = $lpdata->{'revtime'} if ($lpdata->{'revtime'} || 0) > $binfo->{'revtime'};
}
}
$binfo->{'imagetype'} = $info->{'imagetype'} if $info->{'imagetype'};
my $release = $pdata->{'versrel'};
$release = '0' unless defined $release;
$release =~ s/.*-//;
if (defined($bconf->{'release'})) {
$binfo->{'release'} = $bconf->{'release'};
$binfo->{'release'} =~ s/\<CI_CNT\>/$release/g;
$binfo->{'release'} =~ s/\<B_CNT\>/$bcnt/g;
}
my $debuginfo = $bconf->{'debuginfo'};
$debuginfo = BSUtil::enabled($repoid, $proj->{'debuginfo'}, $debuginfo, $myarch);
$debuginfo = BSUtil::enabled($repoid, $pdata->{'debuginfo'}, $debuginfo, $myarch);
$binfo->{'debuginfo'} = 1 if $debuginfo;
writejob($gctx, $job, $binfo);
# all done. the dispatcher will now pick up the job and send it
# to a worker.
return ('scheduled', $job);
}
=head2 jobname - calculate the first part of the name of a new build job
input: $prp - prp the job belongs to
$packid - package we are building
output: first part of job identification
append srcmd5 for full identification
=cut
sub jobname {
my ($prp, $packid) = @_;
my $job = "$prp/$packid";
$job =~ s/\//::/g;
$job = ':'.Digest::MD5::md5_hex($prp).'::'.(length($packid) > 160 ? ':'.Digest::MD5::md5_hex($packid) : $packid) if length($job) > 200;
return $job;
}
=head2 path2buildinfopath - TODO: add summary
TODO: add description
=cut
sub path2buildinfopath {
my ($gctx, $path) = @_;
my $remoteprojs = $gctx->{'remoteprojs'};
my @ret;
for (@{$path || []}) {
my @pr = split('/', $_, 2);
my $server = $workerreposerver;
if ($remoteprojs->{$pr[0]}) {
$server = $workersrcserver;
my $par = $remoteprojs->{$pr[0]}->{'partition'};
if ($par) {
# XXX: should also come from src server
if ($BSConfig::workerpartitionservers) {
$server = $BSConfig::workerpartitionservers->{$par} || $remoteprojs->{$pr[0]}->{'remoteurl'};
} else {
$server = $remoteprojs->{$pr[0]}->{'remoteurl'};
}
}
}
push @ret, {'project' => $pr[0], 'repository' => $pr[1], 'server' => $server};
}
return \@ret;
}
=head2 metacheck - check if the old meta is different to the new meta
TODO: add description
=cut
sub metacheck {
my ($ctx, $packid, $buildtype, $new_meta, $data) = @_;
my $gctx = $ctx->{'gctx'};
my $prp = $ctx->{'prp'};
my $gdst = $ctx->{'gdst'};
my @meta = split("\n", (readstr("$gdst/:meta/$packid", 1) || ''));
if (!@meta) {
print " - $packid ($buildtype)\n";
print " no former build, start build\n";
return ('scheduled', [ @$data, {'explain' => 'new build'} ]);
}
if ($meta[0] ne $new_meta->[0]) {
print " - $packid ($buildtype)\n";
print " src change, start build\n";
return ('scheduled', [ @$data, {'explain' => 'source change', 'oldsource' => substr($meta[0], 0, 32)} ]);
}
if (@meta == 2 && $meta[1] =~ /^fake/) {
my @s = stat("$gdst/:meta/$packid");
if (!@s || $s[9] + 14400 > time()) {
print " - $packid ($buildtype)\n";
print " buildsystem setup failure\n";
return ('failed')
}
print " - $packid ($buildtype)\n";
print " retrying bad build\n";
return ('scheduled', [ @$data, { 'explain' => 'retrying bad build' } ]);
}
if (join('\n', @meta) eq join('\n', @$new_meta)) {
if (($buildtype eq 'kiwi-image' || $buildtype eq 'kiwi-product') && $ctx->{'relsynctrigger'}->{$packid}) {
print " - $packid ($buildtype)\n";
print " rebuild counter sync\n";
return ('scheduled', [ @$data, {'explain' => 'rebuild counter sync'} ]);
}
#print " - $packid ($buildtype)\n";
#print " nothing changed\n";
return ('done');
}
my $repo = $ctx->{'repo'};
if ($buildtype eq 'kiwi-image' || $buildtype eq 'kiwi-product') {
my $rebuildmethod = $repo->{'rebuild'} || 'transitive';
if ($rebuildmethod eq 'local') {
#print " - $packid ($buildtype)\n";
#print " nothing changed\n";
return ('done');
}
}
my @diff = diffsortedmd5(\@meta, $new_meta);
print " - $packid ($buildtype)\n";
print " $_\n" for @diff;
print " meta change, start build\n";
return ('scheduled', [ @$data, {'explain' => 'meta change', 'packagechange' => sortedmd5toreason(@diff)} ]);
}
=head2 sortedmd5toreason - convert the diffsortedmd5 output to a reason string
TODO: add description
=cut
sub sortedmd5toreason {
my @res;
for my $line (@_) {
my $tag = substr($line, 0, 1); # just the first char
$tag = 'md5sum' if $tag eq '!';
$tag = 'added' if $tag eq '+';
$tag = 'removed' if $tag eq '-';
push @res, { 'change' => $tag, 'key' => substr($line, 1) };
}
return \@res;
}
=head2 diffsortedmd5 - diff two meta arrays
TODO: add description
=cut
sub diffsortedmd5 {
my ($fromp, $top) = @_;
my @ret;
my @from = map {[$_, substr($_, 34)]} @$fromp;
my @to = map {[$_, substr($_, 34)]} @$top;
@from = sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0]} @from;
@to = sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0]} @to;
for my $f (@from) {
if (@to && $f->[1] eq $to[0]->[1]) {
push @ret, "!$f->[1]" if $f->[0] ne $to[0]->[0];
shift @to;
next;
}
if (!@to || $f->[1] lt $to[0]->[1]) {
push @ret, "-$f->[1]";
next;
}
while (@to && $f->[1] gt $to[0]->[1]) {
push @ret, "+$to[0]->[1]";
shift @to;
}
redo;
}
push @ret, "+$_->[1]" for @to;
return @ret;
}
=head2 expandkiwipath - TODO: add summary
TODO: add description
=cut
sub expandkiwipath {
my ($info, $prpsearchpath) = @_;
my @path;
for (@{$info->{'path'} || []}) {
if ($_->{'project'} eq '_obsrepositories') {
push @path, @{$prpsearchpath || []};
} else {
push @path, "$_->{'project'}/$_->{'repository'}";
}
}
return @path;
}
1;
|