-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbclient.pl
717 lines (601 loc) · 17.5 KB
/
rbclient.pl
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
#!/usr/bin/perl -swW
#
# This is the client-side implementation of Rockbuild.
#
# http://rockbuild.haxx.se
#
# Copyright (C) 2010-2012 Björn Stenberg <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
use strict;
use IO::Socket;
use IO::Select;
use IO::File;
use IO::Pipe;
use File::Basename;
use File::Path;
use POSIX 'nice';
use POSIX 'strftime';
use POSIX ":sys_wait_h";
my $perlfile = "rbclient.pl";
my $revision = 52;
my $cwd = `pwd`;
chomp $cwd;
sub tprint {
my $line = strftime("%F %T ", localtime()) . $_[0];
print $line;
if (open LOG, ">>$cwd/rbclient.log") {
print LOG $line;
close LOG;
}
}
# read -parameters
our $username = $username;
our $password = $password;
our $clientname = $clientname;
our $archlist = $archlist;
our $buildmaster = $buildmaster;
our $port = $port || 19999;
our $ulspeed = $ulspeed || 0;
our $commandhook = $commandhook || '';
my $upload_url = "http://$buildmaster/upload.cgi";
my ($probecores) = &probecores;
our $cores = $cores || $probecores;
my $cpu = `uname -m`;
chomp $cpu;
my $os = `uname -s`;
chomp $os;
our $bits;
if ($cpu eq "i686" or $cpu eq "i386" or $cpu eq "armv5tel") {
$bits = 32;
}
elsif ($cpu eq "x86_64") {
$bits = 64;
}
else {
printf("Unrecognised cpu $cpu - please fix rbclient.pl to know of this\n");
exit 22;
}
our $config;
&readconfig($config) if ($config);
unless ($username and $password and $archlist and $clientname) {
print <<MOO
Insufficient parameters. You must specify:
-username=[user]
This is your user name given to you by the server admins
-password=[password]
The secret password given to you by the server admins
-clientname=[client name]
The unique name of this particular instances of your build clients. After
all, you at least run one on your desktop, one on your laptop and one in
your toaster. Right?
-archlist=[list,of,archs]
May include arm,m68k,sh,sdl,mipsel and should be a comma-separated list with
no spaces
optional setting:
-cores=[num]
Override rbclient\'s probed results
-buildmaster=[host]
Connect to this given server instead of the default.
-ulspeed=[speed]
Limit upload speed to max [speed] kilobytes per second.
-commandhook=[script]
Run this script whenever a command comes in, with the command itself as the
first argument, and the command parameters in the second argument. This can
be used for fancy monitoring of the client.
You can also specify -config=file where parameters are stored as 'label: value'
MOO
;
exit 22;
}
&testsystem();
# no localized messages, please
$ENV{LC_ALL} = 'C';
beginning:
tprint "Starting client $clientname, revision $revision, cores $cores\n";
my $sock;
while (1) {
$sock = IO::Socket::INET->new(PeerAddr => $buildmaster,
PeerPort => $port,
Proto => 'tcp')
or sleep 1;
last if ($sock and $sock->connected);
}
our %conntype;
$sock->blocking(0);
# Add the master socket to select mask
my $read_set = new IO::Select();
$read_set->add($sock);
$conntype{$sock->fileno} = 'socket';
my $auth = "$username:$password";
tprint "HELLO $revision $archlist $auth $clientname $cpu $bits $os\n";
print $sock "HELLO $revision $archlist $auth $clientname $cpu $bits $os\n";
my $busy = 0;
my %builds = ();
my $buildnum = 0;
my $lastcomm = time();
my $input;
$SIG{INT} = sub {
warn "received interrupt.\n";
for my $id (keys %builds) {
if ($builds{$id}{pid}) {
&killchild($id);
}
}
exit -1;
};
while (1) {
my ($rh_set, $timeleft) =
IO::Select->select($read_set, undef, undef, 1);
foreach my $rh (@$rh_set) {
if ($conntype{$rh->fileno} eq "socket") {
#print "Got from socket\n";
$lastcomm = time();
my $data = <$rh>;
if (length $data) {
$input .= $data;
while (1) {
my $pos = index($input, "\n");
last if ($pos == -1);
my $line = substr($input, 0, $pos);
&parsecmd($line);
$input = substr($input, $pos+1);
}
}
else {
# socket dropped. stop all builds and restart
tprint "Server socket disconnected! Cleanup and restart.\n";
for my $id (keys %builds) {
if ($builds{$id}{pid}) {
&killchild($id);
}
}
goto beginning;
}
}
elsif ($conntype{$rh->fileno} eq "pipe") {
#print "Got from pipe\n";
my $data = <$rh>;
if (length $data) {
if ($data =~ /uploading (.*?) (\d+)/) {
# client has started uploading
my ($id, $pid) = ($1, $2);
tprint "child $id ($pid) is uploading\n";
print $sock "UPLOADING $id\n";
# we're no longer busy
$busy -= $builds{$id}{cores};
$builds{$id}{cores} = 0;
print $sock "GIMMEMORE\n";
}
elsif ($data =~ /done (.*?) (\d+) (\d+) (\d+) (.*)/) {
my ($id, $pid, $ultime, $ulsize, $status) = ($1, $2, $3, $4, $5);
waitpid $pid, 0;
$read_set->remove($rh);
delete $conntype{$rh->fileno};
close $rh;
my $dir = "$cwd/build-$id";
if (-d $dir) {
rmtree $dir;
}
my $timespent = time() - $builds{$id}{started};
if ($status eq "ok") {
tprint "Completed build $id\n";
print $sock "COMPLETED $id $timespent $ultime $ulsize\n";
}
else {
tprint "Failed build $id: Status $status\n";
print $sock "COMPLETED $id $timespent $ultime $ulsize\n";
}
delete $builds{$id};
}
}
else {
tprint sprintf "Child %d died unexpectedly!\n", $rh->fileno;
exit;
}
}
else {
die "Got from other (%d)\n", $rh->fileno;
}
}
if ($lastcomm + 60 < time()) {
tprint "Server connection stalled. Exiting in 5s...\n";
close $sock;
sleep 5;
exit;
}
}
#################################################
sub startbuild
{
my ($id) = @_;
tprint "Starting build $id\n";
# make mother/child pipe
my $pipe = new IO::Pipe();
$builds{$id}{pipe} = $pipe;
if (-d ".svn") {
# check svn
my $buf = `svnversion`;
my $rev;
if ($buf =~ /(\d\w+)/) {
$rev = $1;
if ($rev =~ /M/) {
tprint "*** Your source tree is modified! Clean it up and restart.\n";
exit 22;
}
}
if ($rev != $builds{$id}{rev}) {
# (using system() to make stderr messages appear on client console)
system("svn up -q -r $builds{$id}{rev}");
}
}
elsif (-d ".git") {
# check git
my $mod = `git status --porcelain --untracked-files=no`;
if ($mod =~ / M /) {
tprint "Your source tree is modified! Clean it up and restart.\n";
exit 22;
}
# (using system() to make stderr messages appear on client console)
system("git remote update");
system("git checkout --quiet --force $builds{$id}{rev}");
if ($?) { # abort if git failed
tprint "*** git error!\n";
return;
}
}
# start timer
$builds{$id}{started} = time();
my $pid = fork;
if ($pid) {
# mother
$builds{$id}{pid} = $pid;
$pipe->reader();
$read_set->add($pipe);
$conntype{$pipe->fileno} = 'pipe';
$busy += $builds{$id}{cores};
}
else {
# child
setpgrp;
$pipe->writer();
$pipe->autoflush();
nice 19; # go to background priority
my $starttime = time();
# It is important that we name the uploaded files
# [client]-[user]-[build].log/zip as otherwise the server won't
# find/use it
my $base="$clientname-$username-$id";
if (-d "build-$id") {
rmtree "build-$id";
}
mkdir "build-$id";
chdir "build-$id";
my $logfile = "$base.log";
my $log = ">> $logfile 2>&1";
my $cmdline = $builds{$id}{cmdline};
if (not open DEST, ">$logfile") {
tprint "Failed creating log file $logfile: $!\n";
exit;
}
# to keep all other scripts working, use the same output as buildall.pl:
print DEST "Build Start Single\n";
printf DEST "Build Date: %s\n", strftime("%Y%m%dT%H%M%SZ", gmtime);
print DEST "Build Type: $id\n";
print DEST "Build Dir: $cwd/build-$id\n";
print DEST "Build Client: $clientname-$username\n";
print DEST "Build Command: $cmdline\n";
close DEST;
if ($cmdline) {
if ($builds{$id}{cores} > 1) {
my $c = $cores + 1;
$ENV{MAKEFLAGS} = "-j$c";
}
else {
$ENV{MAKEFLAGS} = "-j1";
}
tprint "($cmdline) $log\n";
`($cmdline) $log`;
}
# report
open DEST, ">>$logfile";
if (-f $builds{$id}{result}) {
print DEST "Build Status: Fine\n";
}
else {
print DEST "Build Failure: No '$builds{$id}{result}' was produced.\n";
print DEST "Build Status: Failed\n";
}
my $tooktime = time() - $starttime;
print DEST "Build Time: $tooktime\n";
close DEST;
print $pipe "uploading $id $$\n";
&upload($logfile);
tprint "No result file $builds{$id}{result}\n"
if (not -f $builds{$id}{result});
# create upload file
my ($ultime, $ulsize) = (0,0);
if ($builds{$id}{upload})
{
my $newname = "$base-$builds{$id}{result}";
if (rename $builds{$id}{result}, $newname) {
my $ulstart = time();
&upload($newname);
$ultime = time() - $ulstart;
$ulsize = (stat($newname))[7];
}
}
else {
tprint "No upload\n";
}
tprint "child: $id ($$) done\n";
print $pipe "done $id $$ $ultime $ulsize ok\n";
close $pipe;
exit;
}
}
sub upload
{
my ($file) = @_;
tprint "Uploading $file...\n";
if (not -f $file) {
tprint "$file: no such file\n";
return;
}
my $limit = "";
if ($ulspeed) {
$limit = "--limit-rate ${ulspeed}k";
}
tprint "curl $limit -s -F upfile=\@$file $upload_url\n";
`curl $limit -s -F upfile=\@$file $upload_url`;
}
sub probecores
{
open CPUINFO, "</proc/cpuinfo" or return 0;
my @cores = grep /^processor/i, <CPUINFO>;
close CPUINFO;
return (scalar @cores);
}
sub _HELLO
{
if ($_[0] ne "ok") {
tprint "HELLO failed: @_\n";
close $sock;
sleep 10;
exit;
}
}
sub _COMPLETED
{
}
sub _UPLOADING
{
}
sub _GIMMEMORE
{
}
sub PING
{
my ($arg) = @_;
print $sock "_PING $arg\n";
}
sub CANCEL
{
my ($id) = @_;
&killchild($id);
print $sock "_CANCEL\n";
if ($busy < $cores) {
print $sock "GIMMEMORE\n";
}
}
sub BUILD
{
my ($buildparams) = @_;
# ipodcolorboot:29961:mt:bootloader-ipodcolor.ipod:0:../tools/configure --target=ipodcolor --type=b && make
my ($id, $rev, $mt, $result, $upload, $cmdline) =
split(':', $buildparams);
tprint "Got BUILD $buildparams\n";
if (defined $builds{$id}) {
print $sock "_BUILD 0\n";
return;
}
$builds{$id}{rev} = $rev;
$builds{$id}{cores} = $mt eq "mt" ? $cores : 1;
$builds{$id}{result} = $result;
$builds{$id}{upload} = $upload;
$builds{$id}{cmdline} = $cmdline;
$builds{$id}{seqnum} = $buildnum++;
print $sock "_BUILD $id\n";
&startbuild($id);
}
sub UPDATE
{
my ($url) = @_;
tprint "Update from $url\n";
`curl -o $perlfile.new "$url"`;
# This might fail, but runclient.sh will save us
rename("$perlfile.new", $perlfile);
print $sock "_UPDATE\n";
close $sock;
sleep 1;
exit;
}
sub MESSAGE
{
tprint "Server message: @_\n";
print $sock "_MESSAGE\n";
}
sub SYSTEM
{
my ($cmd) = @_;
tprint "Server system command: $cmd\n";
system($cmd);
}
sub parsecmd
{
no strict 'refs';
my ($cmdstr)=@_;
my %functions = ('_HELLO', 1,
'_COMPLETED', 1,
'_UPLOADING', 1,
'_GIMMEMORE', 1,
'BUILD', 1,
'PING', 1,
'UPDATE', 1,
'CANCEL', 1,
'MESSAGE', 1,
'SYSTEM', 1);
if($cmdstr =~ /^([_A-Z]*) *(.*)/) {
my $func = $1;
my $rest = $2;
chomp $rest;
#tprint "$func $rest\n";
if (defined $functions{$func}) {
&$func($rest);
}
else {
tprint "Unknown command '$func'\n";
}
if($commandhook ne '') {
system(sprintf("%s '%s' '%s'",$commandhook,$func,$rest));
}
}
else {
tprint "Unrecognized command '$cmdstr'\n";
}
}
sub testsystem
{
# this is still rockbox specific. change this to suit your project.
# check compilers
my %compilers = (
"arm", ["arm-elf-gcc --version", "4.0.3"],
"arm-eabi-gcc444", ["arm-elf-eabi-gcc --version", "4.4.4"],
"sh", ["sh-elf-gcc --version", "4.0.3"],
"m68k", ["m68k-elf-gcc --version", "3.4.6"],
"m68k-gcc452", ["m68k-elf-gcc --version", "4.5.2"],
"mipsel", ["mipsel-elf-gcc --version", "4.1.2"],
"sdl", ["sdl-config --version", ".*"],
"android15", ["android list target", "API level: 15"],
"latex", ["pdflatex --version", "pdfTeX 3.1415926"],
);
for (split ',', $archlist) {
my $p = `$compilers{$_}[0]`;
if (not $p =~ /$compilers{$_}[1]/) {
tprint "Error: You specified arch $_ but the output of '$compilers{$_}[0]' did not include '$compilers{$_}[1]'.\n";
exit 22;
}
}
if (0) {
# rockbox svn-to-git client transition, preserved here
# as an example
if (not -d ".git") {
`curl -o svn2git.sh http://www.rockbox.org/buildserver/svn2git.sh`;
`sh svn2git.sh`;
}
if (not -d ".git") {
tprint "git transition failed.\n";
exit 22;
}
}
# check curl
my $p = `which curl`;
if (not $p =~ m|^/|) {
tprint "I couldn't find 'curl' in your path.\n";
exit 22;
}
# check zip
$p = `which zip`;
if (not $p =~ m|^/|) {
tprint "I couldn't find 'zip' in your path.\n";
exit 22;
}
# check perlfile
if (not -w $perlfile) {
tprint "$perlfile must be located in the current directory, and writable by current\nuser, to allow automatic updates.";
exit 22;
}
# check an upgrade file
if (-e "$perlfile.new") {
tprint "An upgrade didn't complete. Rename $perlfile.new to $perlfile\n";
exit 22;
}
# check that source tree is unmodified
my $modified = 0;
if (-d ".svn") {
my $rev = `svnversion`;
if ($rev =~ /M/) {
$modified = 1;
}
}
elsif (-d ".git") {
my $mod = `git status --porcelain --untracked-files=no`;
if ($mod =~ / M /) {
$modified = 1;
}
}
if ($modified) {
tprint "Your source tree is modified! Clean it up and restart.\n";
exit 22;
}
# update to latest version
if (-d ".svn") {
system("svn up");
}
elsif (-d ".git") {
system("git fetch");
}
}
sub readconfig
{
my ($file) = @_;
if (!open CFG, "<$file") {
print "$file: $!\n";
return;
}
for (<CFG>) {
if (/^username:\s*(.*)/) {
$username = $1;
}
elsif (/^password:\s*(.*)/) {
$password = $1;
}
elsif (/^clientname:\s*(.*)/) {
$clientname = $1;
}
elsif (/^archlist:\s*(.*)/) {
$archlist = $1;
}
elsif (/^cores:\s*(.*)/) {
$cores = $1;
}
}
close CFG;
}
sub killchild
{
my ($id) = @_;
return if (not defined $builds{$id});
my $pipe = $builds{$id}{pipe};
$read_set->remove($pipe);
$busy -= $builds{$id}{cores};
my $pid = $builds{$id}{pid};
if ($pid) {
kill -15, $pid;
tprint "Killed build $id\n";
waitpid $pid, 0;
}
my $dir = "$cwd/build-$id";
if (-d $dir) {
tprint "Removing $dir\n";
rmtree $dir;
}
delete $builds{$id};
}