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

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

Attempt ro remove a pb on recent bash version (maybe)

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