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

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

Fix problem building in RPM_BUILD_ROOT

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