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

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

Improves pbsetupqemu with pb build + pkg install

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