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

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

Adds pbpackager variable for filtering (Debian)

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