source: devel/pb/bin/pb @ 1127

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