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

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

ntpdate placed in pbsetupqemu

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