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

Last change on this file since 329 was 329, checked in by Bruno Cornec, 16 years ago
  • All modules are packages now (simplifies use and spec)
  • Property svn:executable set to *
File size: 39.8 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 File::Basename;
[26]17use File::Copy;
[13]18use Time::localtime qw(localtime);
19use POSIX qw(strftime);
[5]20
[73]21# Global variables
[17]22use lib qw (lib);
[329]23use ProjectBuilder::Distribution;
24use ProjectBuilder::Version;
[318]25use ProjectBuilder::Base;
[5]26
27my %opts; # CLI Options
[9]28my $action; # action to realize
[320]29my $test = "FALSE"; # Not used
30my $force = 0; # Force VE/VM rebuild
31my $option = ""; # Not used
32my @pkgs; # list of packages
[95]33my $pbtag; # Global Tag variable
34my $pbver; # Global Version variable
[141]35my $pbscript; # Name of the script
[77]36my %pbver; # per package
37my %pbtag; # per package
[53]38my $pbrev; # Global REVISION variable
[152]39my $pbaccount; # Login to use to connect to the VM
[162]40my $pbport; # Port to use to connect to the VM
[199]41my $newver; # New version to create
[262]42my $iso; # ISO iage for the VM to create
[5]43
[320]44my @date = pb_get_date();
45my $pbdate = strftime("%Y-%m-%d", @date);
[5]46
[320]47getopts('a:fhi:l:m:P:p:qr:s:tvV:',\%opts);
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'}) {
[318]55 $debug = 2;
56 #$debug = $opts{'v'};
57 pb_log(0,"Debug value: $debug\n");
[21]58}
[320]59if (defined $opts{'f'}) {
60 $force=1;
61}
[21]62if (defined $opts{'q'}) {
63 $debug=-1;
64}
[22]65if (defined $opts{'l'}) {
66 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
67 $LOG = *LOG;
68 $debug = 0 if ($debug == -1);
69 }
[315]70pb_log_init($debug, $LOG);
71
[9]72# Handles test option
73if (defined $opts{'t'}) {
74 $test = "TRUE";
75 # Works only for SVN
76 $option = "-r BASE";
77}
[5]78
[67]79# Handle root of the project if defined
80if (defined $opts{'r'}) {
81 $ENV{'PBROOT'} = $opts{'r'};
82}
[91]83# Handle virtual machines if any
84if (defined $opts{'m'}) {
[320]85 $ENV{'PBV'} = $opts{'m'};
[91]86}
[141]87if (defined $opts{'s'}) {
88 $pbscript = $opts{'s'};
89}
[152]90if (defined $opts{'a'}) {
91 $pbaccount = $opts{'a'};
92}
[162]93if (defined $opts{'P'}) {
94 $pbport = $opts{'P'};
95}
[199]96if (defined $opts{'V'}) {
97 $newver = $opts{'V'};
98}
[262]99if (defined $opts{'i'}) {
100 $iso = $opts{'i'};
101}
[108]102
103# Get Action
104$action = shift @ARGV;
105die pb_syntax() if (not defined $action);
106
[314]107my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
[273]108my $pbinit = undef;
109$pbinit = 1 if ($action =~ /^newproj$/);
[108]110
[59]111# Handles project name if any
[108]112# And get global params
[59]113if (defined $opts{'p'}) {
[315]114 ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir)
[273]115 = pb_env_init($opts{'p'},$pbinit);
[59]116} else {
[315]117 ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir)
[273]118 = pb_env_init(undef,$pbinit);
[59]119}
120
[315]121pb_log(0,"Project: $ENV{'PBPROJ'}\n");
122pb_log(0,"Action: $action\n");
[9]123
[91]124# Keep those project values to store them at the end each time
[83]125my $pbprojtag = $ENV{'PBTAG'};
126my $pbprojver = $ENV{'PBVER'};
127
[9]128# Act depending on action
129if ($action =~ /^cms2build$/) {
[77]130 pb_cms2build();
131} elsif ($action =~ /^build2pkg$/) {
132 pb_build2pkg();
133} elsif ($action =~ /^cms2pkg$/) {
134 pb_cms2build();
135 pb_build2pkg();
[88]136} elsif ($action =~ /^build2ssh$/) {
137 pb_build2ssh();
[220]138} elsif ($action =~ /^cms2ssh$/) {
139 pb_cms2build();
140 pb_build2ssh();
[88]141} elsif ($action =~ /^pkg2ssh$/) {
142 pb_pkg2ssh();
[320]143} elsif ($action =~ /^build2ve$/) {
144 pb_build2v("ve");
[91]145} elsif ($action =~ /^build2vm$/) {
[320]146 pb_build2v("vm");
147} elsif ($action =~ /^cms2ve$/) {
148 pb_cms2build();
149 pb_build2v("ve");
[91]150} elsif ($action =~ /^cms2vm$/) {
151 pb_cms2build();
[320]152 pb_build2v("vm");
[141]153} elsif ($action =~ /^launchvm$/) {
[320]154 pb_launchv("vm",$ENV{'PBV'},0);
155} elsif ($action =~ /^launchve$/) {
156 pb_launchv("ve",$ENV{'PBV'},0);
[142]157} elsif ($action =~ /^script2vm$/) {
[320]158 pb_script2v($pbscript,"vm");
159} elsif ($action =~ /^script2ve$/) {
160 pb_script2v($pbscript,"ve");
[199]161} elsif ($action =~ /^newver$/) {
162 pb_newver();
[320]163} elsif ($action =~ /^newve$/) {
164 pb_launchv("ve",$ENV{'PBV'},1);
[262]165} elsif ($action =~ /^newvm$/) {
[320]166 pb_launchv("vm",$ENV{'PBV'},1);
[273]167} elsif ($action =~ /^newproj$/) {
168 # Nothing to do - already done in pb_env_init
[106]169} elsif ($action =~ /^clean$/) {
[77]170} else {
[315]171 pb_log(0,"\'$action\' is not available\n");
[77]172 pb_syntax();
173}
174
175sub pb_cms2build {
176
[112]177 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
[22]178 @pkgs = @$ptr;
[315]179 my ($scheme,$uri)=pb_cms_init($ENV{'PBPROJ'});
[9]180
[327]181 #
182 # Check project cms compliance
183 #
184 pb_cms_compliant("pbdir",'PBDIR',"$ENV{'PBDEFDIR'}/$ENV{'PBPROJ'}",$uri,$pbinit);
185
[313]186 my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
[174]187
188 # declare packager for filtering
[320]189 my ($tmp) = pb_conf_get("pbpackager");
[174]190 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
191
[27]192 foreach my $pbpkg (@pkgs) {
[115]193 $ENV{'PBPKG'} = $pbpkg;
[274]194 $ENV{'PBVER'} = $pbprojver;
195 $ENV{'PBTAG'} = $pbprojtag;
[98]196 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
197 $pbver = $pkgv->{$pbpkg};
[115]198 $ENV{'PBVER'} = $pbver;
[9]199 } else {
[16]200 $pbver = $ENV{'PBVER'};
[9]201 }
[98]202 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
203 $pbtag = $pkgt->{$pbpkg};
[115]204 $ENV{'PBTAG'} = $pbtag;
[9]205 } else {
[16]206 $pbtag = $ENV{'PBTAG'};
[9]207 }
[95]208
[16]209 $pbrev = $ENV{'PBREVISION'};
[319]210 pb_log(0,"\n");
211 pb_log(0,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
[9]212 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
[16]213 # Clean up dest if necessary. The export will recreate it
[27]214 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
[74]215 pb_rm_rf($dest) if (-d $dest);
[9]216
217 # Export CMS tree for the concerned package to dest
218 # And generate some additional files
219 $OUTPUT_AUTOFLUSH=1;
[29]220
[9]221 # computes in which dir we have to work
[113]222 my $dir = $defpkgdir->{$pbpkg};
223 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
[315]224 pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");
[9]225
[315]226 # If it isn't a flat CMS, then we have the choice to export subdir
[319]227 #$dir = "$uri/$dir" if ($ENV{'PBREVISION'} ne "flat");
[327]228 pb_cms_export($uri,"$ENV{'PBDIR'}/$pbprojver/$dir",$dest);
[315]229
[285]230 # Get project info on authors and log file
[328]231 my $chglog = "$ENV{'PBROOT'}/$pbpkg/pbcl";
232 $chglog = "$ENV{'PBROOT'}/pbcl" if (! -f $chglog);
[285]233 $chglog = undef if (! -f $chglog);
234
[328]235 my $authors = "$ENV{'PBROOT'}/$pbpkg/pbauthors";
236 $authors = "$ENV{'PBROOT'}/pbauthors" if (! -f $authors);
[285]237 $authors = "/dev/null" if (! -f $authors);
238
[9]239 # Extract cms log history and store it
[285]240 if ((defined $chglog) && (! -f "$dest/NEWS")) {
[315]241 pb_log(2,"Generating NEWS file from $chglog\n");
[285]242 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
243 }
[328]244 pb_cms_log($scheme,"$ENV{'PBDIR'}/$pbprojver/$dir",$dest,$chglog,$authors);
[29]245
[21]246 my %build;
[91]247
[328]248 my @pt;
249 @pt = pb_conf_get_if("vmlist","velist");
250 foreach my $d (split(/,/,$pt[0]->{$ENV{'PBPROJ'}}),split(/,/,$pt[1]->{$ENV{'PBPROJ'}})) {
[315]251 my ($name,$ver,$arch) = split(/-/,$d);
252 chomp($arch);
[99]253 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
[315]254 pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n");
255 pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
[13]256
[16]257 # Filter build files from the less precise up to the most with overloading
[13]258 # Filter all files found, keeping the name, and generating in dest
[16]259
260 # Find all build files first relatively to PBROOT
[300]261 # Find also all specific files referenced in the .pb conf file
[320]262 my %bfiles = ();
263 my %pkgfiles = ();
[21]264 $build{"$ddir-$dver"} = "yes";
[315]265
266 if (-d "$ENV{'PBROOT'}/$pbpkg/$dtype") {
267 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$dtype",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
268 } elsif (-d "$ENV{'PBROOT'}/$pbpkg/$dfam") {
269 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
270 } elsif (-d "$ENV{'PBROOT'}/$pbpkg/$ddir") {
271 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
272 } elsif (-d "$ENV{'PBROOT'}/$pbpkg/$ddir-$dver") {
273 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
[13]274 } else {
[21]275 $build{"$ddir-$dver"} = "no";
[13]276 next;
277 }
[315]278 pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");
[13]279
[15]280 # Get all filters to apply
[77]281 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
[15]282
[19]283 # Apply now all the filters on all the files concerned
284 # destination dir depends on the type of file
285 if (defined $ptr) {
[300]286 foreach my $f (values %bfiles,values %pkgfiles) {
[297]287 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$pbsuf,$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$defpkgdir,$extpkgdir,$pbpackager,$chglog);
[16]288 }
[15]289 }
[18]290 }
[315]291 my @found;
292 my @notfound;
293 foreach my $b (keys %build) {
294 push @found,$b if ($build{$b} =~ /yes/);
295 push @notfound,$b if ($build{$b} =~ /no/);
[21]296 }
[315]297 pb_log(0,"Build files generated for ".join(',',@found)."\n");
298 pb_log(0,"No Build files found for ".join(',',@notfound)."\n") if (@notfound);
[236]299 # Get the generic filter (all.pbf) and
300 # apply those to the non-build files including those
301 # generated by pbinit if applicable
302
303 # Get only all.pbf filter
[237]304 $ptr = pb_get_filters($pbpkg);
[236]305
306 my $liste ="";
307 if (defined $filteredfiles->{$pbpkg}) {
308 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
[315]309 pb_filter_file_inplace($ptr,"$dest/$f",$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
[238]310 $liste = "$f $liste";
[236]311 }
312 }
[315]313 pb_log(2,"Files ".$liste."have been filtered\n");
[236]314
[265]315 # Prepare the dest directory for archive
[315]316 if (-x "$ENV{'PBROOT'}/$pbpkg/pbinit") {
317 pb_filter_file("$ENV{'PBROOT'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
[266]318 chmod 0755,"$ENV{'PBTMP'}/pbinit";
[315]319 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOT'}/$pbpkg/pbinit");
[265]320 }
321
[18]322 # Archive dest dir
[69]323 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
[25]324 # Possibility to look at PBSRC to guess more the filename
[94]325 pb_system("tar cfz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
[315]326 pb_log(2,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");
[83]327
[314]328 # Keep track of what is generated by default
329 open(LAST,"> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
330 #print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
331 # Why not use pbproj ?
332 print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOT'}\n";
[83]333 close(LAST);
334
335 # Keep track of per package version
[314]336 my ($pkg) = pb_conf_read_if("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
[83]337 $pkg = { } if (not defined $pkg);
[88]338 if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
339 $pkg->{$pbpkg} = "$pbver-$pbtag";
[83]340 }
341
[315]342 pb_log(2,"DEBUG pkg: ".Dumper($pkg)."\n");
[83]343 open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
[88]344 foreach my $p (keys %$pkg) {
345 print PKG "pbpkg $p = $pkg->{$p}\n";
[83]346 }
347 close(PKG);
[288]348
349 # Final cleanup
350 pb_rm_rf($dest) if (-d $dest);
[9]351 }
[77]352}
[22]353
[77]354sub pb_build2pkg {
355
[22]356 # Get list of packages to build
[112]357 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
[22]358 @pkgs = @$ptr;
359
360 # Get the running distro to build on
[99]361 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
[315]362 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
[22]363
[83]364 # Get content saved in cms2build
[89]365 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
[83]366 $pkg = { } if (not defined $pkg);
367
[320]368 # declare packager (via env var in VM/VE
369 my $pbpackager;
370 if (not defined $ENV{'PBPACKAGER'}) {
371 my ($tmp) = pb_conf_get("pbpackager");
372 $pbpackager = $tmp->{$ENV{'PBPROJ'}};
373 } else {
374 $pbpackager = $ENV{'PBPACKAGER'};
375 }
[174]376
[25]377 chdir "$ENV{'PBBUILDDIR'}";
[126]378 my $made = ""; # pkgs made during build
[27]379 foreach my $pbpkg (@pkgs) {
[88]380 my $vertag = $pkg->{$pbpkg};
[77]381 # get the version of the current package - maybe different
382 ($pbver,$pbtag) = split(/-/,$vertag);
383
[27]384 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
[315]385 pb_log(2,"Source file: $src\n");
[25]386
[315]387 pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
[25]388 if ($dtype eq "rpm") {
389 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
[28]390 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
[96]391 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]392 }
[25]393 }
394
[315]395 # Remove in case a previous link/file was there
[301]396 unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
[188]397 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
[25]398 # We need to first extract the spec file
[28]399 my @specfile;
[77]400 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
[25]401
[315]402 pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
[25]403 # set LANGUAGE to check for correct log messages
404 $ENV{'LANGUAGE'}="C";
[28]405 foreach my $f (@specfile) {
406 if ($f =~ /\.spec$/) {
[291]407 pb_system("rpmbuild --define \'packager $pbpackager\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}");
[28]408 last;
409 }
410 }
[136]411 $made="$made RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm";
[149]412 if (-f "/usr/bin/rpmlint") {
413 pb_system("rpmlint $made","Checking validity of rpms with rpmlint");
[150]414 }
[118]415 } elsif ($dtype eq "deb") {
[149]416 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
[274]417 pb_system("tar xfz $src","Extracting sources");
[149]418
419 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
[310]420 pb_rm_rf("debian");
[149]421 symlink "pbconf/$ddir-$dver","debian" || die "Unable to symlink to pbconf/$ddir-$dver";
[199]422 chmod 0755,"debian/rules";
[149]423 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package");
[136]424 $made="$made $pbpkg"."_*.deb $pbpkg"."_*.dsc $pbpkg"."_*.tar.gz";
[315]425 if (-f "/usr/bin/lintian") {
426 pb_system("lintian $made","Checking validity of debs with lintian");
427 }
[87]428 } elsif ($dtype eq "ebuild") {
[149]429 my @ebuildfile;
430 # For gentoo we need to take pb as subsystem name
[295]431 # We put every apps here under sys-apps. hope it's correct
432 # We use pb's home dir in order o have a single OVERLAY line
433 my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
434 pb_mkdir_p($tmpd) if (! -d "$tmpd");
435 pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
[149]436
[295]437 # We need to first extract the ebuild file
438 @ebuildfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$tmpd");
[149]439
440 # Prepare the build env for gentoo
441 my $found = 0;
[295]442 my $pbbd = $ENV{'HOME'};
[150]443 $pbbd =~ s|/|\\/|g;
[295]444 if (-r "/etc/make.conf") {
445 open(MAKE,"/etc/make.conf");
446 while (<MAKE>) {
447 $found = 1 if (/$pbbd\/portage/);
448 }
449 close(MAKE);
[149]450 }
451 if ($found == 0) {
[295]452 pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
[149]453 }
[295]454 #$found = 0;
455 #if (-r "/etc/portage/package.keywords") {
456 #open(KEYW,"/etc/portage/package.keywords");
457 #while (<KEYW>) {
458 #$found = 1 if (/portage\/pb/);
459 #}
460 #close(KEYW);
461 #}
462 #if ($found == 0) {
463 #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
464 #}
[149]465
466 # Build
467 foreach my $f (@ebuildfile) {
468 if ($f =~ /\.ebuild$/) {
[295]469 move($f,"$tmpd/$pbpkg-$pbver.ebuild");
470 pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package");
471 # Now move it where pb expects it
472 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
473 move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
[149]474 }
475 }
476
[295]477 $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver.ebuild";
[118]478 } elsif ($dtype eq "slackware") {
[136]479 $made="$made build-$pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
[118]480 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
[87]481 } else {
[118]482 die "Unknown dtype format $dtype";
[87]483 }
484 }
[118]485 # Keep track of what is generated so that we can get them back from VMs
[129]486 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
[118]487 print KEEP "$made\n";
488 close(KEEP);
[87]489}
490
[88]491sub pb_build2ssh {
[320]492 pb_send2target("Sources");
[90]493}
[87]494
[90]495sub pb_pkg2ssh {
[320]496 pb_send2target("Packages");
[90]497}
498
[108]499# By default deliver to the the public site hosting the
[320]500# ftp structure (or whatever) or a VM/VE
501sub pb_send2target {
[90]502
503 my $cmt = shift;
[320]504 my $v = shift || undef;
[142]505 my $vmexist = shift || 0; # 0 is FALSE
[200]506 my $vmpid = shift || 0; # 0 is FALSE
[320]507
508 my $host = "sshhost";
509 my $login = "sshlogin";
510 my $dir = "sshdir";
511 my $port = "sshport";
512 my $tmout = "sshtmout";
513 my $path = "sshpath";
514 my $conf = "sshconf";
515 my $rebuild = "sshrebuild";
516 if ($cmt eq "vm") {
517 $login = "vmlogin";
[322]518 $dir = "pbdefdir";
[320]519 $tmout = "vmtmout";
520 $rebuild = "vmrebuild";
521 # Specific VM
522 $host = "vmhost";
523 $port = "vmport";
524 } elsif ($cmt eq "ve") {
525 $login = "velogin";
[322]526 $dir = "pbdefdir";
[320]527 $tmout = "vetmout";
528 # Specific VE
529 $path = "vepath";
530 $conf = "veconf";
531 $rebuild = "verebuild";
532 }
[158]533 my $cmd = "";
[90]534
[87]535 # Get list of packages to build
[112]536 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
[87]537 @pkgs = @$ptr;
538
[136]539 # Get the running distro to consider
[320]540 my ($odir,$over,$oarch) = (undef, undef);
541 if (defined $v) {
542 ($odir,$over,$oarch) = split(/-/,$v);
[118]543 }
544 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
[315]545 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
[87]546
547 # Get content saved in cms2build
[89]548 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
[87]549 $pkg = { } if (not defined $pkg);
550
[118]551 my $src = "";
[87]552 chdir "$ENV{'PBBUILDDIR'}";
553 foreach my $pbpkg (@pkgs) {
[88]554 my $vertag = $pkg->{$pbpkg};
[87]555 # get the version of the current package - maybe different
556 ($pbver,$pbtag) = split(/-/,$vertag);
557
[320]558 if (($cmt eq "Sources") || ($cmt eq "vm") || ($cmt eq "ve")) {
[158]559 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
[166]560 if ($cmd eq "") {
561 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
562 } else {
563 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
564 }
[118]565 }
566 }
[320]567 if (($cmt eq "vm") || ($cmt eq "ve")) {
568 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBROOT'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
[142]569 } elsif ($cmt eq "Script") {
570 $src="$src $ENV{'PBDESTDIR'}/pbscript";
[118]571 } elsif ($cmt eq "Packages") {
572 # Get package list from file made during build2pkg
[129]573 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
[126]574 $src = <KEEP>;
[118]575 chomp($src);
576 close(KEEP);
577 if ($dtype eq "rpm") {
578 # Also make a pbscript to generate yum/urpmi bases
[108]579 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
[118]580 } elsif ($dtype eq "deb") {
581 # Also make a pbscript to generate apt bases
582 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
[90]583 }
[87]584 }
[320]585 # Remove potential leading spaces (cause problem with basename)
[132]586 $src =~ s/^ *//;
[129]587 my $basesrc = "";
[131]588 foreach my $i (split(/ +/,$src)) {
589 $basesrc .= " ".basename($i);
[130]590 }
[118]591
[320]592 pb_log(0,"Sources handled ($cmt): $src\n");
593 my ($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf) = pb_conf_get($host,$login,$dir,$port,$tmout,$rebuild,$path,$conf);
594 pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf))."\n");
595 # Not mandatory
596 my ($testver) = pb_conf_get_if("testver");
597
598 my $mac;
599 # Useless for VE
600 if ($cmt ne "ve") {
601 $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
602 # Overwrite account value if passed as parameter
603 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
604 }
605
[108]606 my $tdir;
[136]607 my $bdir;
[142]608 if (($cmt eq "Sources") || ($cmt eq "Script")) {
[108]609 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
[320]610 } elsif (($cmt eq "vm") || ($cmt eq "ve")) {
611 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery";
612 $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build";
[136]613 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
614 $bdir =~ s|\$ENV.+\}/||;
[90]615 } elsif ($cmt eq "Packages") {
[108]616 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
[320]617 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} eq "true")) {
[289]618 # This is a test pkg => target dir is under test
619 $tdir .= "/test";
[291]620 }
[90]621 } else {
622 return;
[22]623 }
[239]624
[320]625 # Useless for VE
626 my $nport;
627 if ($cmt ne "ve") {
628 $nport = $sshport->{$ENV{'PBPROJ'}};
629 $nport = "$pbport" if (defined $pbport);
630 }
631
[136]632 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
[132]633 $tdir =~ s|\$ENV.+\}/||;
634
[320]635 my $tm = $vtmout->{$ENV{'PBPROJ'}};
636
637 # ssh communication if not VE
638 my ($shcmd,$cpcmd,$cptarget,$cp2target);
639 if ($cmt ne "ve") {
640 $shcmd = "ssh -q -p $nport $mac";
641 $cpcmd = "scp -p -P $nport";
642 $cptarget = "$mac:$tdir";
643 $cp2target = "$mac:$bdir";
644 } else {
645 my $tp = $vepath->{$ENV{'PBPROJ'}};
646 $shcmd = "sudo chroot $tp/$v /bin/su - $sshlogin->{$ENV{'PBPROJ'}} -c ";
647 $cpcmd = "cp -a ";
648 $cptarget = "$tp/$tdir";
649 $cp2target = "$tp/$bdir";
650 }
651
652 pb_system("$shcmd \"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 $cptarget");
653 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
654 # For VE we need to change the owner manually - To be tested if needed
655 #if ($cmt eq "ve") {
656 #pb_system("cd $cptarget ; sudo chown -R $sshlogin->{$ENV{'PBPROJ'}} .","$cmt chown in $cptarget to $sshlogin->{$ENV{'PBPROJ'}}");
657 #}
658 pb_system("$shcmd \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $cptarget if needed");
659 if (($cmt eq "vm") || ($cmt eq "ve")) {
[128]660 # Get back info on pkg produced, compute their name and get them from the VM
[320]661 pb_system("$cpcmd $cp2target/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target");
[129]662 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
[118]663 my $src = <KEEP>;
664 chomp($src);
665 close(KEEP);
[131]666 $src =~ s/^ *//;
[136]667 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
[320]668 # Change pgben to make the next send2target happy
[143]669 my $made = "";
[139]670 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
[136]671 foreach my $p (split(/ +/,$src)) {
[139]672 my $j = basename($p);
[320]673 pb_system("$cpcmd $cp2target/\'$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $cp2target");
[144]674 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
[136]675 }
[143]676 print KEEP "$made\n";
[139]677 close(KEEP);
[320]678 pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
679 pb_send2target("Packages","$odir"."_"."$over");
680 if ((! $vmexist) && ($cmt eq "vm")) {
681 pb_system("$shcmd \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $v halt (pid $vmpid)");
[142]682 }
[141]683 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
[108]684 }
[9]685}
[16]686
[320]687sub pb_script2v {
[142]688 my $pbscript=shift;
[320]689 my $vtype=shift;
[141]690
691 # Prepare the script to be executed on the VM
692 # in $ENV{'PBDESTDIR'}/pbscript
[142]693 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
694 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
695 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
696 }
[141]697
[320]698 my ($vm,$all) = pb_get_v($vtype);
699 my ($vmexist,$vmpid) = (undef,undef);
[142]700
[141]701 foreach my $v (@$vm) {
[320]702 # Launch the VM/VE
703 if ($vtype eq "vm") {
704 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
[141]705
[320]706 # Skip that VM if something went wrong
707 next if (($vmpid == 0) && ($vmexist ==0));
708 }
[274]709
[142]710 # Gather all required files to send them to the VM
[320]711 # and launch the build through pbscript
712 pb_send2target("Script","$v",$vmexist,$vmpid);
[169]713
[142]714 }
715}
716
[320]717sub pb_launchv {
718 my $vtype = shift;
719 my $v = shift;
720 my $create = shift || 0; # By default do not create a VM
[310]721
[320]722 die "No VM/VE defined, unable to launch" if (not defined $v);
723 # Keep only the first VM in case many were given
724 $v =~ s/,.*//;
[310]725
[320]726 # Which is our local arch ? (standardize on i386 for those platforms)
727 my $arch = `uname -m`;
728 chomp($arch);
729 $arch =~ s/i.86/i386/;
[310]730
[320]731 # Launch the VMs/VEs
732 if ($vtype eq "vm") {
733 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
[310]734
[320]735 my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout","vmsize");
736
737 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
738 if (not defined $ENV{'PBVMOPT'}) {
739 $ENV{'PBVMOPT'} = "";
[310]740 }
[320]741 if (defined $vmopt->{$ENV{'PBPROJ'}}) {
742 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
743 }
744 my $nport = $vmport->{$ENV{'PBPROJ'}};
745 $nport = "$pbport" if (defined $pbport);
746
747 my $cmd;
748 my $vmcmd; # has to be used for pb_check_ps
749 my $vmm; # has to be used for pb_check_ps
750 if ($vmtype eq "qemu") {
751 my $qemucmd32;
752 my $qemucmd64;
753 if ($arch eq "x86_64") {
754 $qemucmd32 = "/usr/bin/qemu-system-i386";
755 $qemucmd64 = "/usr/bin/qemu";
756 } else {
757 $qemucmd32 = "/usr/bin/qemu";
758 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
759 }
760 if ($v =~ /x86_64/) {
761 $vmcmd = "$qemucmd64 -no-kqemu";
762 } else {
763 $vmcmd = "$qemucmd32";
764 }
765 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
766 if ($create != 0) {
767 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
768 }
769 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
770 } elsif ($vmtype eq "xen") {
771 } elsif ($vmtype eq "vmware") {
[310]772 } else {
[320]773 die "VM of type $vmtype not supported. Report to the dev team";
[310]774 }
[320]775 my ($tmpcmd,$void) = split(/ +/,$cmd);
776 my $vmexist = pb_check_ps($tmpcmd,$vmm);
777 my $vmpid = 0;
778 if (! $vmexist) {
779 if ($create != 0) {
780 if (($vmtype eq "qemu") || ($vmtype eq "xen")) {
781 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
782 } elsif ($vmtype eq "vmware") {
783 } else {
784 }
785 }
786 if (! -f "$vmm") {
787 pb_log(0,"Unable to find VM $vmm\n");
788 } else {
789 pb_system("$cmd &","Launching the VM $vmm");
790 pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $v to come up");
791 $vmpid = pb_check_ps($tmpcmd,$vmm);
792 }
[310]793 } else {
[320]794 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
[310]795 }
[320]796 return($vmexist,$vmpid);
797 # VE here
[310]798 } else {
[320]799 # Get VE context
800 my ($ptr,$vepath,$vetmout,$verebuild,$veconf) = pb_conf_get("vetype","vepath","vetmout","verebuild","veconf");
801 my $vetype = $ptr->{$ENV{'PBPROJ'}};
[310]802
[320]803 # Get distro context
804 my ($name,$ver,$darch) = split(/-/,$v);
805 chomp($darch);
806 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
[142]807
[320]808 if ($vetype eq "chroot") {
809 # Architecture consistency
810 if ($arch ne $darch) {
811 die "Unable to launch a VE of architecture $darch on a $arch platform" if (not (($darch eq "x86_64") && ($arch =~ /i?86/)));
812 }
[142]813
[320]814 if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($force == 1)) {
815 # We have to rebuild the chroot
816 if ($dtype eq "rpm") {
817 pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
818 # Once setup we need to install some packages, the pb account, ...
819 pb_system("sudo /usr/sbin/mock --install --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
820 #pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" --basedir=\"$vepath->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
821 } elsif ($dtype eq "deb") {
822 pb_system("","Creating the pbuilder VE");
823 } elsif ($dtype eq "ebuild") {
824 die "Please teach the dev team how to build gentoo chroot";
825 } else {
826 die "Unknown distribution type $dtype. Report to dev team";
827 }
[310]828 }
[320]829 # Nothing more to do for VE. No real launch
[274]830 } else {
[320]831 die "VE of type $vetype not supported. Report to the dev team";
[262]832 }
[141]833 }
834}
[142]835
[320]836sub pb_build2v {
[118]837
[320]838my $vtype = shift;
[142]839
[320]840# Prepare the script to be executed on the VM/VE
841# in $ENV{'PBDESTDIR'}/pbscript
842my ($ntp) = pb_conf_get($vtype."ntp");
843my $vntp = $ntp->{$ENV{'PBPROJ'}};
[118]844
[320]845open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
846print SCRIPT "#!/bin/bash\n";
847print SCRIPT "echo ... Execution needed\n";
848print SCRIPT "# This is in directory delivery\n";
849print SCRIPT "# Setup the variables required for building\n";
850print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
851print SCRIPT "# Preparation for pb\n";
852print SCRIPT "mv .pbrc \$HOME\n";
853print SCRIPT "cd ..\n";
854# Force new date to be in the future compared to the date of the tar file by adding 1 minute
855my @date=pb_get_date();
856$date[1]++;
857my $upddate = strftime("%m%d%H%M%Y", @date);
858print SCRIPT "echo Setting up date on $vntp...\n";
859# Or use ntpdate if available TBC
860print SCRIPT "sudo date $upddate\n";
861# This exports avoids pb_env_init to deal with PBCONF stuff
862print SCRIPT "export PBCONF=/tmp\n";
863print SCRIPT "export PBVER=$ENV{'PBVER'}\n";
864print SCRIPT "export PBTAG=$ENV{'PBTAG'}\n";
865print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
866print SCRIPT "# Build\n";
867# Get list of packages to build
868my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
869@pkgs = @$ptr;
870my $p = join(' ',@pkgs) if (@pkgs);
871print SCRIPT "echo Building packages on $vtype...\n";
872print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
873close(SCRIPT);
874chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
875
876my ($v,$all) = pb_get_v($vtype);
877
878# Send tar files when we do a global generation
879pb_build2ssh() if ($all == 1);
880
881my ($vmexist,$vmpid) = (undef,undef);
882
883foreach my $v (@$v) {
884 if ($vtype eq "vm") {
[142]885 # Launch the VM
[320]886 my ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
[118]887
[274]888 # Skip that VM if it something went wrong
889 next if (($vmpid == 0) && ($vmexist == 0));
[118]890 }
[320]891 # Gather all required files to send them to the VM/VE
892 # and launch the build thourgh pbscript
893 pb_send2target($vtype,"$v",$vmexist,$vmpid);
[105]894}
[320]895}
[105]896
[262]897
[199]898sub pb_newver {
899
[204]900 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
[315]901 my $scheme=pb_cms_init($ENV{'PBPROJ'});
902 if ($scheme ne "svn") {
[199]903 die "Only SVN is supported at the moment";
904 }
[315]905 my $res = pb_cms_isdiff($scheme);
[199]906 die "You need to have no differences before creating a new version" if ($res != 0);
[315]907 my $cmsurl = pb_cms_getinfo($scheme,$ENV{'PBROOT'},"URL:");
[204]908 my $newurl = dirname($cmsurl)."/$newver";
[315]909 pb_cms_copy($scheme,$cmsurl,$newurl);
910 pb_cms_checkout($scheme,$newurl,"$ENV{'PBROOT'}/../$newver");
[208]911 my $oldver=basename($cmsurl);
[211]912 open(FILE,"$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb";
913 open(OUT,"> $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new";
[208]914 while(<FILE>) {
[209]915 s/projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
[211]916 print OUT $_;
[208]917 }
918 close(FILE);
[211]919 close(OUT);
920 rename("$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb");
[315]921 pb_cms_checkin($scheme,"$ENV{'PBROOT'}/../$newver");
[199]922}
923
[293]924#
[320]925# Return the list of VMs/VEs we are working on
[105]926# $all is a flag to know if we return all of them
927# or only some (if all we publish also tar files in addition to pkgs
928#
[320]929sub pb_get_v {
[91]930
[320]931my $vtype = shift;
932my @v;
[105]933my $all = 0;
[320]934my $vlist;
935my $pbv = 'PBV';
[91]936
[320]937if ($vtype eq "vm") {
938 $vlist = "vmlist";
939} elsif ($vtype eq "ve") {
940 $vlist = "velist";
941}
942# Get VM/VE list
943if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
944 my ($ptr) = pb_conf_get($vlist);
945 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
[105]946 $all = 1;
[91]947}
[320]948pb_log(2,"$vtype: $ENV{$pbv}\n");
949@v = split(/,/,$ENV{$pbv});
950return(\@v,$all);
[91]951}
952
[320]953sub pb_setup_v {
954# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
955# Needs to use root account to connect to the VM/VE
956
957# pb will take your local public SSH key to access
958# the pb account in the VM later on
959
960my $vtype = shift;
961
962my $file = "$ENV{'HOME'}/.ssh/id_dsa.pub";
963die "Unable to find your public ssh key as $file";
964
965open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
966print SCRIPT << 'EOF';
967#!/usr/bin/perl -w
968
969$file="/etc/passwd";
970open(PBFILE,$file) || die "Unable to open $file";
971my $found = 0;
972while (<PBFILE>) {
973 $found = 1 if (/^pb:/);
974}
975close(PBFILE);
976
977if ( $found == 0 ) {
978 if ( ! -d "/home" ) {
979 mkdir "/home";
980 }
981 system "groupadd pb";
982 system "useradd pb -g pb -m -d /home/pb";
983}
984
985# For root
986mkdir ".ssh",0700;
987system 'cp /tmp/pbkey .ssh/authorized_keys';
988chmod 0600,".ssh/authorized_keys";
989
990# For pb
991chdir "/home/pb";
992mkdir ".ssh",0700;
993system 'cp /tmp/pbkey .ssh/authorized_keys';
994chmod 0600,".ssh/authorized_keys";
995system 'chown -R pb:pb .ssh';
996
997# No passwd for pb only keys
998$file="/etc/shadow";
999open(PBFILE,$file) || die "Unable to open $file";
1000open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1001while (<PBFILE>) {
1002 s/^pb:\!\!:/pb:*:/;
1003 s/^pb:\!:/pb:*:/; #SLES 9 e.g.
1004 print PBOUT $_;
1005}
1006close(PBFILE);
1007close(PBOUT);
1008rename("$file.new",$file);
1009chmod 0640,$file;
1010
1011# pb has to be added to portage group on gentoo
1012unlink "/tmp/pbkey";
1013
1014my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
1015print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n";
1016
1017# Get and install pb
1018if ( $ddir eq "fedora" ) {
1019 system "yum clean all";
1020 system "yum update -y";
1021 my $arch=`uname -m`;
1022 my $opt = "";
1023 chomp($arch);
1024 if ($arch eq "x86_64") {
1025 $opt="--exclude=*.i?86";
1026 }
1027
1028 system "yum -y $opt install rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker";
1029} elsif (( $dfam eq "rh" ) || ($ddir eq "sles") || (($ddir eq "suse") && (($dver eq "10.1") || ($dver eq "10.0"))) || (($ddir eq "mandrake") && ($dver eq "10.1"))) {
1030 # Suppose pkg are installed already
1031 system "rpm -e lsb 2>&1 > /dev/null";
1032 system "rm -rf DateManip* ; wget http://search.cpan.org/CPAN/authors/id/S/SB/SBECK/Date-Manip-5.46.tar.gz ; tar xvfz Date-Manip-5.46.tar.gz ; cd Date-Manip* ; perl Makefile.PL ; make ; make install ; cd .. ";
1033} elsif ($ddir eq "suse") {
1034 # New OpenSuSE
1035 system "export TERM=linux ; liste=\"\" ; for i in make wget patch sudo perl-DateManip perl-File-HomeDir xntp; do rpm -q \$i 1> /dev/null 2> /dev/null ; if [ \$\? != 0 ]; then liste=\"\$liste \$i\"; fi; done; echo \"Liste: \$liste\" ; if [ \"\$liste\" != \"\" ]; then yast2 -i \$liste ; fi";
1036} elsif ( $dfam eq "md" ) {
1037 system "urpmi.update -a ; urpmi --auto rpm-build wget sudo patch ntp-client perl-DateManip";
1038} elsif ( $dfam eq "du" ) {
1039 if (( $dver eq "3.1" ) && ($ddir eq "debian")) {
1040 system "apt-get update; apt-get -y install wget patch ssh sudo debian-builder dh-make fakeroot ntpdate libdate-manip-perl";
1041 } else {
1042 system "apt-get update; apt-get -y install wget patch openssh-server dpkg-dev sudo debian-builder dh-make fakeroot ntpdate rses5-dev libdate-manip-perl";
1043 }
1044} elsif ( $dfam eq "gen" ) {
1045 system "emerge -u system ; emerge wget sudo ntp DateManip";
1046} else {
1047 print "No pkg to install\n";
1048}
1049
1050# Adapt sudoers
1051$file="/etc/sudoers";
1052open(PBFILE,$file) || die "Unable to open $file";
1053open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1054while (<PBFILE>) {
1055 next if (/^pb /);
1056 s/Defaults[ \t]+requiretty//;
1057 print PBOUT $_;
1058}
1059close(PBFILE);
1060print PBOUT "pb ALL=(ALL) NOPASSWD:ALL\n";
1061close(PBOUT);
1062rename("$file.new",$file);
1063chmod 0440,$file;
1064
1065# Suse wants sudoers as 640
1066if (($ddir eq "sles") || (($ddir eq "suse")) && ($dver ne "10.3")) {
1067 chmod 0640,$file;
1068}
1069
1070# Sync date
1071system "/usr/sbin/ntpdate ntp.pool.org";
1072
1073system "rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd ..";
1074EOF
1075close(SCRIPT);
1076}
1077
[145]1078# Returns the pid of a running VM command using a specific VM file
[142]1079sub pb_check_ps {
1080 my $vmcmd = shift;
1081 my $vmm = shift;
1082 my $vmexist = 0; # FALSE by default
1083
1084 open(PS, "ps auxhww|") || die "Unable to call ps";
1085 while (<PS>) {
1086 next if (! /$vmcmd/);
1087 next if (! /$vmm/);
1088 my ($void1, $void2);
1089 ($void1, $vmexist, $void2) = split(/ +/);
1090 last;
1091 }
1092 return($vmexist);
1093}
1094
1095
[77]1096sub pb_extract_build_files {
[25]1097
1098my $src=shift;
1099my $dir=shift;
[26]1100my $ddir=shift;
[28]1101my @files;
[25]1102
[188]1103if ($src =~ /tar\.gz$/) {
1104 pb_system("tar xfpz $src $dir","Extracting build files");
1105} elsif ($src =~ /tar\.bz2$/) {
1106 pb_system("tar xfpj $src $dir","Extracting build files");
1107} else {
1108 die "Unknown compression algorithm for $src";
1109}
[25]1110opendir(DIR,"$dir") || die "Unable to open directory $dir";
1111foreach my $f (readdir(DIR)) {
1112 next if ($f =~ /^\./);
[26]1113 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
[315]1114 pb_log(2,"mv $dir/$f $ddir\n");
[28]1115 push @files,"$ddir/$f";
[25]1116}
1117closedir(DIR);
[26]1118# Not enough but still a first cleanup
[74]1119pb_rm_rf("$dir");
[28]1120return(@files);
[25]1121}
1122
[315]1123sub pb_list_bfiles {
1124
1125my $dir = shift;
1126my $pbpkg = shift;
1127my $bfiles = shift;
1128my $pkgfiles = shift;
1129my $supfiles = shift;
1130
1131opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
1132foreach my $f (readdir(BDIR)) {
1133 next if ($f =~ /^\./);
1134 $bfiles->{$f} = "$dir/$f";
1135 $bfiles->{$f} =~ s~$ENV{'PBROOT'}~~;
1136 if (defined $supfiles->{$pbpkg}) {
1137 $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
1138 }
1139}
1140closedir(BDIR);
1141}
1142
[74]1143sub pb_syntax {
[21]1144
[53]1145 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
1146 print "\n";
[313]1147 print "Syntax: pb [-vhqt][-r pbroot][-p project][[-s script -a account -P port] -m \"mach-1[,...]\"][-i iso] <action> [<pkg1> ...]\n";
[21]1148 print "\n";
[310]1149 print "-h: This help file\n";
1150 print "-q: Quiet mode\n";
1151 print "-t: Test mode (not done yet)\n";
1152 print "-v: Verbose mode\n";
[21]1153 print "\n";
[310]1154 print "-m machine: Name of the Virtual Machines (VM) you want\n";
1155 print " to build on (coma separated). All if none precised\n";
[320]1156 print " (or use the env variable PBV) \n";
[69]1157 print "\n";
[310]1158 print "-s script: Name of the script you want\n";
1159 print " to execute on the related VMs.\n";
[141]1160 print "\n";
[310]1161 print "-i iso: Name of the ISO image of the distribution you want\n";
1162 print " to install on the related VMs.\n";
[288]1163 print "\n";
[310]1164 print "-a account: Name of the account to use\n";
1165 print " to connect on the related VMs.\n";
[152]1166 print "\n";
[313]1167 print "-P port: Port number to use\n";
[310]1168 print " to connect on the related VMs.\n";
[199]1169 print "\n";
[310]1170 print "-p project: Name of the project you're working on\n";
1171 print " (or use the env variable PBPROJ) \n";
[21]1172 print "\n";
[310]1173 print "-r pbroot: Path Name of project under the CMS \n";
1174 print " (or use the env variable PBROOT) \n";
[91]1175 print "\n";
[310]1176 print "-V newver: New version of the project to create\n";
1177 print " from the current one. \n";
[199]1178 print "\n";
[21]1179 print "<action> can be:\n";
1180 print "\n";
[310]1181 print "\tcms2build: Create tar files for the project under your CMS\n";
1182 print "\t CMS supported are SVN and CVS\n";
1183 print "\t parameters are packages to build\n";
1184 print "\t if not using default list\n";
[21]1185 print "\n";
[310]1186 print "\tbuild2pkg: Create packages for your running distribution \n";
[21]1187 print "\n";
[310]1188 print "\tcms2pkg: cms2build + build2pkg\n";
[169]1189 print "\n";
[310]1190 print "\tbuild2ssh: Send the tar files to a SSH host \n";
[105]1191 print "\n";
[310]1192 print "\tcms2ssh: cms2build + build2ssh\n";
[220]1193 print "\n";
[310]1194 print "\tpkg2ssh: Send the packages built to a SSH host \n";
[105]1195 print "\n";
[310]1196 print "\tbuild2vm: Create packages in VMs, launching them if needed\n";
1197 print "\t and send those packages to a SSH host once built\n";
1198 print "\t VM type supported are QEMU \n";
[136]1199 print "\n";
[320]1200 print "\tbuild2ve: Create packages in VEs, creating it if needed\n";
[310]1201 print "\t and send those packages to a SSH host once built\n";
[169]1202 print "\n";
[310]1203 print "\tcms2vm: cms2build + build2vm\n";
[141]1204 print "\n";
[320]1205 print "\tcms2ve: cms2build + build2ve\n";
1206 print "\n";
[310]1207 print "\tlaunchvm: Launch one virtual machine\n";
[142]1208 print "\n";
[320]1209 print "\tlaunchve: Launch one virtual environment\n";
1210 print "\n";
[310]1211 print "\tscript2vm: Launch one virtual machine if needed \n";
1212 print "\t and executes a script on it \n";
[199]1213 print "\n";
[320]1214 print "\tscript2ve: Execute a script in a virtual environment \n";
1215 print "\n";
[310]1216 print "\tnewvm: Create a new virtual machine\n";
[262]1217 print "\n";
[320]1218 print "\tnewve: Create a new virtual environment\n";
[273]1219 print "\n";
[310]1220 print "\tnewver: Create a new version of the project derived \n";
1221 print "\t from the current one \n";
1222 print "\n";
1223 print "\tnewproj: Create a new project and a template set of \n";
1224 print "\t configuration files under pbconf \n";
1225 print "\n";
[21]1226}
Note: See TracBrowser for help on using the repository browser.