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

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

Handles pbpackager correctly, and especially in rpm build with a define

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