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

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

Adds Debian build support

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