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

Last change on this file since 285 was 285, checked in by Bruno Cornec, 16 years ago

Adds support of NEWS, AUTHORS, svn|cvs.log, ChangeLog files and changelog info from single files under pbconf

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