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

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

Really fix it

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