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

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