source: ProjectBuilder/0.8.5/pb/bin/pb@ 217

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

ntpdate done in the VM before running pb to avoid time issues

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