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

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

Adds pbpackager variable for filtering (Debian)

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