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

Last change on this file since 291 was 291, checked in by Bruno Cornec, 16 years ago
  • Removes AppConfig dependency by using just a perl regexp instead.
  • Interface to the version field has also changed
  • Property svn:executable set to *
File size: 31.8 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 File::Basename;
17use File::Copy;
18use Time::localtime qw(localtime);
19use POSIX qw(strftime);
20
21# Global variables
22use lib qw (lib);
23use ProjectBuilder::Distribution qw (pb_distro_init);
24use ProjectBuilder::Version qw (pb_version_init);
25use 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 pb_cms_isdiff pb_cms_copy pb_cms_checkout);
26
27my %opts; # CLI Options
28my $action; # action to realize
29my $test = "FALSE";
30my $option = "";
31my @pkgs;
32my $pbtag; # Global Tag variable
33my $pbver; # Global Version variable
34my $pbscript; # Name of the script
35my %pbver; # per package
36my %pbtag; # per package
37my $pbrev; # Global REVISION variable
38my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
39my $pbdate = strftime("%Y-%m-%d", @date);
40my $pbdatecvs = strftime("%Y-%m-%d %H:%M:%S", @date);
41my $debug = 0;
42my $pbaccount; # Login to use to connect to the VM
43my $pbport; # Port to use to connect to the VM
44my $newver; # New version to create
45my $iso; # ISO iage for the VM to create
46my $LOG = \*STDOUT;
47
48getopts('a:hi:l:m:P:p:qr:s:tvV:',\%opts);
49
50my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
51if (defined $opts{'h'}) {
52 pb_syntax();
53 exit(0);
54}
55if (defined $opts{'v'}) {
56 $debug++;
57}
58if (defined $opts{'q'}) {
59 $debug=-1;
60}
61if (defined $opts{'l'}) {
62 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
63 $LOG = *LOG;
64 $debug = 0 if ($debug == -1);
65 }
66# Handles test option
67if (defined $opts{'t'}) {
68 $test = "TRUE";
69 # Works only for SVN
70 $option = "-r BASE";
71}
72
73# Handle root of the project if defined
74if (defined $opts{'r'}) {
75 $ENV{'PBROOT'} = $opts{'r'};
76}
77# Handle virtual machines if any
78if (defined $opts{'m'}) {
79 $ENV{'PBVM'} = $opts{'m'};
80}
81if (defined $opts{'s'}) {
82 $pbscript = $opts{'s'};
83}
84if (defined $opts{'a'}) {
85 $pbaccount = $opts{'a'};
86}
87if (defined $opts{'P'}) {
88 $pbport = $opts{'P'};
89}
90if (defined $opts{'V'}) {
91 $newver = $opts{'V'};
92}
93if (defined $opts{'i'}) {
94 $iso = $opts{'i'};
95}
96
97# Get Action
98$action = shift @ARGV;
99die pb_syntax() if (not defined $action);
100
101my ($pbrc, $filteredfiles, $defpkgdir, $extpkgdir);
102my $pbinit = undef;
103$pbinit = 1 if ($action =~ /^newproj$/);
104
105# Handles project name if any
106# And get global params
107if (defined $opts{'p'}) {
108 ($ENV{'PBPROJ'},$debug,$LOG, $pbrc, $filteredfiles, $defpkgdir, $extpkgdir)
109 = pb_env_init($opts{'p'},$pbinit);
110} else {
111 ($ENV{'PBPROJ'},$debug,$LOG, $pbrc, $filteredfiles, $defpkgdir, $extpkgdir)
112 = pb_env_init(undef,$pbinit);
113}
114
115print $LOG "Project: $ENV{'PBPROJ'}\n" if ($debug >= 0);
116print $LOG "Action: $action\n" if ($debug >= 0);
117
118# Keep those project values to store them at the end each time
119my $pbprojtag = $ENV{'PBTAG'};
120my $pbprojver = $ENV{'PBVER'};
121
122# Act depending on action
123if ($action =~ /^cms2build$/) {
124 pb_cms2build();
125} elsif ($action =~ /^build2pkg$/) {
126 pb_build2pkg();
127} elsif ($action =~ /^cms2pkg$/) {
128 pb_cms2build();
129 pb_build2pkg();
130} elsif ($action =~ /^build2ssh$/) {
131 pb_build2ssh();
132} elsif ($action =~ /^cms2ssh$/) {
133 pb_cms2build();
134 pb_build2ssh();
135} elsif ($action =~ /^pkg2ssh$/) {
136 pb_pkg2ssh();
137} elsif ($action =~ /^build2vm$/) {
138 pb_build2vm();
139} elsif ($action =~ /^cms2vm$/) {
140 pb_cms2build();
141 pb_build2vm();
142} elsif ($action =~ /^launchvm$/) {
143 pb_launchvm($ENV{'PBVM'},0);
144} elsif ($action =~ /^script2vm$/) {
145 pb_script2vm($pbscript);
146} elsif ($action =~ /^newver$/) {
147 pb_newver();
148} elsif ($action =~ /^newvm$/) {
149 pb_launchvm($ENV{'PBVM'},1);
150} elsif ($action =~ /^newproj$/) {
151 # Nothing to do - already done in pb_env_init
152} elsif ($action =~ /^clean$/) {
153} else {
154 print $LOG "'$action' is not available\n";
155 pb_syntax();
156}
157
158sub pb_cms2build {
159
160 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
161 @pkgs = @$ptr;
162 my $cms=pb_cms_init($ENV{'PBPROJ'});
163
164 my ($pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb","pkgver","pkgtag");
165
166 # declare packager for filtering
167 my ($tmp) = pb_conf_get("packager");
168 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
169
170 foreach my $pbpkg (@pkgs) {
171 $ENV{'PBPKG'} = $pbpkg;
172 $ENV{'PBVER'} = $pbprojver;
173 $ENV{'PBTAG'} = $pbprojtag;
174 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
175 $pbver = $pkgv->{$pbpkg};
176 $ENV{'PBVER'} = $pbver;
177 } else {
178 $pbver = $ENV{'PBVER'};
179 }
180 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
181 $pbtag = $pkgt->{$pbpkg};
182 $ENV{'PBTAG'} = $pbtag;
183 } else {
184 $pbtag = $ENV{'PBTAG'};
185 }
186
187 $pbrev = $ENV{'PBREVISION'};
188 print $LOG "\n";
189 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n";
190 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
191 # Clean up dest if necessary. The export will recreate it
192 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
193 pb_rm_rf($dest) if (-d $dest);
194
195 # Export CMS tree for the concerned package to dest
196 # And generate some additional files
197 $OUTPUT_AUTOFLUSH=1;
198
199 # computes in which dir we have to work
200 my $dir = $defpkgdir->{$pbpkg};
201 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
202 print "def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n" if ($debug >= 1);
203 pb_cms_export($cms,$pbdatecvs,"$ENV{'PBROOT'}/$dir",$dest);
204
205 # Get project info on authors and log file
206 my $chglog = "$ENV{'PBCONF'}/$pbpkg/pbcl";
207 $chglog = "$ENV{'PBCONF'}/pbcl" if (! -f $chglog);
208 $chglog = undef if (! -f $chglog);
209
210 my $authors = "$ENV{'PBCONF'}/$pbpkg/pbauthors";
211 $authors = "$ENV{'PBCONF'}/pbauthors" if (! -f $authors);
212 $authors = "/dev/null" if (! -f $authors);
213
214 # Extract cms log history and store it
215 if ((defined $chglog) && (! -f "$dest/NEWS")) {
216 print $LOG "Generating NEWS file from $chglog\n";
217 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
218 }
219 pb_cms_log($cms,"$ENV{'PBROOT'}/$dir",$dest,$chglog,$authors);
220
221 my %build;
222
223 my ($ptr) = pb_conf_get("vmlist");
224 foreach my $d (split(/,/,$ptr->{$ENV{'PBPROJ'}})) {
225 my ($name,$ver) = split(/_/,$d);
226 chomp($ver);
227 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
228 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n" if ($debug >= 1);
229 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
230
231 # Filter build files from the less precise up to the most with overloading
232 # Filter all files found, keeping the name, and generating in dest
233
234 # Find all build files first relatively to PBROOT
235 my %bfiles;
236 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
237 $build{"$ddir-$dver"} = "yes";
238 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
239 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
240 foreach my $f (readdir(BDIR)) {
241 next if ($f =~ /^\./);
242 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
243 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
244 }
245 closedir(BDIR);
246 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
247 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
248 foreach my $f (readdir(BDIR)) {
249 next if ($f =~ /^\./);
250 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
251 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
252 }
253 closedir(BDIR);
254 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
255 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
256 foreach my $f (readdir(BDIR)) {
257 next if ($f =~ /^\./);
258 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
259 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
260 }
261 closedir(BDIR);
262 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
263 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
264 foreach my $f (readdir(BDIR)) {
265 next if ($f =~ /^\./);
266 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
267 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
268 }
269 closedir(BDIR);
270 } else {
271 $build{"$ddir-$dver"} = "no";
272 next;
273 }
274 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
275
276 # Get all filters to apply
277 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
278
279 # Apply now all the filters on all the files concerned
280 # destination dir depends on the type of file
281 if (defined $ptr) {
282 foreach my $f (values %bfiles) {
283 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$pbsuf,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$defpkgdir,$extpkgdir,$pbpackager,$chglog);
284 }
285 }
286 }
287 if ($debug >= 0) {
288 my @found;
289 my @notfound;
290 foreach my $b (keys %build) {
291 push @found,$b if ($build{$b} =~ /yes/);
292 push @notfound,$b if ($build{$b} =~ /no/);
293 }
294 print $LOG "Build files generated for ".join(',',@found)."\n";
295 print $LOG "No Build files found for ".join(',',@notfound)."\n";
296 }
297 # Get the generic filter (all.pbf) and
298 # apply those to the non-build files including those
299 # generated by pbinit if applicable
300
301 # Get only all.pbf filter
302 $ptr = pb_get_filters($pbpkg);
303
304 my $liste ="";
305 if (defined $filteredfiles->{$pbpkg}) {
306 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
307 pb_filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f",$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
308 $liste = "$f $liste";
309 }
310 }
311 print $LOG "Files ".$liste."have been filtered\n";
312
313 # Prepare the dest directory for archive
314 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
315 pb_filter_file("$ENV{'PBCONF'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
316 chmod 0755,"$ENV{'PBTMP'}/pbinit";
317 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBCONF'}/$pbpkg/pbinit");
318 }
319
320 # Archive dest dir
321 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
322 # Possibility to look at PBSRC to guess more the filename
323 pb_system("tar cfz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
324 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
325
326 # Keep track of what is generated for default
327 open(LAST,"> $pbrc->{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc->{$ENV{'PBPROJ'}}";
328 print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
329 close(LAST);
330
331 # Keep track of per package version
332 if (! -f "$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") {
333 open(PKG,">$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
334 print PKG "# Empty\n";
335 close(PKG);
336 }
337 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
338 $pkg = { } if (not defined $pkg);
339 if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
340 $pkg->{$pbpkg} = "$pbver-$pbtag";
341 }
342
343 print $LOG "DEBUG pkg: ".Dumper($pkg)."\n" if ($debug >= 1);
344 open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
345 foreach my $p (keys %$pkg) {
346 print PKG "pbpkg $p = $pkg->{$p}\n";
347 }
348 close(PKG);
349
350 # Final cleanup
351 pb_rm_rf($dest) if (-d $dest);
352 }
353}
354
355sub pb_build2pkg {
356
357 # Get list of packages to build
358 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
359 @pkgs = @$ptr;
360
361 # Get the running distro to build on
362 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
363 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
364
365 # Get content saved in cms2build
366 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
367 $pkg = { } if (not defined $pkg);
368
369 # declare packager
370 my ($tmp) = pb_conf_get("packager");
371 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
372
373 chdir "$ENV{'PBBUILDDIR'}";
374 my $made = ""; # pkgs made during build
375 foreach my $pbpkg (@pkgs) {
376 my $vertag = $pkg->{$pbpkg};
377 # get the version of the current package - maybe different
378 ($pbver,$pbtag) = split(/-/,$vertag);
379
380 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
381 print $LOG "Source file: $src\n" if ($debug >= 0);
382
383 print $LOG "Working directory: $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
384 if ($dtype eq "rpm") {
385 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
386 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
387 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";
388 }
389 }
390
391 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
392 # We need to first extract the spec file
393 my @specfile;
394 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
395
396 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
397 # set LANGUAGE to check for correct log messages
398 $ENV{'LANGUAGE'}="C";
399 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
400 foreach my $f (@specfile) {
401 if ($f =~ /\.spec$/) {
402 pb_system("rpmbuild --define \'packager $pbpackager\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}");
403 last;
404 }
405 }
406 $made="$made RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm";
407 if (-f "/usr/bin/rpmlint") {
408 pb_system("rpmlint $made","Checking validity of rpms with rpmlint");
409 }
410 } elsif ($dtype eq "deb") {
411 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
412 pb_system("tar xfz $src","Extracting sources");
413
414 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
415 symlink "pbconf/$ddir-$dver","debian" || die "Unable to symlink to pbconf/$ddir-$dver";
416 chmod 0755,"debian/rules";
417 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package");
418 $made="$made $pbpkg"."_*.deb $pbpkg"."_*.dsc $pbpkg"."_*.tar.gz";
419 } elsif ($dtype eq "ebuild") {
420 my @ebuildfile;
421 # For gentoo we need to take pb as subsystem name
422 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/$pbpkg") if (! -d "$ENV{'PBBUILDDIR'}/portage/pb/$pbpkg");
423
424 # We need to first extract the ebuild file
425 @ebuildfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/portage/pb/$pbpkg");
426
427 # Prepare the build env for gentoo
428 my $found = 0;
429 my $pbbd = $ENV{'PBBUILDDIR'};
430 $pbbd =~ s|/|\\/|g;
431 open(MAKE,"/etc/make.conf") || die "Unable to open /etc/make.conf";
432 while (<MAKE>) {
433 $found = 1 if (/$pbbd\/portage/);
434 }
435 close(MAKE);
436 if ($found == 0) {
437 pb_system("sudo \'echo \"$ENV{'PBBUILDDIR'}/portage\" >> /etc/make.conf\'");
438 }
439 $found = 0;
440 open(KEYW,"/etc/portage/package.keywords") || die "Unable to open /etc/portage/package.keywords";
441 while (<KEYW>) {
442 $found = 1 if (/portage\/pb/);
443 }
444 close(KEYW);
445 if ($found == 0) {
446 pb_system("sudo \'echo \"portage/pb\" >> /etc/portage/package.keywords\'");
447 }
448
449 # Build
450 foreach my $f (@ebuildfile) {
451 if ($f =~ /\.ebuild$/) {
452 pb_system("ebuild $f digest ; ebuild $f package");
453 }
454 }
455 print $LOG "ebuild file: ".Dumper(\@ebuildfile)."\n" if ($debug >= 1);
456
457 $made="$made portage/pb/$pbpkg/$pbpkg-$pbver.ebuild";
458 } elsif ($dtype eq "slackware") {
459 $made="$made build-$pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
460 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
461 } else {
462 die "Unknown dtype format $dtype";
463 }
464 }
465 # Keep track of what is generated so that we can get them back from VMs
466 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
467 print KEEP "$made\n";
468 close(KEEP);
469}
470
471sub pb_build2ssh {
472 pb_send2ssh("Sources");
473}
474
475sub pb_pkg2ssh {
476 pb_send2ssh("Packages");
477}
478
479# By default deliver to the the public site hosting the
480# ftp structure (or whatever) or a VM
481sub pb_send2ssh {
482
483 my $cmt = shift;
484 my $vm = shift || undef;
485 my $vmexist = shift || 0; # 0 is FALSE
486 my $vmpid = shift || 0; # 0 is FALSE
487 my $host = shift || "sshhost";
488 my $login = shift || "sshlogin";
489 my $dir = shift || "sshdir";
490 my $port = shift || "sshport";
491 my $tmout = shift || "vmtmout";
492 my $cmd = "";
493
494 # Get list of packages to build
495 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
496 @pkgs = @$ptr;
497
498 # Get the running distro to consider
499 my ($odir,$over) = (undef, undef);
500 if (defined $vm) {
501 ($odir,$over) = split(/_/,$vm);
502 }
503 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
504 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
505
506 # Get content saved in cms2build
507 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
508 $pkg = { } if (not defined $pkg);
509
510 my $src = "";
511 chdir "$ENV{'PBBUILDDIR'}";
512 foreach my $pbpkg (@pkgs) {
513 my $vertag = $pkg->{$pbpkg};
514 # get the version of the current package - maybe different
515 ($pbver,$pbtag) = split(/-/,$vertag);
516
517 if (($cmt eq "Sources") || ($cmt eq "VMs")) {
518 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
519 if ($cmd eq "") {
520 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
521 } else {
522 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
523 }
524 }
525 }
526 if ($cmt eq "VMs") {
527 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
528 } elsif ($cmt eq "Script") {
529 $src="$src $ENV{'PBDESTDIR'}/pbscript";
530 } elsif ($cmt eq "Packages") {
531 # Get package list from file made during build2pkg
532 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
533 $src = <KEEP>;
534 chomp($src);
535 close(KEEP);
536 if ($dtype eq "rpm") {
537 # Also make a pbscript to generate yum/urpmi bases
538 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
539 } elsif ($dtype eq "deb") {
540 # Also make a pbscript to generate apt bases
541 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
542 }
543 }
544 # Remove potential leading spaces (cause pb with basename)
545 $src =~ s/^ *//;
546 my $basesrc = "";
547 foreach my $i (split(/ +/,$src)) {
548 $basesrc .= " ".basename($i);
549 }
550
551 print $LOG "Sources handled ($cmt): $src\n" if ($debug >= 0);
552 my ($sshhost,$sshlogin,$sshdir,$sshport,$vmtmout,$testver) = pb_conf_get($host,$login,$dir,$port,$tmout,"testver");
553 my $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
554 # Overwrite account value if passed as parameter
555 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
556 my $tdir;
557 my $bdir;
558 if (($cmt eq "Sources") || ($cmt eq "Script")) {
559 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
560 } elsif ($cmt eq "VMs") {
561 $tdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/delivery";
562 $bdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/build";
563 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
564 $bdir =~ s|\$ENV.+\}/||;
565 } elsif ($cmt eq "Packages") {
566 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
567 if (defined $testver->{$ENV{'PBPROJ'}}) {
568 # This is a test pkg => target dir is under test
569 $tdir .= "/test";
570 }
571 } else {
572 return;
573 }
574 my $nport = $sshport->{$ENV{'PBPROJ'}};
575 $nport = "$pbport" if (defined $pbport);
576
577 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
578 $tdir =~ s|\$ENV.+\}/||;
579
580 my $tm = $vmtmout->{$ENV{'PBPROJ'}};
581 pb_system("ssh -q -p $nport $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");
582 pb_system("cd $ENV{'PBBUILDDIR'} ; scp -p -P $nport $src $mac:$tdir 2> /dev/null","$cmt delivery in $tdir on $mac");
583 pb_system("ssh -q -p $nport $mac \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $mac if needed");
584 if ($cmt eq "VMs") {
585 # Get back info on pkg produced, compute their name and get them from the VM
586 pb_system("scp -p -P $nport $mac:$bdir/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $bdir on $mac");
587 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
588 my $src = <KEEP>;
589 chomp($src);
590 close(KEEP);
591 $src =~ s/^ *//;
592 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
593 # Change pgben to make the next send2ssh happy
594 my $made = "";
595 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
596 foreach my $p (split(/ +/,$src)) {
597 my $j = basename($p);
598 pb_system("scp -p -P $nport $mac:\'$bdir/$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $bdir from $mac");
599 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
600 }
601 print KEEP "$made\n";
602 close(KEEP);
603 pb_system("ssh -q -p $nport $mac \"rm -rf $tdir $bdir\"","VM cleanup on $mac");
604 pb_send2ssh("Packages","$odir"."_"."$over");
605 if (! $vmexist) {
606 pb_system("ssh -q -p $nport $mac \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $vm halt (pid $vmpid)");
607 }
608 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
609 }
610}
611
612sub pb_script2vm {
613 my $pbscript=shift;
614
615 # Prepare the script to be executed on the VM
616 # in $ENV{'PBDESTDIR'}/pbscript
617 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
618 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
619 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
620 }
621
622 my ($vm,$all) = pb_get_vm();
623
624 foreach my $v (@$vm) {
625 # Launch the VM
626 my ($vmexist,$vmpid) = pb_launchvm($v,0);
627
628 # Skip that VM if it something went wrong
629 next if (($vmpid == 0) && ($vmexist ==0));
630
631 # Gather all required files to send them to the VM
632 # and launch the build thourgh pbscript
633 pb_send2ssh("Script","$v",$vmexist,$vmpid,"vmhost","vmlogin","pbrc","vmport","vmtmout");
634
635 }
636}
637
638sub pb_launchvm {
639 my $vm = shift;
640 my $create = shift || 0; # By default do not create a VM
641
642 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
643 die "No VM defined, unable to launch" if (not defined $vm);
644 # Keep only the first VM in case many were given
645 $vm =~ s/,.*//;
646
647 # Launch the VMs
648 my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout","vmsize");
649 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
650 if (not defined $ENV{'PBVMOPT'}) {
651 $ENV{'PBVMOPT'} = "";
652 }
653 if (defined $vmopt->{$ENV{'PBPROJ'}}) {
654 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}";
655 }
656 my $nport = $vmport->{$ENV{'PBPROJ'}};
657 $nport = "$pbport" if (defined $pbport);
658
659 my $cmd;
660 my $vmcmd; # has to be used for pb_check_ps
661 my $vmm; # has to be used for pb_check_ps
662 if ($vmtype eq "qemu") {
663 my $arch = `uname -m`;
664 chomp($arch);
665 my $qemucmd32;
666 my $qemucmd64;
667 if ($arch eq "x86_64") {
668 $qemucmd32 = "/usr/bin/qemu-system-i386";
669 $qemucmd64 = "/usr/bin/qemu";
670 } else {
671 $qemucmd32 = "/usr/bin/qemu";
672 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
673 }
674 if ($vm =~ /_64/) {
675 $vmcmd = "$qemucmd64 -no-kqemu";
676 } else {
677 $vmcmd = "$qemucmd32";
678 }
679 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$vm.qemu";
680 if ($create != 0) {
681 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
682 }
683 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
684 } elsif ($vmtype eq "xen") {
685 } elsif ($vmtype eq "vmware") {
686 } else {
687 die "VM of type $vmtype not supported. Report to the dev team";
688 }
689 my ($tmpcmd,$void) = split(/ +/,$cmd);
690 my $vmexist = pb_check_ps($tmpcmd,$vmm);
691 my $vmpid = 0;
692 if (! $vmexist) {
693 if ($create != 0) {
694 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
695 }
696 if (! -f "$vmm") {
697 print "Unable to find VM $vmm\n";
698 } else {
699 pb_system("$cmd &","Launching the VM $vmm");
700 pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $vm to come up");
701 $vmpid = pb_check_ps($tmpcmd,$vmm);
702 }
703 } else {
704 print "Found an existing VM $vmm (pid $vmexist)\n";
705 }
706 return($vmexist,$vmpid);
707}
708
709sub pb_build2vm {
710 # Prepare the script to be executed on the VM
711 # in $ENV{'PBDESTDIR'}/pbscript
712 my ($ntp) = pb_conf_get("vmntp");
713 my $vmntp = $ntp->{$ENV{'PBPROJ'}};
714 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
715 print SCRIPT "#!/bin/bash\n";
716 print SCRIPT "echo ... Execution needed\n";
717 print SCRIPT "# This is in directory delivery\n";
718 print SCRIPT "# Setup the variables required for building\n";
719 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
720 print SCRIPT "# Preparation for pb\n";
721 print SCRIPT "mkdir -p ../pbconf\n";
722 print SCRIPT "mv $ENV{'PBPROJ'}.pb ../pbconf\n";
723 print SCRIPT "mv .pbrc \$HOME\n";
724 print SCRIPT "cd ..\n";
725 # Force new date to be in the future compared to the date of the tar file by adding 1 minute
726 my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
727 $date[1]++;
728 my $upddate = strftime("%m%d%H%M%Y", @date);
729 print SCRIPT "echo Setting up date on $vmntp...\n";
730 # Or use ntpdate if available
731 print SCRIPT "sudo date $upddate\n";
732 print SCRIPT "export PBROOT=\`pwd\`\n";
733 print SCRIPT "# Build\n";
734 my $p = "";
735 $p = join(' ',@ARGV) if (defined $ARGV[0]);
736 print SCRIPT "echo Building packages on VM...\n";
737 print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
738 close(SCRIPT);
739 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
740
741 my ($vm,$all) = pb_get_vm();
742
743 # Send tar files when we do a global generation
744 pb_build2ssh() if ($all == 1);
745
746 foreach my $v (@$vm) {
747 # Launch the VM
748 my ($vmexist,$vmpid) = pb_launchvm($v,0);
749
750 # Skip that VM if it something went wrong
751 next if (($vmpid == 0) && ($vmexist == 0));
752
753 # Gather all required files to send them to the VM
754 # and launch the build thourgh pbscript
755 pb_send2ssh("VMs","$v",$vmexist,$vmpid,"vmhost","vmlogin","pbrc","vmport","vmtmout");
756 }
757}
758
759
760sub pb_newver {
761
762 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
763 my $cms=pb_cms_init($ENV{'PBPROJ'});
764 if ($cms->{$ENV{'PBPROJ'}} ne "svn") {
765 die "Only SVN is supported at the moment";
766 }
767 my $res = pb_cms_isdiff($cms);
768 die "You need to have no differences before creating a new version" if ($res != 0);
769 my $cmsurl = pb_cms_getinfo($cms);
770 my $newurl = dirname($cmsurl)."/$newver";
771 pb_cms_copy($cms,$cmsurl,$newurl);
772 pb_cms_checkout($cms,$newurl,"$ENV{'PBROOT'}/../$newver");
773 my $oldver=basename($cmsurl);
774 open(FILE,"$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb";
775 open(OUT,"> $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new";
776 while(<FILE>) {
777 s/projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
778 print OUT $_;
779 }
780 close(FILE);
781 close(OUT);
782 rename("$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb");
783 pb_cms_checkin($cms,"$ENV{'PBROOT'}/../$newver");
784}
785
786sub pb_get_pkg {
787
788my @pkgs;
789my $defpkgdir = shift;
790my $extpkgdir = shift;
791
792# Get packages list
793if (not defined $ARGV[0]) {
794 @pkgs = keys %$defpkgdir;
795} elsif ($ARGV[0] =~ /^all$/) {
796 @pkgs = keys %$defpkgdir;
797 push(@pkgs, keys %$extpkgdir);
798} else {
799 @pkgs = @ARGV;
800}
801print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
802return(\@pkgs);
803}
804
805#
806# Return the list of VMs we are working on
807# $all is a flag to know if we return all of them
808# or only some (if all we publish also tar files in addition to pkgs
809#
810sub pb_get_vm {
811
812my @vm;
813my $all = 0;
814
815# Get VM list
816if ((not defined $ENV{'PBVM'}) || ($ENV{'PBVM'} =~ /^all$/)) {
817 my ($ptr) = pb_conf_get("vmlist");
818 my $ptr2 = $ptr->{$ENV{'PBPROJ'}};
819 $ptr2 =~ s/,/_64,/g;
820 $ptr2 =~ s/$/_64/g;
821 $ENV{'PBVM'} = "$ptr->{$ENV{'PBPROJ'}},$ptr2";
822 $all = 1;
823}
824print $LOG "VMs: $ENV{'PBVM'}\n";
825@vm = split(/,/,$ENV{'PBVM'});
826return(\@vm,$all);
827}
828
829# Returns the pid of a running VM command using a specific VM file
830sub pb_check_ps {
831 my $vmcmd = shift;
832 my $vmm = shift;
833 my $vmexist = 0; # FALSE by default
834
835 open(PS, "ps auxhww|") || die "Unable to call ps";
836 while (<PS>) {
837 next if (! /$vmcmd/);
838 next if (! /$vmm/);
839 my ($void1, $void2);
840 ($void1, $vmexist, $void2) = split(/ +/);
841 last;
842 }
843 return($vmexist);
844}
845
846
847sub pb_extract_build_files {
848
849my $src=shift;
850my $dir=shift;
851my $ddir=shift;
852my @files;
853
854if ($src =~ /tar\.gz$/) {
855 pb_system("tar xfpz $src $dir","Extracting build files");
856} elsif ($src =~ /tar\.bz2$/) {
857 pb_system("tar xfpj $src $dir","Extracting build files");
858} else {
859 die "Unknown compression algorithm for $src";
860}
861opendir(DIR,"$dir") || die "Unable to open directory $dir";
862foreach my $f (readdir(DIR)) {
863 next if ($f =~ /^\./);
864 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
865 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
866 push @files,"$ddir/$f";
867}
868closedir(DIR);
869# Not enough but still a first cleanup
870pb_rm_rf("$dir");
871return(@files);
872}
873
874sub pb_syntax {
875
876 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
877 print "\n";
878 print "Syntax: pb [-vhqt][-r pbroot][-p project][[-s script -a account -P port] -m \"mach-1[,...]\"][-i iso] <action> [<pkg1>...]\n";
879 print "\n";
880 print "-h : This help file\n";
881 print "-q : Quiet mode\n";
882 print "-t : Test mode (not done yet)\n";
883 print "-v : Verbose mode\n";
884 print "\n";
885 print "-m machine : Name of the Virtual Machines (VM) you want\n";
886 print " to build on (coma separated). All if none precised\n";
887 print " (or use the env variable PBVM) \n";
888 print "\n";
889 print "-s script : Name of the script you want\n";
890 print " to execute on the related VMs.\n";
891 print "\n";
892 print "-i iso : Name of the ISO image of the distribution you want\n";
893 print " to install on the related VMs.\n";
894 print "\n";
895 print "-a account : Name of the account to use\n";
896 print " to connect on the related VMs.\n";
897 print "\n";
898 print "-P port : Number of the port to use\n";
899 print " to connect on the related VMs.\n";
900 print "\n";
901 print "-p project : Name of the project you're working on\n";
902 print " (or use the env variable PBPROJ) \n";
903 print "\n";
904 print "-r pbroot : Path Name of project under the CMS \n";
905 print " (or use the env variable PBROOT) \n";
906 print "\n";
907 print "-V newver : New version of the project to create\n";
908 print " from the current one. \n";
909 print "\n";
910 print "<action> can be:\n";
911 print "\n";
912 print "\tcms2build: Create tar files for the project under your CMS\n";
913 print "\t CMS supported are SVN and CVS\n";
914 print "\t parameters are packages to build\n";
915 print "\t if not using default list\n";
916 print "\n";
917 print "\tbuild2pkg: Create packages for your running distribution \n";
918 print "\n";
919 print "\tcms2pkg: cms2build + build2pkg\n";
920 print "\n";
921 print "\tbuild2ssh: Send the tar files to a SSH host \n";
922 print "\n";
923 print "\tcms2ssh: cms2build + build2ssh\n";
924 print "\n";
925 print "\tpkg2ssh: Send the packages built to a SSH host \n";
926 print "\n";
927 print "\tbuild2vm: Create packages in VMs, launching them if needed\n";
928 print "\t and send those packages to a SSH host once built\n";
929 print "\t VM type supported are QEMU \n";
930 print "\n";
931 print "\tcms2vm: cms2build + build2vm\n";
932 print "\n";
933 print "\tlaunchvm: Launch one virtual machine\n";
934 print "\n";
935 print "\tscript2vm: Launch one virtual machine if needed \n";
936 print "\t and executes a script on it \n";
937 print "\n";
938 print "\tnewvm: Create a new virtual machine\n";
939 print "\n";
940 print "\tnewver: Create a new version of the project derived \n";
941 print "\t from the current one \n";
942 print "\n";
943 print "\tnewproj: Create a new project and a template set of \n";
944 print "\t configuration files under pbconf \n";
945 print "\n";
946}
Note: See TracBrowser for help on using the repository browser.