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

Last change on this file since 873 was 873, checked in by Bruno Cornec, 15 years ago

First round of Solaris fixes

  • Property svn:executable set to *
File size: 93.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::Long qw(:config auto_abbrev no_ignore_case);
14use Data::Dumper;
15use English;
16use File::Basename;
17use File::Copy;
18use File::stat;
19use File::Temp qw(tempdir);
20use File::Find;
21use Time::localtime qw(localtime);
22use POSIX qw(strftime);
23use lib qw (lib);
24use ProjectBuilder::Version;
25use ProjectBuilder::Base;
26use ProjectBuilder::Display;
27use ProjectBuilder::Conf;
28use ProjectBuilder::Distribution;
29use ProjectBuilder::CMS;
30use ProjectBuilder::Env;
31use ProjectBuilder::Filter;
32use ProjectBuilder::Changelog;
33use Mail::Sendmail;
34
35# Global variables
36my %opts; # CLI Options
37my $action; # action to realize
38my $test = "FALSE"; # Not used
39my $pbforce = 0; # Force VE/VM rebuild
40my $pbsnap = 0; # Use snapshot mode for VM/VE
41my $option = ""; # Not used
42my @pkgs; # list of packages
43my $pbtag; # Global Tag variable
44my $pbver; # Global Version variable
45my $pbscript; # Name of the script
46my %pbver; # per package
47my %pbtag; # per package
48my $pbrev; # Global REVISION variable
49my $pbaccount; # Login to use to connect to the VM
50my $pbport; # Port to use to connect to the VM
51my $newver; # New version to create
52my $iso = undef; # ISO image for the VM to create
53
54my @date = pb_get_date();
55my $pbdate = strftime("%Y-%m-%d", @date);
56
57=pod
58
59=head1 NAME
60
61pb, aka project-builder.org - builds packages for your projects
62
63=head1 DESCRIPTION
64
65pb helps you build various packages directly from your project sources.
66Those sources could be handled by a CMS (Configuration Management System)
67such as Subversion, CVS, Git, Mercurial... or being a simple reference to a compressed tar file.
68It's based on a set of configuration files, a set of provided macros to help
69you keeping build files as generic as possible. For example, a single .spec
70file should be required to generate for all rpm based distributions, even
71if you could also have multiple .spec files if required.
72
73=head1 SYNOPSIS
74
75pb [-vhSq][-r pbroot][-p project][[-s script -a account -P port][-m mach-1[,...]]][-i iso] <action> [<pkg1> ...]
76
77pb [--verbose][--help][--man][--quiet][--snapshot][--revision pbroot][--project project][[--script script --account account --port port][--machine mach-1[,...]]][--iso iso] <action> [<pkg1> ...]
78
79=head1 OPTIONS
80
81=over 4
82
83=item B<-v|--verbose>
84
85Print a brief help message and exits.
86
87=item B<-q|--quiet>
88
89Do not print any output.
90
91=item B<-h|--help>
92
93Print a brief help message and exits.
94
95=item B<-S|--snapshot>
96
97Use the snapshot mode of VMs or VEs
98
99=item B<--man>
100
101Prints the manual page and exits.
102
103=item B<-m|--machine machine1[,machine2,...]>
104
105Name of the Virtual Machines (VM) or Virtual Environments (VE) you want to build on (coma separated).
106All if none precised (or use the env variable PBV).
107
108=item B<-s|--script script>
109
110Name of the script you want to execute on the related VMs or VEs.
111
112=item B<-i|--iso iso_image>
113
114Name of the ISO image of the distribution you want to install on the related VMs.
115
116=item B<-a|--account account>
117
118Name of the account to use to connect on the related VMs.
119
120=item B<-P|--port port_number>
121
122Port number to use to connect on the related VMs.\n";
123
124=item B<-p|--project project_name>
125
126Name of the project you're working on (or use the env variable PBPROJ)
127
128=item B<-r|--revision revision>
129
130Path Name of the project revision under the CMS (or use the env variable PBROOT)
131
132=item B<-V|--version new_version>
133
134New version of the project to create based on the current one.
135
136=back
137
138=head1 ARGUMENTS
139
140<action> can be:
141
142=over 4
143
144=item B<cms2build>
145
146Create tar files for the project under your CMS.
147CMS supported are SVN, CVS and Mercurial
148parameters are packages to build
149if not using default list
150
151=item B<build2pkg>
152
153Create packages for your running distribution
154
155=item B<cms2pkg>
156
157cms2build + build2pkg
158
159=item B<build2ssh>
160
161Send the tar files to a SSH host
162
163=item B<cms2ssh>
164
165cms2build + build2ssh
166
167=item B<pkg2ssh>
168
169Send the packages built to a SSH host
170
171=item B<build2vm>
172
173Create packages in VMs, launching them if needed
174and send those packages to a SSH host once built
175VM type supported are QEMU
176
177=item B<build2ve>
178
179Create packages in VEs, creating it if needed
180and send those packages to a SSH host once built
181
182=item B<cms2vm>
183
184cms2build + build2vm
185
186=item B<cms2ve>
187
188cms2build + build2ve
189
190=item B<launchvm>
191
192Launch one virtual machine
193
194=item B<launchve>
195
196Launch one virtual environment
197
198=item B<script2vm>
199
200Launch one virtual machine if needed
201and executes a script on it
202
203=item B<script2ve>
204
205Execute a script in a virtual environment
206
207=item B<newvm>
208
209Create a new virtual machine
210
211=item B<newve>
212
213Create a new virtual environment
214
215=item B<setupvm>
216
217Setup a virtual machine for pb usage
218
219=item B<setupve>
220
221Setup a virtual environment for pb usage
222
223=item B<snapvm>
224
225Snapshot a virtual machine for pb usage
226
227=item B<snapve>
228
229Snapshot a virtual environment for pb usage
230
231=item B<test2pkg>
232
233Test a package locally
234
235=item B<test2vm>
236
237Test a package in a virtual machine
238
239=item B<test2ve>
240
241Test a package in a virtual environment
242
243=item B<newver>
244
245Create a new version of the project derived
246from the current one
247
248=item B<newproj>
249
250Create a new project and a template set of
251configuration files under pbconf
252
253=item B<announce>
254
255Announce the availability of the project through various means
256
257=item B<web2ssh>
258
259Deliver the Web site content to the target server using ssh.
260
261=item B<clean>
262
263Purge the build and delivery directories related to the current project
264
265=back
266
267<pkgs> can be a list of packages, the keyword 'all' or nothing, in which case the default list of packages is taken (corresponding to the defpkgdir list of arguments in the configuration file).
268
269=head1 WEB SITES
270
271The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
272
273=head1 USER MAILING LIST
274
275None exists for the moment.
276
277=head1 CONFIGURATION FILES
278
279Each pb user may have a configuration in F<$HOME/.pbrc>. The values in this file may overwrite any other configuration file value.
280
281Here is an example of such a configuration file:
282
283 #
284 # Define for each project the URL of its pbconf repository
285 # No default option allowed here as they need to be all different
286 #
287 # URL of the pbconf content
288 # This is the format of a classical URL with the extension of additional schema such as
289 # svn+ssh, cvs+ssh, ...
290 #
291 pbconfurl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe/pbconf
292
293 # This is normaly defined in the project's configuration file
294 # Url of the project
295 #
296 pburl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe
297
298 # All these URLs needs to be defined here as the are the entry point
299 # for how to build packages for the project
300 #
301 pbconfurl pb = svn+ssh://svn.project-builder.org/mondo/svn/pb/pbconf
302 pbconfurl mondorescue = svn+ssh://svn.project-builder.org/mondo/svn/project-builder/mondorescue/pbconf
303 pbconfurl collectl = svn+ssh://bruno@svn.mondorescue.org/mondo/svn/project-builder/collectl/pbconf
304 pbconfurl netperf = svn+ssh://svn.mondorescue.org/mondo/svn/project-builder/netperf/pbconf
305
306 # Under that dir will take place everything related to pb
307 # If you want to use VMs/chroot/..., then use $ENV{'HOME'} to make it portable
308 # to your VMs/chroot/...
309 # if not defined then /var/cache
310 pbdefdir default = $ENV{'HOME'}/project-builder
311 pbdefdir pb = $ENV{'HOME'}
312 pbdefdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
313 pbdefdir mondorescue = $ENV{'HOME'}/mondo/svn
314
315 # pbconfdir points to the directory where the CMS content of the pbconfurl is checked out
316 # If not defined, pbconfdir is under pbdefdir/pbproj/pbconf
317 pbconfdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs/pbconf
318 pbconfdir mondorescue = $ENV{'HOME'}/mondo/svn/pbconf
319
320 # pbdir points to the directory where the CMS content of the pburl is checked out
321 # If not defined, pbdir is under pbdefdir/pbproj
322 # Only defined if we have access to the dev of the project
323 pbdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
324 pbdir mondorescue = $ENV{'HOME'}/mondo/svn
325
326 # -daemonize doesn't work with qemu 0.8.2
327 vmopt default = -m 384
328
329=head1 AUTHORS
330
331The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
332
333=head1 COPYRIGHT
334
335Project-Builder.org is distributed under the GPL v2.0 license
336described in the file C<COPYING> included with the distribution.
337
338=cut
339
340# ---------------------------------------------------------------------------
341
342my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
343
344# Initialize the syntax string
345
346pb_syntax_init("pb (aka project-builder.org) Version $projectbuilderver-$projectbuilderrev\n");
347
348GetOptions("help|?|h" => \$opts{'h'},
349 "man" => \$opts{'man'},
350 "verbose|v+" => \$opts{'v'},
351 "snapshot|S" => \$opts{'S'},
352 "quiet|q" => \$opts{'q'},
353 "log-files|l=s" => \$opts{'l'},
354 "force|f" => \$opts{'f'},
355 "account|a=s" => \$opts{'a'},
356 "revision|r=s" => \$opts{'r'},
357 "script|s=s" => \$opts{'s'},
358 "machines|mock|m=s" => \$opts{'m'},
359 "port|P=i" => \$opts{'P'},
360 "project|p=s" => \$opts{'p'},
361 "iso|i=s" => \$opts{'i'},
362 "version|V=s" => \$opts{'V'},
363) || pb_syntax(-1,0);
364
365if (defined $opts{'h'}) {
366 pb_syntax(0,1);
367}
368if (defined $opts{'man'}) {
369 pb_syntax(0,2);
370}
371if (defined $opts{'v'}) {
372 $pbdebug = $opts{'v'};
373}
374if (defined $opts{'f'}) {
375 $pbforce=1;
376}
377if (defined $opts{'q'}) {
378 $pbdebug=-1;
379}
380if (defined $opts{'S'}) {
381 $pbsnap=1;
382}
383if (defined $opts{'l'}) {
384 open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
385 $pbLOG = \*pbLOG;
386 $pbdebug = 0 if ($pbdebug == -1);
387 }
388pb_log_init($pbdebug, $pbLOG);
389pb_display_init("text","");
390
391# Handle root of the project if defined
392if (defined $opts{'r'}) {
393 $ENV{'PBROOTDIR'} = $opts{'r'};
394}
395# Handle virtual machines if any
396if (defined $opts{'m'}) {
397 $ENV{'PBV'} = $opts{'m'};
398}
399if (defined $opts{'s'}) {
400 $pbscript = $opts{'s'};
401}
402if (defined $opts{'a'}) {
403 $pbaccount = $opts{'a'};
404 die "option -a requires a -s script option" if (not defined $pbscript);
405}
406if (defined $opts{'P'}) {
407 $pbport = $opts{'P'};
408}
409if (defined $opts{'V'}) {
410 $newver = $opts{'V'};
411}
412if (defined $opts{'i'}) {
413 $iso = $opts{'i'};
414}
415
416# Get Action
417$action = shift @ARGV;
418die pb_syntax(-1,1) if (not defined $action);
419
420my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
421my $pbinit = undef;
422$pbinit = 1 if ($action =~ /^newproj$/);
423
424# Handles project name if any
425# And get global params
426($filteredfiles, $supfiles, $defpkgdir, $extpkgdir) = pb_env_init($opts{'p'},$pbinit,$action);
427
428pb_log(0,"Project: $ENV{'PBPROJ'}\n");
429pb_log(0,"Action: $action\n");
430
431# Act depending on action
432if ($action =~ /^cms2build$/) {
433 pb_cms2build();
434} elsif ($action =~ /^build2pkg$/) {
435 pb_build2pkg();
436} elsif ($action =~ /^cms2pkg$/) {
437 pb_cms2build();
438 pb_build2pkg();
439} elsif ($action =~ /^build2ssh$/) {
440 pb_build2ssh();
441} elsif ($action =~ /^cms2ssh$/) {
442 pb_cms2build();
443 pb_build2ssh();
444} elsif ($action =~ /^pkg2ssh$/) {
445 pb_pkg2ssh();
446} elsif ($action =~ /^build2ve$/) {
447 pb_build2v("ve","build");
448} elsif ($action =~ /^build2vm$/) {
449 pb_build2v("vm","build");
450} elsif ($action =~ /^cms2ve$/) {
451 pb_cms2build();
452 pb_build2v("ve","build");
453} elsif ($action =~ /^cms2vm$/) {
454 pb_cms2build();
455 pb_build2v("vm","build");
456} elsif ($action =~ /^launchvm$/) {
457 pb_launchv("vm",$ENV{'PBV'},0);
458} elsif ($action =~ /^launchve$/) {
459 pb_launchv("ve",$ENV{'PBV'},0);
460} elsif ($action =~ /^script2vm$/) {
461 pb_script2v($pbscript,"vm");
462} elsif ($action =~ /^script2ve$/) {
463 pb_script2v($pbscript,"ve");
464} elsif ($action =~ /^newver$/) {
465 pb_newver();
466} elsif ($action =~ /^newve$/) {
467 pb_launchv("ve",$ENV{'PBV'},1);
468} elsif ($action =~ /^newvm$/) {
469 pb_launchv("vm",$ENV{'PBV'},1);
470} elsif ($action =~ /^setupve$/) {
471 pb_setup2v("ve");
472} elsif ($action =~ /^setupvm$/) {
473 pb_setup2v("vm");
474} elsif ($action =~ /^snapve$/) {
475 pb_snap2v("ve");
476} elsif ($action =~ /^snapvm$/) {
477 pb_snap2v("vm");
478} elsif ($action =~ /^test2pkg$/) {
479 pb_test2pkg();
480} elsif ($action =~ /^test2ve$/) {
481 pb_build2v("ve","test");
482} elsif ($action =~ /^test2vm$/) {
483 pb_build2v("vm","test");
484} elsif ($action =~ /^newproj$/) {
485 # Nothing to do - already done in pb_env_init
486} elsif ($action =~ /^clean$/) {
487 pb_clean();
488} elsif ($action =~ /^announce$/) {
489 # For announce only. Require avoids the systematic load of these modules
490 require DBI;
491 require DBD::SQLite;
492
493 pb_announce();
494} elsif ($action =~ /^web2ssh$/) {
495 require DBI;
496 require DBD::SQLite;
497
498 pb_cms2build("Web");
499 pb_send2target("Web");
500} else {
501 pb_log(0,"\'$action\' is not available\n");
502 pb_syntax(-2,1);
503}
504
505sub pb_cms2build {
506
507 my $param = shift || undef;
508
509 my $pkg;
510 my @pkgs;
511 my $webdir;
512
513 my %pkgs;
514 my %pb; # Structure to store conf info
515
516 # If Website, then pkg is only the website
517 if ((defined $param) && ($param eq "Web")) {
518 ($webdir) = pb_conf_get("webdir");
519 pb_log(2,"webdir: ".Dumper($webdir)."\n");
520 $pkgs[0] = $webdir->{$ENV{'PBPROJ'}};
521 $extpkgdir = $webdir;
522 pb_log(0,"Package: $pkgs[0]\n");
523 } else {
524 $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
525 @pkgs = @$pkg;
526 }
527
528 my ($scheme, $uri) = pb_cms_init($pbinit);
529
530 my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
531
532 # declare packager and repo for filtering
533 my ($tmp1, $tmp2) = pb_conf_get("pbpackager","pbrepo");
534 $ENV{'PBPACKAGER'} = $tmp1->{$ENV{'PBPROJ'}};
535 $ENV{'PBREPO'} = $tmp2->{$ENV{'PBPROJ'}};
536
537 foreach my $pbpkg (@pkgs) {
538 my ($testver) = pb_conf_get_if("testver");
539 $ENV{'PBPKG'} = $pbpkg;
540
541 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
542 $pbver = $pkgv->{$pbpkg};
543 } else {
544 $pbver = $ENV{'PBPROJVER'};
545 }
546 # If it's a test version, then tag == 0.date
547 if (defined $testver->{$ENV{'PBPROJ'}}) {
548 $pbtag = "0.".strftime("%Y%m%d%H%M%S", @date);
549 $ENV{'PBPROJTAG'} = $pbtag;
550 } elsif ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
551 $pbtag = $pkgt->{$pbpkg};
552 } else {
553 $pbtag = $ENV{'PBPROJTAG'};
554 }
555
556 $pbrev = $ENV{'PBREVISION'};
557 pb_log(0,"\n");
558 pb_log(0,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
559 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
560
561 # Clean up dest if necessary. The export will recreate it
562 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
563 pb_rm_rf($dest) if (-d $dest);
564
565 # Export CMS tree for the concerned package to dest
566 # And generate some additional files
567 $OUTPUT_AUTOFLUSH=1;
568
569 # computes in which dir we have to work
570 my $dir = $defpkgdir->{$pbpkg};
571 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
572 $dir = $webdir->{$ENV{'PBPROJ'}} if ((defined $param) && ($param eq "Web"));
573 pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");
574
575 # Exporting content from CMS
576 my $preserve = pb_cms_export($uri,"$ENV{'PBDIR'}/$dir",$dest);
577
578 # Generated fake content for test versions to speed up stuff
579 my $chglog;
580
581 # Get project info on authors and log file
582 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
583 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
584 $chglog = undef if (! -f $chglog);
585
586 my $authors = "$ENV{'PBROOTDIR'}/$pbpkg/pbauthors";
587 $authors = "$ENV{'PBROOTDIR'}/pbauthors" if (! -f $authors);
588 $authors = "/dev/null" if (! -f $authors);
589
590 # Extract cms log history and store it
591 if ((defined $chglog) && (! -f "$dest/NEWS")) {
592 pb_log(2,"Generating NEWS file from $chglog\n");
593 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
594 }
595 pb_cms_log($scheme,"$ENV{'PBDIR'}/$dir",$dest,$chglog,$authors,$testver);
596
597 my %build;
598 my @pt;
599 # We want to at least build for the underlying distro
600 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
601 my $tmpl = "$ddir-$dver-$arch,";
602 my %patches;
603
604 # Get list of distributions for which I need to generate build files
605 @pt = pb_conf_get_if("vmlist","velist");
606 if (defined $pt[0]->{$ENV{'PBPROJ'}}) {
607 $tmpl .= $pt[0]->{$ENV{'PBPROJ'}};
608 }
609 if (defined $pt[1]->{$ENV{'PBPROJ'}}) {
610 # the 2 lists needs to be grouped with a ',' separating them
611 if ($tmpl ne "") {
612 $tmpl .= ",";
613 }
614 $tmpl .= $pt[1]->{$ENV{'PBPROJ'}}
615 }
616
617 # Setup %pb structure to allow filtering later on, on files using that structure
618 $pb{'tag'} = $pbtag;
619 $pb{'rev'} = $pbrev;
620 $pb{'ver'} = $pbver;
621 $pb{'pkg'} = $pbpkg;
622 $pb{'date'} = $pbdate;
623 $pb{'defpkgdir'} = $defpkgdir;
624 $pb{'extpkgdir'} = $extpkgdir;
625 $pb{'chglog'} = $chglog;
626 $pb{'packager'} = $ENV{'PBPACKAGER'};
627 $pb{'proj'} = $ENV{'PBPROJ'};
628 $pb{'repo'} = $ENV{'PBREPO'};
629 $pb{'patches'} = \%patches;
630 pb_log(2,"DEBUG: pb: ".Dumper(%pb)."\n");
631
632 # Do not do that for website
633 if ((not defined $param) || ($param ne "Web")) {
634 my %virt;
635 # De-duplicate similar VM and VE
636 foreach my $d (split(/,/,$tmpl)) {
637 # skip ill-formatted vms (name-ver-arch)
638 next if ($d !~ /-/);
639 $virt{$d} = $d;
640 }
641
642 foreach my $d (keys %virt) {
643 my ($name,$ver,$arch) = split(/-/,$d);
644 pb_log(0,"Bad format for $d") if ((not defined $name) || (not defined $ver) || (not defined $arch)) ;
645 chomp($arch);
646 my ($ddir, $dver, $dfam);
647 ($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'}, $pb{'upd'}, $pb{'arch'}) = pb_distro_init($name,$ver,$arch);
648 pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'})."\n");
649 pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
650
651 # We need to compute the real name of the package
652 my $pbrealpkg = pb_cms_get_real_pkg($pbpkg,$pb{'dtype'});
653 $pb{'realpkg'} = $pbrealpkg;
654 pb_log(1,"Virtual package $pbpkg has a real package name of $pbrealpkg on $ddir-$dver\n") if ($pbrealpkg ne $pbpkg);
655
656 # Filter build files from the less precise up to the most with overloading
657 # Filter all files found, keeping the name, and generating in dest
658
659 # Find all build files first relatively to PBROOTDIR
660 # Find also all specific files referenced in the .pb conf file
661 my %bfiles = ();
662 my %pkgfiles = ();
663 $build{"$ddir-$dver-$arch"} = "yes";
664
665 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}") {
666 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
667 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$dfam") {
668 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
669 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir") {
670 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
671 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver") {
672 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
673 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch") {
674 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
675 } else {
676 $build{"$ddir-$dver-$arch"} = "no";
677 next;
678 }
679 pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");
680
681 # Get all filters to apply
682 my $ptr = pb_get_filters($pbpkg, $pb{'dtype'}, $dfam, $ddir, $dver);
683
684 # Prepare local patches for this distro - They are always applied first - May be a problem one day
685 foreach my $p (sort(<$ENV{'PBROOTDIR'}/$pbpkg/pbpatch/*>)) {
686 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.all$/));
687 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.all$/);
688 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$pb{'dtype'}$/));
689 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$pb{'dtype'}$/);
690 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$dfam$/));
691 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$dfam$/);
692 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir$/));
693 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir$/);
694 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver$/));
695 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver$/);
696 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver-$arch$/));
697 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver-$arch$/);
698 }
699
700 # Prepare also remote patches to be included - Applied after the local ones
701 foreach my $p ("all","$pb{'dtype'}","$dfam","$ddir","$ddir-$dver","$ddir-$dver-$arch") {
702 my $f = "$ENV{'PBROOTDIR'}/$pbpkg/pbextpatch.$p";
703 next if (not -f $f);
704 if (not open(PATCH,$f)) {
705 pb_display("Unable to open existing external patch file content $f\n");
706 next;
707 }
708 while (<PATCH>) {
709 chomp();
710 $patches{"$ddir-$dver-$arch"} .= "," if (defined $patches{"$ddir-$dver-$arch"});
711 $patches{"$ddir-$dver-$arch"} .= "$_";
712 }
713 close(PATCH);
714 }
715 pb_log(2,"DEBUG: pb->patches: ".Dumper($pb{'patches'})."\n");
716
717 # Apply now all the filters on all the files concerned
718 # destination dir depends on the type of file
719 if (defined $ptr) {
720 # For patch support
721 $pb{'tuple'} = "$ddir-$dver-$arch";
722 foreach my $f (values %bfiles,values %pkgfiles) {
723 pb_filter_file("$ENV{'PBROOTDIR'}/$f",$ptr,"$dest/pbconf/$ddir-$dver-$arch/".basename($f),\%pb);
724 }
725 }
726 }
727 my @found;
728 my @notfound;
729 foreach my $b (keys %build) {
730 push @found,$b if ($build{$b} =~ /yes/);
731 push @notfound,$b if ($build{$b} =~ /no/);
732 }
733 pb_log(0,"Build files generated for ".join(',',sort(@found))."\n");
734 pb_log(0,"No Build files found for ".join(',',sort(@notfound))."\n") if (@notfound);
735 pb_log(2,"DEBUG: patches: ".Dumper(%patches)."\n");
736 }
737
738 # Get the generic filter (all.pbf) and
739 # apply those to the non-build files including those
740 # generated by pbinit if applicable
741
742 # Get only all.pbf filter
743 my $ptr = pb_get_filters($pbpkg);
744
745 my $liste ="";
746 if (defined $filteredfiles->{$pbpkg}) {
747 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
748 pb_filter_file_inplace($ptr,"$dest/$f",\%pb);
749 $liste = "$f $liste";
750 }
751 }
752 pb_log(2,"Files ".$liste."have been filtered\n");
753
754 # Do not do that for website
755 if ((not defined $param) || ($param ne "Web")) {
756 my %tmp;
757 # Filter potential patches (local + remote)
758 pb_log(0,"Delivering and compressing patches ");
759 foreach my $v (keys %patches) {
760 pb_mkdir_p("$dest/pbconf/$v/pbpatch");
761 foreach my $pf (split(/,/,$patches{$v})) {
762 my $pp = basename($pf);
763 pb_cms_export($pf,undef,"$dest/pbconf/$v/pbpatch");
764 pb_filter_file_inplace($ptr,"$dest/pbconf/$v/pbpatch/$pp",\%pb);
765 pb_system("gzip -9f $dest/pbconf/$v/pbpatch/$pp","","quiet");
766 $tmp{$pf} = "";
767 }
768 }
769 foreach my $v (keys %tmp) {
770 pb_log(0,"$v ");
771 }
772 pb_log(0,"\n");
773 } else {
774 # Instead call News generation
775 pb_web_news2html($dest);
776 # And create an empty pbconf
777 pb_mkdir_p("$dest/pbconf");
778 # And prepare the pbscript to execute remotely
779 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
780 print SCRIPT "#!/bin/bash\n";
781 print SCRIPT "#set -x\n";
782 print SCRIPT "echo ... Extracting Website content\n";
783 print SCRIPT "find . -type f | grep -Ev '^./$pbpkg-$pbver.tar.gz|^./pbscript' | xargs rm -f non-existent\n";
784 print SCRIPT "find * -type d -depth | xargs rmdir 2> /dev/null \n";
785 print SCRIPT "tar xfz $pbpkg-$pbver.tar.gz\n";
786 print SCRIPT "mv $pbpkg-$pbver/* .\n";
787 print SCRIPT "rm -f $pbpkg-$pbver.tar.gz\n";
788 print SCRIPT "rmdir $pbpkg-$pbver\n";
789 close(SCRIPT);
790 }
791
792 # Prepare the dest directory for archive
793 if (-x "$ENV{'PBROOTDIR'}/$pbpkg/pbinit") {
794 pb_filter_file("$ENV{'PBROOTDIR'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",\%pb);
795 chmod 0755,"$ENV{'PBTMP'}/pbinit";
796 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOTDIR'}/$pbpkg/pbinit","verbose");
797 }
798
799 # Do we have additional script to run to prepare the environement for the project ?
800 # Then include it in the pbconf delivery
801 foreach my $pbvf (<$ENV{'PBROOTDIR'}/pbv*.pre>,<$ENV{'PBROOTDIR'}/pbv*.post>, <$ENV{'PBROOTDIR'}/pbtest*>) {
802 if (-x "$pbvf") {
803 my $target = "$ENV{'PBDESTDIR'}/".basename($pbvf);
804 pb_filter_file("$pbvf",$ptr,$target,\%pb);
805 chmod 0755,"$target";
806 }
807 }
808
809 # Archive dest dir
810 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
811 if (defined $preserve) {
812 # In that case we want to preserve the original tar file for checksum purposes
813 # The one created is btw equivalent in that case to this one
814 # Maybe check basename of both to be sure they are the same ?
815 pb_log(0,"Preserving original tar file ");
816 move("$preserve","$pbpkg-$pbver.tar.gz");
817 } else {
818 # Possibility to look at PBSRC to guess more the filename
819 pb_system("tar cfz $pbpkg-$pbver.tar.gz --exclude=$pbpkg-$pbver/pbconf $pbpkg-$pbver","Creating $pbpkg tar files compressed");
820 }
821 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");
822 pb_system("tar cfz $pbpkg-$pbver.pbconf.tar.gz $pbpkg-$pbver/pbconf","Creating pbconf tar files compressed");
823 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz\n");
824
825 # Keep track of version-tag per pkg
826 $pkgs{$pbpkg} = "$pbver-$pbtag";
827
828 # Final cleanup
829 pb_rm_rf($dest) if (-d $dest);
830 }
831
832 # Keep track of per package version
833 pb_log(2,"DEBUG pkgs: ".Dumper(%pkgs)."\n");
834 open(PKG,"> $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb";
835 foreach my $pbpkg (keys %pkgs) {
836 print PKG "pbpkg $pbpkg = $pkgs{$pbpkg}\n";
837 }
838 close(PKG);
839
840 # Keep track of what is generated by default
841 # We need to store the dir and info on version-tag
842 # Base our content on the existing .pb file
843 copy("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb","$ENV{'PBDESTDIR'}/pbrc");
844 open(LAST,">> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
845 print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOTDIR'}\n";
846 print LAST "projver $ENV{'PBPROJ'} = $ENV{'PBPROJVER'}\n";
847 print LAST "projtag $ENV{'PBPROJ'} = $ENV{'PBPROJTAG'}\n";
848 print LAST "pbpackager $ENV{'PBPROJ'} = $ENV{'PBPACKAGER'}\n";
849 close(LAST);
850}
851
852sub pb_test2pkg {
853 # Get the running distro to test on
854 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
855 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch))."\n");
856
857 # Get list of packages to test
858 # Get content saved in cms2build
859 my $ptr = pb_get_pkg();
860 @pkgs = @$ptr;
861
862 # Additional potential repo
863 pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
864 foreach my $pbpkg (@pkgs) {
865 # We need to install the package to test, and deps brought with it
866 pb_distro_installdeps(undef,$dtype,$pbupd,$pbpkg);
867 pb_system("$ENV{'PBDESTDIR'}/pbtest","Launching test for $pbpkg","verbose");
868 }
869}
870
871sub pb_build2pkg {
872
873 # Get the running distro to build on
874 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
875 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch))."\n");
876
877 # Get list of packages to build
878 # Get content saved in cms2build
879 my $ptr = pb_get_pkg();
880 @pkgs = @$ptr;
881
882 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
883 $pkg = { } if (not defined $pkg);
884
885 chdir "$ENV{'PBBUILDDIR'}";
886 my $made = ""; # pkgs made during build
887 foreach my $pbpkg (@pkgs) {
888 my $vertag = $pkg->{$pbpkg};
889 # get the version of the current package - maybe different
890 ($pbver,$pbtag) = split(/-/,$vertag);
891
892 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
893 my $src2="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
894 pb_log(2,"Source file: $src\n");
895 pb_log(2,"Pbconf file: $src2\n");
896
897 pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
898 if ($dtype eq "rpm") {
899 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
900 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
901 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";
902 }
903 }
904
905 # Remove in case a previous link/file was there
906 unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
907 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
908 # We need to first extract the spec file
909 my @specfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$ENV{'PBBUILDDIR'}/SPECS","spec");
910
911 # We need to handle potential patches to upstream sources
912 pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/pbpatch/","$ENV{'PBBUILDDIR'}/SOURCES","patch");
913
914 pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
915 # set LANGUAGE to check for correct log messages
916 $ENV{'LANGUAGE'}="C";
917 # Older Redhat use _target_platform in %configure incorrectly
918 my $specialdef = "";
919 if (($ddir eq "redhat") || (($ddir eq "rhel") && ($dver eq "2.1"))) {
920 $specialdef = "--define \'_target_platform \"\"\'";
921 }
922
923 # If needed we may add repository to the build env
924 pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
925 foreach my $f (@specfile) {
926 if ($f =~ /\.spec$/) {
927 pb_distro_installdeps("$f",$dtype,$pbupd);
928 pb_system("rpmbuild $specialdef --define \'packager $ENV{'PBPACKAGER'}\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}","verbose");
929 last;
930 }
931 }
932 # Get the name of the generated packages
933 open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
934 while (<LOG>) {
935 chomp($_);
936 next if ($_ !~ /^Wrote:/);
937 s|.*/([S]*RPMS.*)|$1|;
938 $made .=" $_";
939 }
940 close(LOG);
941
942 } elsif ($dtype eq "deb") {
943 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
944 pb_system("tar xfz $src","Extracting sources");
945 pb_system("tar xfz $src2","Extracting pbconf");
946
947 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
948 pb_rm_rf("debian");
949 symlink "pbconf/$ddir-$dver-$arch","debian" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
950 chmod 0755,"debian/rules";
951
952 pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
953 pb_distro_installdeps("debian/control",$dtype,$pbupd);
954 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package","verbose");
955 # Get the name of the generated packages
956 open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
957 while (<LOG>) {
958 chomp();
959 my $tmp = $_;
960 next if ($tmp !~ /^dpkg-deb.*:/);
961 $tmp =~ s|.*../(.*)_(.*).deb.*|$1|;
962 $made="$made $tmp.dsc $tmp.tar.gz $tmp"."_*.deb $tmp"."_*.changes";
963 }
964 close(LOG);
965 } elsif ($dtype eq "ebuild") {
966 my @ebuildfile;
967 # For gentoo we need to take pb as subsystem name
968 # We put every apps here under sys-apps. hope it's correct
969 # We use pb's home dir in order to have a single OVERLAY line
970 my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
971 pb_mkdir_p($tmpd) if (! -d "$tmpd");
972 pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
973
974 # We need to first extract the ebuild file
975 @ebuildfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$tmpd","ebuild");
976
977 # Prepare the build env for gentoo
978 my $found = 0;
979 my $pbbd = $ENV{'HOME'};
980 $pbbd =~ s|/|\\/|g;
981 if (-r "/etc/make.conf") {
982 open(MAKE,"/etc/make.conf");
983 while (<MAKE>) {
984 $found = 1 if (/$pbbd\/portage/);
985 }
986 close(MAKE);
987 }
988 if ($found == 0) {
989 pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
990 }
991 #$found = 0;
992 #if (-r "/etc/portage/package.keywords") {
993 #open(KEYW,"/etc/portage/package.keywords");
994 #while (<KEYW>) {
995 #$found = 1 if (/portage\/pb/);
996 #}
997 #close(KEYW);
998 #}
999 #if ($found == 0) {
1000 #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
1001 #}
1002
1003 # Build
1004 foreach my $f (@ebuildfile) {
1005 if ($f =~ /\.ebuild$/) {
1006 pb_distro_installdeps("$f",$dtype,$pbupd);
1007 move($f,"$tmpd/$pbpkg-$pbver.ebuild");
1008 pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package","verbose");
1009 # Now move it where pb expects it
1010 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
1011 move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
1012 }
1013 }
1014
1015 $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver.ebuild";
1016 } elsif ($dtype eq "tgz") {
1017 # Slackware family
1018 $made="$made $pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
1019
1020 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1021 pb_system("tar xfz $src","Extracting sources");
1022 pb_system("tar xfz $src2","Extracting pbconf");
1023 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
1024 symlink "pbconf/$ddir-$dver-$arch","install" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
1025 if (-x "install/pbslack") {
1026 pb_distro_installdeps("./install/pbslack",$dtype,$pbupd);
1027 pb_system("./install/pbslack","Building software");
1028 pb_system("sudo /sbin/makepkg -p -l y -c y $pbpkg","Packaging $pbpkg","verbose");
1029 }
1030 } elsif ($dtype eq "pkg") {
1031 # Solaris
1032 $made="$made $dtype/";
1033 my $pkgdestdir="$ENV{'PBBUILDDIR'}/install";
1034
1035 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1036 # Will host resulting packages
1037 pb_mkdir_p("$dtype");
1038 pb_mkdir_p("$pkgdestdir");
1039 pb_system("tar xfz $src","Extracting sources under $ENV{'PBBUILDDIR'}");
1040 pb_system("tar xfz $src2","Extracting pbconf under $ENV{'PBBUILDDIR'}");
1041 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
1042 symlink "pbconf/$ddir-$dver-$arch","install" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
1043 if (-x "install/pbbuild") {
1044 # pkginfo file is mandatory
1045 die "Unable to find pkginfo file in pbconf/$ddir-$dver-$arch" if (! -f "install/pkginfo");
1046 # Build
1047 pb_system("install/pbbuild $pkgdestdir","Building software");
1048 # Copy complementary files
1049 if (-f "install/prototype") {
1050 copy("install/prototype", $pkgdestdir)
1051 } else {
1052 # No prototype provided, calculating it
1053 open(PROTO,"> install/prototype") || die "Unable to create prototype file";
1054 print PROTO "i pkginfo\n";
1055 print PROTO "i depend\n" if (-f "install/depend");
1056 find(\&create_solaris_prototype, $pkgdestdir);
1057 }
1058 copy("install/depend", $pkgdestdir) if (-f "install/depend");
1059 copy("install/pkginfo", $pkgdestdir);
1060 pb_system("pkgmk -o -r $pkgdestdir -d $ENV{'PBBUILDDIR'}/$dtype","Packaging $pbpkg","verbose");
1061 pb_system("pkgtrans -s $ENV{'PBBUILDDIR'}/$dtype $pbpkg-$pbver.pkg $pbpkg","Transformaing $pbpkg","verbose");
1062 pb_system("rm -rf $ENV{'PBBUILDDIR'}/$dtype");
1063 }
1064 } else {
1065 die "Unknown dtype format $dtype";
1066 }
1067 }
1068 # Packages check if needed
1069 if ($dtype eq "rpm") {
1070 if (-f "/usr/bin/rpmlint") {
1071 pb_system("rpmlint $made","Checking validity of rpms with rpmlint","verbose");
1072 } else {
1073 pb_log(0,"rpm packages generated: $made\n");
1074 }
1075 } elsif ($dtype eq "deb") {
1076 my $made2 = "";
1077 if (-f "/usr/bin/lintian") {
1078 foreach my $f (split(/ /,$made)) {
1079 $made2 .= "../$f " if ($f =~ /\.changes$/);
1080 }
1081 pb_system("lintian $made2","Checking validity of debs with lintian","verbose");
1082 } else {
1083 pb_log(0,"deb packages generated: $made2\n");
1084 }
1085 } else {
1086 pb_log(0,"No check done for $dtype yet\n");
1087 pb_log(0,"Packages generated: $made\n");
1088 }
1089
1090 # Keep track of what is generated so that we can get them back from VMs
1091 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1092 print KEEP "$made\n";
1093 close(KEEP);
1094}
1095
1096sub create_solaris_protype {
1097
1098 my $uidgid = "bin bin";
1099
1100 print PROTO "d none $_ 0755 $uidgid\n" if (-d $_);
1101 if (-x $_) {
1102 print PROTO "f none $_ 0755 $uidgid\n";
1103 } elsif (-f $_) {
1104 print PROTO "f none $_ 0644 $uidgid\n" if (-f $_);
1105 }
1106}
1107
1108sub pb_build2ssh {
1109 pb_send2target("Sources");
1110}
1111
1112sub pb_pkg2ssh {
1113 pb_send2target("Packages");
1114}
1115
1116# By default deliver to the the public site hosting the
1117# ftp structure (or whatever) or a VM/VE
1118sub pb_send2target {
1119
1120 my $cmt = shift;
1121 my $v = shift || undef;
1122 my $vmexist = shift || 0; # 0 is FALSE
1123 my $vmpid = shift || 0; # 0 is FALSE
1124 my $snapme = shift || 0; # 0 is FALSE
1125
1126 pb_log(2,"DEBUG: pb_send2target($cmt,".Dumper($v).",$vmexist,$vmpid)\n");
1127 my $host = "sshhost";
1128 my $login = "sshlogin";
1129 my $dir = "sshdir";
1130 my $port = "sshport";
1131 my $conf = "sshconf";
1132 my $rebuild = "sshrebuild";
1133 my $tmout = "vmtmout";
1134 my $path = "vmpath";
1135 if ($cmt =~ /^VM/) {
1136 $login = "vmlogin";
1137 $dir = "pbdefdir";
1138 $tmout = "vmtmout";
1139 $rebuild = "vmrebuild";
1140 # Specific VM
1141 $host = "vmhost";
1142 $port = "vmport";
1143 } elsif ($cmt =~ /^VE/) {
1144 $login = "velogin";
1145 $dir = "pbdefdir";
1146 # Specific VE
1147 $path = "vepath";
1148 $conf = "veconf";
1149 $rebuild = "verebuild";
1150 } elsif ($cmt eq "Web") {
1151 $host = "websshhost";
1152 $login = "websshlogin";
1153 $dir = "websshdir";
1154 $port = "websshport";
1155 }
1156 my $cmd = "";
1157 my $src = "";
1158 my ($odir,$over,$oarch) = (undef, undef, undef);
1159 my ($ddir, $dver, $dfam, $dtype, $pbsuf);
1160
1161 if ($cmt ne "Announce") {
1162 my $ptr = pb_get_pkg();
1163 @pkgs = @$ptr;
1164
1165 # Get the running distro to consider
1166 if (defined $v) {
1167 ($odir,$over,$oarch) = split(/-/,$v);
1168 }
1169 ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over,$oarch);
1170 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
1171
1172 # Get list of packages to build
1173 # Get content saved in cms2build
1174 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
1175 $pkg = { } if (not defined $pkg);
1176
1177 chdir "$ENV{'PBBUILDDIR'}";
1178 foreach my $pbpkg (@pkgs) {
1179 my $vertag = $pkg->{$pbpkg};
1180 # get the version of the current package - maybe different
1181 ($pbver,$pbtag) = split(/-/,$vertag);
1182
1183 if (($cmt eq "Sources") || ($cmt =~ /V[EM]build/)) {
1184 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
1185 if ($cmd eq "") {
1186 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
1187 } else {
1188 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
1189 }
1190 } elsif ($cmt eq "Web") {
1191 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz"
1192 }
1193 }
1194 # Adds conf file for availability of conf elements
1195 pb_conf_add("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb");
1196 }
1197
1198 if ($cmt =~ /V[EM]build/) {
1199 $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript";
1200 } elsif (($cmt =~ /V[EM]Script/) || ($cmt eq "Web")) {
1201 $src="$src $ENV{'PBDESTDIR'}/pbscript";
1202 } elsif ($cmt =~ /V[EM]test/) {
1203 $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript $ENV{'PBDESTDIR'}/pbtest";
1204 } elsif ($cmt eq "Announce") {
1205 $src="$src $ENV{'PBTMP'}/pbscript";
1206 } elsif ($cmt eq "Packages") {
1207 # Get package list from file made during build2pkg
1208 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1209 $src = <KEEP>;
1210 chomp($src);
1211 close(KEEP);
1212 $src="$src $ENV{'PBBUILDDIR'}/pbscript";
1213 }
1214 # Remove potential leading spaces (cause problem with basename)
1215 $src =~ s/^ *//;
1216 my $basesrc = "";
1217 foreach my $i (split(/ +/,$src)) {
1218 $basesrc .= " ".basename($i);
1219 }
1220
1221 pb_log(0,"Sources handled ($cmt): $src\n");
1222 pb_log(2,"values: ".Dumper(($host,$login,$dir,$port,$tmout,$rebuild,$path,$conf))."\n");
1223 my ($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vepath) = pb_conf_get($host,$login,$dir,$port,$tmout,$path);
1224 # Not mandatory
1225 my ($vrebuild,$veconf,$testver) = pb_conf_get_if($rebuild,$conf,"testver");
1226 pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf))."\n");
1227
1228 my $mac;
1229 if ($cmt !~ /^VE/) {
1230 $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
1231 # Overwrite account value if passed as parameter
1232 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
1233 pb_log(2, "DEBUG: pbaccount: $pbaccount => mac: $mac\n") if (defined $pbaccount);
1234 } else {
1235 # VE
1236 # Overwrite account value if passed as parameter (typically for setup2ve)
1237 $mac = $sshlogin->{$ENV{'PBPROJ'}};
1238 $mac = $pbaccount if (defined $pbaccount);
1239 }
1240
1241 my $tdir;
1242 my $bdir;
1243 if (($cmt eq "Sources") || ($cmt =~ /V[EM]Script/)) {
1244 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/src";
1245 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1246 # This is a test pkg => target dir is under test
1247 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/src";
1248 }
1249 } elsif (($cmt =~ /V[EM]build/) || ($cmt =~ /V[EM]test/)) {
1250 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery";
1251 $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build";
1252 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
1253 $bdir =~ s|\$ENV.+\}/||;
1254 } elsif ($cmt eq "Announce") {
1255 $tdir = "$sshdir->{$ENV{'PBPROJ'}}";
1256 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1257 # This is a test pkg => target dir is under test
1258 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test";
1259 }
1260 } elsif ($cmt eq "Web") {
1261 $tdir = "$sshdir->{$ENV{'PBPROJ'}}";
1262 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1263 # This is a test website => target dir is under test
1264 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/../test";
1265 }
1266 } elsif ($cmt eq "Packages") {
1267 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ddir/$dver";
1268
1269 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1270 # This is a test pkg => target dir is under test
1271 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/$ddir/$dver";
1272 }
1273
1274 my $repodir = $tdir;
1275 $repodir =~ s|^$sshdir->{$ENV{'PBPROJ'}}/||;
1276
1277 my ($pbrepo) = pb_conf_get("pbrepo");
1278
1279 # Repository management
1280 open(PBS,"> $ENV{'PBBUILDDIR'}/pbscript") || die "Unable to create $ENV{'PBBUILDDIR'}/pbscript";
1281 if ($dtype eq "rpm") {
1282 # Also make a pbscript to generate yum/urpmi bases
1283 print PBS << "EOF";
1284#!/bin/bash
1285# Prepare a script to ease yum setup
1286cat > $ENV{'PBPROJ'}.repo << EOT
1287[$ENV{'PBPROJ'}]
1288name=$ddir $dver - $ENV{'PBPROJ'} Vanilla Packages
1289baseurl=$pbrepo->{$ENV{'PBPROJ'}}/$repodir
1290enabled=1
1291gpgcheck=0
1292EOT
1293chmod 644 $ENV{'PBPROJ'}.repo
1294
1295# Clean up old repo content
1296rm -rf headers/ repodata/
1297# Create yum repo
1298yum-arch .
1299# Create repodata
1300createrepo .
1301EOF
1302 if ($dfam eq "md") {
1303 # For Mandriva add urpmi management
1304 print PBS << "EOF";
1305# Prepare a script to ease urpmi setup
1306cat > $ENV{'PBPROJ'}.addmedia << EOT
1307urpmi.addmedia $ENV{'PBPROJ'} $pbrepo->{$ENV{'PBPROJ'}}/$repodir with hdlist.cz
1308EOT
1309chmod 755 $ENV{'PBPROJ'}.addmedia
1310
1311# Clean up old repo content
1312rm -f hdlist.cz synthesis.hdlist.cz
1313# Create urpmi repo
1314genhdlist .
1315EOF
1316 }
1317 if ($ddir eq "fedora") {
1318 # Extract the spec file to please Fedora maintainers :-(
1319 print PBS << "EOF";
1320for p in $basesrc; do
1321 echo \$p | grep -q 'src.rpm'
1322 if [ \$\? -eq 0 ]; then
1323 rpm2cpio \$p | cpio -ivdum --quiet '*.spec'
1324 fi
1325done
1326EOF
1327 }
1328 } elsif ($dtype eq "deb") {
1329 # Also make a pbscript to generate apt bases
1330 # Cf: http://www.debian.org/doc/manuals/repository-howto/repository-howto.fr.html
1331 my $rpd = dirname("$pbrepo->{$ENV{'PBPROJ'}}/$repodir");
1332 print PBS << "EOF";
1333#!/bin/bash
1334# Prepare a script to ease apt setup
1335cat > $ENV{'PBPROJ'}.sources.list << EOT
1336deb $rpd $dver contrib
1337deb-src $rpd $dver contrib
1338EOT
1339chmod 644 $ENV{'PBPROJ'}.sources.list
1340
1341# Prepare a script to create apt info file
1342(cd .. ; for a in i386 amd64 ia64; do mkdir -p dists/$dver/contrib/binary-\$a; dpkg-scanpackages -a\$a $dver /dev/null | gzip -c9 > dists/$dver/contrib/binary-\$a/Packages.gz; done; mkdir -p dists/$dver/contrib/source; dpkg-scansources $dver /dev/null | gzip -c9 > dists/$dver/contrib/source/Sources.gz)
1343#(cd .. ; rm -f dists/$dver/Release ; apt-ftparchive release dists/$dver > dists/$dver/Release; gpg --sign -ba -o dists/$dver/Release.gpg dists/$dver/Release)
1344EOF
1345 } elsif ($dtype eq "ebuild") {
1346 # make a pbscript to generate links to latest version
1347 print PBS << "EOF";
1348#!/bin/bash
1349# Prepare a script to create correct links
1350for p in $src; do
1351 echo \$p | grep -q '.ebuild'
1352 if [ \$\? -eq 0 ]; then
1353 j=`basename \$p`
1354 pp=`echo \$j | cut -d'-' -f1`
1355 ln -sf \$j \$pp.ebuild
1356 fi
1357done
1358EOF
1359 }
1360 close(PBS);
1361 chmod 0755,"$ENV{'PBBUILDDIR'}/pbscript";
1362 } else {
1363 return;
1364 }
1365
1366 # Useless for VE
1367 my $nport;
1368 if ($cmt !~ /^VE/) {
1369 $nport = $sshport->{$ENV{'PBPROJ'}};
1370 $nport = "$pbport" if (defined $pbport);
1371 }
1372
1373 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
1374 $tdir =~ s|\$ENV.+\}/||;
1375
1376 my $tm = $vtmout->{$ENV{'PBPROJ'}};
1377
1378 # ssh communication if not VE
1379 # should use a hash instead...
1380 my ($shcmd,$cpcmd,$cptarget,$cp2target);
1381 if ($cmt !~ /^VE/) {
1382 my $keyfile = pb_ssh_get(0);
1383 $shcmd = "ssh -i $keyfile -q -o UserKnownHostsFile=/dev/null -p $nport $mac";
1384 $cpcmd = "scp -i $keyfile -p -o UserKnownHostsFile=/dev/null -P $nport";
1385 $cptarget = "$mac:$tdir";
1386 if ($cmt =~ /^VMbuild/) {
1387 $cp2target = "$mac:$bdir";
1388 }
1389 } else {
1390 my $tp = $vepath->{$ENV{'PBPROJ'}};
1391 ($odir,$over,$oarch) = split(/-/,$v);
1392 my $tpdir = "$tp/$odir/$over/$oarch";
1393 my ($ptr) = pb_conf_get("vetype");
1394 my $vetype = $ptr->{$ENV{'PBPROJ'}};
1395 if ($vetype eq "chroot") {
1396 $shcmd = "sudo chroot $tpdir /bin/su - $mac -c ";
1397 $cpcmd = "sudo cp -r ";
1398 } elsif ($vetype eq "schroot") {
1399 $shcmd = "schroot $tp -u $mac -- ";
1400 $cpcmd = "sudo cp -r ";
1401 }
1402 # We need to get the home dir of the target account to deliver in the right place
1403 open(PASS,"$tpdir/etc/passwd") || die "Unable to open $tpdir/etc/passwd";
1404 my $homedir = "";
1405 while (<PASS>) {
1406 my ($c1,$c2,$c3,$c4,$c5,$c6,$c7) = split(/:/);
1407 $homedir = $c6 if ($c1 =~ /^$mac$/);
1408 pb_log(3,"Homedir: $homedir - account: $c6\n");
1409 }
1410 close(PASS);
1411 $cptarget = "$tpdir/$homedir/$tdir";
1412 if ($cmt eq "VEbuild") {
1413 $cp2target = "$tpdir/$homedir/$bdir";
1414 }
1415 pb_log(2,"On VE using $cptarget as target dir to copy to\n");
1416 }
1417
1418 my $logres = "";
1419 # Do not touch when just announcing
1420 if ($cmt ne "Announce") {
1421 pb_system("$shcmd \"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 $cptarget");
1422 } else {
1423 $logres = "> ";
1424 }
1425 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
1426
1427 # For VE we need to change the owner manually
1428 if ($cmt =~ /^VE/) {
1429 pb_system("$shcmd \"sudo chown -R $mac $tdir\"","Adapt owner in $tdir to $mac");
1430 }
1431
1432 pb_system("$shcmd \"echo \'cd $tdir ; if [ -x pbscript ]; then ./pbscript; fi ; rm -f ./pbscript\' | bash\"","Executing pbscript on $cptarget if needed","verbose");
1433 if ($cmt =~ /^V[EM]build/) {
1434 # Get back info on pkg produced, compute their name and get them from the VM
1435 pb_system("$cpcmd $cp2target/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'} $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target");
1436 # For VE we need to change the owner manually
1437 if ($cmt eq "VEbuild") {
1438 pb_system("sudo chown $UID $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}","Adapt owner in $tdir to $UID");
1439 }
1440 # return here to avoid breaking the load on VMs/VEs in case of a bad one
1441 if (not -f "$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") {
1442 pb_log(0,"Problem with VM $v on $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}");
1443 return;
1444 }
1445 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1446 my $src = <KEEP>;
1447 chomp($src);
1448 close(KEEP);
1449 $src =~ s/^ *//;
1450 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
1451 # Change pgben to make the next send2target happy
1452 my $made = "";
1453 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1454 foreach my $p (split(/ +/,$src)) {
1455 my $j = basename($p);
1456 pb_system("$cpcmd $cp2target/\'$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Recovery of package $j in $ENV{'PBBUILDDIR'}/$odir/$over");
1457 $made="$made $odir/$over/$j"; # if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
1458 }
1459 print KEEP "$made\n";
1460 close(KEEP);
1461 pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
1462
1463 # Sign packages locally
1464 if ($dtype eq "rpm") {
1465 #pb_system("rpm --addsign --define \'_signature gpg\' --define \'__gpg_sign_cmd /usr/bin/gpg --batch --no-verbose --no-armor --no-secmem-warning -u \"$ENV{'PBPACKAGER'}\" -sbo %{__signature_filename} %{__plaintext_filename} --use-agent\' $made","Signing RPM packages packages");
1466 } elsif ($dtype eq "deb") {
1467 #pb_system("debsign $made","Signing DEB packages");
1468 } else {
1469 pb_log(0,"I don't know yet how to sign packages for type $dtype. Please give feedback to dev team");
1470 }
1471
1472 # We want to send them to the ssh account so overwrite what has been done before
1473 undef $pbaccount;
1474 pb_log(2,"Before sending pkgs, vmexist: $vmexist, vmpid: $vmpid\n");
1475 pb_send2target("Packages",$odir."-".$over."-".$oarch,$vmexist,$vmpid);
1476 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
1477 }
1478 pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
1479 if ((! $vmexist) && ($cmt =~ /^VM/)) {
1480 # If in setupvm then takes a snapshot just before halting
1481 if ($snapme != 0) {
1482 my ($vmmonport,$vmtype) = pb_conf_get("vmmonport","vmtype");
1483 # For monitoring control
1484 if ((($vmtype->{$ENV{'PBPROJ'}}) eq "kvm") || (($vmtype->{$ENV{'PBPROJ'}}) eq "qemu")) {
1485 require Net::Telnet;
1486 my $t = new Net::Telnet (Timeout => 120, Host => "localhost", Port => $vmmonport->{$ENV{'PBPROJ'}}) || die "Unable to dialog on the monitor";
1487 # move to monitor mode
1488 my @lines = $t->cmd("c");
1489 # Create a snapshot named pb
1490 @lines = $t->cmd("savevm pb");
1491 # Write the new status in the VM
1492 @lines = $t->cmd("commit all");
1493 # End
1494 @lines = $t->cmd("quit");
1495 }
1496 }
1497 pb_system("$shcmd \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $v halt (pid $vmpid)");
1498 }
1499 if (($cmt =~ /^VE/) && ($snapme != 0)) {
1500 ($odir,$over,$oarch) = split(/-/,$v);
1501 my $tpdir = "$vepath->{$ENV{'PBPROJ'}}/$odir/$over/$oarch";
1502 pb_system("sudo tar cz -f $vepath->{$ENV{'PBPROJ'}}/$odir-$over-$oarch.tar.gz -C $tpdir .","Creating a snapshot of $tpdir");
1503 }
1504}
1505
1506sub pb_script2v {
1507 my $pbscript=shift;
1508 my $vtype=shift;
1509 my $pbforce=shift || 0; # Force stop of VM. Default not
1510 my $vm1=shift || undef; # Only that VM to treat
1511 my $snapme=shift || 0; # Do we have to create a snapshot
1512 my $vm;
1513 my $all;
1514
1515 pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$pbforce,".Dumper($vm1).",$snapme)\n");
1516 # Prepare the script to be executed on the VM
1517 # in $ENV{'PBDESTDIR'}/pbscript
1518 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
1519 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1520 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1521 }
1522
1523 if (not defined $vm1) {
1524 ($vm,$all) = pb_get2v($vtype);
1525 } else {
1526 @$vm = ($vm1);
1527 }
1528 my ($vmexist,$vmpid) = (undef,undef);
1529
1530 foreach my $v (@$vm) {
1531 # Launch VM/VE - Usage of snapshot disabled
1532 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,$snapme,0);
1533
1534 if ($vtype eq "vm") {
1535 pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
1536
1537 # Skip that VM if something went wrong
1538 next if (($vmpid == 0) && ($vmexist == 0));
1539
1540 # If force stopping the VM then reset vmexist
1541 if ($pbforce == 1) {
1542 $vmpid = $vmexist;
1543 $vmexist = 0;
1544 }
1545 } else {
1546 #VE
1547 $vmexist = 0;
1548 $vmpid = 0;
1549 }
1550
1551 # Gather all required files to send them to the VM
1552 # and launch the build through pbscript
1553 pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
1554 pb_send2target(uc($vtype)."Script","$v",$vmexist,$vmpid,$snapme);
1555
1556 }
1557}
1558
1559sub pb_launchv {
1560 my $vtype = shift;
1561 my $v = shift;
1562 my $create = shift || 0; # By default do not create a VM/VE
1563 my $snapme = shift || 0; # By default do not snap a VM/VE
1564 my $usesnap = shift || 1; # By default study the usage of the snapshot feature of VM/VE
1565
1566 # If creation or snapshot creation mode, no snapshot usable
1567 if (($create == 1) || ($snapme == 1)) {
1568 $usesnap = 0;
1569 }
1570
1571 pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create,$snapme,$usesnap)\n");
1572 die "No VM/VE defined, unable to launch" if (not defined $v);
1573 # Keep only the first VM in case many were given
1574 $v =~ s/,.*//;
1575
1576 my $arch = pb_get_arch();
1577
1578 # Launch the VMs/VEs
1579 if ($vtype eq "vm") {
1580 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
1581
1582 my ($ptr,$vmpath,$vmport,$vmsize,$vmmonport) = pb_conf_get("vmtype","vmpath","vmport","vmsize","vmmonport");
1583 my ($vmopt,$vmtmout,$vmsnap) = pb_conf_get_if("vmopt","vmtmout","vmsnap");
1584
1585 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
1586 if (not defined $ENV{'PBVMOPT'}) {
1587 $ENV{'PBVMOPT'} = "";
1588 }
1589 # Save the current status for later restoration
1590 $ENV{'PBOLDVMOPT'} = $ENV{'PBVMOPT'};
1591 # Set a default timeout of 2 minutes
1592 if (not defined $ENV{'PBVMTMOUT'}) {
1593 $ENV{'PBVMTMOUT'} = "120";
1594 }
1595 if (defined $vmopt->{$v}) {
1596 $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
1597 } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
1598 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
1599 }
1600
1601 # Are we allowed to use snapshot feature
1602 if ($usesnap == 1) {
1603 if ((defined $vmsnap->{$v}) && ($vmsnap->{$v} =~ /true/i)) {
1604 $ENV{'PBVMOPT'} .= "-snapshot ";
1605 } elsif ((defined $vmsnap->{$ENV{'PBPROJ'}}) && ($vmsnap->{$ENV{'PBPROJ'}} =~ /true/i)) {
1606 $ENV{'PBVMOPT'} .= "-snapshot ";
1607 } elsif ($pbsnap eq 1) {
1608 $ENV{'PBVMOPT'} .= "-snapshot ";
1609 }
1610 }
1611 if ($snapme != 0) {
1612 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
1613 # Configure the monitoring to automize the creation of the 'pb' snapshot
1614 $ENV{'PBVMOPT'} .= "-serial mon:telnet::$vmmonport->{$ENV{'PBPROJ'}},server,nowait ";
1615 # In that case no snapshot call needed
1616 $ENV{'PBVMOPT'} =~ s/-snapshot //;
1617 }
1618 }
1619 if (defined $vmtmout->{$v}) {
1620 $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
1621 } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
1622 $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
1623 }
1624 my $nport = $vmport->{$ENV{'PBPROJ'}};
1625 $nport = "$pbport" if (defined $pbport);
1626
1627 my $cmd;
1628 my $vmcmd; # has to be used for pb_check_ps
1629 my $vmm; # has to be used for pb_check_ps
1630 if (($vmtype eq "qemu") || ($vmtype eq "kvm")) {
1631 my $qemucmd32;
1632 my $qemucmd64;
1633 if ($arch eq "x86_64") {
1634 $qemucmd32 = "/usr/bin/qemu-system-i386";
1635 $qemucmd64 = "/usr/bin/qemu";
1636 } else {
1637 $qemucmd32 = "/usr/bin/qemu";
1638 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
1639 }
1640 if ($v =~ /x86_64/) {
1641 $vmcmd = "$qemucmd64";
1642 } else {
1643 $vmcmd = "$qemucmd32";
1644 }
1645 if ($vmtype eq "kvm") {
1646 $vmcmd = "/usr/bin/kvm";
1647 }
1648 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
1649 if (($create != 0) || (defined $iso)) {
1650 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
1651 }
1652 # Always redirect the network and always try to use a 'pb' snapshot
1653 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 -loadvm pb $vmm"
1654 } elsif ($vmtype eq "xen") {
1655 } elsif ($vmtype eq "vmware") {
1656 } else {
1657 die "VM of type $vmtype not supported. Report to the dev team";
1658 }
1659 # Restore the ENV VAR Value
1660 $ENV{'PBVMOPT'} = $ENV{'PBOLDVMOPT'};
1661
1662 my ($tmpcmd,$void) = split(/ +/,$cmd);
1663 my $vmexist = pb_check_ps($tmpcmd,$vmm);
1664 my $vmpid = 0;
1665 if (! $vmexist) {
1666 if ($create != 0) {
1667 die("Found an existing Virtual machine $vmm. Won't overwrite") if (-r $vmm);
1668 if (($vmtype eq "qemu") || ($vmtype eq "xen") || ($vmtype eq "kvm")) {
1669 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
1670 } elsif ($vmtype eq "vmware") {
1671 } else {
1672 }
1673 }
1674 if (! -f "$vmm") {
1675 pb_log(0,"Unable to find VM $vmm\n");
1676 } else {
1677 pb_system("$cmd &","Launching the VM $vmm");
1678 pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up");
1679 $vmpid = pb_check_ps($tmpcmd,$vmm);
1680 pb_log(0,"VM $vmm launched (pid $vmpid)\n");
1681 }
1682 } else {
1683 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
1684 }
1685 pb_log(2,"DEBUG: pb_launchv returns ($vmexist,$vmpid)\n");
1686 return($vmexist,$vmpid);
1687 # VE here
1688 } else {
1689 # Get VE context
1690 my ($ptr,$vetmout,$vepath,$verebuild,$veconf,$vepostinstall) = pb_conf_get("vetype","vetmout","vepath","verebuild","veconf");
1691 my ($veb4pi,$vepi,$vepkglist,$vesnap) = pb_conf_get_if("veb4pi","vepi","vepkglist","vesnap");
1692 my $vetype = $ptr->{$ENV{'PBPROJ'}};
1693
1694 # Get distro context
1695 my ($name,$ver,$darch) = split(/-/,$v);
1696 chomp($darch);
1697 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver,$darch);
1698
1699 if (($vetype eq "chroot") || ($vetype eq "schroot")) {
1700 # Architecture consistency
1701 if ($arch ne $darch) {
1702 die "Unable to launch a VE of architecture $darch on a $arch platform" if (($darch eq "x86_64") && ($arch =~ /i?86/));
1703 }
1704
1705 my ($verpmtype) = pb_conf_get("verpmtype");
1706 if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($pbforce == 1)) {
1707 # We have to rebuild the chroot
1708 if ($dtype eq "rpm") {
1709
1710 my $verpmstyle = pb_distro_get_param($ddir,$dver,$darch,$verpmtype);
1711 if ($verpmstyle eq "rinse") {
1712 # Need to reshape the mirrors generated with local before-post-install script
1713 my $b4post = "--before-post-install ";
1714 my $postparam = pb_distro_get_param($ddir,$dver,$darch,$veb4pi);
1715 if ($postparam eq "") {
1716 $b4post = "";
1717 } else {
1718 $b4post .= $postparam;
1719 }
1720 # Do we have a local post-install script
1721 my $post = "--post-install ";
1722 $postparam = pb_distro_get_param($ddir,$dver,$darch,$vepi);
1723 if ($postparam eq "") {
1724 $post = "";
1725 } else {
1726 $post .= $postparam;
1727 }
1728
1729 # Need to reshape the package list for pb
1730 my $addpkgs;
1731 $postparam = pb_distro_get_param($ddir,$dver,$darch,$vepkglist);
1732 if ($postparam eq "") {
1733 $addpkgs = "";
1734 } else {
1735 my $pkgfile = "$ENV{'PBTMP'}/addpkgs.lis";
1736 open(PKG,"> $pkgfile") || die "Unable to create $pkgfile";
1737 foreach my $p (split(/,/,$postparam)) {
1738 print PKG "$p\n";
1739 }
1740 close(PKG);
1741 $addpkgs = "--add-pkg-list $pkgfile";
1742 }
1743 my $rinseverb = "";
1744 $rinseverb = "--verbose" if ($pbdebug gt 0);
1745
1746 pb_system("sudo /usr/sbin/rinse --directory \"$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch\" --arch \"$darch\" --distribution \"$ddir-$dver\" --config \"$veconf->{$ENV{'PBPROJ'}}\" $b4post $post $addpkgs $rinseverb","Creating the rinse VE for $ddir-$dver ($darch)", "verbose");
1747 } elsif ($verpmstyle eq "mock") {
1748 pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE for $ddir-$dver ($darch)");
1749 # Once setup we need to install some packages, the pb account, ...
1750 pb_system("sudo /usr/sbin/mock --install --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
1751 }
1752 } elsif ($dtype eq "deb") {
1753 pb_system("","Creating the pbuilder VE TBD");
1754 } elsif ($dtype eq "ebuild") {
1755 die "Please teach the dev team how to build gentoo chroot";
1756 } else {
1757 die "Unknown distribution type $dtype. Report to dev team";
1758 }
1759 }
1760 # Test if an existing snapshot exists and use it if appropriate
1761 # And also use it of no local extracted VE is present
1762 if ((-f "$vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz") &&
1763 (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
1764 ((defined $vesnap->{$ENV{'PBPROJ'}}) && ($vesnap->{$ENV{'PBPROJ'}} =~ /true/i)) ||
1765 ($pbsnap eq 1) ||
1766 (! -d "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch"))) {
1767 pb_system("sudo rm -rf $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch ; sudo mkdir -p $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch ; sudo tar xz -C $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch -f $vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz","Extracting snapshot of $ddir-$dver-$darch.tar.gz under $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch");
1768 }
1769 # Nothing more to do for VE. No real launch
1770 } else {
1771 die "VE of type $vetype not supported. Report to the dev team";
1772 }
1773 }
1774}
1775
1776# Return string for date synchro
1777sub pb_date2v {
1778
1779my $vtype = shift;
1780my $v = shift;
1781
1782my ($ntp) = pb_conf_get($vtype."ntp");
1783my $vntp = $ntp->{$ENV{'PBPROJ'}};
1784my $ntpline;
1785
1786if (defined $vntp) {
1787 my ($ntpcmd) = pb_conf_get($vtype."ntpcmd");
1788 my $vntpcmd;
1789 if (defined $ntpcmd->{$v}) {
1790 $vntpcmd = $ntpcmd->{$v};
1791 } elsif (defined $ntpcmd->{$ENV{'PBPROJ'}}) {
1792 $vntpcmd = $ntpcmd->{$ENV{'PBPROJ'}};
1793 } else {
1794 $vntpcmd = "/bin/true";
1795 }
1796 $ntpline = "sudo $vntpcmd $vntp";
1797} else {
1798 $ntpline = undef;
1799}
1800# Force new date to be in the future compared to the date
1801# of the host by adding 1 minute
1802my @date=pb_get_date();
1803$date[1]++;
1804my $upddate = strftime("%m%d%H%M%Y", @date);
1805my $dateline = "sudo date $upddate";
1806return($ntpline,$dateline);
1807}
1808
1809sub pb_build2v {
1810
1811my $vtype = shift;
1812my $action = shift || "build";
1813
1814my ($v,$all) = pb_get2v($vtype);
1815
1816# Send tar files when we do a global generation
1817pb_build2ssh() if (($all == 1) && ($action eq "build"));
1818
1819my ($vmexist,$vmpid) = (undef,undef);
1820
1821foreach my $v (@$v) {
1822 # Prepare the script to be executed on the VM/VE
1823 # in $ENV{'PBDESTDIR'}/pbscript
1824 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1825 print SCRIPT "#!/bin/bash\n";
1826
1827 # Transmit the verbosity level to the virtual env/mach.
1828 my $verbose = "";
1829 my $i = 0; # minimal debug level
1830 while ($i lt $pbdebug) {
1831 $verbose .= "-v ";
1832 $i++;
1833 }
1834 # Activate script verbosity if at least 2 for pbdebug
1835 print SCRIPT "set -x\n" if ($i gt 1);
1836 # Quiet if asked to be so on the original system
1837 $verbose = "-q" if ($pbdebug eq -1);
1838
1839 print SCRIPT "echo ... Execution needed\n";
1840 print SCRIPT "# This is in directory delivery\n";
1841 print SCRIPT "# Setup the variables required for building\n";
1842 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
1843
1844 if ($action eq "build") {
1845 print SCRIPT "# Preparation for pb\n";
1846 print SCRIPT "mv .pbrc \$HOME\n";
1847 print SCRIPT "cd ..\n";
1848 }
1849
1850 # VE needs a good /proc
1851 if ($vtype eq "ve") {
1852 print SCRIPT "sudo mount -t proc /proc /proc\n";
1853 }
1854
1855 my ($ntpline,$dateline) = pb_date2v($vtype,$v);
1856 print SCRIPT "# Time sync\n";
1857 print SCRIPT "echo 'setting up date with '";
1858 if (defined $ntpline) {
1859 print SCRIPT "echo $ntpline\n";
1860 print SCRIPT "$ntpline\n";
1861 } else {
1862 print SCRIPT "echo $dateline\n";
1863 print SCRIPT "$dateline\n";
1864 }
1865 # Use potential local proxy declaration in case we need it to download repo, pkgs, ...
1866 if (defined $ENV{'http_proxy'}) {
1867 print SCRIPT "export http_proxy=\"$ENV{'http_proxy'}\"\n";
1868 }
1869
1870 if (defined $ENV{'ftp_proxy'}) {
1871 print SCRIPT "export ftp_proxy=\"$ENV{'ftp_proxy'}\"\n";
1872 }
1873
1874 # Get list of packages to build/test and get some ENV vars as well
1875 my $ptr = pb_get_pkg();
1876 @pkgs = @$ptr;
1877 my $p = join(' ',@pkgs) if (@pkgs);
1878 print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
1879 print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
1880 print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
1881
1882 # We may need to do some other tasks before building. Read a script here to finish setup
1883 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".pre") {
1884 print SCRIPT "# Special pre-instructions to be launched\n";
1885 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".pre");
1886 }
1887
1888 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre") {
1889 print SCRIPT "# Special pre-$action instructions to be launched\n";
1890 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre");
1891 }
1892
1893 print SCRIPT "# $action\n";
1894 print SCRIPT "echo $action"."ing packages on $vtype...\n";
1895
1896 if (($action eq "test") && (! -x "$ENV{'PBDESTDIR'}/pbtest")) {
1897 die "No test script ($ENV{'PBDESTDIR'}/pbtest) found when in test mode. Aborting ...";
1898 }
1899 print SCRIPT "pb $verbose -p $ENV{'PBPROJ'} $action"."2pkg $p\n";
1900
1901 if ($vtype eq "ve") {
1902 print SCRIPT "sudo umount /proc\n";
1903 }
1904
1905 # We may need to do some other tasks after building. Read a script here to exit properly
1906 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.post") {
1907 print SCRIPT "# Special post-$action instructions to be launched\n";
1908 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.post");
1909 }
1910
1911 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".post") {
1912 print SCRIPT "# Special post-instructions to be launched\n";
1913 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".post");
1914 }
1915
1916 close(SCRIPT);
1917 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1918
1919 # Launch the VM/VE
1920 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
1921
1922 if ($vtype eq "vm") {
1923 # Skip that VM if it something went wrong
1924 next if (($vmpid == 0) && ($vmexist == 0));
1925 } else {
1926 # VE
1927 $vmexist = 0;
1928 $vmpid = 0;
1929 }
1930 # Gather all required files to send them to the VM/VE
1931 # and launch the build through pbscript
1932 pb_log(2,"Calling send2target $vtype,$v,$vmexist,$vmpid\n");
1933 pb_send2target(uc($vtype).$action,"$v",$vmexist,$vmpid);
1934}
1935}
1936
1937
1938sub pb_clean {
1939
1940 my $sleep=10;
1941 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
1942 die "Unable to get env var PBBUILDDIR" if (not defined $ENV{'PBBUILDDIR'});
1943 pb_log(0,"We will now wait $sleep s before removing both directories\n$ENV{'PBDESTDIR'} and $ENV{'PBBUILDDIR'}.\nPlease break me if this is wrong\n");
1944 sleep $sleep;
1945 pb_rm_rf($ENV{'PBDESTDIR'});
1946 pb_rm_rf($ENV{'PBBUILDDIR'});
1947}
1948
1949sub pb_newver {
1950
1951 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
1952
1953 # Need this call for PBDIR
1954 my ($scheme2,$uri) = pb_cms_init($pbinit);
1955
1956 my ($pbconf) = pb_conf_get("pbconfurl");
1957 $uri = $pbconf->{$ENV{'PBPROJ'}};
1958 my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
1959
1960 # Checking CMS repositories status
1961 my ($pburl) = pb_conf_get("pburl");
1962 ($scheme2, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
1963
1964 if ($scheme !~ /^svn/) {
1965 die "Only SVN is supported at the moment";
1966 }
1967
1968 my $res = pb_cms_isdiff($scheme,$ENV{'PBROOTDIR'});
1969 die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
1970
1971 $res = pb_cms_isdiff($scheme2,$ENV{'PBDIR'});
1972 die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
1973
1974 # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
1975 # we want to get for the root of the new URL
1976
1977 my $tmp = $ENV{'PBROOTDIR'};
1978 $tmp =~ s|^$ENV{'PBCONFDIR'}||;
1979
1980 my $newurl = "$uri/".dirname($tmp)."/$newver";
1981 # Should probably use projver in the old file
1982 my $oldver= basename($tmp);
1983
1984 # Duplicate and extract project-builder part
1985 pb_log(2,"Copying $uri/$tmp to $newurl\n");
1986 pb_cms_copy($scheme,"$uri/$tmp",$newurl);
1987 pb_log(2,"Checkout $newurl to $ENV{'PBROOTDIR'}/../$newver\n");
1988 pb_cms_up($scheme,"$ENV{'PBCONFDIR'}/..");
1989
1990 # Duplicate and extract project
1991 my $newurl2 = "$pburl->{$ENV{'PBPROJ'}}/".dirname($tmp)."/$newver";
1992
1993 pb_log(2,"Copying $pburl->{$ENV{'PBPROJ'}}/$tmp to $newurl2\n");
1994 pb_cms_copy($scheme2,"$pburl->{$ENV{'PBPROJ'}}/$tmp",$newurl2);
1995 pb_log(2,"Checkout $newurl2 to $ENV{'PBDIR'}/../$newver\n");
1996 pb_cms_up($scheme2,"$ENV{'PBDIR'}/..");
1997
1998 # Update the .pb file
1999 open(FILE,"$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb";
2000 open(OUT,"> $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new";
2001 while(<FILE>) {
2002 s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
2003 pb_log(0,"Changing projver from $oldver to $newver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/);
2004 s/^testver/#testver/;
2005 pb_log(0,"Commenting testver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^testver/);
2006 print OUT $_;
2007 }
2008 close(FILE);
2009 close(OUT);
2010 rename("$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb");
2011
2012 # Checking pbcl files
2013 foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
2014 # Compute new pbcl file
2015 my $f2 = $f;
2016 $f2 =~ s|$ENV{'PBROOTDIR'}|$ENV{'PBROOTDIR'}/../$newver/|;
2017 open(PBCL,$f) || die "Unable to open $f";
2018 my $foundnew = 0;
2019 while (<PBCL>) {
2020 $foundnew = 1 if (/^$newver \(/);
2021 }
2022 close(PBCL);
2023 open(OUT,"> $f2") || die "Unable to write to $f2: $!";
2024 open(PBCL,$f) || die "Unable to open $f";
2025 while (<PBCL>) {
2026 print OUT "$_" if (not /^$oldver \(/);
2027 if ((/^$oldver \(/) && ($foundnew == 0)) {
2028 print OUT "$newver ($pbdate)\n";
2029 print OUT "- TBD\n";
2030 print OUT "\n";
2031 pb_log(0,"WARNING: version $newver not found in $f so added to $f2...\n") if ($foundnew == 0);
2032 }
2033 }
2034 close(OUT);
2035 close(PBCL);
2036 }
2037
2038 pb_log(2,"Checkin $ENV{'PBROOTDIR'}/../$newver\n");
2039 pb_cms_checkin($scheme,"$ENV{'PBROOTDIR'}/../$newver",undef);
2040}
2041
2042#
2043# Return the list of VMs/VEs we are working on
2044# $all is a flag to know if we return all of them
2045# or only some (if all we publish also tar files in addition to pkgs
2046#
2047sub pb_get2v {
2048
2049my $vtype = shift;
2050my @v;
2051my $all = 0;
2052my $vlist;
2053my $pbv = 'PBV';
2054
2055if ($vtype eq "vm") {
2056 $vlist = "vmlist";
2057} elsif ($vtype eq "ve") {
2058 $vlist = "velist";
2059}
2060# Get VM/VE list
2061if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
2062 my ($ptr) = pb_conf_get($vlist);
2063 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
2064 $all = 1;
2065}
2066pb_log(2,"$vtype: $ENV{$pbv}\n");
2067@v = split(/,/,$ENV{$pbv});
2068return(\@v,$all);
2069}
2070
2071# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
2072# Needs to use root account to connect to the VM/VE
2073# pb will take your local public SSH key to access
2074# the pb account in the VM later on if needed
2075sub pb_setup2v {
2076
2077my $vtype = shift;
2078
2079my ($vm,$all) = pb_get2v($vtype);
2080
2081# Script generated
2082my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
2083
2084foreach my $v (@$vm) {
2085 # Deal with date sync.
2086 my ($ntpline,$dateline) = pb_date2v($vtype,$v);
2087
2088 # Name of the account to deal with for VM/VE
2089 # Do not use the one passed potentially with -a
2090 my ($pbac) = pb_conf_get($vtype."login");
2091 my ($key,$zero0,$zero1,$zero2);
2092 my ($vmexist,$vmpid);
2093
2094 # Prepare the script to be executed on the VM/VE
2095 # in $ENV{'PBDESTDIR'}/setupv
2096 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2097
2098 print SCRIPT << 'EOF';
2099#!/usr/bin/perl -w
2100
2101use strict;
2102use File::Copy;
2103
2104# We should not need in this script more functions than what is provided
2105# by Base and Distribution to avoid problems at exec time.
2106# They are appended at the end.
2107
2108our $pbdebug;
2109our $pbLOG;
2110our $pbsynmsg = "pbscript";
2111our $pbdisplaytype = "text";
2112our $pblocale = "";
2113pb_log_init($pbdebug, $pbLOG);
2114pb_temp_init();
2115
2116EOF
2117
2118 # Launch the VM/VE - Usage of snapshot disabled
2119 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,0,0);
2120
2121 if ($vtype eq "vm") {
2122 # Prepare the key to be used and transfered remotely
2123 my $keyfile = pb_ssh_get(1);
2124
2125 my ($vmhost,$vmport,$vmntp) = pb_conf_get("vmhost","vmport","vmntp");
2126 my $nport = $vmport->{$ENV{'PBPROJ'}};
2127 $nport = "$pbport" if (defined $pbport);
2128
2129 # Skip that VM if something went wrong
2130 next if (($vmpid == 0) && ($vmexist == 0));
2131
2132 # Store the pub key part in a variable
2133 open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
2134 ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
2135 close(FILE);
2136
2137 $key = "\Q$zero1";
2138
2139 pb_system("cat $keyfile.pub | ssh -q -o UserKnownHostsFile=/dev/null -p $nport -i $keyfile root\@$vmhost->{$ENV{'PBPROJ'}} \"mkdir -p .ssh ; chmod 700 .ssh ; cat >> .ssh/authorized_keys ; chmod 600 .ssh/authorized_keys\"","Copying local keys to $vtype. This may require the root password");
2140 # once this is done, we can do what we want on the VM remotely
2141 } elsif ($vtype eq "ve") {
2142 # We need to finish the setup of packages needed in the VE if needed
2143 # rinse at least needs it
2144 my ($vepkglist) = pb_conf_get_if("vepkglist");
2145
2146 if (defined $vepkglist) {
2147 # Get distro context
2148 my ($name,$ver,$darch) = split(/-/,$v);
2149 chomp($darch);
2150 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd) = pb_distro_init($name,$ver,$darch);
2151
2152 my $postparam = pb_distro_get_param($ddir,$dver,$darch,$vepkglist);
2153 # Change the list of pkg in to a space separated list
2154 $postparam =~ s/,/ /g;
2155 # remove potential sudo from the update command for this time,
2156 # as this will be run as root anyway, and if not we will have
2157 # a problem with tty
2158 $pbupd =~ s/sudo //g;
2159
2160 print SCRIPT << "EOF";
2161# For VE we first need to mount some FS
2162pb_system("mount -t proc /proc /proc");
2163
2164# For VE we need a good null dev
2165pb_system("rm -f /dev/null; mknod /dev/null c 1 3; chmod 777 /dev/null");
2166
2167# For VE we need some additional packages that are not there yet
2168pb_system("$pbupd $postparam");
2169
2170EOF
2171 }
2172 }
2173 if ($vtype eq "vm") {
2174 print SCRIPT << 'EOF';
2175# Removes duplicate in .ssh/authorized_keys of our key if needed
2176#
2177my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
2178open(PBFILE,$file1) || die "Unable to open $file1";
2179open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
2180my $count = 0;
2181while (<PBFILE>) {
2182
2183EOF
2184 print SCRIPT << "EOF";
2185 if (/ $key /) {
2186 \$count++;
2187 }
2188print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
2189}
2190close(PBFILE);
2191close(PBOUT);
2192rename("\$file1.new",\$file1);
2193chmod 0600,\$file1;
2194
2195# Sync date
2196EOF
2197 if (defined $ntpline) {
2198 print SCRIPT "system(\"$ntpline\");\n";
2199 } else {
2200 print SCRIPT "system(\"$dateline\");\n";
2201 }
2202 }
2203 print SCRIPT << 'EOF';
2204
2205# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
2206#
2207my $file="/etc/passwd";
2208open(PBFILE,$file) || die "Unable to open $file";
2209my $found = 0;
2210while (<PBFILE>) {
2211EOF
2212 print SCRIPT << "EOF";
2213 \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
2214EOF
2215 print SCRIPT << 'EOF';
2216}
2217close(PBFILE);
2218
2219if ( $found == 0 ) {
2220 if ( ! -d "/home" ) {
2221 pb_mkdir("/home");
2222 }
2223EOF
2224 print SCRIPT << "EOF";
2225pb_system("groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
2226pb_system("useradd $pbac->{$ENV{'PBPROJ'}} -g $pbac->{$ENV{'PBPROJ'}} -m -d /home/$pbac->{$ENV{'PBPROJ'}}","Adding user $pbac->{$ENV{'PBPROJ'}} (group $pbac->{$ENV{'PBPROJ'}} - home /home/$pbac->{$ENV{'PBPROJ'}}");
2227}
2228EOF
2229
2230 if ($vtype eq "vm") {
2231 print SCRIPT << "EOF";
2232# allow ssh entry to build
2233#
2234mkdir "/home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
2235# Allow those accessing root to access the build account
2236copy("\$ENV{'HOME'}/.ssh/authorized_keys","/home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
2237chmod 0600,".ssh/authorized_keys";
2238pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} /home/$pbac->{$ENV{'PBPROJ'}}","Finish setting up the account env for $pbac->{$ENV{'PBPROJ'}}");
2239
2240EOF
2241}
2242 print SCRIPT << 'EOF';
2243# No passwd for build account only keys
2244$file="/etc/shadow";
2245if (-f $file) {
2246 open(PBFILE,$file) || die "Unable to open $file";
2247 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2248 while (<PBFILE>) {
2249EOF
2250 print SCRIPT << "EOF";
2251 s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
2252 s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/; #SLES 9 e.g.
2253EOF
2254 print SCRIPT << 'EOF';
2255 print PBOUT $_;
2256 }
2257 close(PBFILE);
2258 close(PBOUT);
2259 rename("$file.new",$file);
2260 chmod 0640,$file;
2261 }
2262
2263# Keep the VM in text mode
2264$file="/etc/inittab";
2265if (-f $file) {
2266 open(PBFILE,$file) || die "Unable to open $file";
2267 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2268 while (<PBFILE>) {
2269 s/^(..):5:initdefault:$/$1:3:initdefault:/;
2270 print PBOUT $_;
2271 }
2272 close(PBFILE);
2273 close(PBOUT);
2274 rename("$file.new",$file);
2275 chmod 0640,$file;
2276}
2277
2278# pb has to be added to portage group on gentoo
2279
2280# Adapt sudoers
2281$file="/etc/sudoers";
2282open(PBFILE,$file) || die "Unable to open $file";
2283open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2284while (<PBFILE>) {
2285EOF
2286 print SCRIPT << "EOF";
2287 next if (/^$pbac->{$ENV{'PBPROJ'}} /);
2288EOF
2289 print SCRIPT << 'EOF';
2290 s/Defaults[ \t]+requiretty//;
2291 print PBOUT $_;
2292}
2293close(PBFILE);
2294EOF
2295 print SCRIPT << "EOF";
2296# This is needed in order to be able to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
2297print PBOUT "Defaults:pb env_keep += \\\"http_proxy ftp_proxy\\\"\n";
2298print PBOUT "$pbac->{$ENV{'PBPROJ'}} ALL=(ALL) NOPASSWD:ALL\n";
2299EOF
2300 print SCRIPT << 'EOF';
2301close(PBOUT);
2302rename("$file.new",$file);
2303chmod 0440,$file;
2304
2305EOF
2306
2307 my $SCRIPT = \*SCRIPT;
2308
2309 pb_install_deps($SCRIPT);
2310
2311 print SCRIPT << 'EOF';
2312# Suse wants sudoers as 640
2313if (($ddir eq "sles") || (($ddir eq "opensuse") && ($dver =~ /10.[012]/))) {
2314 chmod 0640,$file;
2315}
2316
2317pb_system("rm -rf ProjectBuilder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/ProjectBuilder-latest.tar.gz ; tar xvfz ProjectBuilder-latest.tar.gz ; cd ProjectBuilder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf ProjectBuilder-* ; rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf project-builder-* ;","Building Project-Builder");
2318system "pb 2>&1 | head -5";
2319EOF
2320 if ($vtype eq "ve") {
2321 print SCRIPT << 'EOF';
2322# For VE we need to umount some FS at the end
2323
2324pb_system("umount /proc");
2325
2326# Create a basic network file if not already there
2327
2328my $nf="/etc/sysconfig/network";
2329if (! -f $nf) {
2330 open(NF,"> $nf") || die "Unable to create $nf";
2331 print NF "NETWORKING=yes\n";
2332 print NF "HOSTNAME=localhost\n";
2333 close(NF);
2334}
2335chmod 0755,$nf;
2336EOF
2337 }
2338
2339 # Adds pb_distro_init from ProjectBuilder::Distribution and Base
2340 foreach my $d (@INC) {
2341 my @f = ("$d/ProjectBuilder/Base.pm","$d/ProjectBuilder/Distribution.pm");
2342 foreach my $f (@f) {
2343 if (-f "$f") {
2344 open(PBD,"$f") || die "Unable to open $f";
2345 while (<PBD>) {
2346 next if (/^package/);
2347 next if (/^use Exporter/);
2348 next if (/^use ProjectBuilder::/);
2349 next if (/^our /);
2350 print SCRIPT $_;
2351 }
2352 close(PBD);
2353 }
2354 }
2355 }
2356 close(SCRIPT);
2357 chmod 0755,"$pbscript";
2358
2359 # That build script needs to be run as root and force stop of VM at end
2360 $pbaccount = "root";
2361
2362 # Force shutdown of VM exept if it was already launched
2363 my $pbforce = 0;
2364 if ((! $vmexist) && ($vtype eq "vm")) {
2365 $pbforce = 1;
2366 }
2367
2368 pb_script2v($pbscript,$vtype,$pbforce,$v);
2369}
2370return;
2371}
2372
2373# Function to create a snapshot named 'pb' for VMs and a compressed tar for VEs
2374sub pb_snap2v {
2375
2376my $vtype = shift;
2377
2378my ($vm,$all) = pb_get2v($vtype);
2379
2380# Script generated
2381my $pbscript = "$ENV{'PBDESTDIR'}/snapv";
2382
2383foreach my $v (@$vm) {
2384 # Name of the account to deal with for VM/VE
2385 # Do not use the one passed potentially with -a
2386 my ($pbac) = pb_conf_get($vtype."login");
2387 my ($vmexist,$vmpid);
2388
2389 # Prepare the script to be executed on the VM/VE
2390 # in $ENV{'PBDESTDIR'}/setupv
2391 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2392
2393 print SCRIPT << 'EOF';
2394 #!/bin/bash
2395 sleep 2
2396EOF
2397 close(SCRIPT);
2398 chmod 0755,"$pbscript";
2399
2400 # Force shutdown of VM/VE
2401 # Force snapshot of VM/VE
2402 pb_script2v($pbscript,$vtype,1,$v,1);
2403}
2404return;
2405}
2406
2407sub pb_install_deps {
2408
2409my $SCRIPT = shift;
2410
2411print {$SCRIPT} << 'EOF';
2412# We need to have that pb_distro_init function
2413# Get it from Project-Builder::Distribution
2414my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
2415print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $arch))."\n";
2416
2417# We may need a proxy configuration. Get it from the local env
2418EOF
2419
2420if (defined $ENV{'http_proxy'}) {
2421 print SCRIPT "\$ENV\{'http_proxy'\}=\"$ENV{'http_proxy'}\";\n";
2422}
2423
2424if (defined $ENV{'ftp_proxy'}) {
2425 print SCRIPT "\$ENV\{'ftp_proxy'\}=\"$ENV{'ftp_proxy'}\";\n";
2426}
2427
2428print {$SCRIPT} << 'EOF';
2429# Get and install pb
2430my $insdm = "rm -rf Date-Manip* ; wget http://search.cpan.org/CPAN/authors/id/S/SB/SBECK/Date-Manip-5.54.tar.gz ; tar xvfz Date-Manip-5.54.tar.gz ; cd Date-Manip* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf Date-Manip*";
2431my $insmb = "rm -rf Module-Build* ; wget http://search.cpan.org/CPAN/authors/id/K/KW/KWILLIAMS/Module-Build-0.2808.tar.gz ; tar xvfz Module-Build-0.2808.tar.gz ; cd Module-Build* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf Module-Build*";
2432my $insfm = "rm -rf File-MimeInfo* ; wget http://search.cpan.org/CPAN/authors/id/P/PA/PARDUS/File-MimeInfo/File-MimeInfo-0.15.tar.gz ; tar xvfz File-MimeInfo-0.15.tar.gz ; cd File-MimeInfo* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf File-MimeInfo*";
2433my $insfb = "rm -rf File-Basedir* ; wget http://search.cpan.org/CPAN/authors/id/P/PA/PARDUS/File-BaseDir-0.03.tar.gz ; tar xvfz File-BaseDir-0.03.tar.gz ; cd File-BaseDir* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf File-BaseDir*";
2434my $insms = "rm -rf Mail-Sendmail* ; wget http://search.cpan.org/CPAN/authors/id/M/MI/MIVKOVIC/Mail-Sendmail-0.79.tar.gz ; tar xvfz Mail-Sendmail-0.79.tar.gz ; cd Mail-Sendmail* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf Mail-Sendmail*";
2435my $cmtdm = "Installing Date-Manip perl module";
2436my $cmtmb = "Installing Module-Build perl module";
2437my $cmtfm = "Installing File-MimeInfo perl module";
2438my $cmtfb = "Installing File-Basedir perl module";
2439my $cmtms = "Installing Perl-Sendmail perl module";
2440my $cmtall = "Installing required modules";
2441
2442if ( $ddir eq "fedora" ) {
2443 pb_system("yum clean all","Cleaning yum env");
2444 if ($dver == 4) {
2445 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker"));
2446 pb_system("$insmb","$cmtmb");
2447 pb_system("$insfm","$cmtfm");
2448 pb_system("$insfb","$cmtfb");
2449 pb_system("$insms","$cmtms");
2450 } else {
2451 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker perl-File-MimeInfo perl-Mail-Sendmail"));
2452 }
2453} elsif ($ddir eq "asianux") {
2454 pb_system("yum clean all","Cleaning yum env");
2455 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"rpm-build wget patch ntp sudo perl-DateManip"));
2456 pb_system("$insmb","$cmtmb");
2457 pb_system("$insfm","$cmtfm");
2458 pb_system("$insfb","$cmtfb");
2459 pb_system("$insms","$cmtms");
2460} elsif (( $dfam eq "rh" ) || ($ddir eq "suse") || ($ddir eq "sles") || (($ddir eq "opensuse") && (($dver eq "10.1") || ($dver eq "10.0"))) || ($ddir eq "slackware")) {
2461 # Suppose pkg are installed already as no online mirror available
2462 pb_system("rpm -e lsb 2>&1 > /dev/null","Removing lsb package");
2463 pb_system("$insdm","$cmtdm");
2464 pb_system("$insmb","$cmtmb");
2465 pb_system("$insfm","$cmtfm");
2466 pb_system("$insfb","$cmtfb");
2467 pb_system("$insms","$cmtms");
2468} elsif ($ddir eq "opensuse") {
2469 # New OpenSuSE
2470 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"make wget patch sudo ntp"));
2471 pb_system("$insmb","$cmtmb");
2472 pb_system("$insfm","$cmtfm");
2473 pb_system("$insfb","$cmtfb");
2474 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"perl-Date-Manip perl-File-HomeDir perl-Mail-Sendmail"));
2475 if ($dver < 11) {
2476 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"ntp"));
2477 } else {
2478 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"sntp"));
2479 }
2480} elsif ( $dfam eq "md" ) {
2481 my $addp = "";
2482 if (($ddir eq "mandrake") && ($dver eq "10.1")) {
2483 pb_system("$insdm","$cmtdm");
2484 } else {
2485 $addp ="perl-DateManip";
2486 }
2487 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"rpm-build wget sudo patch ntp-client perl-File-MimeInfo perl-Mail-Sendmail $addp"));
2488} elsif ( $dfam eq "du" ) {
2489 if (( $dver eq "3.1" ) && ($ddir eq "debian")) {
2490 pb_system("$insfb","$cmtfb");
2491 pb_system("$insfm","$cmtfm");
2492 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"wget patch ssh sudo debian-builder dh-make fakeroot ntpdate libmodule-build-perl libdate-manip-perl libmail-sendmail-perl"));
2493 } else {
2494 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"wget patch openssh-server dpkg-dev sudo debian-builder dh-make fakeroot ntpdate libfile-mimeinfo-perl libmodule-build-perl libdate-manip-perl libmail-sendmail-perl"));
2495 }
2496} elsif ( $dfam eq "gen" ) {
2497 #system "emerge -u system";
2498 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,"wget sudo ntp DateManip File-MimeInfo Mail-Sendmail"));
2499} else {
2500 pb_log(0,"No pkg to install\n");
2501}
2502EOF
2503}
2504
2505sub pb_announce {
2506
2507 # Get all required parameters
2508 my ($pbpackager,$pbrepo,$pbml,$pbsmtp) = pb_conf_get("pbpackager","pbrepo","pbml","pbsmtp");
2509 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
2510 my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
2511 my @pkgs = @$pkg;
2512 my %pkgs;
2513 my $first = 0;
2514
2515 # Command to find packages on repo
2516 my $findstr = "find . ";
2517 # Generated announce files
2518 my @files;
2519
2520 foreach my $pbpkg (@pkgs) {
2521 if ($first != 0) {
2522 $findstr .= "-o ";
2523 }
2524 $first++;
2525 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
2526 $pbver = $pkgv->{$pbpkg};
2527 } else {
2528 $pbver = $ENV{'PBPROJVER'};
2529 }
2530 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
2531 $pbtag = $pkgt->{$pbpkg};
2532 } else {
2533 $pbtag = $ENV{'PBPROJTAG'};
2534 }
2535
2536 # TODO: use virtual/real names here now
2537 $findstr .= "-name \'$pbpkg-$pbver-$pbtag\.*.rpm\' -o -name \'$pbpkg"."_$pbver*\.deb\' -o -name \'$pbpkg-$pbver\.ebuild\' ";
2538
2539 my $chglog;
2540
2541 # Get project info on log file and generate tmp files used later on
2542 pb_cms_init($pbinit);
2543 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
2544 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
2545 $chglog = undef if (! -f $chglog);
2546
2547 open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
2548 my %pb;
2549 $pb{'dtype'} = "announce";
2550 $pb{'realpkg'} = $pbpkg;
2551 $pb{'ver'} = $pbver;
2552 $pb{'tag'} = $pbtag;
2553 $pb{'suf'} = "N/A"; # Should not be empty even if unused
2554 $pb{'date'} = $pbdate;
2555 $pb{'chglog'} = $chglog;
2556 $pb{'packager'} = $pbpackager;
2557 $pb{'proj'} = $ENV{'PBPROJ'};
2558 $pb{'repo'} = $pbrepo;
2559 pb_changelog(\%pb,\*OUT,"yes");
2560 close(OUT);
2561 push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
2562 }
2563 $findstr .= " | grep -Ev \'src.rpm\'";
2564 if ((not defined $testver) || (not defined $testver->{$ENV{'PBPROJ'}}) || ($testver->{$ENV{'PBPROJ'}} !~ /true/i)) {
2565 $findstr .= " | grep -v ./test/";
2566 }
2567
2568 # Prepare the command to run and execute it
2569 open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
2570 print PBS "$findstr\n";
2571 close(PBS);
2572 chmod 0755,"$ENV{'PBTMP'}/pbscript";
2573 pb_send2target("Announce");
2574
2575 # Get subject line
2576 my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
2577 pb_log(0,"Please enter the title of your announce\n");
2578 pb_log(0,"(By default: $sl)\n");
2579 my $sl2 = <STDIN>;
2580 $sl = $sl2 if ($sl2 !~ /^$/);
2581
2582 # Prepare a template of announce
2583 open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
2584 print ANN << "EOF";
2585$sl</p>
2586
2587<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
2588<p>
2589Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
2590</p>
2591<p>
2592EOF
2593 open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to read $ENV{'PBTMP'}/system.log: $!";
2594 my $col = 2;
2595 my $i = 1;
2596 print ANN << 'EOF';
2597<TABLE WIDTH="700" CELLPADDING="0" CELLSPACING="0" BORDER="0">
2598<TR>
2599EOF
2600 while (<LOG>) {
2601 print ANN "<TD><A HREF=\"$pbrepo->{$ENV{'PBPROJ'}}/$_\">$_</A></TD>";
2602 $i++;
2603 if ($i > $col) {
2604 print ANN "</TR>\n<TR>";
2605 $i = 1;
2606 }
2607 }
2608 close(LOG);
2609 print ANN << "EOF";
2610</TR>
2611</TABLE>
2612</p>
2613
2614<p>As usual source packages are also available in the same directory.</p>
2615
2616<p>
2617Changes are :
2618</p>
2619<p>
2620EOF
2621 # Get each package changelog content
2622 foreach my $f (sort(@files)) {
2623 open(IN,"$f") || die "Unable to read $f:$!";
2624 while (<IN>) {
2625 print ANN $_;
2626 }
2627 close(IN);
2628 print ANN "</p><p>\n";
2629 }
2630 print ANN "</p>\n";
2631 close(ANN);
2632
2633 # Allow for modification
2634 pb_system("vi $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
2635
2636 # Store it in DB for external usage (Web pages generation)
2637 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
2638
2639 my $precmd = "";
2640 if (! -f $db) {
2641 $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
2642 }
2643
2644 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
2645 { RaiseError => 1, AutoCommit => 1 })
2646 || die "Unable to connect to $db";
2647
2648 if ($precmd ne "") {
2649 my $sth = $dbh->prepare(qq{$precmd})
2650 || die "Unable to create table into $db";
2651 $sth->execute();
2652 }
2653
2654 # To read whole file
2655 local $/;
2656 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2657 my $announce = <ANN>;
2658 close(ANN);
2659
2660 pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
2661 my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
2662 || die "Unable to insert into $db";
2663 $sth->execute($pbdate, $announce);
2664 $sth->finish();
2665 $dbh->disconnect;
2666
2667 # Then deliver it on the Web
2668 # $TOOLHOME/livwww www
2669
2670 # Mail it to project's ML
2671 open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
2672 print ML << 'EOF';
2673<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
2674
2675<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
2676 <head>
2677 </head>
2678 <body>
2679 <p>
2680EOF
2681 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2682 while(<ANN>) {
2683 print ML $_;
2684 }
2685 print ML << 'EOF';
2686</body>
2687</html>
2688EOF
2689 close(ML);
2690
2691 # To read whole file
2692 local $/;
2693 open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
2694 my $msg = <ANN>;
2695 close(ANN);
2696
2697 # Preparation of headers
2698
2699 my %mail = (
2700 To => $pbml->{$ENV{'PBPROJ'}},
2701 From => $pbpackager->{$ENV{'PBPROJ'}},
2702 Smtp => $pbsmtp->{$ENV{'PBPROJ'}},
2703 Body => $msg,
2704 Subject => "[ANNOUNCE] $sl",
2705 );
2706
2707 # Send mail
2708 sendmail(%mail) or die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
2709}
2710
2711#
2712# Creates a set of HTML file containing the news for the project
2713# based on what has been generated by the pb_announce function
2714#
2715sub pb_web_news2html {
2716
2717 my $dest = shift || $ENV{'PBTMP'};
2718
2719 # Get all required parameters
2720 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
2721
2722 # DB of announces for external usage (Web pages generation)
2723 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
2724
2725 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
2726 { RaiseError => 1, AutoCommit => 1 })
2727 || die "Unable to connect to $db";
2728 # For date handling
2729 $ENV{LANGUAGE}="C";
2730 my $firstjan = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year(), 0, 0, -1);
2731 my $oldfirst = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year()-1, 0, 0, -1);
2732 pb_log(2,"firstjan: $firstjan, oldfirst: $oldfirst, pbdate:$pbdate\n");
2733 my $all = $dbh->selectall_arrayref("SELECT id,date,announce FROM announces ORDER BY date DESC");
2734 my %news;
2735 $news{"cy"} = ""; # current year's news
2736 $news{"ly"} = ""; # last year news
2737 $news{"py"} = ""; # previous years news
2738 $news{"fp"} = ""; # first page news
2739 my $cpt = 4; # how many news for first page
2740 # Extract info from DB
2741 foreach my $row (@$all) {
2742 my ($id, $date, $announce) = @$row;
2743 $news{"cy"} = $news{"cy"}."<p><B>$date</B> $announce\n" if ((($date cmp $pbdate) le 0) && (($firstjan cmp $date) le 0));
2744 $news{"ly"} = $news{"ly"}."<p><B>$date</B> $announce\n" if ((($date cmp $firstjan) le 0) && (($oldfirst cmp $date) le 0));
2745 $news{"py"} = $news{"py"}."<p><B>$date</B> $announce\n" if (($date cmp $oldfirst) le 0);
2746 $news{"fp"} = $news{"fp"}."<p><B>$date</B> $announce\n" if ($cpt > 0);
2747 $cpt--;
2748 }
2749 pb_log(1,"news{fp}: ".$news{"fp"}."\n");
2750 $dbh->disconnect;
2751
2752 # Generate the HTML content
2753 foreach my $pref (keys %news) {
2754 open(NEWS,"> $dest/pb_web_$pref"."news.html") || die "Unable to create $dest/pb_web_$pref"."news.html: $!";
2755 print NEWS "$news{$pref}";
2756 close(NEWS);
2757 }
2758}
2759
2760
2761# Return the SSH key file to use
2762# Potentially create it if needed
2763
2764sub pb_ssh_get {
2765
2766my $create = shift || 0; # Do not create keys by default
2767
2768# Check the SSH environment
2769my $keyfile = undef;
2770
2771# We have specific keys by default
2772$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa";
2773if (!(-e $keyfile) && ($create eq 1)) {
2774 pb_system("ssh-keygen -q -b 1024 -N '' -f $keyfile -t dsa","Generating SSH keys for pb");
2775}
2776
2777$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
2778$keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if (-s "$ENV{'HOME'}/.ssh/id_dsa");
2779$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
2780die "Unable to find your public ssh key under $keyfile" if (not defined $keyfile);
2781return($keyfile);
2782}
2783
2784
2785# Returns the pid of a running VM command using a specific VM file
2786sub pb_check_ps {
2787 my $vmcmd = shift;
2788 my $vmm = shift;
2789 my $vmexist = 0; # FALSE by default
2790
2791 open(PS, "ps auxhww|") || die "Unable to call ps";
2792 while (<PS>) {
2793 next if (! /$vmcmd/);
2794 next if (! /$vmm/);
2795 my ($void1, $void2);
2796 ($void1, $vmexist, $void2) = split(/ +/);
2797 last;
2798 }
2799 return($vmexist);
2800}
2801
2802
2803sub pb_extract_build_files {
2804
2805my $src=shift;
2806my $dir=shift;
2807my $ddir=shift;
2808my $mandatory=shift || "spec";
2809my @files;
2810
2811my $flag = "mayfail" if ($mandatory eq "patch");
2812my $res;
2813
2814if ($src =~ /tar\.gz$/) {
2815 $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
2816} elsif ($src =~ /tar\.bz2$/) {
2817 $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
2818} else {
2819 die "Unknown compression algorithm for $src";
2820}
2821# If not mandatory return now
2822return() if (($res != 0) and ($mandatory eq "patch"));
2823opendir(DIR,"$dir") || die "Unable to open directory $dir";
2824foreach my $f (readdir(DIR)) {
2825 next if ($f =~ /^\./);
2826 # Skip potential patch dir
2827 next if ($f =~ /^pbpatch/);
2828 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
2829 pb_log(2,"mv $dir/$f $ddir\n");
2830 push @files,"$ddir/$f";
2831}
2832closedir(DIR);
2833# Not enough but still a first cleanup
2834pb_rm_rf("$dir");
2835return(@files);
2836}
2837
2838sub pb_list_bfiles {
2839
2840my $dir = shift;
2841my $pbpkg = shift;
2842my $bfiles = shift;
2843my $pkgfiles = shift;
2844my $supfiles = shift;
2845
2846opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
2847foreach my $f (readdir(BDIR)) {
2848 next if ($f =~ /^\./);
2849 $bfiles->{$f} = "$dir/$f";
2850 $bfiles->{$f} =~ s~$ENV{'PBROOTDIR'}~~;
2851 if (defined $supfiles->{$pbpkg}) {
2852 $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
2853 }
2854}
2855closedir(BDIR);
2856}
2857
2858
2859#
2860# Return the list of packages we are working on in a non CMS action
2861#
2862sub pb_get_pkg {
2863
2864my @pkgs = ();
2865
2866my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
2867@pkgs = keys %$var;
2868
2869pb_log(0,"Packages: ".join(',',@pkgs)."\n");
2870return(\@pkgs);
2871}
2872
28731;
Note: See TracBrowser for help on using the repository browser.