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

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

Improves pbsetupqemu with pb build + pkg install

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