source: ProjectBuilder/devel/pb/bin/pb@ 239

Last change on this file since 239 was 239, checked in by Bruno Cornec, 17 years ago

Attempt to fix port issues with VM
Some debian build fixes

  • Property svn:executable set to *
File size: 29.9 KB
Line 
1#!/usr/bin/perl -w
2#
3# Project Builder main application
4#
5# $Id$
6#
7# Copyright B. Cornec 2007
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13use Getopt::Std;
14use Data::Dumper;
15use English;
16use AppConfig qw(:argcount :expand);
17use File::Basename;
18use File::Copy;
19use Time::localtime qw(localtime);
20use POSIX qw(strftime);
21
22# Global variables
23use lib qw (lib);
24use ProjectBuilder::Distribution qw (pb_distro_init);
25use ProjectBuilder::Version qw (pb_version_init);
26use ProjectBuilder::Base qw (pb_conf_read pb_conf_get pb_cms_init pb_mkdir_p pb_system pb_rm_rf pb_get_filters pb_filter_file pb_filter_file_pb pb_cms_export pb_cms_log pb_cms_isdiff pb_cms_copy pb_cms_checkout);
27
28my %opts; # CLI Options
29my $action; # action to realize
30my $test = "FALSE";
31my $option = "";
32my @pkgs;
33my $pbtag; # Global Tag variable
34my $pbver; # Global Version variable
35my $pbscript; # Name of the script
36my %pbver; # per package
37my %pbtag; # per package
38my $pbrev; # Global REVISION variable
39my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
40my $pbdate = strftime("%Y-%m-%d", @date);
41my $pbdatecvs = strftime("%Y-%m-%d %H:%M:%S", @date);
42my $debug = 0;
43my $pbaccount; # Login to use to connect to the VM
44my $pbport; # Port to use to connect to the VM
45my $newver; # New version to create
46my $LOG = \*STDOUT;
47
48getopts('a:hl:m:P:p:qr:s:tvV:',\%opts);
49
50my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
51if (defined $opts{'h'}) {
52 pb_syntax();
53 exit(0);
54}
55if (defined $opts{'v'}) {
56 $debug++;
57}
58if (defined $opts{'q'}) {
59 $debug=-1;
60}
61if (defined $opts{'l'}) {
62 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
63 $LOG = *LOG;
64 $debug = 0 if ($debug == -1);
65 }
66# Handles test option
67if (defined $opts{'t'}) {
68 $test = "TRUE";
69 # Works only for SVN
70 $option = "-r BASE";
71}
72
73# Handle root of the project if defined
74if (defined $opts{'r'}) {
75 $ENV{'PBROOT'} = $opts{'r'};
76}
77# Handle virtual machines if any
78if (defined $opts{'m'}) {
79 $ENV{'PBVM'} = $opts{'m'};
80}
81if (defined $opts{'s'}) {
82 $pbscript = $opts{'s'};
83}
84if (defined $opts{'a'}) {
85 $pbaccount = $opts{'a'};
86}
87if (defined $opts{'P'}) {
88 $pbport = $opts{'P'};
89}
90if (defined $opts{'V'}) {
91 $newver = $opts{'V'};
92}
93
94# Get Action
95$action = shift @ARGV;
96die pb_syntax() if (not defined $action);
97
98my ($pbrc, $filteredfiles, $defpkgdir, $extpkgdir);
99
100# Handles project name if any
101# And get global params
102if (defined $opts{'p'}) {
103 ($ENV{'PBPROJ'},$debug,$LOG, $pbrc, $filteredfiles, $defpkgdir, $extpkgdir)
104 = pb_env_init($opts{'p'});
105} else {
106 ($ENV{'PBPROJ'},$debug,$LOG, $pbrc, $filteredfiles, $defpkgdir, $extpkgdir)
107 = pb_env_init();
108}
109
110print $LOG "Project: $ENV{'PBPROJ'}\n" if ($debug >= 0);
111print $LOG "Action: $action\n" if ($debug >= 0);
112
113# Keep those project values to store them at the end each time
114my $pbprojtag = $ENV{'PBTAG'};
115my $pbprojver = $ENV{'PBVER'};
116
117# Act depending on action
118if ($action =~ /^cms2build$/) {
119 pb_cms2build();
120} elsif ($action =~ /^build2pkg$/) {
121 pb_build2pkg();
122} elsif ($action =~ /^cms2pkg$/) {
123 pb_cms2build();
124 pb_build2pkg();
125} elsif ($action =~ /^build2ssh$/) {
126 pb_build2ssh();
127} elsif ($action =~ /^cms2ssh$/) {
128 pb_cms2build();
129 pb_build2ssh();
130} elsif ($action =~ /^pkg2ssh$/) {
131 pb_pkg2ssh();
132} elsif ($action =~ /^build2vm$/) {
133 pb_build2vm();
134} elsif ($action =~ /^cms2vm$/) {
135 pb_cms2build();
136 pb_build2vm();
137} elsif ($action =~ /^launchvm$/) {
138 pb_launchvm($ENV{'PBVM'});
139} elsif ($action =~ /^script2vm$/) {
140 pb_script2vm($pbscript);
141} elsif ($action =~ /^newver$/) {
142 pb_newver();
143} elsif ($action =~ /^clean$/) {
144} else {
145 print $LOG "'$action' is not available\n";
146 pb_syntax();
147}
148
149sub pb_cms2build {
150
151 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
152 @pkgs = @$ptr;
153 my $cms=pb_cms_init($ENV{'PBPROJ'});
154
155 my ($pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb","pkgver","pkgtag");
156
157 # declare packager for filtering
158 my ($tmp) = pb_conf_get("packager");
159 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
160
161 foreach my $pbpkg (@pkgs) {
162 $ENV{'PBPKG'} = $pbpkg;
163 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
164 $pbver = $pkgv->{$pbpkg};
165 $ENV{'PBVER'} = $pbver;
166 } else {
167 $pbver = $ENV{'PBVER'};
168 }
169 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
170 $pbtag = $pkgt->{$pbpkg};
171 $ENV{'PBTAG'} = $pbtag;
172 } else {
173 $pbtag = $ENV{'PBTAG'};
174 }
175
176 $pbrev = $ENV{'PBREVISION'};
177 print $LOG "\n";
178 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n";
179 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
180 # Clean up dest if necessary. The export will recreate it
181 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
182 pb_rm_rf($dest) if (-d $dest);
183
184 # Export CMS tree for the concerned package to dest
185 # And generate some additional files
186 $OUTPUT_AUTOFLUSH=1;
187
188 # computes in which dir we have to work
189 my $dir = $defpkgdir->{$pbpkg};
190 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
191 print "def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n" if ($debug >= 1);
192 pb_cms_export($cms,$pbdatecvs,"$ENV{'PBROOT'}/$dir",$dest);
193
194 # Extract cms log history and store it
195 pb_cms_log($cms,"$ENV{'PBROOT'}/$dir","$dest/$ENV{'PBCMSLOGFILE'}");
196
197 my %build;
198
199 my ($ptr) = pb_conf_get("vmlist");
200 foreach my $d (split(/,/,$ptr->{$ENV{'PBPROJ'}})) {
201 my ($name,$ver) = split(/_/,$d);
202 chomp($ver);
203 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
204 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n" if ($debug >= 1);
205 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
206
207 # Filter build files from the less precise up to the most with overloading
208 # Filter all files found, keeping the name, and generating in dest
209
210 # Find all build files first relatively to PBROOT
211 my %bfiles;
212 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
213 $build{"$ddir-$dver"} = "yes";
214 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
215 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
216 foreach my $f (readdir(BDIR)) {
217 next if ($f =~ /^\./);
218 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
219 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
220 }
221 closedir(BDIR);
222 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
223 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
224 foreach my $f (readdir(BDIR)) {
225 next if ($f =~ /^\./);
226 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
227 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
228 }
229 closedir(BDIR);
230 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
231 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
232 foreach my $f (readdir(BDIR)) {
233 next if ($f =~ /^\./);
234 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
235 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
236 }
237 closedir(BDIR);
238 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
239 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
240 foreach my $f (readdir(BDIR)) {
241 next if ($f =~ /^\./);
242 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
243 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
244 }
245 closedir(BDIR);
246 } else {
247 $build{"$ddir-$dver"} = "no";
248 next;
249 }
250 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
251
252 # Get all filters to apply
253 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
254
255 # Apply now all the filters on all the files concerned
256 # destination dir depends on the type of file
257 if (defined $ptr) {
258 foreach my $f (values %bfiles) {
259 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$pbsuf,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$defpkgdir,$extpkgdir,$pbpackager);
260 }
261 }
262 }
263 if ($debug >= 0) {
264 my @found;
265 my @notfound;
266 foreach my $b (keys %build) {
267 push @found,$b if ($build{$b} =~ /yes/);
268 push @notfound,$b if ($build{$b} =~ /no/);
269 }
270 print $LOG "Build files generated for ".join(',',@found)."\n";
271 print $LOG "No Build files found for ".join(',',@notfound)."\n";
272 }
273 # Prepare the dest directory for archive
274 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
275 #pb_system("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit","Executing init script $ENV{'PBCONF'}/$pbpkg/pbinit");
276 print $LOG "Executing init script $ENV{'PBCONF'}/$pbpkg/pbinit\n";
277 system("cd $dest ; $ENV{'PBCONF'}/$pbpkg/pbinit");
278 }
279
280 # Get the generic filter (all.pbf) and
281 # apply those to the non-build files including those
282 # generated by pbinit if applicable
283
284 # Get only all.pbf filter
285 $ptr = pb_get_filters($pbpkg);
286
287 my $liste ="";
288 if (defined $filteredfiles->{$pbpkg}) {
289 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
290 pb_filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f",$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
291 $liste = "$f $liste";
292 }
293 }
294 print $LOG "Files ".$liste."have been filtered\n";
295
296 # Archive dest dir
297 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
298 # Possibility to look at PBSRC to guess more the filename
299 pb_system("tar cfz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
300 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
301
302 # Keep track of what is generated for default
303 open(LAST,"> $pbrc->{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc->{$ENV{'PBPROJ'}}";
304 print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
305 close(LAST);
306
307 # Keep track of per package version
308 if (! -f "$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") {
309 open(PKG,">$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
310 print PKG "# Empty\n";
311 close(PKG);
312 }
313 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
314 $pkg = { } if (not defined $pkg);
315 if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
316 $pkg->{$pbpkg} = "$pbver-$pbtag";
317 }
318
319 print $LOG "DEBUG pkg: ".Dumper($pkg)."\n" if ($debug >= 1);
320 open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
321 foreach my $p (keys %$pkg) {
322 print PKG "pbpkg $p = $pkg->{$p}\n";
323 }
324 close(PKG);
325 }
326}
327
328sub pb_build2pkg {
329
330 # Get list of packages to build
331 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
332 @pkgs = @$ptr;
333
334 # Get the running distro to build on
335 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
336 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
337
338 # Get content saved in cms2build
339 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
340 $pkg = { } if (not defined $pkg);
341
342 # declare packager
343 my ($tmp) = pb_conf_get("packager");
344 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
345
346 chdir "$ENV{'PBBUILDDIR'}";
347 my $made = ""; # pkgs made during build
348 foreach my $pbpkg (@pkgs) {
349 my $vertag = $pkg->{$pbpkg};
350 # get the version of the current package - maybe different
351 ($pbver,$pbtag) = split(/-/,$vertag);
352
353 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
354 # Suse 10.0 forces tar.bz2 usage :-(
355 if (($ddir eq "suse") && ($dver eq "10.0")) {
356 print "SuSE 10.0 needs bz2 type of packages so recompressing...\n";
357 my $newsrc="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.bz2";
358 system "gzip -cd $src | bzip2 -c6 > $newsrc";
359 $src = $newsrc;
360 }
361 print $LOG "Source file: $src\n" if ($debug >= 0);
362
363 print $LOG "Working directory: $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
364 if ($dtype eq "rpm") {
365 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
366 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
367 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$d") || die "Please ensure that you can write into $ENV{'PBBUILDDIR'} to create $d\nchown the $ENV{'PBBUILDDIR'} directory to your uid";
368 }
369 }
370
371 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
372 # We need to first extract the spec file
373 my @specfile;
374 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
375
376 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
377 # set LANGUAGE to check for correct log messages
378 $ENV{'LANGUAGE'}="C";
379 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
380 foreach my $f (@specfile) {
381 if ($f =~ /\.spec$/) {
382 pb_system("rpmbuild --define \"packager $pbpackager\" --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}");
383 last;
384 }
385 }
386 $made="$made RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm";
387 if (-f "/usr/bin/rpmlint") {
388 pb_system("rpmlint $made","Checking validity of rpms with rpmlint");
389 }
390 } elsif ($dtype eq "deb") {
391 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
392 pb_system("tar xpfz $src","Extracting sources");
393
394 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
395 symlink "pbconf/$ddir-$dver","debian" || die "Unable to symlink to pbconf/$ddir-$dver";
396 chmod 0755,"debian/rules";
397 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package");
398 $made="$made $pbpkg"."_*.deb $pbpkg"."_*.dsc $pbpkg"."_*.tar.gz";
399 } elsif ($dtype eq "ebuild") {
400 my @ebuildfile;
401 # For gentoo we need to take pb as subsystem name
402 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/$pbpkg") if (! -d "$ENV{'PBBUILDDIR'}/portage/pb/$pbpkg");
403
404 # We need to first extract the ebuild file
405 @ebuildfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/portage/pb/$pbpkg");
406
407 # Prepare the build env for gentoo
408 my $found = 0;
409 my $pbbd = $ENV{'PBBUILDDIR'};
410 $pbbd =~ s|/|\\/|g;
411 open(MAKE,"/etc/make.conf") || die "Unable to open /etc/make.conf";
412 while (<MAKE>) {
413 $found = 1 if (/$pbbd\/portage/);
414 }
415 close(MAKE);
416 if ($found == 0) {
417 pb_system("sudo \'echo \"$ENV{'PBBUILDDIR'}/portage\" >> /etc/make.conf\'");
418 }
419 $found = 0;
420 open(KEYW,"/etc/portage/package.keywords") || die "Unable to open /etc/portage/package.keywords";
421 while (<KEYW>) {
422 $found = 1 if (/portage\/pb/);
423 }
424 close(KEYW);
425 if ($found == 0) {
426 pb_system("sudo \'echo \"portage/pb\" >> /etc/portage/package.keywords\'");
427 }
428
429 # Build
430 foreach my $f (@ebuildfile) {
431 if ($f =~ /\.ebuild$/) {
432 pb_system("ebuild $f digest ; ebuild $f package");
433 }
434 }
435 print $LOG "ebuild file: ".Dumper(\@ebuildfile)."\n" if ($debug >= 1);
436
437 $made="$made portage/pb/$pbpkg/$pbpkg-$pbver.ebuild";
438 } elsif ($dtype eq "slackware") {
439 $made="$made build-$pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
440 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
441 } else {
442 die "Unknown dtype format $dtype";
443 }
444 }
445 # Keep track of what is generated so that we can get them back from VMs
446 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
447 print KEEP "$made\n";
448 close(KEEP);
449}
450
451sub pb_build2ssh {
452 pb_send2ssh("Sources");
453}
454
455sub pb_pkg2ssh {
456 pb_send2ssh("Packages");
457}
458
459# By default deliver to the the public site hosting the
460# ftp structure (or whatever) or a VM
461sub pb_send2ssh {
462
463 my $cmt = shift;
464 my $vm = shift || undef;
465 my $vmexist = shift || 0; # 0 is FALSE
466 my $vmpid = shift || 0; # 0 is FALSE
467 my $host = shift || "sshhost";
468 my $login = shift || "sshlogin";
469 my $dir = shift || "sshdir";
470 my $port = shift || "sshport";
471 my $tmout = shift || "vmtmout";
472 my $cmd = "";
473
474 # Get list of packages to build
475 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
476 @pkgs = @$ptr;
477
478 # Get the running distro to consider
479 my ($odir,$over) = (undef, undef);
480 if (defined $vm) {
481 ($odir,$over) = split(/_/,$vm);
482 }
483 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
484 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
485
486 # Get content saved in cms2build
487 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
488 $pkg = { } if (not defined $pkg);
489
490 my $src = "";
491 chdir "$ENV{'PBBUILDDIR'}";
492 foreach my $pbpkg (@pkgs) {
493 my $vertag = $pkg->{$pbpkg};
494 # get the version of the current package - maybe different
495 ($pbver,$pbtag) = split(/-/,$vertag);
496
497 if (($cmt eq "Sources") || ($cmt eq "VMs")) {
498 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
499 if ($cmd eq "") {
500 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
501 } else {
502 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
503 }
504 }
505 }
506 if ($cmt eq "VMs") {
507 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
508 } elsif ($cmt eq "Script") {
509 $src="$src $ENV{'PBDESTDIR'}/pbscript";
510 } elsif ($cmt eq "Packages") {
511 # Get package list from file made during build2pkg
512 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
513 $src = <KEEP>;
514 chomp($src);
515 close(KEEP);
516 if ($dtype eq "rpm") {
517 # Also make a pbscript to generate yum/urpmi bases
518 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
519 } elsif ($dtype eq "deb") {
520 # Also make a pbscript to generate apt bases
521 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
522 }
523 }
524 # Remove potential leading spaces (cause pb with basename)
525 $src =~ s/^ *//;
526 my $basesrc = "";
527 foreach my $i (split(/ +/,$src)) {
528 $basesrc .= " ".basename($i);
529 }
530
531 print $LOG "Sources handled ($cmt): $src\n" if ($debug >= 0);
532 my ($sshhost,$sshlogin,$sshdir,$sshport,$vmtmout) = pb_conf_get($host,$login,$dir,$port,$tmout);
533 my $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
534 # Overwrite account value if passed as parameter
535 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
536 my $tdir;
537 my $bdir;
538 if (($cmt eq "Sources") || ($cmt eq "Script")) {
539 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
540 } elsif ($cmt eq "VMs") {
541 $tdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/delivery";
542 $bdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/build";
543 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
544 $bdir =~ s|\$ENV.+\}/||;
545 } elsif ($cmt eq "Packages") {
546 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
547 } else {
548 return;
549 }
550 my $nport = $sshport->{$ENV{'PBPROJ'}};
551 $nport = "$pbport" if (defined $pbport);
552
553 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
554 $tdir =~ s|\$ENV.+\}/||;
555
556 my $tm = $vmtmout->{$ENV{'PBPROJ'}};
557 pb_system("ssh -q -p $nport $mac \"mkdir -p $tdir ; cd $tdir ; echo \'for i in $basesrc; do if [ -f \$i ]; then rm -f \$i; fi; done\ ; $cmd' | bash\"","Preparing $tdir on $mac");
558 pb_system("cd $ENV{'PBBUILDDIR'} ; scp -p -P $nport $src $mac:$tdir 2> /dev/null","$cmt delivery in $tdir on $mac");
559 pb_system("ssh -q -p $nport $mac \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $mac if needed");
560 if ($cmt eq "VMs") {
561 # Get back info on pkg produced, compute their name and get them from the VM
562 pb_system("scp -p -P $nport $mac:$bdir/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $bdir on $mac");
563 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
564 my $src = <KEEP>;
565 chomp($src);
566 close(KEEP);
567 $src =~ s/^ *//;
568 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
569 # Change pgben to make the next send2ssh happy
570 my $made = "";
571 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
572 foreach my $p (split(/ +/,$src)) {
573 my $j = basename($p);
574 pb_system("scp -p -P $nport $mac:\'$bdir/$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $bdir from $mac");
575 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
576 }
577 print KEEP "$made\n";
578 close(KEEP);
579 pb_system("ssh -q -p $nport $mac \"rm -rf $tdir $bdir\"","VM cleanup on $mac");
580 if (! $vmexist) {
581 pb_system("ssh -q -p $nport $mac \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $vm halt (pid $vmpid)");
582 }
583 pb_send2ssh("Packages","$odir"."_"."$over");
584 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
585 }
586}
587
588sub pb_script2vm {
589 my $pbscript=shift;
590
591 # Prepare the script to be executed on the VM
592 # in $ENV{'PBDESTDIR'}/pbscript
593 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
594 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
595 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
596 }
597
598 my ($vm,$all) = pb_get_vm();
599
600 foreach my $v (@$vm) {
601 # Launch the VM
602 my ($vmexist,$vmpid) = pb_launchvm($v);
603
604 # Gather all required files to send them to the VM
605 # and launch the build thourgh pbscript
606 pb_send2ssh("Script","$v",$vmexist,$vmpid,"vmhost","vmlogin","pbrc","vmport","vmtmout");
607
608 }
609}
610
611sub pb_launchvm {
612 my $vm = shift;
613
614 die "No VM defined, unable to launch" if (not defined $vm);
615 # Keep only the first VM in case many were given
616 $vm =~ s/,.*//;
617
618 # Launch the VMs
619 my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout");
620 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
621 if (defined $vmopt->{$ENV{'PBPROJ'}}) {
622 $ENV{'PBVMOPT'} = $vmopt->{$ENV{'PBPROJ'}};
623 } else {
624 $ENV{'PBVMOPT'} = "";
625 }
626 my $nport = $vmport->{$ENV{'PBPROJ'}};
627 $nport = "$pbport" if (defined $pbport);
628
629 my $cmd;
630 my $vmcmd; # has to be used for pb_check_ps
631 my $vmm; # has to be used for pb_check_ps
632 if ($vmtype eq "qemu") {
633 my $arch = `uname -m`;
634 chomp($arch);
635 my $qemucmd32;
636 my $qemucmd64;
637 if ($arch eq "x86_64") {
638 $qemucmd32 = "/usr/bin/qemu-system-i386";
639 $qemucmd64 = "/usr/bin/qemu";
640 } else {
641 $qemucmd32 = "/usr/bin/qemu";
642 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
643 }
644 if ($vm =~ /_64/) {
645 $vmcmd = "$qemucmd64 -no-kqemu";
646 } else {
647 $vmcmd = "$qemucmd32";
648 }
649 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$vm.qemu";
650 if (! -f "$vmm") {
651 die "Unable to find VM $vmm";
652 }
653 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
654 } elsif ($vmtype eq "xen") {
655 } elsif ($vmtype eq "vmware") {
656 } else {
657 die "VM of type $vmtype not supported. Report to the dev team";
658 }
659 my ($tmpcmd,$void) = split(/ +/,$cmd);
660 my $vmexist = pb_check_ps($tmpcmd,$vmm);
661 my $vmpid = 0;
662 if (! $vmexist) {
663 pb_system("$cmd &","Launching the VM $vmm");
664 pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $vm to come up");
665 $vmpid = pb_check_ps($tmpcmd,$vmm);
666 } else {
667 print "Found an existing VM $vmm (pid $vmexist)\n";
668 }
669 return($vmexist,$vmpid);
670}
671
672sub pb_build2vm {
673 # Prepare the script to be executed on the VM
674 # in $ENV{'PBDESTDIR'}/pbscript
675 my ($ntp) = pb_conf_get("vmntp");
676 my $vmntp = $ntp->{$ENV{'PBPROJ'}};
677 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
678 print SCRIPT "#!/bin/bash\n";
679 print SCRIPT "echo ... Execution needed\n";
680 print SCRIPT "# This is in directory delivery\n";
681 print SCRIPT "# Setup the variables required for building\n";
682 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
683 print SCRIPT "# Preparation for pb\n";
684 print SCRIPT "mkdir -p ../pbconf\n";
685 print SCRIPT "mv $ENV{'PBPROJ'}.pb ../pbconf\n";
686 print SCRIPT "mv .pbrc \$HOME\n";
687 print SCRIPT "cd ..\n";
688 # Force new date to be in the future compared to the date of the tar file by removing 1 minute
689 my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
690 $date[1]--;
691 my $upddate = strftime("%m%d%H%M%Y", @date);
692 print SCRIPT "echo Setting up date on $vmntp...\n";
693 print SCRIPT "sudo date $upddate\n";
694 print SCRIPT "export PBROOT=\`pwd\`\n";
695 print SCRIPT "# Build\n";
696 my $p = "";
697 $p = $ARGV[0] if (defined $ARGV[0]);
698 print SCRIPT "echo Building packages on VM...\n";
699 print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
700 close(SCRIPT);
701 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
702
703 my ($vm,$all) = pb_get_vm();
704
705 # Send tar files when we do a global generation
706 pb_build2ssh() if ($all == 1);
707
708 foreach my $v (@$vm) {
709 # Launch the VM
710 my ($vmexist,$vmpid) = pb_launchvm($v);
711
712 # Gather all required files to send them to the VM
713 # and launch the build thourgh pbscript
714 pb_send2ssh("VMs","$v",$vmexist,$vmpid,"vmhost","vmlogin","pbrc","vmport","vmtmout");
715 }
716}
717
718sub pb_newver {
719
720 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
721 my $cms=pb_cms_init($ENV{'PBPROJ'});
722 if ($cms->{$ENV{'PBPROJ'}} ne "svn") {
723 die "Only SVN is supported at the moment";
724 }
725 my $res = pb_cms_isdiff($cms);
726 die "You need to have no differences before creating a new version" if ($res != 0);
727 my $cmsurl = pb_cms_getinfo($cms);
728 my $newurl = dirname($cmsurl)."/$newver";
729 pb_cms_copy($cms,$cmsurl,$newurl);
730 pb_cms_checkout($cms,$newurl,"$ENV{'PBROOT'}/../$newver");
731 my $oldver=basename($cmsurl);
732 open(FILE,"$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb";
733 open(OUT,"> $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new";
734 while(<FILE>) {
735 s/projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
736 print OUT $_;
737 }
738 close(FILE);
739 close(OUT);
740 rename("$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb");
741 pb_cms_checkin($cms,"$ENV{'PBROOT'}/../$newver");
742}
743
744sub pb_get_pkg {
745
746my @pkgs;
747my $defpkgdir = shift;
748my $extpkgdir = shift;
749
750# Get packages list
751if (not defined $ARGV[0]) {
752 @pkgs = keys %$defpkgdir;
753} elsif ($ARGV[0] =~ /^all$/) {
754 @pkgs = keys %$defpkgdir;
755 push(@pkgs, keys %$extpkgdir);
756} else {
757 @pkgs = @ARGV;
758}
759print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
760return(\@pkgs);
761}
762
763#
764# Return the list of VMs we are working on
765# $all is a flag to know if we return all of them
766# or only some (if all we publish also tar files in addition to pkgs
767#
768sub pb_get_vm {
769
770my @vm;
771my $all = 0;
772
773# Get VM list
774if ((not defined $ENV{'PBVM'}) || ($ENV{'PBVM'} =~ /^all$/)) {
775 my ($ptr) = pb_conf_get("vmlist");
776 $ENV{'PBVM'} = $ptr->{$ENV{'PBPROJ'}};
777 $all = 1;
778}
779@vm = split(/,/,$ENV{'PBVM'});
780print $LOG "VMs: ".join(',',@vm)."\n";
781return(\@vm,$all);
782}
783
784# Returns the pid of a running VM command using a specific VM file
785sub pb_check_ps {
786 my $vmcmd = shift;
787 my $vmm = shift;
788 my $vmexist = 0; # FALSE by default
789
790 open(PS, "ps auxhww|") || die "Unable to call ps";
791 while (<PS>) {
792 next if (! /$vmcmd/);
793 next if (! /$vmm/);
794 my ($void1, $void2);
795 ($void1, $vmexist, $void2) = split(/ +/);
796 last;
797 }
798 return($vmexist);
799}
800
801
802sub pb_extract_build_files {
803
804my $src=shift;
805my $dir=shift;
806my $ddir=shift;
807my @files;
808
809if ($src =~ /tar\.gz$/) {
810 pb_system("tar xfpz $src $dir","Extracting build files");
811} elsif ($src =~ /tar\.bz2$/) {
812 pb_system("tar xfpj $src $dir","Extracting build files");
813} else {
814 die "Unknown compression algorithm for $src";
815}
816opendir(DIR,"$dir") || die "Unable to open directory $dir";
817foreach my $f (readdir(DIR)) {
818 next if ($f =~ /^\./);
819 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
820 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
821 push @files,"$ddir/$f";
822}
823closedir(DIR);
824# Not enough but still a first cleanup
825pb_rm_rf("$dir");
826return(@files);
827}
828
829sub pb_syntax {
830
831 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
832 print "\n";
833 print "Syntax: pb [-vhqt][-r pbroot][-p project][[-s script -a account -P port] -m \"mach-1[,...]\"] <action> [<pkg1>...]\n";
834 print "\n";
835 print "-h : This help file\n";
836 print "-q : Quiet mode\n";
837 print "-t : Test mode (not done yet)\n";
838 print "-v : Verbose mode\n";
839 print "\n";
840 print "-m machine : Name of the Virtual Machines (VM) you want\n";
841 print " to build on (coma separated). All if none precised\n";
842 print " (or use the env variable PBVM) \n";
843 print "\n";
844 print "-s script : Name of the script you want\n";
845 print " to execute on the related VMs.\n";
846 print "\n";
847 print "-a account : Name of the account to use\n";
848 print " to connect on the related VMs.\n";
849 print "\n";
850 print "-P port : Number of the port to use\n";
851 print " to connect on the related VMs.\n";
852 print "\n";
853 print "-p project : Name of the project you're working on\n";
854 print " (or use the env variable PBPROJ) \n";
855 print "\n";
856 print "-r pbroot : Path Name of project under the CMS \n";
857 print " (or use the env variable PBROOT) \n";
858 print "\n";
859 print "-V newver : New version of the project to create\n";
860 print " from the current one. \n";
861 print "\n";
862 print "<action> can be:\n";
863 print "\n";
864 print "\tcms2build: Create tar files for the project under your CMS\n";
865 print "\t CMS supported are SVN and CVS\n";
866 print "\t parameters are packages to build\n";
867 print "\t if not using default list\n";
868 print "\n";
869 print "\tbuild2pkg: Create packages for your running distribution \n";
870 print "\n";
871 print "\tcms2pkg: cms2build + build2pkg\n";
872 print "\n";
873 print "\tbuild2ssh: Send the tar files to a SSH host \n";
874 print "\n";
875 print "\tcms2ssh: cms2build + build2ssh\n";
876 print "\n";
877 print "\tpkg2ssh: Send the packages built to a SSH host \n";
878 print "\n";
879 print "\tbuild2vm: Create packages in VMs, launching them if needed\n";
880 print "\t and send those packages to a SSH host once built\n";
881 print "\t VM type supported are QEMU \n";
882 print "\n";
883 print "\tcms2vm: cms2build + build2vm\n";
884 print "\n";
885 print "\tlaunchvm: Launch one virtual machine\n";
886 print "\n";
887 print "\tscript2vm: Launch one virtual machine if needed \n";
888 print "\t and executes a script on it \n";
889 print "\n";
890 print "\tnewver: Create a new version of the project derived \n";
891 print "\t from the current one \n";
892 print "\n";
893}
Note: See TracBrowser for help on using the repository browser.