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

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

First completely good version for VM management

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