source: devel/pb/bin/pb @ 1420

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