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

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

Fixes 2 syntax issues

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