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

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