source: devel/pb/bin/pb @ 1540

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