source: devel/pb/bin/pb @ 1128

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