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

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

First completely good version for VM management

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