/
bs_worker
executable file
·3457 lines (3225 loc) · 110 KB
/
bs_worker
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
#!/usr/bin/perl -w
#
# Copyright (c) 2006-2009 Michael Schroeder, Novell Inc.
#
# 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
#
################################################################
#
# Worker build process. Builds jobs received from a Repository Server,
# sends build binary packages back.
#
BEGIN {
my ($wd) = $0 =~ m-(.*)/- ;
$wd ||= '.';
unshift @INC, "$wd/build";
unshift @INC, "$wd";
}
use Digest::MD5 ();
use XML::Structured ':bytes';
use Data::Dumper;
use POSIX;
use Fcntl qw(:DEFAULT :flock);
BEGIN { Fcntl->import(':seek') unless defined &SEEK_SET; }
use Storable;
use BSRPC;
use BSServer;
use BSConfiguration;
use BSUtil;
use BSXML;
use BSKiwiXML;
use BSHTTP;
use BSBuild;
use BSCando;
use strict;
my @binsufs = qw{rpm deb pkg.tar.gz pkg.tar.xz};
my $binsufsre = join('|', map {"\Q$_\E"} @binsufs);
my $buildroot;
my $port;
my $statedir;
my $hostarch;
my $vm = '';
my $vm_tmpfs_mode;
my $vm_root = '';
my $vm_swap = '';
my $vm_kernel;
my $vm_initrd;
my $vm_memory;
my $vm_worker_name;
my $vm_worker_instance;
my $vmdisk_rootsize;
my $vmdisk_swapsize;
my $vmdisk_filesystem;
my $vmdisk_mount_options;
my $vmdisk_clean;
my $emulator_script;
my $hugetlbfs;
my $workerid;
my $srcserver;
my @reposervers;
my $testmode;
my $noworkercheck;
my $nobuildcodecheck;
my $oneshot;
my $silent;
my $hostcheck;
my $localkiwi;
my $owner;
my @hostlabel;
my $getbinariesproxy;
my $hardstatus;
my $jobs;
my $threads;
my $cachedir;
my $cachesize;
# current XEN has no xenstore anymore
my $xenstore_maxsize = 20 * 1000000;
# 1 hour timeout to avoid forever hanging workers
my $gettimeout = 1 * 3600;
my $buildlog_maxsize = $BSConfig::buildlog_maxsize ? $BSConfig::buildlog_maxsize : 500 * 1000000;
my $buildlog_maxidle = $BSConfig::buildlog_maxidle ? $BSConfig::buildlog_maxidle : 8 * 3600;
# for collecting statistics
my $binariesdownload;
my $binariescachehits;
my $binariesdownloadsize;
$hostcheck = $BSConfig::workerhostcheck if defined($BSConfig::workerhostcheck);
sub lockstate {
while (1) {
open(STATELOCK, '>>', "$statedir/state") || die("$statedir/state: $!\n");
flock(STATELOCK, LOCK_EX) || die("flock $statedir/state: $!\n");
my @s = stat(STATELOCK);
last if $s[3]; # check nlink
close(STATELOCK); # race, try again
}
my $oldstate = readxml("$statedir/state", $BSXML::workerstate, 1);
$oldstate = {} unless $oldstate;
return $oldstate;
}
sub unlockstate {
close(STATELOCK);
}
sub commitstate {
my ($newstate) = @_;
writexml("$statedir/state.new", "$statedir/state", $newstate, $BSXML::workerstate) if $newstate;
close(STATELOCK);
}
sub hardstatus {
return unless $hardstatus;
my $l = $_[0] || '';
$l =~ s/[\000-\037\177-\377]//g; # ascii only, please
$l = substr($l, 0, 40) if length($l) > 40;
print "\033_$l\033\\";
}
sub trunc_logfile {
my ($lf) = @_;
open(LF, '<', $lf) || return;
my $buf;
sysread(LF, $buf, 1000000);
$buf .= "\n\n[truncated]\n\n";
sysseek(LF, -1000000, 2);
sysread(LF, $buf, 1000000, length($buf));
close LF;
$buf .= "\nLogfile got too big, killed job.\n";
open(LF, ">$lf.new") || return;
syswrite(LF, $buf);
close LF;
rename("$lf.new", $lf);
}
sub tail_logfile {
my ($lf, $size) = @_;
local *LF;
open(LF, '<', $lf) || return '';
if (-s LF > $size) {
defined(sysseek(LF, -$size, 2)) || return '';
}
my $buf = '';
sysread(LF, $buf, $size);
close LF;
return $buf;
}
sub cleanup_job {
if ($vm_tmpfs_mode) {
qsystem("umount", "-l", $buildroot) && die("umount tmpfs failed: $!\n");
}
}
sub kill_job {
my @args;
# same args as in the build call
if ($vm =~ /(xen|kvm|zvm|emulator)/) {
push @args, '--root', "$buildroot/.mount";
if ($vm =~ /emulator/) {
push @args, '--vm-type', 'emulator';
push @args, '--vm-disk', $vm_root;
push @args, '--emulator-script', $emulator_script if $emulator_script;
} else {
push @args, '--vm-worker', $vm_worker_name if $vm_worker_name;
push @args, '--vm-worker-nr', $vm_worker_instance if $vm_worker_instance;
push @args, $vm, $vm_root;
}
} else {
push @args, '--root', $buildroot;
push @args, '--vm-type', $vm if $vm =~ /lxc/;
}
if (system("$statedir/build/build", @args, "--kill")) {
return 0;
}
cleanup_job();
return 1;
}
sub usage {
my ($ret) = @_;
print <<EOF;
Usage: $0 [OPTION] --root <directory> --statedir <directory>
--root : buildroot directory
--port : fixed port number
--statedir : state directory
--id : worker id
--reposerver: define reposerver, can be used multiple times
--arch : define hostarch (overrides 'uname -m')
currently supported architectures:
@{[sort keys %BSCando::cando]}
--kvm : enable kvm
--xen : enable xen
--lxc : enable lxc
--emulator : enable emulator (requires prepared emulator.sh script)
--zvm : enable z/VM
--tmpfs : uses tmpfs (memory) for the build root
--device : set kvm or xen root device (default is <root>/root file)
--swap : set kvm or xen swap device (default is <root>/swap file)
--hostlabel : define a host label for build constraints, can be used multiple times
--vm-kernel : set kernel to use (xen/kvm)
--vm-initrd : set initrd to use (xen/kvm)
--vm-memory : set amount of memory to use (xen/kvm)
--vm-worker NAME
: (z/VM) set name of the actual worker
--vm-worker-nr NUMBER
: (z/VM) set instance number of the actual worker
--vmdisk-rootsize <size>
: size of the root disk image (default 4096M)
--vmdisk-swapsize <size>
: size of the swap disk image (default 1024M)
--vmdisk-filesystem <none|ext3|ext4>
: filesystem to use for autosetup root disk image
--emulator-script <script>
: Define the emulator script
--hugetlbfs HUGETLBFSPATH
: Use hugetlb for memory management, path to mounted hugetlbfs.
--test : enable test mode
--build : just build the package, don't send anything back
(needs a buildinfo file as argument)
--noworkerupdate
: do not check if the worker is up-to-date
--nobuildupdate
: do not check if the build code is up-to-date
--nocodeupdate
: do not update both worker and build code
--jobs <nr> : hand over the number of parallel jobs to build
--threads <nr> : sets number of threads for KVM
--oneshot <seconds>
: just build one package, do not wait more then
<seconds> seconds if nothing is available
--hostcheck <hostcheck>
: call to check if the host can build the package
--owner <name>
: report the build host owner name to server
--cachedir <cachedir>
--cachesize <size_in_mb>
: use cachedir to cache fetched binaries
--getbinariesproxy <proxyserver>
: define a proxy server for getbinaries requests
--help : this message
EOF
exit($ret || 0);
}
my @saveargv = @ARGV; # so we can restart ourself
my $justbuild;
my $exitrestart;
my $exitrestart_timeout = 300; # wait max 5 minuntes
exit(0) if @ARGV == 1 && $ARGV[0] eq '--selftest';
while (@ARGV) {
usage(0) if $ARGV[0] eq '--help';
if ($ARGV[0] eq '--exit' || $ARGV[0] eq '--stop') {
shift @ARGV;
$exitrestart = 'exit';
next;
}
if ($ARGV[0] eq '--restart') {
shift @ARGV;
$exitrestart = 'restart';
next;
}
if ($ARGV[0] eq '--root') {
shift @ARGV;
$buildroot = shift @ARGV;
next;
}
if ($ARGV[0] eq '--port') {
shift @ARGV;
$port = shift @ARGV;
next;
}
if ($ARGV[0] eq '--arch') {
shift @ARGV;
$hostarch = shift @ARGV;
next;
}
if ($ARGV[0] eq '--statedir') {
shift @ARGV;
$statedir = shift @ARGV;
next;
}
if ($ARGV[0] eq '--srcserver') {
# default value used if buildinfo does not contain srcserver element
shift @ARGV;
$srcserver = shift @ARGV;
next;
}
if ($ARGV[0] eq '--reposerver') {
shift @ARGV;
my $server = shift @ARGV;
push @reposervers, $server unless grep {$_ eq $server} @reposervers;
next;
}
if ($ARGV[0] eq '--getbinariesproxy') {
shift @ARGV;
$getbinariesproxy = shift @ARGV;
next;
}
if ($ARGV[0] eq '--hostlabel') {
shift @ARGV;
my $label = shift @ARGV;
push @hostlabel, $label unless grep {$_ eq $label} @hostlabel;
next;
}
if ($ARGV[0] eq '--id') {
shift @ARGV;
$workerid = shift @ARGV;
next;
}
if ($ARGV[0] eq '--test') {
shift @ARGV;
$testmode = 1;
next;
}
if ($ARGV[0] =~ /^--(kvm|xen|lxc|emulator|zvm)$/) {
$vm = "--$1";
shift @ARGV;
next;
}
if ($ARGV[0] eq '--tmpfs') {
shift @ARGV;
$vm_tmpfs_mode = 1;
next;
}
if ($ARGV[0] eq '--xendevice' || $ARGV[0] eq '--device') {
shift @ARGV;
$vm_root = shift @ARGV;
next;
}
if ($ARGV[0] eq '--xenswap' || $ARGV[0] eq '--swap') {
shift @ARGV;
$vm_swap = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-kernel') {
shift @ARGV;
$vm_kernel = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-initrd') {
shift @ARGV;
$vm_initrd = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-memory') {
shift @ARGV;
$vm_memory = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-worker') {
shift @ARGV;
$vm_worker_name = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vm-worker-nr') {
shift @ARGV;
$vm_worker_instance = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-rootsize') {
shift @ARGV;
$vmdisk_rootsize = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-swapsize') {
shift @ARGV;
$vmdisk_swapsize = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-filesystem') {
shift @ARGV;
$vmdisk_filesystem = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-mount-options') {
shift @ARGV;
$vmdisk_mount_options = shift @ARGV;
next;
}
if ($ARGV[0] eq '--vmdisk-clean') {
shift @ARGV;
$vmdisk_clean = 1;
next;
}
if ($ARGV[0] eq '--emulator-script') {
shift @ARGV;
$emulator_script = shift @ARGV;
next;
}
if ($ARGV[0] eq '--hugetlbfs') {
shift @ARGV;
$hugetlbfs = shift @ARGV;
next;
}
if ($ARGV[0] eq '--build') {
shift @ARGV;
$justbuild = 1;
next;
}
if ($ARGV[0] eq '--oneshot') {
shift @ARGV;
$oneshot = shift @ARGV;
next;
}
if ($ARGV[0] eq '--hostcheck') {
shift @ARGV;
$hostcheck = shift @ARGV;
next;
}
if ($ARGV[0] eq '--nocodeupdate') {
shift @ARGV;
$noworkercheck = 1;
$nobuildcodecheck = 1;
next;
}
if ($ARGV[0] eq '--noworkerupdate') {
shift @ARGV;
$noworkercheck = 1;
next;
}
if ($ARGV[0] eq '--nobuildupdate') {
shift @ARGV;
$nobuildcodecheck = 1;
next;
}
if ($ARGV[0] eq '--silent') {
shift @ARGV;
$silent= 1;
next;
}
if ($ARGV[0] eq '--localkiwi') {
shift @ARGV;
$localkiwi = shift @ARGV;
next;
}
if ($ARGV[0] eq '--owner') {
shift @ARGV;
$owner = shift @ARGV;
next;
}
if ($ARGV[0] eq '--jobs') {
shift @ARGV;
$jobs = shift @ARGV;
next;
}
if ($ARGV[0] eq '--threads') {
shift @ARGV;
$threads = shift @ARGV;
next;
}
if ($ARGV[0] eq '--cachedir') {
shift @ARGV;
$cachedir = shift @ARGV;
next;
}
if ($ARGV[0] eq '--cachesize') {
shift @ARGV;
$cachesize = shift @ARGV;
$cachesize *= 1024*1024;
next;
}
if ($ARGV[0] eq '--hardstatus') {
shift @ARGV;
$hardstatus = 1;
next;
}
last;
}
if ($exitrestart) {
usage(1) unless $statedir;
if ((! -e "$statedir/lock") || BSUtil::lockcheck('>>', "$statedir/lock")) {
die("worker not running.\n") if $exitrestart eq 'restart';
print "worker not running.\n";
exit(0);
}
my $state = lockstate();
$state->{'state'} = 'idle' unless $state->{'state'};
if ($state->{'state'} eq 'building' || $state->{'state'} eq 'killed' || $state->{'state'} eq 'discarded') {
$state->{'state'} = 'discarded';
$state->{'nextstate'} = $exitrestart eq 'exit' ? 'exit' : 'rebooting';
} else {
$state->{'state'} = $exitrestart eq 'exit' ? 'exit' : 'rebooting';
}
commitstate($state);
# now wait till the state changes...
while(1) {
select(undef, undef, undef, .1);
if (defined($exitrestart_timeout)) {
$exitrestart_timeout -= .1;
die("timeout reached, worker not responding\n") if $exitrestart_timeout < 0;
}
my $curstate = readxml("$statedir/state", $BSXML::workerstate, 1);
last unless $curstate && $curstate->{'state'};
next if $curstate->{'nextstate'};
last if $curstate->{'state'} ne 'exit' && $curstate->{'state'} ne 'rebooting';
}
exit(0);
}
usage(1) unless $buildroot && $statedir;
usage(1) if ($cachedir && !$cachesize) || ($cachesize && !$cachedir);
# default needs to be in sync with build(1)
# otherwise job killing does not work in a default setup
$vm_root = "${buildroot}.img" unless $vm_root;
$vm_swap = "${buildroot}.swap" unless $vm_swap;
# here's the build code we want to use
$::ENV{'BUILD_DIR'} = "$statedir/build";
if (!$hostarch) {
$hostarch = `uname -m`;
chomp $hostarch;
die("could not determine hostarch\n") unless $hostarch;
}
die("arch $hostarch cannot build anything!\n") unless $BSCando::cando{$hostarch} || ($hostarch eq 'local' && $localkiwi);
$srcserver = $BSConfig::srcserver unless defined $srcserver;
@reposervers = @BSConfig::reposervers unless @reposervers;
if ($justbuild) {
my $buildinfo = readxml($ARGV[0], $BSXML::buildinfo);
if ($localkiwi) {
# make sure this is the right job for us
die("not a kiwi job\n") unless $buildinfo->{'file'} =~ /\.kiwi$/;
die("not a kiwi product job\n") unless $buildinfo->{'imagetype'} && $buildinfo->{'imagetype'}->[0] eq 'product';
}
$| = 1;
dobuild($buildinfo);
exit(0);
}
# collect environment information for the dispatcher. To be checked with job constraints
sub device_size($) {
my ($device) = @_;
if ($device) {
if (-b $device) {
if (open(SIZE, '-|', '/sbin/blockdev', '--getsize64', $device)) {
my $line = <SIZE>;
chomp $line;
my $size = $line / ( 1024 * 1024 );
close SIZE;
return $size;
}
} else {
# implement me for loop device files
}
}
return undef;
}
my $workerenvstate = {};
{
# host labels
if (@hostlabel) {
$workerenvstate->{'hostlabel'} = \@hostlabel if @hostlabel;
}
# sandbox
$workerenvstate->{'sandbox'} = 'chroot';
$workerenvstate->{'sandbox'} = 'xen' if $vm =~ /xen/;
$workerenvstate->{'sandbox'} = 'kvm' if $vm =~ /kvm/;
$workerenvstate->{'sandbox'} = 'emulator' if $vm =~ /emulator/;
$workerenvstate->{'sandbox'} = 'lxc' if $vm =~ /lxc/;
$workerenvstate->{'sandbox'} = 'zvm' if $vm =~ /zvm/;
# build host owner
$workerenvstate->{'owner'} = $owner if $owner;
# linux kernel
my $str = readstr("/proc/version");
if ($str =~ /^Linux version ([^-]*)-([^-]*)-([^ ]*) .*$/s) {
my $linux = {};
$linux->{'version'} = $1."-".$2;
$linux->{'flavor'} = $3;
$workerenvstate->{'linux'} = $linux;
}
my $hw = {};
$hw->{'cpu'} = {};
$hw->{'processors'} = 0;
# memory, undefined in non vm mode since no guarantees are exist
if (open(FILE, "<", "/proc/cpuinfo")) {
while(<FILE>) {
chomp;
if (/^processor/s) {
$hw->{'processors'} = $hw->{'processors'} + 1;
}
elsif (/^flags[\s]*:\s(.*)$/) {
my @cpuflags = split(' ', $1);
$hw->{'cpu'}->{'flag'} = \@cpuflags;
}
}
close FILE;
}
$hw->{'memory'} = $vm_memory if $vm_memory;
$hw->{'swap'} = $vmdisk_swapsize if $vmdisk_swapsize;
$hw->{'disk'} = $vmdisk_rootsize if $vmdisk_rootsize;
if ($vm_root) {
my $size = device_size($vm_root);
$hw->{'disk'} = $size if $size;
}
if ($vm_swap) {
my $size = device_size($vm_swap);
$hw->{'swap'} = $size if $size;
}
$workerenvstate->{'hardware'} = $hw;
};
sub stream_logfile {
my ($nostream, $start, $end) = @_;
open(F, "<$buildroot/.build.log") || die("$buildroot/.build.log: $!\n");
my @s = stat(F);
$start ||= 0;
if (defined($end)) {
$end -= $start;
die("end is smaller than start\n") if $end < 0;
}
die("Logfile is not that big\n") if $s[7] < abs($start);
defined(sysseek(F, $start, $start < 0 ? Fcntl::SEEK_END : Fcntl::SEEK_SET)) || die("sysseek: $!\n");
BSServer::reply(undef, 'Content-Type: text/plain', 'Transfer-Encoding: chunked');
my $pos = sysseek(F, 0, Fcntl::SEEK_CUR) || die("sysseek: $!\n");
while(!defined($end) || $end) {
@s = stat(F);
if ($s[7] <= $pos) {
last if !$s[3];
select(undef, undef, undef, .5);
next;
}
my $data = '';
my $l = $s[7] - $pos;
$l = 4096 if $l > 4096;
sysread(F, $data, $l);
next unless length($data);
$data = substr($data, 0, $end) if defined($end) && length($data) > $end;
$pos += length($data);
$end -= length($data) if defined $end;
$data = sprintf("%X\r\n", length($data)).$data."\r\n";
BSServer::swrite($data);
last if $nostream && $pos >= $s[7];
}
close F;
BSServer::swrite("0\r\n\r\n");
}
sub send_state {
my ($state, $p, $ba, $exclude) = @_;
my @args = ("state=$state", "arch=$ba", "port=$p");
push @args, "workerid=$workerid" if defined $workerid;
for my $server (@reposervers) {
next if $exclude && $server eq $exclude;
if ($state eq 'idle' && @reposervers > 1) {
my $curstate = readxml("$statedir/state", $BSXML::workerstate, 1);
last if $curstate && $curstate->{'state'} ne $state;
}
my $param = {
'uri' => "$server/worker",
'timeout' => 3,
};
if ($state eq 'idle') {
$param->{'request'} = 'POST';
$param->{'data'} = BSUtil::toxml($workerenvstate, $BSXML::worker);
}
eval {
BSRPC::rpc($param, undef, @args);
};
print "send_state $server: $@" if $@;
}
}
sub send_dispatched {
my ($server, $p, $ba, $buildinfo) = @_;
my @args = ("hostarch=$ba", "port=$p");
push @args, "workerid=$workerid" if defined $workerid;
push @args, "job=$buildinfo->{'job'}";
push @args, "arch=$buildinfo->{'arch'}";
push @args, "jobid=$buildinfo->{'jobid'}";
my $param = {
'uri' => "$server/workerdispatched",
'request' => 'POST',
'timeout' => 300,
};
BSRPC::rpc($param, undef, @args);
}
sub codemd5 {
my ($dir) = @_;
my @files = grep {!/^\./} ls($dir);
my @bfiles = map {"Build/$_"} grep {!/^\./} ls("$dir/Build");
my @efiles = map {"emulator/$_"} grep {!/^\./} ls("$dir/emulator");
@files = sort(@files, @bfiles, @efiles);
my $md5 = '';
for my $file (@files) {
next if -l "$dir/$file" || -d _;
$md5 .= Digest::MD5::md5_hex(readstr("$dir/$file"))." $file\n";
}
$md5 = Digest::MD5::md5_hex($md5);
return $md5;
}
sub getcode {
my ($dir, $uri, $ineval) = @_;
# evalize ourself
if (!$ineval) {
my $md5;
eval {
$md5 = getcode($dir, $uri, 1);
};
if ($@) {
warn($@);
return '';
}
return $md5;
}
my $ndir = "$dir.new";
my $odir = "$dir.old";
# clean up stale runs
rm_rf($ndir) if -e $ndir;
rm_rf($odir) if -e $odir;
mkdir($ndir) || die("mkdir $ndir: $!\n");
my $res = BSRPC::rpc({
'uri' => $uri,
'directory' => $ndir,
'timeout' => $gettimeout,
'withmd5' => 1,
'createsubdirs' => 1,
'receiver' => \&BSHTTP::cpio_receiver,
});
die("getcode error\n") unless $res;
# got everything, clean things up, check if it really works
if ($dir eq 'worker') {
symlink('.', "$ndir/XML") || die("symlink: $!\n");
chmod(0755, "$ndir/bs_worker");
die("bs_worker selftest failed\n") if system("cd $ndir && ./bs_worker --selftest");
} elsif ($dir eq 'build') {
symlink('.', "$ndir/Date") || die("symlink: $!\n");
symlink('.', "$ndir/Time") || die("symlink: $!\n");
# we just change every file to be on the safe side
chmod(0755, "$ndir/$_->{'name'}") for @$res;
}
# ok, commit
if (-e $dir) {
rename($dir, $odir) || die("rename $dir $odir: $!\n");
}
rename($ndir, $dir) || die("rename $ndir $dir: $!\n");
rm_rf($odir) if -e $odir;
my $md5 = '';
for my $file (sort {$a->{'name'} cmp $b->{'name'}} @$res) {
$md5 .= "$file->{'md5'} $file->{'name'}\n" if $file->{'md5'};
}
$md5 = Digest::MD5::md5_hex($md5);
return $md5;
}
sub rm_rf {
my ($dir) = @_;
BSUtil::cleandir($dir);
rmdir($dir);
die("rmdir $dir failed\n") if -d $dir;
}
sub importbuild {
return if defined &Build::queryhdrmd5;
unshift @INC, "$statedir/build";
require Build;
Build->import();
}
sub getsources {
my ($buildinfo, $dir) = @_;
my @meta;
push @meta, ($buildinfo->{'verifymd5'} || $buildinfo->{'srcmd5'})." $buildinfo->{'package'}";
my $server = $buildinfo->{'srcserver'} || $srcserver;
my $res = BSRPC::rpc({
'uri' => "$server/getsources",
'directory' => $dir,
'timeout' => $gettimeout,
'withmd5' => 1,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "project=$buildinfo->{'project'}", "package=$buildinfo->{'package'}", "srcmd5=$buildinfo->{'srcmd5'}");
die("Error\n") unless ref($res) eq 'ARRAY';
if (-e "$dir/.errors") {
my $errors = readstr("$dir/.errors", 1);
die("getsources: $errors");
}
# verify sources
my %res = map {$_->{'name'} => $_} @$res;
my $md5 = '';
my @f = ls($dir);
for my $f (sort @f) {
die("unexpected file: $f") unless $res{$f};
$md5 .= "$res{$f}->{'md5'} $f\n";
}
$md5 = Digest::MD5::md5_hex($md5);
die("source verification fails: $md5 != $buildinfo->{'verifymd5'}\n") if $md5 ne $buildinfo->{'verifymd5'};
return @meta unless $buildinfo->{'file'} =~ /\.kiwi$/;
# get additional kiwi sources
my @sdep = grep {($_->{'repoarch'} || '') eq 'src'} @{$buildinfo->{'bdep'} || []};
for my $src (@sdep) {
print "$src->{'name'}, ";
my $idir = "$src->{'project'}/$src->{'package'}";
$idir = "$dir/images/$idir";
mkdir_p($idir);
my $res = BSRPC::rpc({
'uri' => "$server/getsources",
'directory' => $idir,
'timeout' => $gettimeout,
'withmd5' => 1,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "project=$src->{'project'}", "package=$src->{'package'}", "srcmd5=$src->{'srcmd5'}");
die("Error\n") unless ref($res) eq 'ARRAY';
if (-e "$idir/.errors") {
my $errors = readstr("$idir/.errors", 1);
die("getsources: $errors");
}
push @meta, "$src->{'srcmd5'} $src->{'project'}/$src->{'package'}";
}
return @meta;
}
sub getdeltasources {
my ($buildinfo, $dir) = @_;
my @meta;
push @meta, ($buildinfo->{'verifymd5'} || $buildinfo->{'srcmd5'})." $buildinfo->{'package'}";
my $server = $buildinfo->{'reposerver'};
my $res = BSRPC::rpc({
'uri' => "$server/getjobdata",
'directory' => $dir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "job=$buildinfo->{'job'}", "arch=$buildinfo->{'arch'}", "jobid=$buildinfo->{'jobid'}");
die("Error\n") unless ref($res) eq 'ARRAY';
return @meta;
}
sub getfollowupsources {
my ($buildinfo, $dir) = @_;
my $server = $buildinfo->{'reposerver'};
my $res = BSRPC::rpc({
'uri' => "$server/getjobdata",
'directory' => $dir,
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef, "job=$buildinfo->{'job'}", "arch=$buildinfo->{'arch'}", "jobid=$buildinfo->{'jobid'}");
die("Error\n") unless ref($res) eq 'ARRAY';
die("no old sources in followup job\n") unless @$res;
# need to get buildenv from old sources ;(
$server = $buildinfo->{'srcserver'} || $srcserver;
$res = BSRPC::rpc({
'uri' => "$server/source/$buildinfo->{'project'}/$buildinfo->{'package'}",
'timeout' => $gettimeout,
}, $BSXML::dir, "rev=$buildinfo->{'srcmd5'}");
my %entries = map {$_->{'name'} => $_} @{$res->{'entry'} || []};
my $bifile = "_buildenv.$buildinfo->{'repository'}.$buildinfo->{'arch'}";
$bifile = '_buildenv' unless $entries{$bifile};
return unless $entries{$bifile};
BSRPC::rpc({
'uri' => "$server/source/$buildinfo->{'project'}/$buildinfo->{'package'}/$bifile",
'timeout' => $gettimeout,
'receiver' => \&BSHTTP::file_receiver,
'filename' => "$dir/_buildenv.$buildinfo->{'repository'}.$buildinfo->{'arch'}",
}, undef, "rev=$buildinfo->{'srcmd5'}");
}
sub getsslcert {
my ($buildinfo, $dir) = @_;
my $server = $buildinfo->{'srcserver'} || $srcserver;
my $cert;
eval {
$cert = BSRPC::rpc({
'uri' => "$server/getsslcert",
'timeout' => $gettimeout,
}, undef, "project=$buildinfo->{'project'}", "autoextend=1");
};
die("could not retrieve ssl certificate: $@\n") if $@;
writestr("$dir/_projectcert.crt", undef, $cert) if $cert;
}
sub qsystem {
my (@args) = @_;
my $pid;
if (!($pid = xfork())) {
$SIG{'PIPE'} = 'DEFAULT';
open(STDOUT, ">/dev/null") if $silent;
exec(@args);
die("$args[0]: $!\n");
}
waitpid($pid, 0) == $pid || die("waitpid $pid: $!\n");
return $?;
}
sub link_or_copy {
my ($from, $to) = @_;
unlink($to);
return 1 if link($from, $to);
local *F;
local *G;
return undef unless open(F, '<', $from);
if (!open(G, '>', $to)) {
close F;
return undef;
}
my $buf;
while (sysread(F, $buf, 8192)) {
(syswrite(G, $buf) || 0) == length($buf) || die("$to write: $!\n");
}
close(F);
if (!close(G)) {
unlink($to);
return undef;
}
return 1;
}
sub manage_cache {
my ($prunesize, $cacheold, $cachenew) = @_;
# get the lock
local *F;
BSUtil::lockopen(\*F, '+>>', "$cachedir/content", 1) || return;
my $content;
if (-s F) {