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

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

Adds the possibility to connect to the VM using a given account (useful for root access)

  • Property svn:executable set to *
File size: 25.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::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
431 # Get list of packages to build
432 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
433 @pkgs = @$ptr;
434
435 # Get the running distro to consider
436 my ($odir,$over) = (undef, undef);
437 if (defined $vm) {
438 ($odir,$over) = split(/_/,$vm);
439 }
440 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
441 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
442
443 # Get content saved in cms2build
444 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
445 $pkg = { } if (not defined $pkg);
446
447 my $src = "";
448 chdir "$ENV{'PBBUILDDIR'}";
449 foreach my $pbpkg (@pkgs) {
450 my $vertag = $pkg->{$pbpkg};
451 # get the version of the current package - maybe different
452 ($pbver,$pbtag) = split(/-/,$vertag);
453
454 if (($cmt eq "Sources") || ($cmt eq "VMs")) {
455 $src="$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
456 }
457 }
458 if ($cmt eq "VMs") {
459 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
460 } elsif ($cmt eq "Script") {
461 $src="$src $ENV{'PBDESTDIR'}/pbscript";
462 } elsif ($cmt eq "Packages") {
463 # Get package list from file made during build2pkg
464 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
465 $src = <KEEP>;
466 chomp($src);
467 close(KEEP);
468 if ($dtype eq "rpm") {
469 # Also make a pbscript to generate yum/urpmi bases
470 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
471 } elsif ($dtype eq "deb") {
472 # Also make a pbscript to generate apt bases
473 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
474 }
475 }
476 # Remove potential leading spaces (cause pb with basename)
477 $src =~ s/^ *//;
478 my $basesrc = "";
479 foreach my $i (split(/ +/,$src)) {
480 $basesrc .= " ".basename($i);
481 }
482
483 print $LOG "Sources handled ($cmt): $src\n" if ($debug >= 0);
484 my ($sshhost,$sshlogin,$sshdir,$sshport) = pb_conf_get($host,$login,$dir,$port);
485 my $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
486 # Overwrite account value if passed as parameter
487 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
488 my $tdir;
489 my $bdir;
490 if (($cmt eq "Sources") || ($cmt eq "Script")) {
491 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
492 } elsif ($cmt eq "VMs") {
493 $tdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/delivery";
494 $bdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/build";
495 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
496 $bdir =~ s|\$ENV.+\}/||;
497 } elsif ($cmt eq "Packages") {
498 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
499 } else {
500 return;
501 }
502 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
503 $tdir =~ s|\$ENV.+\}/||;
504
505 $port = $sshport->{$ENV{'PBPROJ'}};
506 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\' | bash\"","Preparing $tdir on $mac");
507 pb_system("cd $ENV{'PBBUILDDIR'} ; scp -p -P $port $src $mac:$tdir 2> /dev/null","$cmt delivery in $tdir on $mac");
508 pb_system("ssh -q -p $port $mac \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $mac if needed");
509 if ($cmt eq "VMs") {
510 # Get back info on pkg produced, compute their name and get them from the VM
511 pb_system("scp -p -P $port $mac:$bdir/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $tdir on $mac");
512 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
513 my $src = <KEEP>;
514 chomp($src);
515 close(KEEP);
516 $src =~ s/^ *//;
517 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
518 # Change pgben to make the next send2ssh happy
519 my $made = "";
520 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
521 foreach my $p (split(/ +/,$src)) {
522 my $j = basename($p);
523 pb_system("scp -p -P $port $mac:\'$bdir/$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $tdir from $mac");
524 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
525 }
526 print KEEP "$made\n";
527 close(KEEP);
528 pb_system("ssh -q -p $port $mac \"rm -rf $tdir\"","VM cleanup on $mac");
529 if (! $vmexist) {
530 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");
531 }
532 pb_send2ssh("Packages","$odir"."_"."$over");
533 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
534 }
535}
536
537sub pb_script2vm {
538 my $pbscript=shift;
539
540 # Prepare the script to be executed on the VM
541 # in $ENV{'PBDESTDIR'}/pbscript
542 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
543 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
544 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
545 }
546
547 my ($vm,$all) = pb_get_vm();
548
549 foreach my $v (@$vm) {
550 # Launch the VM
551 my $vmexist = pb_launchvm($v);
552 if (! $vmexist) {
553 pb_system("sleep 300","Waiting for it to come up");
554 }
555
556 # Gather all required files to send them to the VM
557 # and launch the build thourgh pbscript
558 pb_send2ssh("Script","$v",$vmexist,"vmhost","vmlogin","pbrc","vmport");
559 }
560}
561
562sub pb_launchvm {
563 my $vm = shift;
564
565 die "No VM defined, unable to launch" if (not defined $vm);
566 # Keep only the first VM in case many were given
567 $vm =~ s/,.*//;
568
569 # Launch the VMs
570 my ($ptr,$vmopt,$vmport,$vmpath) = pb_conf_get("vmtype","vmopt","vmport","vmpath");
571 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
572 if (defined $vmopt->{$ENV{'PBPROJ'}}) {
573 $ENV{'PBVMOPT'} = $vmopt->{$ENV{'PBPROJ'}};
574 } else {
575 $ENV{'PBVMOPT'} = "";
576 }
577
578 my $cmd;
579 my $vmcmd; # has to be used for pb_check_ps
580 my $vmm; # has to be used for pb_check_ps
581 if ($vmtype eq "qemu") {
582 my $arch = `uname -m`;
583 chomp($arch);
584 my $qemucmd32;
585 my $qemucmd64;
586 if ($arch eq "x86_64") {
587 $qemucmd32 = "/usr/bin/qemu-system-i386";
588 $qemucmd64 = "/usr/bin/qemu";
589 } else {
590 $qemucmd32 = "/usr/bin/qemu";
591 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
592 }
593 if ($vm =~ /_64/) {
594 $vmcmd = "$qemucmd64 -no-kqemu";
595 } else {
596 $vmcmd = "$qemucmd32";
597 }
598 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$vm.qemu";
599 if (! -f "$vmm") {
600 print "Unable to find VM $vmm";
601 return;
602 }
603 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$vmport->{$ENV{'PBPROJ'}}:10.0.2.15:22 $vmm"
604 } elsif ($vmtype eq "xen") {
605 } elsif ($vmtype eq "vmware") {
606 } else {
607 die "VM of type $vmtype not supported. Report to the dev team";
608 }
609 my $vmexist = pb_check_ps($vmcmd,$vmm);
610 if (! $vmexist) {
611 pb_system("$cmd &","Launching the VM");
612 }
613 return($vmexist);
614}
615
616sub pb_build2vm {
617 # Prepare the script to be executed on the VM
618 # in $ENV{'PBDESTDIR'}/pbscript
619 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
620 print SCRIPT "#!/bin/bash\n";
621 print SCRIPT "echo ... Execution needed\n";
622 print SCRIPT "# This is in directory delivery\n";
623 print SCRIPT "# Setup the variables required for building\n";
624 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
625 print SCRIPT "# Preparation for pb\n";
626 print SCRIPT "mkdir -p ../pbconf\n";
627 print SCRIPT "mv $ENV{'PBPROJ'}.pb ../pbconf\n";
628 print SCRIPT "mv .pbrc \$HOME\n";
629 print SCRIPT "cd ..\n";
630 print SCRIPT "export PBROOT=\`pwd\`\n";
631 print SCRIPT "# Build\n";
632 my $p = "";
633 $p = $ARGV[0] if (defined $ARGV[0]);
634 print SCRIPT "echo Building packages on VM...\n";
635 print SCRIPT "pb build2pkg $p\n";
636 close(SCRIPT);
637 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
638
639 my ($vm,$all) = pb_get_vm();
640
641 # Send tar files when we do a global generation
642 pb_build2ssh() if ($all == 1);
643
644 foreach my $v (@$vm) {
645 # Launch the VM
646 my $vmexist = pb_launchvm($v);
647 if (! $vmexist) {
648 pb_system("sleep 300","Waiting for it to come up");
649 }
650
651 # Gather all required files to send them to the VM
652 # and launch the build thourgh pbscript
653 pb_send2ssh("VMs","$v",$vmexist,"vmhost","vmlogin","pbrc","vmport");
654 }
655}
656
657sub pb_get_pkg {
658
659my @pkgs;
660my $defpkgdir = shift;
661my $extpkgdir = shift;
662
663# Get packages list
664if (not defined $ARGV[0]) {
665 @pkgs = keys %$defpkgdir;
666} elsif ($ARGV[0] =~ /^all$/) {
667 @pkgs = keys %$defpkgdir;
668 push(@pkgs, keys %$extpkgdir);
669} else {
670 @pkgs = @ARGV;
671}
672print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
673return(\@pkgs);
674}
675
676#
677# Return the list of VMs we are working on
678# $all is a flag to know if we return all of them
679# or only some (if all we publish also tar files in addition to pkgs
680#
681sub pb_get_vm {
682
683my @vm;
684my $all = 0;
685
686# Get VM list
687if ((not defined $ENV{'PBVM'}) || ($ENV{'PBVM'} =~ /^all$/)) {
688 my ($ptr) = pb_conf_get("vmlist");
689 $ENV{'PBVM'} = $ptr->{$ENV{'PBPROJ'}};
690 $all = 1;
691}
692@vm = split(/,/,$ENV{'PBVM'});
693print $LOG "VMs: ".join(',',@vm)."\n";
694return(\@vm,$all);
695}
696
697# Returns the pid of a running VM command using a specific VM file
698sub pb_check_ps {
699 my $vmcmd = shift;
700 my $vmm = shift;
701 my $vmexist = 0; # FALSE by default
702
703 open(PS, "ps auxhww|") || die "Unable to call ps";
704 while (<PS>) {
705 next if (! /$vmcmd/);
706 next if (! /$vmm/);
707 my ($void1, $void2);
708 ($void1, $vmexist, $void2) = split(/ +/);
709 last;
710 }
711 return($vmexist);
712}
713
714
715sub pb_extract_build_files {
716
717my $src=shift;
718my $dir=shift;
719my $ddir=shift;
720my @files;
721
722pb_system("tar xfpz $src $dir","Extracting build files");
723opendir(DIR,"$dir") || die "Unable to open directory $dir";
724foreach my $f (readdir(DIR)) {
725 next if ($f =~ /^\./);
726 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
727 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
728 push @files,"$ddir/$f";
729}
730closedir(DIR);
731# Not enough but still a first cleanup
732pb_rm_rf("$dir");
733return(@files);
734}
735
736sub pb_syntax {
737
738 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
739 print "\n";
740 print "Syntax: pb [-vhqt][-r pbroot][-p project][[-s script -a account] -m \"mach-1[,...]\"] <action> [<pkg1>...]\n";
741 print "\n";
742 print "-h : This help file\n";
743 print "-q : Quiet mode\n";
744 print "-t : Test mode (not done yet)\n";
745 print "-v : Verbose mode\n";
746 print "\n";
747 print "-m machine : Name of the Virtual Machines (VM) you want\n";
748 print " to build on (coma separated). All if none precised\n";
749 print " (or use the env variable PBVM) \n";
750 print "\n";
751 print "-s script : Name of the script you want\n";
752 print " to execute on the related VMs.\n";
753 print "\n";
754 print "-a account : Name of the account to use\n";
755 print " to connect on the related VMs.\n";
756 print "\n";
757 print "-p project : Name of the project you're working on\n";
758 print " (or use the env variable PBPROJ) \n";
759 print "\n";
760 print "-r pbroot : Path Name of project under the CMS \n";
761 print " (or use the env variable PBROOT) \n";
762 print "\n";
763 print "<action> can be:\n";
764 print "\n";
765 print "\tcms2build: Create tar files for the project under your CMS\n";
766 print "\t CMS supported are SVN and CVS\n";
767 print "\t parameters are packages to build\n";
768 print "\t if not using default list\n";
769 print "\n";
770 print "\tbuild2pkg: Create packages for your running distribution \n";
771 print "\n";
772 print "\tbuild2ssh: Send the tar files to a SSH host \n";
773 print "\n";
774 print "\tpkg2ssh: Send the packages built to a SSH host \n";
775 print "\n";
776 print "\tcms2pkg: cms2build + build2pkg\n";
777 print "\n";
778 print "\tbuild2vm: Create packages in VMs, launching them if needed\n";
779 print "\t and send those packages to a SSH host once built\n";
780 print "\t VM type supported are QEMU \n";
781 print "\n";
782 print "\tlaunchvm: Launch one virtual machine\n";
783 print "\n";
784 print "\tscript2vm: Launch one virtual machine if needed \n";
785 print "\t and executes a script on it \n";
786 print "\n";
787 print "\tcms2vm: cms2build + build2vm\n";
788 print "\n";
789}
Note: See TracBrowser for help on using the repository browser.