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

Last change on this file since 266 was 266, checked in by Bruno Cornec, 16 years ago

Realise the filtering of pbinit correctly (validated on mondo)

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