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

Last change on this file since 1598 was 1598, checked in by Bruno Cornec, 12 years ago
  • pb now also build itself in a debian 6 VM again (ntp may error)
  • Property svn:executable set to *
File size: 149.4 KB
Line 
1#!/usr/bin/perl -w
2#
3# Project Builder main application
4#
5# $Id$
6#
7# Copyright B. Cornec 2007-2012
8# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
9# Provided under the GPL v2
10
11# Syntax: see at end
12
13use strict 'vars';
14
15# The modules mentioned here are required by pb when used both
16# locally or inside a VE/VM/RM
17# Additional required modules only used locally are called with a require
18# in their respective section
19use Getopt::Long qw(:config auto_abbrev no_ignore_case);
20use Carp qw/confess cluck/;
21use Data::Dumper;
22use English;
23use File::Basename;
24use File::Copy;
25use File::stat;
26use File::Temp qw(tempdir);
27use File::Find;
28use Time::localtime qw(localtime);
29use POSIX qw(strftime);
30use lib qw (lib);
31use ProjectBuilder::Version;
32use ProjectBuilder::Base;
33use ProjectBuilder::Display;
34use ProjectBuilder::Conf;
35use ProjectBuilder::Distribution;
36use ProjectBuilder::VCS;
37use ProjectBuilder::CMS;
38use ProjectBuilder::Env;
39use ProjectBuilder::Filter;
40use ProjectBuilder::Changelog;
41use ProjectBuilder::VE;
42
43# Global variables
44$Global::pb_stop_on_error = 1; # True by default
45$Global::pb_show_sudo = 0; # False by default
46
47my %opts; # CLI Options
48my $action; # action to realize
49my $test = "FALSE"; # Not used
50my $pbforce = 0; # Force VE/VM rebuild
51my $pbsnap = 0; # Do not use snapshot mode for VM/VE by default
52my $pbkeep = 0; # keep temporary directory at the end
53my $option = ""; # Not used
54my @pkgs; # list of packages
55my $pbtag; # Global Tag variable
56my $pbver; # Global Version variable
57my $pbscript; # Name of the script
58my %pbver; # per package
59my %pbtag; # per package
60my $pbrev; # Global REVISION variable
61my $pbaccount; # Login to use to connect to the VM/RM
62my $pbtarget = undef; # Target os-ver-arch you want to build for
63my $pbport; # Port to use to connect to the VM/RM
64my $newver; # New version to create
65my $iso = undef; # ISO image for the VM to create
66
67my @date = pb_get_date();
68my $pbdate = strftime("%Y-%m-%d", @date);
69
70=pod
71
72=head1 NAME
73
74pb, aka project-builder.org - builds packages for your projects
75
76=head1 DESCRIPTION
77
78pb helps you build various packages directly from your project sources.
79Those sources could be handled by a CMS (Configuration Management System)
80such as Subversion, CVS, Git, Mercurial... or being a simple reference to a compressed tar file.
81It's based on a set of configuration files, a set of provided macros to help
82you keeping build files as generic as possible. For example, a single .spec
83file should be required to generate for all rpm based distributions, even
84if you could also have multiple .spec files if required.
85
86=head1 SYNOPSIS
87
88pb [-vhSq][-r pbroot][-p project][[-s script -a account -P port][-t [os-ver-arch]][-m os-ver-arch[,...]]][-g][-i iso] <action> [<pkg1> ...]
89
90pb [--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> ...]
91
92=head1 OPTIONS
93
94=over 4
95
96=item B<-v|--verbose>
97
98Print a brief help message and exits.
99
100=item B<-q|--quiet>
101
102Do not print any output.
103
104=item B<-h|--help>
105
106Print a brief help message and exits.
107
108=item B<-S|--snapshot>
109
110Use the snapshot mode of VMs or VEs
111
112=item B<--man>
113
114Prints the manual page and exits.
115
116=item B<-t|--target os-ver-arch>
117
118Name of the target system you want to build for.
119All if none precised.
120
121=item B<-m|--machine os-ver-arch[,os-ver-arch,...]>
122
123Name of the Virtual Machines (VM), Virtual Environments (VE) or Remote Machines (RM)
124you want to build on (comma separated).
125All if none precised (or use the env variable PBV).
126
127=item B<-s|--script script>
128
129Name of the script you want to execute on the related VMs/VEs/RMs.
130
131=item B<-g|--nographic>
132
133Do not launch VMs in graphical mode.
134
135=item B<-i|--iso iso_image>
136
137Name of the ISO image of the distribution you want to install on the related VMs.
138
139=item B<-a|--account account>
140
141Name of the account to use to connect on the related VMs/RMs.
142
143=item B<-P|--port port_number>
144
145Port number to use to connect on the related VMs/RMs.";
146
147=item B<-p|--project project_name>
148
149Name of the project you're working on (or use the env variable PBPROJ)
150
151=item B<-r|--revision revision>
152
153Path Name of the project revision under the CMS (or use the env variable PBROOT)
154
155=item B<-V|--version new_version>
156
157New version of the project to create based on the current one.
158
159=item B<-k|--keep>
160
161Keep the temporary dir where files have been created in or der to help debug
162
163=item B<--rebuild>
164
165Only valid with the checkssh action, it alllows to automatically relaunch the build of the failed packages
166
167=item B<--no-stop-on-error>
168
169Continue through errors with best effort.
170
171=back
172
173=head1 ARGUMENTS
174
175<action> can be:
176
177=over 4
178
179=item B<sbx2build>
180
181Create tar files for the project under your CMS.
182Current state of the exported content is taken.
183CMS supported are SVN, SVK, CVS, Git and Mercurial
184parameters are packages to build
185if not using default list
186
187=item B<cms2build>
188
189Create tar files for the project under your CMS.
190Current state of the CMS is taken.
191CMS supported are SVN, SVK, CVS, Git and Mercurial
192parameters are packages to build
193if not using default list
194
195=item B<build2pkg>
196
197Create packages for your running distribution
198
199=item B<cms2pkg>
200
201cms2build + build2pkg
202
203=item B<sbx2pkg>
204
205sbx2build + build2pkg
206
207=item B<build2ssh>
208
209Send the tar files to a SSH host
210
211=item B<sbx2ssh>
212
213sbx2build + build2ssh
214
215=item B<cms2ssh>
216
217cms2build + build2ssh
218
219=item B<pkg2ssh>
220
221Send the packages built to a SSH host
222
223=item B<build2vm>
224
225Create packages in VMs, launching them if needed
226and send those packages to a SSH host once built
227VM type supported are QEMU and KVM
228
229=item B<build2ve>
230
231Create packages in VEs, creating it if needed
232and send those packages to a SSH host once built
233
234=item B<build2rm>
235
236Create packages in RMs, which should pre-exist,
237and send those packages to a SSH host once built
238RM means Remote Machine, and could be a physical or Virtual one.
239This is one buildfarm integration for pb.
240
241=item B<sbx2vm>
242
243sbx2build + build2vm
244
245=item B<sbx2ve>
246
247sbx2build + build2ve
248
249=item B<sbx2rm>
250
251sbx2build + build2rm
252
253=item B<cms2vm>
254
255cms2build + build2vm
256
257=item B<cms2ve>
258
259cms2build + build2ve
260
261=item B<cms2rm>
262
263cms2build + build2rm
264
265=item B<launchvm>
266
267Launch one virtual machine
268
269=item B<launchve>
270
271Launch one virtual environment
272
273=item B<script2vm>
274
275Launch one virtual machine if needed
276and executes a script on it
277
278=item B<script2ve>
279
280Execute a script in a virtual environment
281
282=item B<script2rm>
283
284Execute a script on a remote machine
285
286=item B<newvm>
287
288Create a new virtual machine
289
290=item B<newve>
291
292Create a new virtual environment
293
294=item B<setupvm>
295
296Setup a virtual machine for pb usage
297
298=item B<setupve>
299
300Setup a virtual environment for pb usage
301
302=item B<setuprm>
303
304Setup a remote machine for pb usage
305
306=item B<sbx2setupvm>
307
308Setup a virtual machine for pb usage using the sandbox version of pb instead of the latest stable
309Reserved to dev team.
310
311=item B<sbx2setupve>
312
313Setup a virtual environment for pb usage using the sandbox version of pb instead of the latest stable
314Reserved to dev team.
315
316=item B<sbx2setuprm>
317
318Setup a remote machine for pb usage using the sandbox version of pb instead of the latest stable
319Reserved to dev team.
320
321=item B<snapvm>
322
323Snapshot a virtual machine for pb usage
324
325=item B<snapve>
326
327Snapshot a virtual environment for pb usage
328
329=item B<updatevm>
330
331Update the distribution in the virtual machine
332
333=item B<updateve>
334
335Update the distribution in the virtual environment
336
337=item B<updaterm>
338
339Update the distribution in the remote machine
340
341=item B<test2pkg>
342
343Test a package locally
344
345=item B<test2vm>
346
347Test a package in a virtual machine
348
349=item B<test2ve>
350
351Test a package in a virtual environment
352
353=item B<test2rm>
354
355Test a package in a remote machine
356
357=item B<checkssh>
358
359Check the delivery of the packages on the repository
360
361=item B<newver>
362
363Create a new version of the project derived
364from the current one
365
366=item B<newproj>
367
368Create a new project and a template set of
369configuration files under pbconf
370
371=item B<announce>
372
373Announce the availability of the project through various means
374
375=item B<sbx2webssh>
376
377Create tar files for the website under your CMS.
378Current state of the exported content is taken.
379Deliver the content to the target server using ssh from the exported dir.
380
381=item B<cms2webssh>
382
383Create tar files for the website from your CMS.
384Deliver the content to the target server using ssh from the DVCS.
385
386=item B<sbx2webpkg>
387
388Create tar files for the website under your CMS.
389Current state of the exported content is taken.
390
391=item B<cms2webpkg>
392
393Create tar files for the website under your CMS.
394
395=item B<getconf>
396
397Print the full configuration parameters as found in the various configuration files. help to debug conf issues.
398
399=item B<clean>
400
401Purge the build and delivery directories related to the current project
402
403=item B<cleanssh>
404
405Purge the ssh server of its packages (only for testver and test packages)
406
407=back
408
409<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).
410
411=head1 WEB SITES
412
413The 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/>.
414
415=head1 USER MAILING LIST
416
417None exists for the moment.
418
419=head1 CONFIGURATION FILES
420
421Each pb user may have a configuration in F<$HOME/.pbrc>. The values in this file may overwrite any other configuration file value.
422
423Here is an example of such a configuration file:
424
425 #
426 # Define for each project the URL of its pbconf repository
427 # No default option allowed here as they need to be all different
428 #
429 # URL of the pbconf content
430 # This is the format of a classical URL with the extension of additional schema such as
431 # svn+ssh, cvs+ssh, ...
432 #
433 pbconfurl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe/pbconf
434
435 # This is normaly defined in the project's configuration file
436 # Url of the project
437 #
438 pburl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe
439
440 # All these URLs needs to be defined here as the are the entry point
441 # for how to build packages for the project
442 #
443 pbconfurl pb = svn+ssh://svn.project-builder.org/mondo/svn/pb/pbconf
444 pbconfurl mondorescue = svn+ssh://svn.project-builder.org/mondo/svn/project-builder/mondorescue/pbconf
445 pbconfurl collectl = svn+ssh://bruno@svn.mondorescue.org/mondo/svn/project-builder/collectl/pbconf
446 pbconfurl netperf = svn+ssh://svn.mondorescue.org/mondo/svn/project-builder/netperf/pbconf
447
448 # Under that dir will take place everything related to pb
449 # If you want to use VMs/chroot/..., then use $ENV{'HOME'} to make it portable
450 # to your VMs/chroot/...
451 # if not defined then /var/cache
452 pbdefdir default = $ENV{'HOME'}/project-builder
453 pbdefdir pb = $ENV{'HOME'}
454 pbdefdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
455 pbdefdir mondorescue = $ENV{'HOME'}/mondo/svn
456
457 # pbconfdir points to the directory where the CMS content of the pbconfurl is checked out
458 # If not defined, pbconfdir is under pbdefdir/pbproj/pbconf
459 pbconfdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs/pbconf
460 pbconfdir mondorescue = $ENV{'HOME'}/mondo/svn/pbconf
461
462 # pbdir points to the directory where the CMS content of the pburl is checked out
463 # If not defined, pbdir is under pbdefdir/pbproj
464 # Only defined if we have access to the dev of the project
465 pbdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
466 pbdir mondorescue = $ENV{'HOME'}/mondo/svn
467
468 # -daemonize doesn't work with qemu 0.8.2
469 vmopt default = -m 384
470
471=head1 COMMAND DETAILS
472
473=head2 newproj
474
475The newproj command creates a new project-builder project. To run this command you first need to define two variables in your ~/.pbrc file:
476
477 pbconfurl I<project> = file:///home/anderse/.git/project-builder-config/I<project>
478 pbdefdir default = $ENV{HOME}/cache-project-builder
479
480The first line defines the version controlled configuration information and the second defines the root directory for project-builder to use.
481
482You can then run the command:
483
484 % pb -p I<$project> -r I<$version> newproj I<$pkg>
485
486to create the new project. Running the newproj command will then generate the file $pbdefdir/$project/pbconf/$version/$project.pb, and the directory $pbdefdir/$project/pbconf/$version/$pkg. You will need to edit those files to make the later commands work.
487
488=head2 cms2build
489
490The cms2build command takes your files from the content management system and makes the two tar files that are necessary for building files. You need to have run the newproj command first. Then there are several steps for running this command:
491
492=over 4
493
494=item Update your $project.pb configuration file.
495
496You need to set the pburl, pbrepo, pbwf, pbpackager, projver, projtag, testver, delivery, and defpkgdir lines as described in the configuration file. The pburl entry is used to find the source for your package. The pbrepo entry is used to build the .repo or .sources.list files for use by downloaders of the package. The pbwf entry indicates that the source tar file is named by I<package-name>-I<version>. The pbpackager entry will be stored in the packages and should be you or your team. The projver/projtag entries indicate the version of the software and the version of the packaging scripts. The testver entry when true indicates that the package is in a test version, so no log file is computed (can be long), and version is made up using a timstamp. The delivery entry gives the subdirectory under which the packages will be delivered on the repository, and the defpkgdir entry corresponds to the local subdirectory hosting the package content.
497
498For example:
499
500 pburl Lintel = file:///home/anderse/projects/Lintel-0.2012.02.28.tar.gz
501 pbrepo Lintel = http://tesla.hpl.hp.com/opensource
502 pbwf Lintel = 1
503 pbpackager Lintel = Eric Anderson <eric.anderson4@hp.com>
504 projver Lintel = 0.2012.02.28
505 projtag Lintel = 1
506 testver Lintel = false
507 delivery Lintel = production
508 defpkgdir Lintel = Lintel-0.2012.02.28
509
510=item Create the build .tar.gz files:
511
512Then you need to take those files and create the initial tar files. Run a command like:
513
514 % pb -p $project -r $version cms2build
515
516To create the $pbdefdir/$project/delivery/$project-$version.{,pbconf}.tar.gz files, the $version-$projtag.pb and pbrc files in the same directory.
517
518=back
519
520=head2 build2pkg
521
522The build2pkg command takes the tar files created in the cms2build step and attempts to build binary packages for your current operating system. There are two steps:
523
524=over 4
525
526=item Update your filters and build files.
527
528You probably need to edit the files describing the build steps in one of the $pbdefdir/$project/pbconf/$version/$project/{deb,rpm,pkg} directories and the filters in $pbdefdir/$project/pbconf/$version/pbfilter. Note that you can define additional filters and transforms in the *.pbf files. The build files will be filtered by the filters defined in the *.pbf files to generate the inputs to the build step. Therefore, if you change those files, you need to re-run the cms2build step.
529
530=item Build the package.
531
532Then you can run a command like:
533
534 % pb -p $project -r $version build2pkg
535
536To create the files in $project/build that comprise your binary package(s).
537
538=back
539
540=head2 newve
541
542The newve command creates a new virtual environment, i.e. a chrooted OS for building packages. Using a virtual environment is an efficient way to build packages on a related set of operating systems. The OS's have to be related because the kernel will be shared. Steps:
543
544=over 4
545
546=item Update ~/.pbrc
547
548Update your ~/.pbrc file to specify the vepath, velist, velogin, and vetype variables, e.g.:
549
550 vepath default = $ENV{HOME}/cache-project-builder/chroot
551 velist default = debian-6.0-i386
552 velogin default = pb
553 vetype default = chroot
554
555If you are building for rpm style OS's, update the verpmtype option, and install the appropriate tool.
556
557 verpmtype default = rpmbootstrap
558
559You may also choose to specify a mirror for the OS packages, and optionally http/ftp proxies. You can specify the proxies either through environment variables ($http_proxy/$ftp_proxy) or in the configuration file. The configuration file will be used if no corresponding environment variable has been set. For example, for debian and with a local squid proxy:
560
561 rbsmirrorsrv debian = http://mirrors1.kernel.org/debian/
562 http_proxy default = http://localhost:3128/
563 ftp_proxy default = http://localhost:3128/
564
565=item Run the cms2build command
566
567If you have deleted your $package/delivery directory, re-run the cms2build command as in the earlier step. This step is necessary to generate the I<package>/delivery/pbrc file.
568
569=item Create the new virtual environment
570
571Initialize the new operating system. This step will install the core OS packages for the virtual environment, e.g.:
572
573 % pb -v -p $project -m debian-6.0-i386 newve
574
575=back
576
577=head2 setupve
578
579The setupve command prepares a virtual environment for use by project builder. In particular it installs project-builder from the packages into the virtual environment. Two sub-steps are necessary:
580
581=over 4
582
583=item Update $project.pb
584
585You need to have a sshhost entry for setupve to work, so add one, even an invalid one, e.g.:
586
587 sshhost $project = foo.example.org
588
589=item Setup the virtual environment
590
591 % pb -v -p $project -m debian-6.0-i386 setupve
592
593If you prefer to install the current SVN version of project builder, you can substitute the setupve option by the sbx2setupv one.
594
595=back
596
597=head2 build2ve
598
599The build2ve command is similar to the build2pkg command in that it will take the sources created by cms2build and turn them into binary packages. The command has two differences. First, it creates the packages in a virtual environment, i.e. the one made by an earlier setupve setup. Second it copies the resulting packages to a repository and builds the repository meta-data needed.
600
601Three sub-steps are needed:
602
603=over 4
604
605=item Update $project.pb
606
607You need to have a valid sshdir and sshhost entry for build2ve to work, so add them. Note that you need to be able to ssh from the host you run the command on to the repository host, preferably without needing to type in a password, so using ssh-agent or having a special passwordless project-builder ssh key will make this step easier.
608
609 sshhost $project = localhost
610 sshdir $project = $home/cache-project-builder/repos
611
612You may also need to specify additional repository files to use or rpms to install. Note the URL for repositories is not the URL of the repository, but the URL of a file that can be put in the yum.repos.d or apt.sources.d directory.
613
614 addrepo centos-5-i386 = http://localhost/pb/centos-extras.repo,http://mirror.centos.org/centos/5/extras/i386/RPMS/chrpath-0.13-3.el5.centos.i386.rpm
615
616=item Update your filters and build files
617
618You may need to update your filter files (*.pbf) as in the build2pkg step if you are building for a new OS or architecture.
619
620=item Build the packages and copy them to the repository
621
622 % pb -v -p $project -m debian-6.0-i386 build2ve
623
624=back
625
626*Debugging:* If the build fails (and you did not specify the --no-stop-on-error) option, then the virtual environment and scripts should still be present and configured to build the package. You can run a command like 'sudo setarch i386 chroot $path bash' in order to get into the environment. In your log you should see a command like that. From there you can go into the /home/pb directory as the pb user and run the same style of pb commands as you did when doing build2pkg. This will help you figure out what has gone wrong in the build in the virtual environment.
627
628=head1 AUTHORS
629
630The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
631
632=head1 COPYRIGHT
633
634Project-Builder.org is distributed under the GPL v2.0 license
635described in the file C<COPYING> included with the distribution.
636
637=cut
638
639# ---------------------------------------------------------------------------
640
641my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
642my $appname = "pb";
643
644# Initialize the syntax string
645
646pb_syntax_init("$appname (aka project-builder.org) Version $projectbuilderver-$projectbuilderrev\n");
647
648GetOptions("help|?|h+" => \$opts{'h'},
649 "man" => \$opts{'man'},
650 "verbose|v+" => \$opts{'v'},
651 "snapshot|S" => \$opts{'S'},
652 "quiet|q" => \$opts{'q'},
653 "log-files|l=s" => \$opts{'l'},
654 "force|f" => \$opts{'f'},
655 "account|a=s" => \$opts{'a'},
656 "revision|r=s" => \$opts{'r'},
657 "script|s=s" => \$opts{'s'},
658 "machines|mock|m=s" => \$opts{'m'},
659 "target|t:s" => \$opts{'t'},
660 "nographic|g" => \$opts{'g'},
661 "port|P=i" => \$opts{'P'},
662 "project|p=s" => \$opts{'p'},
663 "rebuild" => \$opts{'rebuild'},
664 "iso|i=s" => \$opts{'i'},
665 "version|V=s" => \$opts{'V'},
666 "keep|k" => \$opts{'k'},
667 "stop-on-error!" => \$Global::pb_stop_on_error,
668) || pb_syntax(-1,0);
669
670if (defined $opts{'l'}) {
671 open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
672 $pbLOG = \*pbLOG;
673 $pbdebug = 0 if ($pbdebug == -1);
674 }
675pb_log_init($pbdebug, $pbLOG);
676
677if (defined $opts{'h'}) {
678 pb_syntax(0,$opts{'h'}-1);
679}
680if (defined $opts{'man'}) {
681 pb_syntax(0,2);
682}
683if (defined $opts{'v'}) {
684 $pbdebug = $opts{'v'};
685}
686if (defined $opts{'f'}) {
687 $pbforce=1;
688}
689if (defined $opts{'q'}) {
690 $pbdebug=-1;
691}
692if (defined $opts{'S'}) {
693 $pbsnap=1;
694}
695if (defined $opts{'k'}) {
696 $pbkeep=1;
697}
698pb_display_init("text","");
699
700# Handle root of the project if defined
701if (defined $opts{'r'}) {
702 $ENV{'PBROOTDIR'} = $opts{'r'};
703}
704# Handle virtual machines if any
705if (defined $opts{'m'}) {
706 $ENV{'PBV'} = $opts{'m'};
707}
708if (defined $opts{'s'}) {
709 $pbscript = $opts{'s'};
710}
711if (defined $opts{'a'}) {
712 $pbaccount = $opts{'a'};
713 die "option -a requires a -s script option" if (not defined $pbscript);
714}
715if (defined $opts{'P'}) {
716 $pbport = $opts{'P'};
717}
718if (defined $opts{'V'}) {
719 $newver = $opts{'V'};
720}
721if (defined $opts{'i'}) {
722 $iso = $opts{'i'};
723}
724if (defined $opts{'t'}) {
725 $pbtarget = $opts{'t'};
726}
727
728# Get Action
729$action = shift @ARGV;
730die pb_syntax(-1,1) if (not defined $action);
731
732my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
733my $pbinit = undef;
734$pbinit = 1 if ($action =~ /^newproj$/);
735
736# Handles project name if any
737# And get global params
738($filteredfiles, $supfiles, $defpkgdir, $extpkgdir) = pb_env_init($opts{'p'},$pbinit,$action,$pbkeep);
739
740#
741# Check for command requirements
742#
743my ($req,$opt,$pbpara,$pbshowsudo) = pb_conf_get_if("oscmd","oscmdopt","pbparallel","pbshowsudo");
744pb_check_requirements($req,$opt,$appname);
745
746$Global::pb_show_sudo = 1 if ((defined $pbshowsudo) && (defined $pbshowsudo->{$ENV{'PBPROJ'}}) && ($pbshowsudo->{$ENV{'PBPROJ'}} =~ /true/oi));
747
748#
749# Check if we can launch some actions in // with Parallel::ForkManager
750#
751my $pbparallel = $pbpara->{$appname} if (defined $pbpara);
752if (not defined $pbparallel) {
753 eval
754 {
755 require Sys::CPU;
756 Sys::CPU->import();
757 };
758 if ($@) {
759 # Sys::CPU not found, defaulting to 1
760 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");
761 $pbparallel = 1;
762 } else {
763 # Using the number of cores
764 $pbparallel = Sys::CPU::cpu_count();
765 pb_log(1,"Using parallel mode with $pbparallel processes\n");
766 }
767}
768
769eval
770{
771 require Parallel::ForkManager;
772 Parallel::ForkManager->import();
773};
774# Parallel::ForkManager not found so no // actions
775if ($@) {
776 $pbparallel = undef;
777 pb_log(1,"ADVISE: Install Parallel::ForkManager to benefit from automatic parallelism optimization.\nOnly 1 process at a time for the moment\n");
778}
779
780pb_log(0,"Project: $ENV{'PBPROJ'}\n");
781pb_log(0,"Action: $action\n");
782
783# Act depending on action
784if ($action =~ /^cms2build$/) {
785 pb_cms2build("CMS");
786} elsif ($action =~ /^sbx2build$/) {
787 pb_cms2build("SandBox");
788} elsif ($action =~ /^build2pkg$/) {
789 pb_build2pkg();
790} elsif ($action =~ /^cms2pkg$/) {
791 pb_cms2build("CMS");
792 pb_build2pkg();
793} elsif ($action =~ /^sbx2pkg$/) {
794 pb_cms2build("SandBox");
795 pb_build2pkg();
796} elsif ($action =~ /^build2ssh$/) {
797 pb_build2ssh();
798} elsif ($action =~ /^cms2ssh$/) {
799 pb_cms2build("CMS");
800 pb_build2ssh();
801} elsif ($action =~ /^sbx2ssh$/) {
802 pb_cms2build("SandBox");
803 pb_build2ssh();
804} elsif ($action =~ /^pkg2ssh$/) {
805 pb_pkg2ssh();
806} elsif ($action =~ /^build2rm$/) {
807 pb_build2v("rm","build");
808} elsif ($action =~ /^build2ve$/) {
809 pb_build2v("ve","build");
810} elsif ($action =~ /^build2vm$/) {
811 pb_build2v("vm","build");
812} elsif ($action =~ /^cms2rm$/) {
813 pb_cms2build("CMS");
814 pb_build2v("rm","build");
815} elsif ($action =~ /^cms2ve$/) {
816 pb_cms2build("CMS");
817 pb_build2v("ve","build");
818} elsif ($action =~ /^sbx2rm$/) {
819 pb_cms2build("SandBox");
820 pb_build2v("rm","build");
821} elsif ($action =~ /^sbx2ve$/) {
822 pb_cms2build("SandBox");
823 pb_build2v("ve","build");
824} elsif ($action =~ /^cms2vm$/) {
825 pb_cms2build("CMS");
826 pb_build2v("vm","build");
827} elsif ($action =~ /^sbx2vm$/) {
828 pb_cms2build("SandBox");
829 pb_build2v("vm","build");
830} elsif ($action =~ /^launchvm$/) {
831 pb_launchv("vm",$ENV{'PBV'},0);
832} elsif ($action =~ /^launchve$/) {
833 pb_launchv("ve",$ENV{'PBV'},0);
834} elsif ($action =~ /^script2vm$/) {
835 pb_script2v($pbscript,"vm");
836} elsif ($action =~ /^script2ve$/) {
837 pb_script2v($pbscript,"ve");
838} elsif ($action =~ /^script2rm$/) {
839 pb_script2v($pbscript,"rm");
840} elsif ($action =~ /^newver$/) {
841 pb_newver();
842} elsif ($action =~ /^newve$/) {
843 pb_launchv("ve",$ENV{'PBV'},1);
844} elsif ($action =~ /^newvm$/) {
845 pb_launchv("vm",$ENV{'PBV'},1);
846 pb_log(0, "Please ensure that sshd is running in your VM by default\n");
847 pb_log(0, "and that it allows remote root login (PermitRootLogin yes in /etc/ssh/sshd_config)\n");
848 pb_log(0, "Also ensure that network is up, firewalling correctly configured\n");
849 pb_log(0, "and perl, sudo, ntpdate and scp/ssh installed\n");
850 pb_log(0, "You should then be able to login with ssh -p VMPORT root\@localhost (if VM started with pb)\n");
851} elsif ($action =~ /^setuprm$/) {
852 pb_setup2v("rm");
853} elsif ($action =~ /^setupve$/) {
854 pb_setup2v("ve");
855} elsif ($action =~ /^setupvm$/) {
856 pb_setup2v("vm");
857} elsif ($action =~ /^sbx2setuprm$/) {
858 die "This feature is limited to the pb project" if ($ENV{'PBPROJ'} ne $appname);
859 pb_cms2build("SandBox");
860 pb_setup2v("rm","SandBox");
861} elsif ($action =~ /^sbx2setupve$/) {
862 die "This feature is limited to the pb project" if ($ENV{'PBPROJ'} ne $appname);
863 pb_cms2build("SandBox");
864 pb_setup2v("ve","SandBox");
865} elsif ($action =~ /^sbx2setupvm$/) {
866 die "This feature is limited to the pb project" if ($ENV{'PBPROJ'} ne $appname);
867 pb_cms2build("SandBox");
868 pb_setup2v("vm","SandBox");
869} elsif ($action =~ /^updaterm$/) {
870 pb_update2v("rm");
871} elsif ($action =~ /^updateve$/) {
872 pb_update2v("ve");
873} elsif ($action =~ /^updatevm$/) {
874 pb_update2v("vm");
875} elsif ($action =~ /^snapve$/) {
876 pb_snap2v("ve");
877} elsif ($action =~ /^snapvm$/) {
878 pb_snap2v("vm");
879} elsif ($action =~ /^test2pkg$/) {
880 pb_test2pkg();
881} elsif ($action =~ /^test2rm$/) {
882 pb_build2v("rm","test");
883} elsif ($action =~ /^test2ve$/) {
884 pb_build2v("ve","test");
885} elsif ($action =~ /^test2vm$/) {
886 pb_build2v("vm","test");
887} elsif ($action =~ /^newproj$/) {
888 # Nothing to do - already done in pb_env_init
889} elsif ($action =~ /^getconf$/) {
890 my $pbos = pb_distro_get_context();
891 pb_conf_print();
892} elsif ($action =~ /^clean$/) {
893 pb_clean();
894} elsif ($action =~ /^announce$/) {
895 # For announce only. Require avoids the systematic load of these modules
896 require DBI;
897 require DBD::SQLite;
898
899 pb_announce("Announce");
900} elsif ($action =~ /^cleanssh$/) {
901 pb_announce("Clean");
902} elsif ($action =~ /^checkssh$/) {
903 pb_announce("Check");
904} elsif ($action =~ /^sbx2webpkg$/) {
905 require DBI;
906 require DBD::SQLite;
907
908 pb_cms2build("SandBox","Web");
909} elsif ($action =~ /^sbx2webssh$/) {
910 require DBI;
911 require DBD::SQLite;
912
913 pb_cms2build("SandBox","Web");
914 pb_send2target("Web");
915} elsif ($action =~ /^cms2webpkg$/) {
916 require DBI;
917 require DBD::SQLite;
918
919 pb_cms2build("CMS","Web");
920} elsif ($action =~ /^cms2webssh$/) {
921 require DBI;
922 require DBD::SQLite;
923
924 pb_cms2build("CMS","Web");
925 pb_send2target("Web");
926} else {
927 pb_log(0,"\'$action\' is not available\n");
928 pb_syntax(-2,1);
929}
930
931sub pb_cms2build {
932
933 my $param = shift || undef;
934 my $web = shift || undef;
935
936 my $pkg;
937 my @pkgs;
938 my $webdir;
939
940 my %pkgs;
941 my $pb; # Structure to store conf info
942
943 die "pb_cms2build requires a parameter: SandBox or CMS" if (not defined $param);
944
945 # If Website, then pkg is only the website
946 if (defined $web) {
947 ($webdir) = pb_conf_get("webdir");
948 pb_log(2,"webdir: ".Dumper($webdir)."\n");
949 $pkgs[0] = $webdir->{$ENV{'PBPROJ'}};
950 $extpkgdir = $webdir;
951 pb_log(0,"Package: $pkgs[0]\n");
952 } else {
953 $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
954 @pkgs = @$pkg;
955 }
956
957 my ($scheme, $uri) = pb_cms_init($pbinit,$param);
958
959 # We need 2 lines here
960 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
961
962 # declare packager and repo for filtering
963 my ($tmp1, $tmp2) = pb_conf_get("pbpackager","pbrepo");
964 $ENV{'PBPACKAGER'} = $tmp1->{$ENV{'PBPROJ'}};
965 $ENV{'PBREPO'} = $tmp2->{$ENV{'PBPROJ'}};
966 my ($delivery) = pb_conf_get_if("delivery");
967 $delivery->{$ENV{'PBPROJ'}} = "" if (not defined $delivery->{$ENV{'PBPROJ'}});
968
969 # If we deal with a test dir, we want to keep the date in tar file and dir name
970 my $pbextdir = "";
971 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
972 $pbextdir = strftime("%Y%m%d%H%M%S", @date);
973 }
974
975 foreach my $pbpkg (@pkgs) {
976 $ENV{'PBPKG'} = $pbpkg;
977
978 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
979 $pbver = $pkgv->{$pbpkg};
980 } else {
981 $pbver = $ENV{'PBPROJVER'};
982 }
983 # If it's a test version, then tag == 0
984 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
985 $pbtag = "0";
986 $ENV{'PBPROJTAG'} = $pbtag;
987 } elsif ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
988 $pbtag = $pkgt->{$pbpkg};
989 } else {
990 $pbtag = $ENV{'PBPROJTAG'};
991 }
992
993 $pbrev = $ENV{'PBREVISION'};
994 pb_log(0,"\n");
995 pb_log(0,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
996 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
997
998 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir";
999 # Create the structure if needed
1000 pb_mkdir_p($dest);
1001 # Clean up dest if necessary. The export will recreate it
1002 pb_rm_rf($dest) if (-d $dest);
1003
1004 # Export CMS tree for the concerned package to dest
1005 # And generate some additional files
1006 $OUTPUT_AUTOFLUSH=1;
1007
1008 # computes in which dir we have to work
1009 my $dir = $defpkgdir->{$pbpkg};
1010 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
1011 $dir = $webdir->{$ENV{'PBPROJ'}} if (defined $web);
1012 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);
1013 pb_log(2,"def:".Dumper($defpkgdir)."ext: ".Dumper($extpkgdir)."dir: $dir\n");
1014
1015 # Exporting content from CMS
1016 my $sourcedir = undef;
1017 my $sourceuri = $uri;
1018 if ($param eq "SandBox") {
1019 # Point to the local instance
1020 $sourcedir = "$ENV{'PBDIR'}/$dir";
1021 } else {
1022 # Get it from a subdir of the URI with same version as localy but different root
1023 # Only if using a real CMS
1024 my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
1025 if (($scheme !~ /^file/) && ($scheme !~ /^(ht|f)tp/)) {
1026 $sourceuri = "$ENV{'PBDIR'}/$dir";
1027 $sourceuri =~ s|^$ENV{'PBPROJDIR'}/|$uri/|;
1028 }
1029 }
1030 my $preserve = pb_vcs_export($sourceuri,$sourcedir,$dest);
1031
1032 # Generated fake content for test versions to speed up stuff
1033 my $chglog;
1034
1035 # Get project info on authors and log file
1036 # TODO: Make it CMS aware
1037 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
1038 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
1039 $chglog = undef if (! -f $chglog);
1040
1041 # TODO: Make it CMS aware
1042 my $authors = "$ENV{'PBROOTDIR'}/$pbpkg/pbauthors";
1043 $authors = "$ENV{'PBROOTDIR'}/pbauthors" if (! -f $authors);
1044 $authors = "/dev/null" if (! -f $authors);
1045
1046 # Extract cms log history and store it
1047 if ((defined $chglog) && (! -f "$dest/NEWS")) {
1048 pb_log(2,"Generating NEWS file from $chglog\n");
1049 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
1050 }
1051 pb_cms_log($scheme,"$ENV{'PBDIR'}/$dir",$dest,$chglog,$authors,$testver);
1052
1053 my %build;
1054 # We want to at least build for the underlying distro
1055 # except if a target was given, in which case we only build for it
1056 # if -t was passed without target then build for the native distro.
1057 my $pbos = pb_distro_get_context($pbtarget);
1058 my $tmpl = pb_get_distros($pbos,$pbtarget);
1059
1060 # Setup $pb structure to allow filtering later on, on files using that structure
1061 $pb->{'tag'} = $pbtag;
1062 $pb->{'rev'} = $pbrev;
1063 $pb->{'ver'} = $pbver;
1064 $pb->{'pkg'} = $pbpkg;
1065 $pb->{'suf'} = $pbos->{'suffix'};
1066 $pb->{'realpkg'} = $pbpkg;
1067 $pb->{'date'} = $pbdate;
1068 $pb->{'defpkgdir'} = $defpkgdir;
1069 $pb->{'extpkgdir'} = $extpkgdir;
1070 $pb->{'chglog'} = $chglog;
1071 $pb->{'extdir'} = $pbextdir;
1072 $pb->{'packager'} = $ENV{'PBPACKAGER'};
1073 $pb->{'proj'} = $ENV{'PBPROJ'};
1074 $pb->{'repo'} = "$ENV{'PBREPO'}/$delivery->{$ENV{'PBPROJ'}}";
1075 $pb->{'patches'} = ();
1076 $pb->{'sources'} = ();
1077
1078 my $tmpd = "$ENV{'PBTMP'}/cms.$$";
1079 pb_mkdir_p($tmpd) if (defined $pbparallel);
1080
1081 # Get only all.pbf filter at this stage for pbinit
1082 my $ptr = pb_get_filters($pbpkg);
1083
1084 # Do not do that for website
1085 if (not defined $web) {
1086 my %virt;
1087 # De-duplicate similar VM/VE/RM
1088 foreach my $d (split(/,/,$tmpl)) {
1089 # skip ill-formatted vms (name-ver-arch)
1090 next if ($d !~ /-/);
1091 $virt{$d} = $d;
1092 }
1093
1094 # Try to use // processing here
1095 my $all_ok = 1;
1096 my $pm = new Parallel::ForkManager($pbparallel) if (defined $pbparallel);
1097 $pm->run_on_finish(sub { my ($pid, $code, $id, $signal, $dump) = @_;
1098 $all_ok = 0 unless (($code == 0) && ($signal == 0) && ($dump == 0)); }) if (defined $pbparallel);
1099
1100 pb_log(0,"Preparing delivery ...\n");
1101 foreach my $v (keys %virt) {
1102 $pm->start and next if (defined $pbparallel);
1103
1104 # Distro context
1105 my $pbos = pb_distro_get_context($v);
1106
1107 $pb->{'pbos'} = $pbos;
1108 $pb->{'suf'} = $pbos->{'suffix'};
1109 pb_log(3,"DEBUG: pb: ".Dumper($pb)."\n");
1110
1111 # Get all filters to apply
1112 $ptr = pb_get_filters($pbpkg,$pbos);
1113
1114 pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
1115
1116 # We need to compute the real name of the package
1117 my $pbrealpkg = pb_cms_get_real_pkg($pbpkg,$pbos->{'type'});
1118 $pb->{'realpkg'} = $pbrealpkg;
1119 pb_log(1,"Virtual package $pbpkg has a real package name of $pbrealpkg on $pbos->{'name'}-$pbos->{'version'}\n") if ($pbrealpkg ne $pbpkg);
1120
1121 # Filter build files from the less precise up to the most with overloading
1122 # Filter all files found, keeping the name, and generating in dest
1123
1124 # Find all build files first relatively to PBROOTDIR
1125 # Find also all specific files referenced in the .pb conf file
1126 my %bfiles = ();
1127 my %pkgfiles = ();
1128 # Used in Filter.pm by pb_filter_file
1129
1130 $build{$v} = "no";
1131 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'os'}") {
1132 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'os'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
1133 $build{$v} = "yes";
1134 }
1135 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'type'}") {
1136 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'type'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
1137 $build{$v} = "yes";
1138 }
1139 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'family'}") {
1140 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'family'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
1141 $build{$v} = "yes";
1142 }
1143 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'name'}") {
1144 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'name'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
1145 $build{$v} = "yes";
1146 }
1147 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'name'}-$pbos->{'version'}") {
1148 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'name'}-$pbos->{'version'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
1149 $build{$v} = "yes";
1150 }
1151 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}") {
1152 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
1153 $build{$v} = "yes";
1154 }
1155 pb_log(2,"DEBUG($v) bfiles: ".Dumper(\%bfiles)."\n");
1156
1157 if ($build{$v} ne "no") {
1158 # Apply now all the filters on all the files concerned
1159 # destination dir depends on the type of file
1160 # For patch support
1161 # TODO: Make it CMS aware
1162 $pb->{'patches'} = pb_list_sfiles("$ENV{'PBROOTDIR'}/$pbpkg/pbpatch", $pb->{'patches'}, $pbos, "$ENV{'PBROOTDIR'}/$pbpkg/pbextpatch");
1163 pb_log(2,"DEBUG($v) patches: ".Dumper($pb->{'patches'})."\n");
1164 # TODO: Make it CMS aware
1165 $pb->{'sources'} = pb_list_sfiles("$ENV{'PBROOTDIR'}/$pbpkg/pbsrc", $pb->{'sources'}, $pbos, "$ENV{'PBROOTDIR'}/$pbpkg/pbextsrc");
1166 pb_log(2,"DEBUG($v) sources: ".Dumper($pb->{'sources'})."\n");
1167
1168 if (defined $pb->{'patches'}->{$v}) {
1169 # Filter potential patches (local + remote)
1170 pb_mkdir_p("$dest/pbconf/$v/pbpatch");
1171 # If Debian based distribution, then prepare what will be done at build time
1172 my ($patchcmd,$patchopt);
1173 if ($pbos->{'type'} eq "deb") {
1174 ($patchcmd,$patchopt) = pb_distro_get_param($pbos,pb_conf_get_if("ospatchcmd","ospatchopt"));
1175 open(SCRIPT,"> $dest/pbconf/$v/pbpatch/pbapplypatch") || die "Unable to create $dest/pbconf/$v/pbpatch/pbapplypatch";
1176 print SCRIPT "#!/bin/bash\n";
1177 print SCRIPT "set -x\n" if ($pbdebug gt 1);
1178 }
1179 foreach my $pf (split(/,/,$pb->{'patches'}->{$v})) {
1180 my $pp = basename($pf);
1181 pb_vcs_export($pf,undef,"$dest/pbconf/$v/pbpatch");
1182 pb_filter_file_inplace($ptr,"$dest/pbconf/$v/pbpatch/$pp",$pb);
1183 pb_system("gzip -9f $dest/pbconf/$v/pbpatch/$pp","","quiet");
1184 if ($pbos->{'type'} eq "deb") {
1185 # If Debian based distribution, then prepare what will be done at build time
1186 # by applying the patches that will be available under the debian/patches dir
1187 print SCRIPT "$patchcmd $patchopt \< debian/patches/$pp\n";
1188 }
1189 }
1190 if ($pbos->{'type'} eq "deb") {
1191 close(SCRIPT);
1192 chmod 0755,"$dest/pbconf/$v/pbpatch/pbapplypatch";
1193 }
1194 #pb_system("cat $dest/pbconf/$v/pbpatch/pbapplypatch","APPLY","verbose");
1195 }
1196 if (defined $pb->{'sources'}->{$v}) {
1197 pb_mkdir_p("$dest/pbconf/$v/pbsrc");
1198 foreach my $pf (split(/,/,$pb->{'sources'}->{$v})) {
1199 my $pp = basename($pf);
1200 pb_vcs_export($pf,undef,"$dest/pbconf/$v/pbsrc");
1201 pb_filter_file_inplace($ptr,"$dest/pbconf/$v/pbsrc/$pp",$pb);
1202 }
1203 }
1204 # Filter build files at the end, as they depend on patches and sources
1205 foreach my $f (keys %bfiles) {
1206 pb_filter_file("$ENV{'PBROOTDIR'}/$bfiles{$f}",$ptr,"$dest/pbconf/$v/$f",$pb);
1207 }
1208 foreach my $f (keys %pkgfiles) {
1209 pb_filter_file("$ENV{'PBROOTDIR'}/$pkgfiles{$f}",$ptr,"$dest/pbconf/$v/$f",$pb);
1210 }
1211 }
1212
1213 if (defined $pbparallel) {
1214 # Communicate results back to parent
1215 my $str = "";
1216 $str .= "build $v = $build{$v}\n" if (defined $build{$v});
1217 $str .= "patches $v = $pb->{'patches'}->{$v}\n" if (defined $pb->{'patches'}->{$v});
1218 $str .= "sources $v = $pb->{'sources'}->{$v}\n" if (defined $pb->{'sources'}->{$v});
1219 pb_set_content("$tmpd/$$","$str");
1220 $pm->finish;
1221 }
1222 }
1223 # In the parent, we need to get the result from the children
1224 $pm->wait_all_children if (defined $pbparallel);
1225 die "Aborting, one or more of the children failed." if ((not $all_ok) && ($Global::pb_stop_on_error));
1226 my $made = "";
1227 my %h = ();
1228 my %tmp;
1229 my %tmp2;
1230 my $pt;
1231 my $k;
1232
1233 foreach $k (<$tmpd/*>) {
1234 $made .= pb_get_content($k);
1235 }
1236 pb_rm_rf($tmpd);
1237 pb_log(3,"MADE:\n$made");
1238
1239 # Rebuild local hashes
1240 foreach $k (split(/\n/,$made)) {
1241 if ($k =~ /^\s*([A-z0-9-_]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
1242 $h{$1}->{$2}=$3;
1243 }
1244 }
1245 pb_log(2,"HASH: ".Dumper(%h));
1246
1247 # Patches
1248 pb_log(0,"Delivered and compressed patches ");
1249 $pt = $h{'patches'};
1250 foreach $k (keys %$pt) {
1251 foreach my $v1 (split(/,/,$pt->{$k})) {
1252 $tmp{$v1} = "";
1253 }
1254 }
1255 if (keys %tmp) {
1256 foreach $k (keys %tmp) {
1257 pb_log(0,"$k ");
1258 }
1259 } else {
1260 pb_log(0,"N/A");
1261 }
1262 pb_log(0,"\n");
1263
1264 # Sources
1265 pb_log(0,"Delivered additional sources ");
1266 $pt = $h{'sources'};
1267 foreach $k (keys %$pt) {
1268 foreach my $v1 (split(/,/,$pt->{$k})) {
1269 $tmp2{$v1} = "";
1270 }
1271 }
1272 if (keys %tmp2) {
1273 foreach $k (keys %tmp2) {
1274 pb_log(0,"$k ");
1275 }
1276 } else {
1277 pb_log(0,"N/A");
1278 }
1279 pb_log(0,"\n");
1280
1281 # Build files
1282 my @found;
1283 my @notfound;
1284 $pt = $h{'build'};
1285 foreach my $b (keys %$pt) {
1286 push @found,$b if ($pt->{$b} =~ /yes/);
1287 push @notfound,$b if ($pt->{$b} =~ /no/);
1288 }
1289 pb_log(0,"Build files have been generated for ... ".join(',',sort(@found))."\n") if (@found);
1290 pb_log(0,"No Build files found for ".join(',',sort(@notfound))."\n") if (@notfound);
1291
1292 } else {
1293 # Instead call News generation
1294 pb_web_news2html($dest);
1295 # And create an empty pbconf
1296 pb_mkdir_p("$dest/pbconf");
1297 # And prepare the pbscript to execute remotely
1298 open(SCRIPT,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
1299 print SCRIPT "#!/bin/bash\n";
1300 print SCRIPT "#set -x\n";
1301 print SCRIPT "echo ... Extracting Website content\n";
1302 print SCRIPT "find . -type f | grep -Ev '^./$pbpkg-$pbver$pbextdir.tar.gz|^./pbscript' | xargs rm -f non-existent\n";
1303 print SCRIPT "find * -type d -depth | xargs rmdir 2> /dev/null \n";
1304 print SCRIPT "tar xfz $pbpkg-$pbver$pbextdir.tar.gz\n";
1305 print SCRIPT "mv $pbpkg-$pbver$pbextdir/* .\n";
1306 print SCRIPT "rm -f $pbpkg-$pbver$pbextdir.tar.gz\n";
1307 print SCRIPT "rmdir $pbpkg-$pbver$pbextdir\n";
1308 print SCRIPT "find . -type f -print0 | xargs -0 chmod 644\n";
1309 print SCRIPT "find . -type d -print0 | xargs -0 chmod 755\n";
1310 close(SCRIPT);
1311 chmod 0755,"$ENV{'PBTMP'}/pbscript";
1312 }
1313
1314 # Apply filters to the non-build files
1315 my $liste ="";
1316 if (defined $filteredfiles->{$pbpkg}) {
1317 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
1318 pb_filter_file_inplace($ptr,"$dest/$f",$pb);
1319 $liste = "$f $liste";
1320 }
1321 }
1322 pb_log(2,"Files ".$liste."have been filtered\n");
1323
1324 # TODO: Make it CMS aware
1325 # Execute the pbinit script if any
1326 if (-x "$ENV{'PBROOTDIR'}/$pbpkg/pbinit") {
1327 pb_filter_file("$ENV{'PBROOTDIR'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",$pb);
1328 chmod 0755,"$ENV{'PBTMP'}/pbinit";
1329 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOTDIR'}/$pbpkg/pbinit under $dest","verbose");
1330 }
1331
1332 # Do we have additional script to run to prepare the environement for the project ?
1333 # Then include it in the pbconf delivery
1334 foreach my $pbvf (<$ENV{'PBROOTDIR'}/pbv*.pre>,<$ENV{'PBROOTDIR'}/pbv*.post>, <$ENV{'PBROOTDIR'}/pbtest*>) {
1335 if (-x "$pbvf") {
1336 my $target = "$ENV{'PBDESTDIR'}/".basename($pbvf);
1337 pb_filter_file("$pbvf",$ptr,$target,$pb);
1338 chmod 0755,"$target";
1339 }
1340 }
1341
1342 # Prepare the dest directory for archive
1343 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
1344 if (defined $preserve) {
1345 # In that case we want to preserve the original tar file for checksum purposes
1346 # The one created is btw equivalent in that case to this one
1347 # Maybe check basename of both to be sure they are the same ?
1348 pb_log(0,"Preserving original tar file ");
1349 move("$preserve","$pbpkg-$pbver$pbextdir.tar.gz");
1350 } else {
1351 # Possibility to look at PBSRC to guess more the filename
1352 pb_system("tar cfz $pbpkg-$pbver$pbextdir.tar.gz --exclude=$pbpkg-$pbver$pbextdir/pbconf $pbpkg-$pbver$pbextdir","Creating $pbpkg tar files compressed");
1353 }
1354 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir.tar.gz\n");
1355 pb_system("tar cfz $pbpkg-$pbver$pbextdir.pbconf.tar.gz $pbpkg-$pbver$pbextdir/pbconf","Creating pbconf tar files compressed");
1356 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir.pbconf.tar.gz\n");
1357
1358 # Keep track of version-tag per pkg
1359 $pkgs{$pbpkg} = "$pbver-$pbtag";
1360
1361 # Final cleanup
1362 pb_rm_rf($dest) if (-d $dest);
1363 }
1364
1365 # Keep track of per package version
1366 pb_log(2,"DEBUG pkgs: ".Dumper(%pkgs)."\n");
1367 open(PKG,"> $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb";
1368 foreach my $pbpkg (keys %pkgs) {
1369 print PKG "pbpkg $pbpkg = $pkgs{$pbpkg}\n";
1370 }
1371 close(PKG);
1372
1373 # Keep track of what is generated by default
1374 # We need to store the dir and info on version-tag
1375 # Base our content on the existing .pb file
1376 copy("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb","$ENV{'PBDESTDIR'}/pbrc");
1377 open(LAST,">> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
1378 print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOTDIR'}\n";
1379 print LAST "projver $ENV{'PBPROJ'} = $ENV{'PBPROJVER'}\n";
1380 print LAST "projtag $ENV{'PBPROJ'} = $ENV{'PBPROJTAG'}\n";
1381 print LAST "pbpackager $ENV{'PBPROJ'} = $ENV{'PBPACKAGER'}\n";
1382 print LAST "pbextdir $ENV{'PBPROJ'} = $pbextdir\n";
1383 close(LAST);
1384}
1385
1386sub pb_test2pkg {
1387 # Get the running distro to test on
1388 my $pbos = pb_distro_get_context();
1389
1390 # Get list of packages to test
1391 # Get content saved in cms2build
1392 my $ptr = pb_get_pkg();
1393 @pkgs = @$ptr;
1394
1395 # Additional potential repo
1396 pb_distro_setuprepo($pbos);
1397 foreach my $pbpkg (@pkgs) {
1398 # We need to install the package to test, and deps brought with it
1399 pb_distro_installdeps(undef,$pbos,$pbpkg);
1400 pb_system("$ENV{'PBDESTDIR'}/pbtest","Launching test for $pbpkg","verbose");
1401 }
1402}
1403
1404sub pb_build2pkg {
1405
1406 pb_log(0,"INFO: ------ Starting to build package ------\n");
1407 # Get the running distro to build on
1408 my $pbos = pb_distro_get_context();
1409
1410 # If needed we may add repository to the build env
1411 pb_distro_setuprepo($pbos);
1412
1413 # Get list of packages to build
1414 my $ptr = pb_get_pkg();
1415 @pkgs = @$ptr;
1416
1417 # Get content saved in cms2build
1418 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
1419 $pkg = { } if (not defined $pkg);
1420 my $pbextdir = pb_get_extdir();
1421
1422 pb_mkdir_p("$ENV{'PBBUILDDIR'}") if (! -d "$ENV{'PBBUILDDIR'}");
1423 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1424 my $made = ""; # pkgs made during build
1425 my $pm;
1426 my $all_ok = 1;
1427
1428 if (defined $pbparallel) {
1429 $pm = new Parallel::ForkManager($pbparallel);
1430 $pm->run_on_finish(sub { my ($pid, $code, $id, $signal, $dump) = @_;
1431 $all_ok = 0 unless (($code == 0) && ($signal == 0) && ($dump == 0)); });
1432 }
1433
1434 # We need to communicate info back from the children if parallel so prepare a dir for that
1435 my $tmpd = "$ENV{'PBTMP'}/build.$$";
1436 pb_mkdir_p($tmpd) if (defined $pbparallel);
1437
1438 # Do it here as this doesn't work in // mode as itdies if it fails
1439 if ($pbos->{'type'} eq "rpm") {
1440 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
1441 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
1442 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$d");
1443 }
1444 }
1445 }
1446
1447 foreach my $pbpkg (@pkgs) {
1448 $pm->start and next if (defined $pbparallel);
1449
1450 my $vertag = $pkg->{$pbpkg};
1451 pb_log(2,"Vertag: $vertag\n");
1452 # get the version of the current package - maybe different
1453 ($pbver,$pbtag) = split(/-/,$vertag);
1454
1455 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir.tar.gz";
1456 my $src2="$ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir.pbconf.tar.gz";
1457 pb_log(2,"Source file: $src\n");
1458 pb_log(2,"Pbconf file: $src2\n");
1459
1460 pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
1461 if ($pbos->{'type'} eq "rpm") {
1462 # Remove in case a previous link/file was there
1463 unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
1464 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
1465 # We need to first extract the spec file
1466 my @specfile = pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/","$ENV{'PBBUILDDIR'}/SPECS","spec");
1467
1468 # We need to handle potential patches to upstream sources
1469 pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbpatch/","$ENV{'PBBUILDDIR'}/SOURCES","patch");
1470
1471 # We need to handle potential additional sources to upstream sources
1472 pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbsrc/","$ENV{'PBBUILDDIR'}/SOURCES","src");
1473
1474 pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
1475 # set LANGUAGE to check for correct log messages
1476 $ENV{'LANGUAGE'}="C";
1477 # Older Redhat use _target_platform in %configure incorrectly
1478 my $specialdef = "";
1479 if (($pbos->{'name'} eq "redhat") || (($pbos->{'name'} eq "rhel") && ($pbos->{'version'} eq "2.1"))) {
1480 $specialdef = "--define \'_target_platform \"\"\'";
1481 }
1482
1483 foreach my $f (@specfile) {
1484 if ($f =~ /\.spec$/) {
1485 # This could cause an issue in // mode
1486 pb_distro_installdeps($f,$pbos);
1487 pb_system("rpmbuild $specialdef --define \"packager $ENV{'PBPACKAGER'}\" --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}","verbose");
1488 last;
1489 }
1490 }
1491 # Get the name of the generated packages
1492 open(LOG,"$ENV{'PBTMP'}/system.$$.log") || die "Unable to open $ENV{'PBTMP'}/system.$$.log";
1493 while (<LOG>) {
1494 chomp($_);
1495 next if ($_ !~ /^Wrote:/);
1496 s|.*/([S]*RPMS.*)|$1|;
1497 $made .=" $_";
1498 }
1499 close(LOG);
1500
1501 } elsif ($pbos->{'type'} eq "deb") {
1502 pb_system("tar xfz $src","Extracting sources");
1503 pb_system("tar xfz $src2","Extracting pbconf");
1504
1505 chdir "$pbpkg-$pbver$pbextdir" || die "Unable to chdir to $pbpkg-$pbver$pbextdir";
1506 pb_rm_rf("debian");
1507 my $confdir = "pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}";
1508 die "Configuration directory $confdir is not a directory\nIs os description listed in your {ve,vm,rm}list values?" if (not -d $confdir);
1509 symlink "$confdir","debian" || die "Unable to symlink 'debian' to $confdir";
1510 chmod 0755,"debian/rules";
1511
1512 # We need to handle potential patches to upstream sources
1513 pb_mkdir_p("debian/patches");
1514 my @f = pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbpatch/","debian/patches","patch");
1515
1516 # By default we use format 1.0 - Cf man dpkg-source
1517 my $debsrcfmt = "1.0";
1518 my $debsrcfile = "debian/source/format";
1519 if (-f $debsrcfile) {
1520 $debsrcfmt = pb_get_content($debsrcfile);
1521 }
1522
1523 if ($debsrcfmt =~ /^3.*quilt/) {
1524 # If we use quilt to manage patches, we then setup the env correctly
1525 # as per http://pkg-perl.alioth.debian.org/howto/quilt.html
1526 # Generate Debian patch series for quilt
1527 open(SERIE,"> debian/patches/series") || die "Unable to write in debian/patches/series";
1528 $ENV{'QUILT_PATCHES'}="debian/patches";
1529 } else {
1530 # If we use dpatch to manage patches, we then setup the 00list file as well
1531 open(SERIE,"> debian/patches/00list") || die "Unable to write in debian/patches/00list";
1532 }
1533 foreach my $f (sort @f) {
1534 # Skip the script made to apply the patches to the Debian tree
1535 next if ($f =~ /pbapplypatch/);
1536 # We also need to uncompress them
1537 pb_system("gzip -d $f","","quiet");
1538 $f =~ s/\.gz$//;
1539 print SERIE "$f\n";
1540 }
1541 close(SERIE);
1542 if (@f) {
1543 # We have patches...
1544 my $patch_file = "debian/patches/pbapplypatch";
1545 if (($debsrcfmt =~ /^1.*/) && (-x $patch_file)) {
1546 # In that case we need to apply the patches ourselves locally
1547 pb_system("cat $patch_file","APPLY","verbose");
1548 pb_system("$patch_file","Applying patches to $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} tree");
1549 }
1550 # ...so modify the name of files to be Debian compliant
1551 move("../$src","../$pbpkg-$pbver$pbextdir.orig.tar.gz");
1552 }
1553
1554 # We need to handle potential additional sources to upstream sources
1555 #pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbsrc/","$ENV{'PBBUILDDIR'}/debian","src");
1556
1557 # This can create problems in // mode as well
1558 pb_distro_installdeps("debian/control",$pbos);
1559 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package","verbose");
1560
1561 # Get the name of the generated packages
1562 open(LOG,"$ENV{'PBTMP'}/system.$$.log") || die "Unable to open $ENV{'PBTMP'}/system.$$.log";
1563 while (<LOG>) {
1564 chomp();
1565 pb_log(3,"In loop: $_\n");
1566 next unless (/^dpkg-deb.*:\s+building\s+package\s+.*\s+in\s+\`\.\.\/(\S+)\'\./o);
1567 my $tmp = $1;
1568 #doesn't work in my case
1569 #die "Missing file $tmp" if (not -f "../$tmp");
1570 $made = "$made $tmp";
1571 }
1572 close(LOG);
1573 pb_log(2,"Now made is: $made\n");
1574
1575 open(CTRL,"debian/control") || die "Unable to open debian/control: $!";
1576 while (<CTRL>) {
1577 pb_log(3,"In loop: $_\n");
1578 next unless (/^Source: (\S+)/o);
1579 my $tmp = $1;
1580 $made = "$made $tmp"."_*.dsc $tmp"."_*.tar.gz $tmp"."_*.deb $tmp"."_*.changes";
1581 #doesn't work in my case
1582 #foreach my $glob (("$1\_*.changes", "$1\_*.dsc", "$1\_*.tar.gz")) {
1583 #my @file = glob($glob);
1584 #die "Missing file for $glob" unless (@file > 0);
1585 #die "Too many files for $glob" if (@file > 1);
1586 #die "Missing file $file[0]" if (not -f $file[0]);
1587 #$made .= " $file[0]";
1588 #}
1589 }
1590 close(CTRL);
1591 #pb_display_file("$ENV{'PBTMP'}/system.$$.log");
1592 pb_log(2,"Finally made is: $made\n");
1593
1594 chdir ".." || die "Unable to chdir to parent dir";
1595 pb_rm_rf("$pbpkg-$pbver$pbextdir");
1596 } elsif ($pbos->{'type'} eq "ebuild") {
1597 my @ebuildfile;
1598 # For gentoo we need to take pb as subsystem name
1599 # We put every apps here under sys-apps. hope it's correct
1600 # We use pb's home dir in order to have a single OVERLAY line
1601 my $tmpe = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
1602 pb_mkdir_p($tmpe) if (! -d "$tmpe");
1603 pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
1604
1605 # We need to first extract the ebuild file
1606 @ebuildfile = pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/","$tmpe","ebuild");
1607
1608 # Prepare the build env for gentoo
1609 my $found = 0;
1610 my $pbbd = $ENV{'HOME'};
1611 $pbbd =~ s|/|\\/|g;
1612 if (-r "/etc/make.conf") {
1613 open(MAKE,"/etc/make.conf");
1614 while (<MAKE>) {
1615 $found = 1 if (/$pbbd\/portage/);
1616 }
1617 close(MAKE);
1618 }
1619 if ($found == 0) {
1620 pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
1621 }
1622 #$found = 0;
1623 #if (-r "/etc/portage/package.keywords") {
1624 #open(KEYW,"/etc/portage/package.keywords");
1625 #while (<KEYW>) {
1626 #$found = 1 if (/portage\/pb/);
1627 #}
1628 #close(KEYW);
1629 #}
1630 #if ($found == 0) {
1631 #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
1632 #}
1633
1634 # Build
1635 foreach my $f (@ebuildfile) {
1636 if ($f =~ /\.ebuild$/) {
1637 pb_distro_installdeps($f,$pbos);
1638 move($f,"$tmpe/$pbpkg-$pbver.ebuild");
1639 pb_system("cd $tmpe ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package","verbose");
1640 # Now move it where pb expects it
1641 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
1642 if ($pbtag eq 0) {
1643 # This is assumed to be a test version
1644 my $nver = substr($pbver,0,-14);
1645 my $ntag = substr($pbver,-14);
1646 my $ebtg = "portage/pb/sys-apps/$pbpkg/$pbpkg-$nver"."_p$ntag.ebuild";
1647 move("$tmpe/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/$ebtg");
1648 $made="$made $ebtg";
1649 } else {
1650 my $ebtg = "portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver-r$pbtag.ebuild";
1651 move("$tmpe/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/$ebtg");
1652 $made="$made $ebtg";
1653 }
1654 }
1655 }
1656
1657 } elsif ($pbos->{'type'} eq "tgz") {
1658 # Slackware family
1659 $made="$made $pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
1660
1661 pb_system("tar xfz $src","Extracting sources");
1662 pb_system("tar xfz $src2","Extracting pbconf");
1663 chdir "$pbpkg-$pbver$pbextdir" || die "Unable to chdir to $pbpkg-$pbver$pbextdir";
1664 symlink "pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}","install" || die "Unable to symlink to pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}";
1665 if (-x "install/pbslack") {
1666 pb_distro_installdeps("./install/pbslack",$pbos);
1667 pb_system("./install/pbslack","Building software");
1668 pb_system("sudo /sbin/makepkg -p -l y -c y $pbpkg","Packaging $pbpkg","verbose");
1669 }
1670 chdir ".." || die "Unable to chdir to parent dir";
1671 pb_rm_rf("$pbpkg-$pbver$pbextdir");
1672 } elsif ($pbos->{'type'} eq "pkg") {
1673 # Solaris
1674 $made="$made $pbpkg-$pbver-$pbtag.pkg.gz";
1675 my $pkgdestdir="$ENV{'PBBUILDDIR'}/install";
1676
1677 # Will host resulting packages
1678 pb_mkdir_p("$pbos->{'type'}");
1679 pb_mkdir_p("$pkgdestdir/delivery");
1680 pb_system("tar xfz $src","Extracting sources under $ENV{'PBBUILDDIR'}");
1681 pb_system("tar xfz $src2","Extracting pbconf under $ENV{'PBBUILDDIR'}");
1682 # We need to handle potential patches to upstream sources
1683 pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbpatch/","$ENV{'PBBUILDDIR'}","patch");
1684
1685 # We need to handle potential additional sources to upstream sources
1686 pb_extract_build_files($src2,"$pbpkg-$pbver$pbextdir/pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbsrc/","$ENV{'PBBUILDDIR'}","src");
1687
1688 chdir "$pbpkg-$pbver$pbextdir" || die "Unable to chdir to $pbpkg-$pbver$pbextdir";
1689 if (-f "pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbbuild") {
1690 chmod 0755,"pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbbuild";
1691 # pkginfo file is mandatory
1692 die "Unable to find pkginfo file in pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}" if (! -f "pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pkginfo");
1693 # Build
1694 pb_system("pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbbuild $pkgdestdir/delivery","Building software and installing under $pkgdestdir/delivery");
1695 # Copy complementary files
1696 if (-f "pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/prototype") {
1697 copy("pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/prototype", $pkgdestdir)
1698 } else {
1699 # No prototype provided, calculating it
1700 open(PROTO,"> $pkgdestdir/prototype") || die "Unable to create prototype file";
1701 print PROTO "i pkginfo\n";
1702 print PROTO "i depend\n" if (-f "pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/depend");
1703 $ENV{'PBSOLDESTDIR'} = "$pkgdestdir/delivery";
1704 find(\&create_solaris_prototype, "$pkgdestdir/delivery");
1705 }
1706 copy("pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/depend", $pkgdestdir) if (-f "pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/depend");
1707 copy("pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pkginfo", $pkgdestdir);
1708 pb_system("cd $pkgdestdir/delivery ; pkgmk -o -f ../prototype -r $pkgdestdir/delivery -d $ENV{'PBBUILDDIR'}/$pbos->{'type'}","Packaging $pbpkg","verbose");
1709 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");
1710 pb_system("cd $ENV{'PBBUILDDIR'} ; gzip -9f $pbpkg-$pbver-$pbtag.pkg","Compressing $pbpkg-$pbver-$pbtag.pkg","verbose");
1711 } else {
1712 pb_log(0,"No pbconf/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}/pbbuild file found for $pbpkg-$pbver in \n");
1713 }
1714 chdir ".." || die "Unable to chdir to parent dir";
1715 pb_rm_rf("$pbpkg-$pbver$pbextdir","$ENV{'PBBUILDDIR'}/$pbos->{'type'}","$pkgdestdir");
1716 } elsif ($pbos->{'type'} eq "hpux") {
1717 # HP-UX
1718 pb_system("tar xfz $src","Extracting sources");
1719 pb_system("tar xfz $src2","Extracting pbconf");
1720
1721 chdir "$pbpkg-$pbver$pbextdir" || die "Unable to chdir to $pbpkg-$pbver$pbextdir";
1722 pb_system("buildpackage ","Building package","verbose");
1723 # Get the name of the generated packages
1724 open(LOG,"$ENV{'PBTMP'}/system.$$.log") || die "Unable to open $ENV{'PBTMP'}/system.$$.log";
1725 while (<LOG>) {
1726 chomp();
1727 my $tmp = $_;
1728 next if ($tmp !~ /^SD BUILD.*:/);
1729 $tmp =~ s|.*../(.*)_(.*).sd.*|$1|;
1730 $made = "$made $tmp"."_*.sd";
1731 }
1732 close(LOG);
1733 $made="$made $pbpkg-$pbver-$pbtag.sd";
1734
1735 chdir ".." || die "Unable to chdir to parent dir";
1736 pb_rm_rf("$pbpkg-$pbver$pbextdir");
1737 } else {
1738 die "Unknown OS type format $pbos->{'type'}";
1739 }
1740 if (defined $pbparallel) {
1741 # Communicate results back to parent
1742 pb_set_content("$tmpd/$$",$made);
1743 $pm->finish;
1744 }
1745 }
1746 if (defined $pbparallel) {
1747 # In the parent, we need to get the result from the children
1748 $pm->wait_all_children;
1749 foreach my $f (<$tmpd/*>) {
1750 $made .= " ".pb_get_content($f);
1751 }
1752 die "Aborting, one or more of the children failed." if ((not $all_ok) && ($Global::pb_stop_on_error));
1753 pb_rm_rf($tmpd);
1754 }
1755
1756 # Sign packages
1757 pb_sign_pkgs($pbos,$made);
1758 pb_log(0,"INFO: ------ Finished building package ------\n");
1759
1760 # Find the appropriate check cmd/opts
1761 my ($chkcmd,$chkopt) = pb_distro_get_param($pbos,pb_conf_get_if("oschkcmd","oschkopt"));
1762
1763 # Packages check if needed
1764 if ($pbos->{'type'} eq "rpm") {
1765 if ((defined $chkcmd) && (-x $chkcmd)) {
1766 my $cmd = "$chkcmd";
1767 $cmd .= " $chkopt" if (defined $chkopt);
1768 $cmd .= " $made";
1769 my $ret = pb_system("$cmd","Checking validity of rpms with $chkcmd","mayfail");
1770 pb_log(0,"ERROR: when checking packages validity\n") if ($ret ne 0);
1771 }
1772 my $rpms ="";
1773 my $srpms ="";
1774 foreach my $f (split(/ /,$made)) {
1775 $rpms .= "$ENV{'PBBUILDDIR'}/$f " if ($f =~ /^RPMS\//);
1776 $srpms .= "$ENV{'PBBUILDDIR'}/$f " if ($f =~ /^SRPMS\//);
1777 }
1778 pb_log(0,"SRPM packages generated: $srpms\n");
1779 pb_log(0,"RPM packages generated: $rpms\n");
1780 } elsif ($pbos->{'type'} eq "deb") {
1781 my $made2 = "";
1782 foreach my $f (split(/ /,$made)) {
1783 $made2 .= "$f " if ($f =~ /\.deb$/);
1784 }
1785 if (-x $chkcmd) {
1786 my $ret = pb_system("$chkcmd $chkopt $made2","Checking validity of debs with $chkcmd","mayfail");
1787 pb_log(0,"ERROR: when checking packages validity\n") if ($ret ne 0);
1788 }
1789 pb_log(0,"deb packages generated: $made2\n");
1790 } else {
1791 pb_log(0,"No check done for $pbos->{'type'} yet\n");
1792 pb_log(0,"Packages generated: $made\n");
1793 }
1794
1795 # Keep track of what is generated so that we can get them back from VMs/RMs
1796 my $pbkeep = "$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}-$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}";
1797 open(KEEP,"> $pbkeep") || die "Unable to create $pbkeep: $!";
1798 print KEEP "$made\n";
1799 close(KEEP);
1800}
1801
1802sub create_solaris_prototype {
1803
1804 my $uidgid = "bin bin";
1805 my $pkgdestdir = $ENV{'PBSOLDESTDIR'};
1806
1807 return if ($_ =~ /^$pkgdestdir$/);
1808 if (-d $_) {
1809 my $n = $File::Find::name;
1810 $n =~ s~$pkgdestdir/~~;
1811 print PROTO "d none $n 0755 $uidgid\n";
1812 } elsif (-x $_) {
1813 my $n = $File::Find::name;
1814 $n =~ s~$pkgdestdir/~~;
1815 print PROTO "f none $n 0755 $uidgid\n";
1816 } elsif (-f $_) {
1817 my $n = $File::Find::name;
1818 $n =~ s~$pkgdestdir/~~;
1819 print PROTO "f none $n 0644 $uidgid\n";
1820 }
1821}
1822
1823sub pb_build2ssh {
1824 pb_send2target("Sources");
1825 pb_send2target("CPAN");
1826}
1827
1828sub pb_pkg2ssh {
1829 pb_send2target("Packages");
1830}
1831
1832# By default deliver to the the public site hosting the
1833# ftp structure (or whatever) or a VM/VE/RM
1834sub pb_send2target {
1835
1836 my $cmt = shift;
1837 my $v = shift || undef;
1838 my $vmexist = shift || 0; # 0 is FALSE
1839 my $vmpid = shift || 0; # 0 is FALSE
1840 my $snapme = shift || 0; # 0 is FALSE
1841
1842 pb_log(2,"DEBUG: pb_send2target($cmt,".Dumper($v).",$vmexist,$vmpid)\n");
1843 my $host = "sshhost";
1844 my $login = "sshlogin";
1845 my $dir = "sshdir";
1846 my $port = "sshport";
1847 my $conf = "sshconf";
1848 my $tmout = undef;
1849 my $path = undef;
1850 if ($cmt =~ /^VM/) {
1851 $login = "vmlogin";
1852 $dir = "pbdefdir";
1853 # Specific VM
1854 $tmout = "vmtmout";
1855 $path = "vmpath";
1856 $host = "vmhost";
1857 $port = "vmport";
1858 } elsif ($cmt =~ /^RM/) {
1859 $login = "rmlogin";
1860 $dir = "pbdefdir";
1861 # Specific RM
1862 $tmout = "rmtmout";
1863 $path = "rmpath";
1864 $host = "rmhost";
1865 $port = "rmport";
1866 } elsif ($cmt =~ /^VE/) {
1867 $login = "velogin";
1868 $dir = "pbdefdir";
1869 # Specific VE
1870 $path = "vepath";
1871 $conf = "rbsconf";
1872 } elsif ($cmt eq "Web") {
1873 $host = "websshhost";
1874 $login = "websshlogin";
1875 $dir = "websshdir";
1876 $port = "websshport";
1877 } elsif ($cmt eq "CPAN") {
1878 $host = "cpanpause";
1879 $login = "";
1880 $dir = "cpandir";
1881 $port = "";
1882 }
1883 my $cmd = "";
1884 my $src = "";
1885 my $cpanpkg = 0;
1886 my $pbos;
1887
1888 my $pbextdir = pb_get_extdir();
1889
1890 if ($cmt ne "Announce") {
1891 # Get list of packages to build
1892 my $ptr = pb_get_pkg();
1893 @pkgs = @$ptr;
1894
1895 # Get the running distro to consider
1896 $pbos = pb_distro_get_context($v);
1897
1898 # Get content saved in cms2build
1899 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
1900 $pkg = { } if (not defined $pkg);
1901
1902 pb_mkdir_p("$ENV{'PBBUILDDIR'}") if (! -d "$ENV{'PBBUILDDIR'}");
1903 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1904 foreach my $pbpkg (@pkgs) {
1905 my $vertag = $pkg->{$pbpkg};
1906 # get the version of the current package - maybe different
1907 pb_log(2,"Vertag: $vertag\n");
1908 ($pbver,$pbtag) = split(/-/,$vertag);
1909
1910 if (($cmt eq "Sources") || ($cmt =~ /(V[EM]|RM)build/)) {
1911 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir.tar.gz $ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir.pbconf.tar.gz";
1912 if ($cmd eq "") {
1913 $cmd = "ln -sf $pbpkg-$pbver$pbextdir.tar.gz $pbpkg-latest.tar.gz";
1914 } else {
1915 $cmd = "$cmd ; ln -sf $pbpkg-$pbver$pbextdir.tar.gz $pbpkg-latest.tar.gz";
1916 }
1917 } elsif ($cmt eq "Web") {
1918 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver$pbextdir.tar.gz"
1919 }
1920
1921 # Do we have one perl package
1922 my @nametype = pb_conf_get_if("namingtype");
1923 my $type = $nametype[0]->{$pbpkg};
1924 if ((defined $type) && ($type eq "perl")) {
1925 $cpanpkg = 1;
1926 }
1927 }
1928 # Adds conf file for availability of conf elements
1929 pb_conf_add("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb");
1930 }
1931 my ($rbsconf,$testver,$delivery) = pb_conf_get_if($conf,"testver","delivery");
1932 if ($cmt eq "CPAN") {
1933 # Do not deliver on Pause if this is a test version
1934 return if (not defined $testver);
1935 return if ($testver =~ /true/);
1936 # Do not deliver on Pause if this is not a perl package
1937 return if ($cpanpkg == 0);
1938 }
1939
1940 if ($cmt =~ /(V[EM]|RM)build/) {
1941 $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript.$$";
1942 } elsif ($cmt =~ /(V[EM]|RM)Script/) {
1943 $src="$src $ENV{'PBDESTDIR'}/pbscript.$$";
1944 } elsif ($cmt =~ /(V[EM]|RM)test/) {
1945 $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";
1946 } elsif (($cmt eq "Announce") || ($cmt eq "Web") || ($cmt eq "CPAN")) {
1947 $src="$src $ENV{'PBTMP'}/pbscript";
1948 } elsif ($cmt eq "Packages") {
1949 # Get package list from file made during build2pkg
1950 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'}";
1951 $src = <KEEP>;
1952 chomp($src);
1953 close(KEEP);
1954 $src = "$src $ENV{'PBBUILDDIR'}/pbscript.$$";
1955 }
1956 if (($cmt eq "Sources") || ($cmt eq "Packages") || ($cmt eq "CPAN")) {
1957 my ($pbpackager) = pb_conf_get("pbpackager");
1958 $ENV{'PBPACKAGER'} = $pbpackager->{$ENV{'PBPROJ'}};
1959 pb_log(0,"Exporting public key for $ENV{'PBPACKAGER'}\n");
1960 # Using pb_system is not working due to redirection below
1961 system("gpg --export -a \'$ENV{'PBPACKAGER'}\' > $ENV{'PBDESTDIR'}/$ENV{'PBPROJ'}.pubkey");
1962 chmod 0644,"$ENV{'PBDESTDIR'}/$ENV{'PBPROJ'}.pubkey";
1963 $src = "$src $ENV{'PBDESTDIR'}/$ENV{'PBPROJ'}.pubkey";
1964 }
1965 # Remove potential leading spaces (cause problem with basename)
1966 $src =~ s/^ *//;
1967 my $basesrc = "";
1968 foreach my $i (split(/ +/,$src)) {
1969 $basesrc .= " ".basename($i);
1970 }
1971
1972 pb_log(0,"Sources handled ($cmt): $src\n");
1973 pb_log(2,"values: ".Dumper(($host,$login,$dir,$port,$tmout,$path,$conf))."\n");
1974 my ($sshhost,$sshdir) = pb_conf_get($host,$dir);
1975 # Not mandatory...
1976 $delivery->{$ENV{'PBPROJ'}} = "" if (not defined $delivery->{$ENV{'PBPROJ'}});
1977 my ($sshlogin,$sshport) = pb_conf_get_if($login,$port);
1978 $sshport->{$ENV{PBPROJ}} = 22 unless (defined $sshport->{$ENV{PBPROJ}});
1979 $sshlogin->{$ENV{PBPROJ}} = getpwuid($UID) unless (defined $sshlogin->{$ENV{PBPROJ}});
1980 my ($vtmout,$vepath);
1981 # ...Except those in virtual context
1982 if ($cmt =~ /^VE/) {
1983 ($vepath) = pb_conf_get($path);
1984 }
1985 if ($cmt =~ /^(V|R)M/) {
1986 $vtmout = pb_distro_get_param($pbos,pb_conf_get_if($tmout));
1987 }
1988 my $remhost = $sshhost->{$ENV{'PBPROJ'}};
1989 my $remdir = $sshdir->{$ENV{'PBPROJ'}};
1990 if ($cmt =~ /^V[EM]|RM/) {
1991 # In that case our real host is in the xxhost with the OS as key, not project as above
1992 $remhost = pb_distro_get_param($pbos,$sshhost);
1993 }
1994 pb_log(2,"ssh: ".Dumper(($remhost,$sshlogin,$remdir,$sshport,$vepath,$rbsconf))."\n");
1995 pb_log(2,"ssh: ".Dumper($vtmout)."\n") if (defined $vtmout);
1996
1997 my $mac;
1998 if ($cmt !~ /^VE/) {
1999 $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$remhost";
2000 # Overwrite account value if passed as parameter
2001 $mac = "$pbaccount\@$remhost" if (defined $pbaccount);
2002 pb_log(2, "DEBUG: pbaccount: $pbaccount => mac: $mac\n") if (defined $pbaccount);
2003 } else {
2004 # VE
2005 # Overwrite account value if passed as parameter (typically for setup2v)
2006 $mac = $sshlogin->{$ENV{'PBPROJ'}};
2007 $mac = $pbaccount if (defined $pbaccount);
2008 }
2009
2010 my $tdir;
2011 my $bdir;
2012 if (($cmt eq "Sources") || ($cmt =~ /(V[EM]|RM)Script/)) {
2013 $tdir = "$remdir/$delivery->{$ENV{'PBPROJ'}}/src";
2014 } elsif ($cmt eq "CPAN") {
2015 $tdir = "$remdir";
2016 } elsif ($cmt =~ /(V[EM]|RM)(build|test)/) {
2017 $tdir = $remdir."/$ENV{'PBPROJ'}/delivery";
2018 $bdir = $remdir."/$ENV{'PBPROJ'}/build";
2019 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
2020 $bdir =~ s|\$ENV.+\}/||;
2021 } elsif ($cmt eq "Announce") {
2022 $tdir = "$remdir/$delivery->{$ENV{'PBPROJ'}}";
2023 } elsif ($cmt eq "Web") {
2024 $tdir = "$remdir/$delivery->{$ENV{'PBPROJ'}}";
2025 } elsif ($cmt eq "Packages") {
2026 if (($pbos->{'type'} eq "rpm") || ($pbos->{'type'} eq "pkg") || ($pbos->{'type'} eq "hpux") || ($pbos->{'type'} eq "tgz")) {
2027 # put packages under an arch subdir
2028 $tdir = "$remdir/$delivery->{$ENV{'PBPROJ'}}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}";
2029 } elsif (($pbos->{'type'} eq "deb") || ($pbos->{'type'} eq "ebuild")) {
2030 # No need for an arch subdir
2031 $tdir = "$remdir/$delivery->{$ENV{'PBPROJ'}}/$pbos->{'name'}/$pbos->{'version'}";
2032 } else {
2033 die "Please teach the dev team where to deliver ($pbos->{'type'} type of packages\n";
2034 }
2035
2036 my $repodir = $tdir;
2037 $repodir =~ s|^$remdir/||;
2038
2039 my ($pbrepo) = pb_conf_get("pbrepo");
2040
2041 # Repository management
2042 open(PBS,"> $ENV{'PBBUILDDIR'}/pbscript.$$") || die "Unable to create $ENV{'PBBUILDDIR'}/pbscript.$$";
2043 if ($pbos->{'type'} eq "rpm") {
2044 my $pbsha = pb_distro_get_param($pbos,pb_conf_get("ossha"));
2045 my $gpgcheck = pb_conf_get_if("pbgpgcheck");
2046 my $pbgpgcheck;
2047 $pbgpgcheck = $gpgcheck->{$ENV{PBPROJ}} if (defined $gpgcheck);
2048 # By default force GPG check in repo even if we support signature of packages to fail. This is a best practice
2049 $pbgpgcheck = 1 if (not defined $pbgpgcheck);
2050 # Also make a pbscript to generate yum/urpmi bases
2051 print PBS << "EOF";
2052#!/bin/bash
2053# Prepare a script to ease yum setup
2054EOF
2055 print PBS "set -x\n" if ($pbdebug gt 1);
2056 print PBS << "EOF";
2057cat > $ENV{'PBPROJ'}.repo << EOT
2058[$ENV{'PBPROJ'}]
2059name=$pbos->{'name'} $pbos->{'version'} $pbos->{'arch'} - $ENV{'PBPROJ'} Vanilla Packages
2060baseurl=$pbrepo->{$ENV{'PBPROJ'}}/$repodir
2061enabled=1
2062gpgcheck=$pbgpgcheck
2063gpgkey=$pbrepo->{$ENV{'PBPROJ'}}/$repodir/$ENV{'PBPROJ'}.pubkey
2064EOT
2065chmod 644 $ENV{'PBPROJ'}.repo
2066
2067# Clean up old repo content
2068rm -rf headers/ repodata/
2069# Create yum repo
2070if [ -x /usr/bin/yum-arch ]; then
2071 yum-arch .
2072fi
2073# Create repodata
2074createrepo -s $pbsha .
2075# Link to the key
2076if [ -s $ENV{'PBPROJ'}.pubkey ]; then
2077 (cd repodata ; ln -sf ../$ENV{'PBPROJ'}.pubkey repomd.xml.key)
2078else
2079 echo "Avoiding creating empty repomd.xml.key; fails on opensuse 12.1"
2080fi
2081# sign the repomd (at least useful for SLES - which requires a local key)
2082# gpg -a --detach-sign repodata/repomd.xml
2083# SLES also looks for media.1/info.txt
2084EOF
2085 if ($pbos->{'family'} eq "md") {
2086 # For Mandriva add urpmi management
2087 print PBS << "EOF";
2088# Prepare a script to ease urpmi setup
2089cat > $ENV{'PBPROJ'}.addmedia << EOT
2090urpmi.addmedia $ENV{'PBPROJ'} $pbrepo->{$ENV{'PBPROJ'}}/$repodir with media_info/hdlist.cz
2091EOT
2092chmod 755 $ENV{'PBPROJ'}.addmedia
2093
2094# Clean up old repo content
2095rm -f hdlist.cz synthesis.hdlist.cz
2096# Create urpmi repo
2097genhdlist2 --clean .
2098if [ \$\? -ne 0 ]; then
2099 genhdlist .
2100fi
2101EOF
2102 }
2103 if ($pbos->{'name'} eq "fedora") {
2104 # Extract the spec file to please Fedora maintainers :-(
2105 print PBS << "EOF";
2106for p in $basesrc; do
2107 echo \$p | grep -q 'src.rpm'
2108 if [ \$\? -eq 0 ]; then
2109 rpm2cpio \$p | cpio -ivdum --quiet '*.spec'
2110 fi
2111done
2112EOF
2113 }
2114 if ($pbos->{'family'} eq "novell") {
2115 # Add ymp scripts for one-click install on SuSE
2116 print PBS << "EOF";
2117# Prepare a script to ease SuSE one-click install
2118# Cf: http://de.opensuse.org/1-Klick-Installation/ISV
2119#
2120cat > $ENV{'PBPROJ'}.ymp << EOT
2121<?xml version="1.0" encoding="utf-8"?>
2122<!-- vim: set sw=2 ts=2 ai et: -->
2123<metapackage xmlns:os="http://opensuse.org/Standards/One_Click_Install" xmlns="http://opensuse.org/Standards/One_Click_Install">
2124 <group><!-- The group of software, typically one for project-builder.org -->
2125 <name>$ENV{'PBPROJ'} Bundle</name> <!-- Name of the software group -->
2126 <summary>Software bundle for the $ENV{'PBPROJ'} project</summary> <!--This message is shown to the user and should describe the whole bundle -->
2127 <description>This is the summary of the $ENV{'PBPROJ'} Project
2128
2129 Details are available on a per package basis below
2130
2131 </description><!--This is also shown to the user -->
2132 <remainSubscribed>false</remainSubscribed> <!-- Don't know what it mean -->
2133 <repositories><!-- List of needed repositories -->
2134 <repository>
2135 <name>$ENV{'PBPROJ'} Repository</name> <!-- Name of the repository -->
2136 <summary>This repository contains the $ENV{'PBPROJ'} project packages.</summary> <!-- Summary of the repository -->
2137 <description>This repository contains the $ENV{'PBPROJ'} project packages.</description><!-- This description is shown to the user -->
2138 <url>$pbrepo->{$ENV{'PBPROJ'}}/$repodir</url><!--URL of repository, which is added -->
2139 </repository>
2140 </repositories>
2141 <software><!-- A List of packages, which should be added through the one-click-installation -->
2142EOT
2143for p in $basesrc; do
2144 sum=`rpm -q --qf '%{SUMMARY}' \$p`
2145 name=`rpm -q --qf '%{NAME}' \$p`
2146 desc=`rpm -q --qf '%{description}' \$p`
2147 cat >> $ENV{'PBPROJ'}.ymp << EOT
2148 <item>
2149 <name>\$name</name><!-- Name of the package, is shown to the user and used to identify the package at the repository -->
2150 <summary>\$sum</summary> <!-- Summary of the package -->
2151 <description>\$desc</description> <!-- Description, is shown to the user -->
2152 </item>
2153EOT
2154done
2155cat >> $ENV{'PBPROJ'}.ymp << EOT
2156 </software>
2157 </group>
2158</metapackage>
2159EOT
2160chmod 644 $ENV{'PBPROJ'}.ymp
2161EOF
2162 }
2163 } elsif ($pbos->{'type'} eq "deb") {
2164 # Also make a pbscript to generate apt bases
2165 # Cf: http://www.debian.org/doc/manuals/repository-howto/repository-howto.fr.html
2166 # This dirname removes ver
2167 my $debarch = $pbos->{'arch'};
2168 $debarch = "amd64" if ($pbos->{'arch'} eq "x86_64");
2169 my $rpd = dirname("$pbrepo->{$ENV{'PBPROJ'}}/$repodir");
2170 # Remove extra . in path to fix #522
2171 $rpd =~ s|/./|/|g;
2172 my ($projcomponent_map) = pb_conf_get_if("projcomponent");
2173 pb_log(2,"projcomponent = ".Dumper($projcomponent_map)."\n");
2174 my $projcomponent = $projcomponent_map->{$ENV{PBPROJ}};
2175 $projcomponent ||= 'contrib';
2176 print PBS << "EOF";
2177#!/bin/bash
2178# Prepare a script to ease apt setup
2179cat > $ENV{'PBPROJ'}.sources.list << EOT
2180deb $rpd $pbos->{'version'} $projcomponent
2181deb-src $rpd $pbos->{'version'} $projcomponent
2182EOT
2183chmod 644 $ENV{'PBPROJ'}.sources.list
2184
2185# Up two levels to deal with the dist dir cross versions
2186cd ..
2187mkdir -p dists/$pbos->{'version'}/$projcomponent/binary-$debarch dists/$pbos->{'version'}/$projcomponent/source
2188
2189# Prepare a script to create apt info file
2190# Reuse twice after
2191TMPD=`mktemp -d /tmp/pb.XXXXXXXXXX` || exit 1
2192mkdir -p \$TMPD
2193cat > \$TMPD/Release << EOT
2194Archive: unstable
2195Component: $projcomponent
2196Origin: $ENV{'PBPROJ'}
2197Label: $ENV{'PBPROJ'} dev repository $pbrepo->{$ENV{'PBPROJ'}}
2198EOT
2199
2200echo "Creating Packages metadata"
2201for i in dists/$pbos->{version}/$projcomponent/binary-*; do
2202 arch=`basename \$i | sed 's/binary-//'`
2203 echo "Packages for \$arch:"
2204 dpkg-scanpackages -a\$arch $pbos->{'version'} /dev/null | dists/$pbos->{'version'}/$projcomponent/binary-\$arch/Packages
2205 gzip -9 < dists/$pbos->{'version'}/$projcomponent/binary-\$arch/Packages > dists/$pbos->{'version'}/$projcomponent/binary-\$arch/Packages.gz
2206 bzip2 -9 < dists/$pbos->{'version'}/$projcomponent/binary-\$arch/Packages > dists/$pbos->{'version'}/$projcomponent/binary-\$arch/Packages.bz2
2207done
2208echo "Creating Contents metadata"
2209apt-ftparchive contents $pbos->{'version'} | gzip -c9 > dists/$pbos->{'version'}/Contents.gz
2210echo "Creating Release metadata ($pbos->{'arch'} aka $debarch)"
2211cat \$TMPD/Release > dists/$pbos->{'version'}/$projcomponent/binary-$debarch/Release
2212echo "Architecture: $debarch" >> dists/$pbos->{'version'}/$projcomponent/binary-$debarch/Release
2213echo "Creating Source metadata"
2214dpkg-scansources $pbos->{'version'} /dev/null > dists/$pbos->{'version'}/$projcomponent/source/Sources
2215gzip -9 < dists/$pbos->{'version'}/$projcomponent/source/Sources > dists/$pbos->{'version'}/$projcomponent/source/Sources.gz
2216bzip2 -9 < dists/$pbos->{'version'}/$projcomponent/source/Sources > dists/$pbos->{'version'}/$projcomponent/source/Sources.bz2
2217cat \$TMPD/Release > dists/$pbos->{'version'}/$projcomponent/source/Release
2218echo "Architecture: Source" >> dists/$pbos->{'version'}/$projcomponent/source/Release
2219echo "Creating Release metadata"
2220# Signing that file would be useful but uneasy as gpg keys are not there
2221# Cf: http://wiki.debian.org/SecureApt
2222# Same as for repomd
2223apt-ftparchive -o APT::FTPArchive::Release::Suite=$pbos->{version} release dists/$pbos->{'version'} > dists/$pbos->{'version'}/Release
2224rm -rf \$TMPD
2225EOF
2226 } elsif ($pbos->{'type'} eq "ebuild") {
2227 # make a pbscript to generate links to latest version
2228 print PBS << "EOF";
2229#!/bin/bash
2230# Prepare a script to create correct links
2231for p in $src; do
2232 echo \$p | grep -q '.ebuild'
2233 if [ \$\? -eq 0 ]; then
2234 j=`basename \$p`
2235 pp=`echo \$j | cut -d'-' -f1`
2236 ln -sf \$j \$pp.ebuild
2237 fi
2238done
2239EOF
2240 }
2241 close(PBS);
2242 chmod 0755,"$ENV{'PBBUILDDIR'}/pbscript.$$";
2243 } else {
2244 return;
2245 }
2246
2247 # Useless for VE
2248 my $nport = pb_get_port($sshport,$pbos,$cmt) if ($cmt !~ /^VE/);
2249
2250 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
2251 if ($cmt =~ /^VE/o) {
2252 $tdir =~ s|\$ENV.+\}/|| or confess "for $cmt: $tdir must have \$ENV{'HOME'} in it or the remainder of the code won't work since some parts use relative pathing and others will try absolute. Use a symlink if necessary to place the real files in a different place than under your home directory";
2253 } else {
2254 # process, ok if not present.
2255 $tdir =~ s|\$ENV.+\}/||;
2256 }
2257
2258 my $tm = "";
2259 if ($cmt =~ /^(V|R)M/) {
2260 $tm = "sleep $vtmout" if (defined $vtmout);
2261 }
2262
2263 # ssh communication if not VE or CPAN
2264 # should use a hash instead...
2265 my ($shcmd,$shcmdroot,$cpcmd,$cptarget,$cp2target);
2266 if ($cmt =~ /^VE/) {
2267 my $tp = pb_path_expand($vepath->{$ENV{'PBPROJ'}});
2268 my $tpdir = pb_path_expand("$tp/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}");
2269 my ($ptr) = pb_conf_get("vetype");
2270 my $vetype = $ptr->{$ENV{'PBPROJ'}};
2271 my $arch = pb_get_arch();
2272 if ($vetype eq "chroot") {
2273 $shcmdroot = "sudo /usr/sbin/chroot $tpdir ";
2274 $shcmd = "$shcmdroot /bin/su - $mac -c ";
2275 } elsif ($vetype eq "schroot") {
2276 $shcmd = "schroot $tp -u $mac -- ";
2277 }
2278 $shcmd = "setarch i386 $shcmd" if (($pbos->{'arch'} =~ /i[3456]86/) && ($arch eq 'x86_64'));
2279 $cpcmd = "sudo /bin/cp -r ";
2280 # We need to get the home dir of the target account to deliver in the right place
2281 open(PASS,"$tpdir/etc/passwd") || die "Unable to open $tpdir/etc/passwd: $!";
2282 my $homedir = "";
2283 while (<PASS>) {
2284 my ($c1,$c2,$c3,$c4,$c5,$c6,$c7) = split(/:/);
2285 $homedir = $c6 if ($c1 =~ /^$mac$/);
2286 pb_log(3,"Homedir: $homedir - account: $c6\n");
2287 }
2288 close(PASS);
2289 $cptarget = "$tpdir/$homedir/$tdir";
2290 if ($cmt eq "VEbuild") {
2291 $cp2target = "$tpdir/$homedir/$bdir";
2292 }
2293 pb_log(2,"On VE using $cptarget as target dir to copy to\n");
2294 } elsif ($cmt eq "CPAN") {
2295 my $ftpput = pb_check_req("ncftpput",1);
2296 my $ftpget = pb_check_req("wget",1);
2297 my ($cpanuser,$cpanpasswd) = pb_conf_get_if("cpanuser","cpanpasswd");
2298 return if ((not defined $cpanuser) || (not defined $cpanuser->{$ENV{'PBPROJ'}}));
2299 return if ((not defined $cpanpasswd) || (not defined $cpanpasswd->{$ENV{'PBPROJ'}}));
2300 my ($cpansubdir) = pb_conf_get_if("cpansubdir");
2301 $shcmd = "$ftpget --post-data \'HIDDENNAME=".$cpanuser->{$ENV{'PBPROJ'}};
2302 $shcmd .= "&user=".$cpanuser->{$ENV{'PBPROJ'}};
2303 $shcmd .= "&password=".$cpanpasswd->{$ENV{'PBPROJ'}};
2304 $shcmd .= "&SUBMIT_pause99_add_uri_upload=\"Upload the checked files\"";
2305 $shcmd .= "&pause99_add_uri_subdirtext=".$cpansubdir->{$ENV{'PBPROJ'}} if ((defined $cpansubdir) && (defined $cpansubdir->{$ENV{'PBPROJ'}}));
2306 foreach my $s (split(/ /,$src)) {
2307 $shcmd .= "&pause99_add_uri_upload=".basename($s);
2308 }
2309 $shcmd .= "'";
2310 $cpcmd = "$ftpput $host $dir";
2311 $cptarget = "CPAN";
2312 } else {
2313 my $keyfile = pb_ssh_get(0);
2314 my $keyopt = defined $keyfile ? "-i $keyfile" : "";
2315 my $sshcmd = pb_check_req("ssh",1);
2316 my $scpcmd = pb_check_req("scp",1);
2317 $shcmd = "$sshcmd $keyopt -q -o NoHostAuthenticationForLocalhost=yes -p $nport $mac";
2318 $cpcmd = "$scpcmd $keyopt -p -o NoHostAuthenticationForLocalhost=yes -P $nport";
2319 $cptarget = "$mac:$tdir";
2320 if ($cmt =~ /^(V|R)Mbuild/) {
2321 $cp2target = "$mac:$bdir";
2322 }
2323 }
2324
2325 my $logres = "";
2326 # Do not touch when just announcing
2327 if (($cmt ne "Announce") && ($cmt ne "CPAN")) {
2328 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 -e\"","Preparing $tdir on $cptarget");
2329 } else {
2330 $logres = "> ";
2331 }
2332 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
2333
2334 # For VE we need to change the owner manually
2335 if ($cmt =~ /^VE/) {
2336 pb_system("$shcmdroot sed -i '/requiretty/d' /etc/sudoers","Removing potential requiretty in sudoers");
2337 pb_system("$shcmd \"sudo chown -R $mac $tdir\"","Adapt owner in $tdir to $mac");
2338 }
2339
2340 # Use the right script name depending on context
2341 my $pbscript;
2342 if (($cmt =~ /^(V[EM]|RM)/) || ($cmt =~ /Packages/)){
2343 $pbscript = "pbscript.$$";
2344 } else {
2345 $pbscript = "pbscript";
2346 }
2347
2348 # It's already ready for CPAN
2349 my $shcmdbase = $shcmd;
2350 if ($cmt ne "CPAN") {
2351 $shcmd .= " \"echo \'cd $tdir ; if [ -x $pbscript ]; then ./$pbscript; fi ; rm -f ./$pbscript\' | bash\"";
2352 }
2353 my $cmdverb = "verbose";
2354 if (($cmt eq "Announce") || ($cmt eq "CPAN")) {
2355 $cmdverb = undef;
2356 }
2357 pb_system("$shcmd","Executing pbscript on $cptarget if needed",$cmdverb);
2358 if ($cmt =~ /^(V[EM]|RM)build/) {
2359 # Get back info on pkg produced, compute their name and get them from the VM/RM
2360 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");
2361 # For VE we need to change the owner manually
2362 if ($cmt eq "VEbuild") {
2363 my $command = pb_check_req("chown",0);
2364 pb_system("sudo $command $UID $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$","Adapt owner in $tdir to $UID");
2365 }
2366 if (not -f "$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$") {
2367 pb_log(0,"Problem with VM/RM $v on $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$");
2368 } else {
2369 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$";
2370 my $src = <KEEP>;
2371 chomp($src);
2372 close(KEEP);
2373 unlink("$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$");
2374
2375 $src =~ s/^ *//;
2376 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}");
2377 # Change pgben to make the next send2target happy
2378 my $made = "";
2379
2380 # For VM/RM we don't want shell expansion to hapen locally but remotely
2381 my $delim = '\'';
2382 if ($cmt =~ /^VEbuild/) {
2383 # For VE we need to support shell expansion locally
2384 $delim = "";
2385 }
2386
2387 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'}";
2388 foreach my $p (split(/ +/,$src)) {
2389 my $j = basename($p);
2390 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'}");
2391 $made="$made $pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/$j"; # if (($pbos->{'type'} ne "rpm") || ($j !~ /.src.rpm$/));
2392 }
2393 print KEEP "$made\n";
2394 close(KEEP);
2395 pb_system("$shcmdbase \"rm -rf $tdir $bdir\"","$cmt cleanup");
2396
2397 # Sign packages locally
2398 pb_sign_pkgs($pbos,$made);
2399
2400 # We want to send them to the ssh account so overwrite what has been done before
2401 undef $pbaccount;
2402 pb_log(2,"Before sending pkgs, vmexist: $vmexist, vmpid: $vmpid\n");
2403 pb_send2target("Packages",$pbos->{'name'}."-".$pbos->{'version'}."-".$pbos->{'arch'},$vmexist,$vmpid);
2404 pb_rm_rf("$ENV{'PBBUILDDIR'}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}");
2405 }
2406 }
2407 unlink("$ENV{'PBDESTDIR'}/pbscript.$$") if ((($cmt =~ /^(V[ME]|RM)/) || ($cmt =~ /Packages/)) && ($pbkeep eq 0));
2408
2409 pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
2410 if ((! $vmexist) && ($cmt =~ /^VM/)) {
2411 # If in setupvm then takes a snapshot just before halting
2412 if ($snapme != 0) {
2413 my ($vmmonport,$vmtype) = pb_conf_get("vmmonport","vmtype");
2414 # For monitoring control
2415 if ((($vmtype->{$ENV{'PBPROJ'}}) eq "kvm") || (($vmtype->{$ENV{'PBPROJ'}}) eq "qemu")) {
2416 eval
2417 {
2418 require Net::Telnet;
2419 Net::Telnet->import();
2420 };
2421 if ($@) {
2422 # Net::Telnet not found
2423 pb_log(1,"ADVISE: Install Net::Telnet to benefit from monitoring control and snapshot feature.\nWARNING: No snapshot created");
2424 } else {
2425 my $t = new Net::Telnet (Timeout => 120, Host => "localhost", Port => $vmmonport->{$ENV{'PBPROJ'}}) || die "Unable to dialog on the monitor";
2426 # move to monitor mode
2427 my @lines = $t->cmd("c");
2428 # Create a snapshot named pb
2429 @lines = $t->cmd("savevm pb");
2430 # Write the new status in the VM
2431 @lines = $t->cmd("commit all");
2432 # End
2433 @lines = $t->cmd("quit");
2434 }
2435 }
2436 }
2437 my $hoption = "-p";
2438 my $hpath = pb_distro_get_param($pbos,pb_conf_get("ospathcmd-halt"));
2439 # Solaris doesn't support -p of halt
2440 if ($pbos->{'type'} eq "pkg") {
2441 $hoption = "" ;
2442 }
2443 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)");
2444 }
2445 if (($cmt =~ /^VE/) && ($snapme != 0)) {
2446 my $tpdir = "$vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}";
2447 pb_system("sudo tar cz -C $tpdir -f $vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz .","Creating a snapshot of $tpdir");
2448 }
2449}
2450
2451sub pb_script2v {
2452 my $pbscript=shift;
2453 my $vtype=shift;
2454 my $pbforce=shift || 0; # Force stop of VM. Default not.
2455 my $vm1=shift || undef; # Only that VM/VE/RM to treat. Default all.
2456 my $snapme=shift || 0; # Do we have to create a snapshot. Default not.
2457 my $vm;
2458 my $all;
2459
2460 pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$pbforce,".Dumper($vm1).",$snapme)\n");
2461 # Prepare the script to be executed on the VM/VE/RM
2462 # in $ENV{'PBDESTDIR'}/pbscript.$$
2463 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript.$$")) {
2464 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript.$$") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript.$$";
2465 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript.$$";
2466 }
2467
2468 if (not defined $vm1) {
2469 ($vm,$all) = pb_get2v($vtype);
2470 } else {
2471 @$vm = ($vm1);
2472 }
2473 my ($vmexist,$vmpid) = (undef,undef);
2474
2475 foreach my $v (@$vm) {
2476 # Launch VM/VE
2477 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,$snapme,$pbsnap);
2478
2479 if ($vtype eq "vm") {
2480 pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
2481
2482 # Skip that VM/RM if something went wrong
2483 next if (($vmpid == 0) && ($vmexist == 0));
2484
2485 # If force stopping the VM then reset vmexist
2486 if ($pbforce == 1) {
2487 $vmpid = $vmexist;
2488 $vmexist = 0;
2489 }
2490 } else {
2491 #VE
2492 $vmexist = 0;
2493 $vmpid = 0;
2494 }
2495
2496 # Gather all required files to send them to the VM/VE/RM
2497 # and launch the build through pbscript
2498 pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
2499 pb_send2target(uc($vtype)."Script","$v",$vmexist,$vmpid,$snapme);
2500
2501 }
2502}
2503
2504sub pb_launchv {
2505 my $vtype = shift;
2506 my $v = shift;
2507 my $create = shift || 0; # By default do not create a VM/VE/RM
2508 my $snapme = shift || 0; # By default do not snap a VM/VE/RM
2509 my $usesnap = shift || 1; # By default study the usage of the snapshot feature of VM/VE/RM
2510
2511 # If creation or snapshot creation mode, no snapshot usable
2512 if (($create == 1) || ($snapme == 1)) {
2513 $usesnap = 0;
2514 }
2515
2516 pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create,$snapme,$usesnap)\n");
2517 die "No VM/VE/RM defined, unable to launch" if (not defined $v);
2518 # Keep only the first VM in case many were given
2519 if ($v =~ /,/) {
2520 pb_log(0,"WARNING: pruning to just the first of several vms listed ($v)\n");
2521 $v =~ s/,.*//;
2522 }
2523
2524 my $pbos = pb_distro_get_context($v);
2525
2526 my $ftp_proxy = pb_distro_get_param($pbos,pb_conf_get_if("ftp_proxy"));
2527 my $http_proxy = pb_distro_get_param($pbos,pb_conf_get_if("http_proxy"));
2528
2529 $ENV{ftp_proxy} ||= $ftp_proxy if ((defined $ftp_proxy) && ($ftp_proxy ne ""));
2530 $ENV{http_proxy} ||= $http_proxy if ((defined $http_proxy) && ($http_proxy ne ""));
2531
2532 # Launch the VMs/VEs
2533 if ($vtype eq "vm") {
2534 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
2535
2536 my ($ptr,$ptr2,$vmpath,$vmport,$vms) = pb_conf_get("vmtype","vmcmd","vmpath","vmport","vmsize");
2537 my ($vmopt,$vmmm,$vmtmout,$vmsnap,$vmbuildtm,$vmmonport) = pb_conf_get_if("vmopt","vmmem","vmtmout","vmsnap","vmbuildtm","vmmonport");
2538 my $vmsize = pb_distro_get_param($pbos,$vms);
2539
2540 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
2541 my $vmcmd = $ptr2->{$ENV{'PBPROJ'}};
2542
2543 if (defined $opts{'g'}) {
2544 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
2545 $ENV{'PBVMOPT'} = "--nographic";
2546 }
2547 }
2548 if (not defined $ENV{'PBVMOPT'}) {
2549 $ENV{'PBVMOPT'} = "";
2550 }
2551 # Save the current status for later restoration
2552 $ENV{'PBOLDVMOPT'} = $ENV{'PBVMOPT'};
2553 # Set a default timeout of 2 minutes
2554 if (not defined $ENV{'PBVMTMOUT'}) {
2555 $ENV{'PBVMTMOUT'} = "120";
2556 }
2557 if (defined $vmopt->{$v}) {
2558 $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
2559 } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
2560 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
2561 }
2562
2563 # How much memory to allocate for VMs
2564 if (defined $vmmm) {
2565 my $vmmem = pb_distro_get_param($pbos,$vmmm);
2566 if (defined $vmmem) {
2567 $ENV{'PBVMOPT'} .= " -m $vmmem";
2568 }
2569 }
2570
2571 # Are we allowed to use snapshot feature
2572 if ($usesnap == 1) {
2573 if ((defined $vmsnap->{$v}) && ($vmsnap->{$v} =~ /true/i)) {
2574 $ENV{'PBVMOPT'} .= " -snapshot";
2575 } elsif ((defined $vmsnap->{$ENV{'PBPROJ'}}) && ($vmsnap->{$ENV{'PBPROJ'}} =~ /true/i)) {
2576 $ENV{'PBVMOPT'} .= " -snapshot";
2577 } elsif ($pbsnap eq 1) {
2578 $ENV{'PBVMOPT'} .= " -snapshot";
2579 }
2580 }
2581 if ($snapme != 0) {
2582 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
2583 # Configure the monitoring to automate the creation of the 'pb' snapshot
2584 $ENV{'PBVMOPT'} .= " -serial mon:telnet::$vmmonport->{$ENV{'PBPROJ'}},server,nowait" if ((defined $vmmonport) && (defined $vmmonport->{$ENV{'PBPROJ'}}));
2585 # In that case no snapshot call needed
2586 $ENV{'PBVMOPT'} =~ s/ -snapshot//;
2587 }
2588 }
2589 if (defined $vmtmout->{$v}) {
2590 $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
2591 } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
2592 $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
2593 }
2594 my $nport = pb_get_port($vmport,$pbos,$vtype);
2595
2596 my $cmd;
2597 my $vmm; # has to be used for pb_check_ps
2598 if (($vmtype eq "qemu") || ($vmtype eq "kvm")) {
2599 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
2600 if (($create != 0) || (defined $iso)) {
2601 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
2602 }
2603 # Always redirect the network and always try to use a 'pb' snapshot
2604 #$cmd = "$vmcmd $ENV{'PBVMOPT'} -net user,hostfwd=tcp:$nport:10.0.2.15:22 -loadvm pb $vmm"
2605 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
2606 } elsif ($vmtype eq "xen") {
2607 } elsif ($vmtype eq "vmware") {
2608 } else {
2609 die "VM of type $vmtype not supported. Report to the dev team";
2610 }
2611 # Restore the ENV VAR Value
2612 $ENV{'PBVMOPT'} = $ENV{'PBOLDVMOPT'};
2613
2614 my ($tmpcmd,$void) = split(/ +/,$cmd);
2615 my $vmexist = pb_check_ps($tmpcmd,$vmm);
2616 my $vmpid = 0;
2617 if (! $vmexist) {
2618 if ($create != 0) {
2619 die("Found an existing Virtual machine $vmm. Won't overwrite") if (-r $vmm);
2620 if (($vmtype eq "qemu") || ($vmtype eq "xen") || ($vmtype eq "kvm")) {
2621 my $command = pb_check_req("qemu-img",0);
2622 pb_system("$command create -f qcow2 $vmm $vmsize","Creating the QEMU VM");
2623 } elsif ($vmtype eq "vmware") {
2624 } else {
2625 }
2626 }
2627 if (! -f "$vmm") {
2628 pb_log(0,"Unable to find VM $vmm\n");
2629 } else {
2630 # Is the SSH port free? if not kill the existing process using it after a build timeout period
2631 my $vmssh = pb_check_ps($tmpcmd,"tcp:$nport:10.0.2.15:22");
2632 if ($vmssh) {
2633 my $buildtm = $ENV{'PBVMTMOUT'};
2634 if (defined $vmbuildtm->{$v}) {
2635 $buildtm = $vmbuildtm->{$v};
2636 } elsif (defined $vmbuildtm->{$ENV{'PBPROJ'}}) {
2637 $buildtm = $vmbuildtm->{$ENV{'PBPROJ'}};
2638 }
2639
2640 sleep $buildtm;
2641 pb_log(0,"WARNING: Killing the process ($vmssh) using port $nport (previous failed VM ?)\n");
2642 kill 15,$vmssh;
2643 # Let it time to exit
2644 sleep 5;
2645 }
2646 pb_system("$cmd &","Launching the VM $vmm");
2647 # Using system allows to kill it externaly if needed,sosupport that in the call
2648 pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up","mayfail");
2649 $vmpid = pb_check_ps($tmpcmd,$vmm);
2650 pb_log(0,"VM $vmm launched (pid $vmpid)\n");
2651 }
2652 } else {
2653 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
2654 }
2655 pb_log(2,"DEBUG: pb_launchv returns ($vmexist,$vmpid)\n");
2656 return($vmexist,$vmpid);
2657 } elsif ($vtype eq "ve") {
2658 # Force the creation of the VE and no snapshot usable
2659 pb_ve_launch($v,$create,$usesnap);
2660 } else {
2661 # RM here
2662 # Get distro context
2663 my $pbos = pb_distro_get_context($v);
2664
2665 # Get RM context
2666 my ($ptr,$rmpath) = pb_conf_get("rmtype","rmpath");
2667
2668 # Nothing more to do for RM. No real launch
2669 # For the moment we support the RM is already running
2670 # For ProLiant may be able to power them on if needed later on as an example.
2671 }
2672}
2673
2674# Return string for date synchro
2675sub pb_date2v {
2676
2677my $vtype = shift;
2678my $pbos = shift;
2679
2680# VE gets time from parent OS.
2681return "/bin/true" if ($vtype) =~ /^ve/o;
2682
2683my ($ntp) = pb_conf_get_if($vtype."ntp");
2684my $vntp = $ntp->{$ENV{'PBPROJ'}} if (defined $ntp);
2685my $ntpline = undef;
2686
2687if ((defined $vntp) && ($vntp !~ /^\s*$/)) {
2688 # ntp command depends on pbos
2689 my $vntpcmd = pb_distro_get_param($pbos,pb_conf_get($vtype."ntpcmd"));
2690 $ntpline = "sudo $vntpcmd $vntp";
2691}
2692# Force new date to be in the future compared to the date
2693# of the host by adding 1 minute
2694my @date=pb_get_date();
2695$date[1]++;
2696my $upddate = strftime("%m%d%H%M%Y", @date);
2697my $dateline = "sudo /bin/date $upddate";
2698if (defined $ntpline) {
2699 return($ntpline);
2700} else {
2701 return($dateline);
2702}
2703}
2704
2705sub pb_build2v {
2706
2707my $vtype = shift;
2708my $action = shift || "build";
2709
2710my ($v,$all) = pb_get2v($vtype);
2711
2712# Send tar files when we do a global generation
2713pb_build2ssh() if (($all == 1) && ($action eq "build"));
2714
2715# Adapt // mode to memory size
2716$pbparallel = pb_set_parallel($vtype);
2717
2718my ($vmexist,$vmpid) = (undef,undef);
2719my $pm;
2720my $all_ok = 1;
2721if (defined $pbparallel) {
2722 $pm = new Parallel::ForkManager($pbparallel);
2723
2724 # Set which port the VM/RM will use to communicate
2725 $pm->run_on_start(\&pb_set_port);
2726 $pm->run_on_finish(sub { my ($pid, $code, $id, $signal, $dump) = @_;
2727 unless ($code == 0 && $signal == 0 && $dump == 0) {
2728 $all_ok = 0;
2729 pb_log(0,"ERROR: pid $pid failed\n");
2730 }
2731 });
2732}
2733
2734my $counter = 0;
2735foreach my $v (@$v) {
2736 $counter++;
2737 # Modulo 2 * pbparallel (to avoid synchronization problems)
2738 $counter = 1 if ((defined $pbparallel) && ($counter > 2 * $pbparallel));
2739 $pm->start($counter) and next if (defined $pbparallel);
2740 # Prepare the script to be executed on the VM/VE/RM
2741 # in $ENV{'PBDESTDIR'}/pbscript.$$
2742 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript.$$") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript.$$";
2743 print SCRIPT "#!/bin/bash\n";
2744
2745 # Transmit the verbosity level to the virtual env/mach.
2746 my $verbose = "";
2747 my $i = 0; # minimal debug level
2748 while ($i lt $pbdebug) {
2749 $verbose .= "-v ";
2750 $i++;
2751 }
2752 print SCRIPT "set -e\n" if ($Global::pb_stop_on_error);
2753 # Activate script verbosity if at least 2 for pbdebug
2754 print SCRIPT "set -x\n" if ($i gt 1);
2755 # Quiet if asked to be so on the original system
2756 $verbose = "-q" if ($pbdebug eq -1);
2757
2758 print SCRIPT "echo ... Execution needed\n";
2759 print SCRIPT "# This is in directory delivery\n";
2760 print SCRIPT "# Setup the variables required for building\n";
2761 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
2762
2763 if ($action eq "build") {
2764 print SCRIPT "# Preparation for pb\n";
2765 print SCRIPT "mv .pbrc \$HOME\n";
2766 print SCRIPT "cd ..\n";
2767 }
2768
2769 # VE needs a good /proc, tolerate one being potentially left around after a failure
2770 if ($vtype eq "ve") {
2771 print SCRIPT "[ -d /proc/1 ] || sudo /bin/mount -t proc /proc /proc\n";
2772 }
2773
2774 # Get distro context
2775 my $pbos = pb_distro_get_context($v);
2776
2777 my $ntpline = pb_date2v($vtype,$pbos);
2778 print SCRIPT "# Time sync\n";
2779 print SCRIPT "echo setting up date with $ntpline\n";
2780 print SCRIPT "$ntpline\n";
2781 # Use potential local proxy declaration in case we need it to download repo, pkgs, ...
2782 if (defined $ENV{'http_proxy'}) {
2783 print SCRIPT "export http_proxy=\"$ENV{'http_proxy'}\"\n";
2784 }
2785
2786 if (defined $ENV{'ftp_proxy'}) {
2787 print SCRIPT "export ftp_proxy=\"$ENV{'ftp_proxy'}\"\n";
2788 }
2789
2790 # Get list of packages to build/test and get some ENV vars as well
2791 my $ptr = pb_get_pkg();
2792 @pkgs = @$ptr;
2793 my $p = join(' ',@pkgs) if (@pkgs);
2794 print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
2795 print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
2796 print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
2797
2798 # We may need to do some other tasks before building. Read a script here to finish setup
2799 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".pre") {
2800 print SCRIPT "# Special pre-instructions to be launched\n";
2801 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".pre");
2802 }
2803
2804 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre") {
2805 print SCRIPT "# Special pre-$action instructions to be launched\n";
2806 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre");
2807 }
2808
2809 print SCRIPT "# $action\n";
2810 print SCRIPT "echo $action"."ing packages on $vtype...\n";
2811
2812 if (($action eq "test") && (! -x "$ENV{'PBDESTDIR'}/pbtest")) {
2813 die "No test script ($ENV{'PBDESTDIR'}/pbtest) found when in test mode. Aborting ...";
2814 }
2815 print SCRIPT "pb $verbose -p $ENV{'PBPROJ'} $action"."2pkg $p\n";
2816
2817 if ($vtype eq "ve") {
2818 print SCRIPT "sudo /bin/umount /proc\n";
2819 }
2820
2821 # We may need to do some other tasks after building. Read a script here to exit properly
2822 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.post") {
2823 print SCRIPT "# Special post-$action instructions to be launched\n";
2824 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.post");
2825 }
2826
2827 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".post") {
2828 print SCRIPT "# Special post-instructions to be launched\n";
2829 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".post");
2830 }
2831
2832 print SCRIPT q{echo "********** Successful exit of script $$ *********************"}, "\n";
2833 close(SCRIPT);
2834 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript.$$";
2835
2836 # Launch the VM/VE/RM
2837 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
2838
2839
2840 if ($vtype eq "vm") {
2841 # Skip that VM if something went wrong
2842 if (($vmpid == 0) && ($vmexist == 0)) {
2843 $pm->finish if (defined $pbparallel);
2844 next;
2845 }
2846 } else {
2847 # VE/RM
2848 $vmexist = 0;
2849 $vmpid = 0;
2850 }
2851 # Gather all required files to send them to the VM/VE
2852 # and launch the build through pbscript
2853 pb_log(2,"Calling send2target $vtype,$v,$vmexist,$vmpid\n");
2854 pb_send2target(uc($vtype).$action,"$v",$vmexist,$vmpid);
2855 $pm->finish if (defined $pbparallel);
2856}
2857$pm->wait_all_children if (defined $pbparallel);
2858die "Aborting, one or more of the children failed." if ((not $all_ok) && ($Global::pb_stop_on_error));
2859}
2860
2861
2862sub pb_clean {
2863
2864 my $sleep=10;
2865 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
2866 die "Unable to get env var PBBUILDDIR" if (not defined $ENV{'PBBUILDDIR'});
2867 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");
2868 sleep $sleep;
2869 pb_rm_rf($ENV{'PBDESTDIR'});
2870 pb_rm_rf($ENV{'PBBUILDDIR'});
2871}
2872
2873sub pb_newver {
2874
2875 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
2876
2877 # Need this call for PBDIR
2878 my ($scheme2,$uri) = pb_cms_init($pbinit);
2879
2880 my ($pbconf,$pburl) = pb_conf_get("pbconfurl","pburl");
2881 $uri = $pbconf->{$ENV{'PBPROJ'}};
2882 my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
2883
2884 # Checking CMS repositories status
2885 ($scheme2, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
2886
2887 if ($scheme !~ /^svn/) {
2888 die "Only SVN is supported at the moment";
2889 }
2890
2891 my $res = pb_vcs_isdiff($scheme,$ENV{'PBROOTDIR'});
2892 die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
2893
2894 $res = pb_vcs_isdiff($scheme2,$ENV{'PBDIR'});
2895 die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
2896
2897 # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
2898 # we want to get for the root of the new URL
2899
2900 my $oldver = $ENV{'PBROOTDIR'};
2901 $oldver =~ s|^$ENV{'PBCONFDIR'}||;
2902
2903 pb_log(2, "PBCONFDIR: $ENV{'PBCONFDIR'}\nPBROOTDIR: $ENV{'PBROOTDIR'}\n");
2904
2905 my $newurl = "$uri/$newver";
2906 # Should probably use projver in the old file
2907 my $oldvertxt= basename($oldver);
2908 my $newvertxt = basename($newver);
2909
2910 # Duplicate and extract project-builder part
2911 pb_log(2,"Copying $uri/$oldver to $newurl\n");
2912 pb_vcs_copy($scheme,"$uri/$oldver",$newurl);
2913 pb_log(2,"Checkout $newurl to $ENV{'PBCONFDIR'}/$newver\n");
2914 pb_vcs_up($scheme,"$ENV{'PBCONFDIR'}");
2915
2916 # Duplicate and extract project
2917 my $newurl2 = "$pburl->{$ENV{'PBPROJ'}}/$newver";
2918
2919 pb_log(2,"Copying $pburl->{$ENV{'PBPROJ'}}/$oldver to $newurl2\n");
2920 pb_vcs_copy($scheme2,"$pburl->{$ENV{'PBPROJ'}}/$oldver",$newurl2);
2921
2922 my $tmp = $ENV{'PBDIR'};
2923 $tmp =~ s|$oldver$||;
2924 pb_log(2,"Checkout $newurl2 to $tmp/$newver\n");
2925 pb_vcs_up($scheme2,"$tmp");
2926
2927 # Update the .pb file
2928 open(FILE,"$ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb";
2929 open(OUT,"> $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb.new";
2930 while(<FILE>) {
2931 if (/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldvertxt$/) {
2932 s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldvertxt$/projver $ENV{'PBPROJ'} = $newvertxt/;
2933 pb_log(0,"Changing projver from $oldvertxt to $newvertxt in $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb\n");
2934 }
2935 if (/^testver/) {
2936 s/^testver/#testver/;
2937 pb_log(0,"Commenting testver in $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb\n") if (/^testver/);
2938 }
2939 if (/^delivery/) {
2940 my $txt = $_;
2941 chomp($txt);
2942 pb_log(0,"Please check delivery ($txt) in $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb\n");
2943 }
2944 print OUT $_;
2945 }
2946 close(FILE);
2947 close(OUT);
2948 rename("$ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb.new","$ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.pb");
2949
2950 # Checking pbcl files
2951 foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
2952 # Compute new pbcl file
2953 my $f2 = $f;
2954 $f2 =~ s|$ENV{'PBROOTDIR'}|$ENV{'PBCONFDIR'}/$newver/|;
2955 open(PBCL,$f) || die "Unable to open $f";
2956 my $foundnew = 0;
2957 while (<PBCL>) {
2958 $foundnew = 1 if (/^$newvertxt \(/);
2959 }
2960 close(PBCL);
2961 open(OUT,"> $f2") || die "Unable to write to $f2: $!";
2962 open(PBCL,$f) || die "Unable to open $f";
2963 while (<PBCL>) {
2964 print OUT "$_" if (not /^$oldvertxt \(/);
2965 if ((/^$oldvertxt \(/) && ($foundnew == 0)) {
2966 print OUT "$newvertxt ($pbdate)\n";
2967 print OUT "- TBD\n";
2968 print OUT "\n";
2969 pb_log(0,"WARNING: version $newvertxt not found in $f so added to $f2...\n") if ($foundnew == 0);
2970 }
2971 }
2972 close(OUT);
2973 close(PBCL);
2974 }
2975
2976 pb_log(2,"Checkin $ENV{'PBCONFDIR'}/$newver\n");
2977 pb_cms_checkin($scheme,"$ENV{'PBCONFDIR'}/$newver",undef);
2978}
2979
2980#
2981# Return the list of VMs/VEs/RMs we are working on
2982# $all is a flag to know if we return all of them
2983# or only some (if all we publish also tar files in addition to pkgs
2984#
2985sub pb_get2v {
2986
2987my $vtype = shift;
2988my @v;
2989my $all = 0;
2990my $pbv = 'PBV';
2991my $vlist = $vtype."list";
2992
2993# Get VM/VE list
2994if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
2995 my ($ptr) = pb_conf_get($vlist);
2996 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
2997 $all = 1;
2998}
2999pb_log(2,"$vtype: $ENV{$pbv}\n");
3000@v = split(/,/,$ENV{$pbv});
3001return(\@v,$all);
3002}
3003
3004# This function creates a giant script to configure a particular VM/VE/RM, it then copies the
3005# script to the target.
3006
3007# Function to create a potentialy missing pb account on the VM/VE/RM, and adds it to sudo
3008# Needs to use root account to connect to the VM/VE/RM
3009# pb will take your local public SSH key to access
3010# the pb account in the VM/VE/RM later on if needed
3011sub pb_setup2v {
3012
3013my $vtype = shift;
3014my $sbx = shift || undef;
3015
3016my ($vm,$all) = pb_get2v($vtype);
3017
3018# Script generated
3019my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
3020
3021# Adapt // mode to memory size
3022$pbparallel = pb_set_parallel($vtype);
3023
3024my $pm;
3025my $all_ok = 1;
3026if (defined $pbparallel) {
3027 $pm = new Parallel::ForkManager($pbparallel);
3028
3029 # Set which port the VM/RM will use to communicate
3030 $pm->run_on_start(\&pb_set_port);
3031 $pm->run_on_finish(sub { my ($pid, $code, $id, $signal, $dump) = @_;
3032 $all_ok = 0 unless (($code == 0) && ($signal == 0) && ($dump == 0));
3033 });
3034}
3035
3036my $counter = 0;
3037foreach my $v (@$vm) {
3038 $counter++;
3039 # Modulo pbparallel
3040 $counter = 1 if ((defined $pbparallel) && ($counter > $pbparallel));
3041 $pm->start($counter) and next if (defined $pbparallel);
3042
3043 # Get distro context
3044 my $pbos = pb_distro_get_context($v);
3045
3046 # Deal with date sync.
3047 my $ntpline = pb_date2v($vtype,$pbos);
3048
3049 # Name of the account to deal with for VM/VE/RM
3050 # Do not use the one passed potentially with -a
3051 my ($pbac) = pb_conf_get($vtype."login");
3052 my ($key,$zero0,$zero1,$zero2);
3053 my ($vmexist,$vmpid);
3054
3055 # Prepare the script to be executed on the VM/VE/RM
3056 # in $ENV{'PBDESTDIR'}/setupv
3057 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
3058
3059 print SCRIPT << 'EOF';
3060#!/usr/bin/perl -w
3061
3062use strict;
3063use File::Copy;
3064
3065# We should not need in this script more functions than what is provided
3066# by Base, Conf and Distribution to avoid problems at exec time.
3067# They are appended at the end.
3068
3069# Define mandatory global vars
3070our $pbdebug;
3071our $pbLOG;
3072our $pbsynmsg = "pbscript";
3073our $pbdisplaytype = "text";
3074our $pblocale = "";
3075pb_log_init($pbdebug, $pbLOG);
3076EOF
3077 print SCRIPT << "EOF";
3078\$Global::pb_stop_on_error = $Global::pb_stop_on_error;
3079pb_temp_init($pbkeep);
3080pb_conf_init("$ENV{'PBPROJ'}");
3081
3082EOF
3083
3084 # Launch the VM/VE/RM - Usage of snapshot disabled
3085 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,0,0);
3086
3087 my $keyfile;
3088 my $nport;
3089 my $vmhost;
3090
3091 # Prepare the key to be used and transfered remotely
3092 $keyfile = pb_ssh_get(1);
3093
3094 if ($vtype =~ /(v|r)m/) {
3095 my ($vmport);
3096 ($vmhost,$vmport) = pb_conf_get($vtype."host",$vtype."port");
3097 $nport = pb_get_port($vmport,$pbos,$vtype);
3098
3099 # Skip that VM/RM if something went wrong
3100 next if (($vmpid == 0) && ($vmexist == 0));
3101
3102 # Store the pub key part in a variable
3103 open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
3104 ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
3105 close(FILE);
3106
3107 $key = "\Q$zero1";
3108
3109 # We call true to avoid problems if SELinux is not activated, but chcon is present and returns in that case 1
3110 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");
3111 # once this is done, we can do what we need on the VM/RM remotely
3112 } elsif ($vtype eq "ve") {
3113 print SCRIPT << "EOF";
3114# For VE we need a good null dev
3115pb_system("rm -f /dev/null; mknod /dev/null c 1 3; chmod 777 /dev/null");
3116
3117# For VE we first need to mount some FS
3118pb_system("mount -t proc /proc /proc") unless (-d "/proc/$$");
3119
3120EOF
3121 } else {
3122 die "Unknown virtual type $vtype";
3123 }
3124
3125 if ($vtype =~ /(v|r)m/) {
3126 print SCRIPT << 'EOF';
3127# Removes duplicate in .ssh/authorized_keys of our key if needed
3128#
3129my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
3130open(PBFILE,$file1) || die "Unable to open $file1";
3131open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
3132my $count = 0;
3133while (<PBFILE>) {
3134
3135EOF
3136 print SCRIPT << "EOF";
3137 if (/ $key /) {
3138 \$count++;
3139 }
3140print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
3141}
3142close(PBFILE);
3143close(PBOUT);
3144rename("\$file1.new",\$file1);
3145chmod 0600,\$file1;
3146
3147EOF
3148 }
3149 print SCRIPT << 'EOF';
3150
3151# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
3152#
3153my $file="/etc/passwd";
3154open(PBFILE,$file) || die "Unable to open $file";
3155my $found = 0;
3156while (<PBFILE>) {
3157EOF
3158 print SCRIPT << "EOF";
3159 \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
3160EOF
3161
3162# TODO: use an external parameter
3163my $home = "/home";
3164# Solaris doesn't like that we use /home
3165$home = "/export/home" if ($pbos->{'type'} eq "pkg");
3166
3167 print SCRIPT << "EOF";
3168}
3169close(PBFILE);
3170
3171if ( \$found == 0 ) {
3172 if ( ! -d "$home" ) {
3173 pb_mkdir_p("$home");
3174 }
3175EOF
3176 # TODO: Level of portability of these cmds ? Critical now for RM
3177 # TODO: Check existence before adding to avoid errors
3178 print SCRIPT << "EOF";
3179pb_system("/usr/sbin/groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
3180pb_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'}})");
3181}
3182EOF
3183
3184 # Copy the content of our local conf file to the VM/VE/RM
3185 my $content = pb_get_content(pb_distro_conffile());
3186 print SCRIPT << "EOF";
3187 #
3188 # Create a temporary local conf file for distribution support
3189 # This is created here before its use later. Its place is hardcoded, so no choice for the path
3190 #
3191 my \$tempconf = pb_distro_conffile();
3192 pb_mkdir_p(dirname(\$tempconf));
3193 open(CONF,"> \$tempconf") || die "Unable to create \$tempconf";
3194 print CONF q{$content};
3195 close(CONF);
3196EOF
3197
3198 if ($vtype =~ /(v|r)m/) {
3199 print SCRIPT << "EOF";
3200# allow ssh entry to build
3201#
3202mkdir "$home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
3203# Allow those accessing root to access the build account
3204copy("\$ENV{'HOME'}/.ssh/authorized_keys","$home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
3205chmod 0600,".ssh/authorized_keys";
3206pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} $home/$pbac->{$ENV{'PBPROJ'}}","Finish setting up the account env for $pbac->{$ENV{'PBPROJ'}}");
3207
3208EOF
3209}
3210 print SCRIPT << 'EOF';
3211# No passwd for build account only keys
3212$file="/etc/shadow";
3213if (-f $file) {
3214 open(PBFILE,$file) || die "Unable to open $file";
3215 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
3216 while (<PBFILE>) {
3217EOF
3218 print SCRIPT << "EOF";
3219 s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
3220 s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/; #SLES 9 e.g.
3221 s/^$pbac->{$ENV{'PBPROJ'}}:\\*LK\\*:/$pbac->{$ENV{'PBPROJ'}}:NP:/; #Solaris e.g.
3222EOF
3223 print SCRIPT << 'EOF';
3224 print PBOUT $_;
3225 }
3226 close(PBFILE);
3227 close(PBOUT);
3228 rename("$file.new",$file);
3229 chmod 0640,$file;
3230 }
3231
3232# Keep the VM in text mode
3233$file="/etc/inittab";
3234if (-f $file) {
3235 open(PBFILE,$file) || die "Unable to open $file";
3236 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
3237 while (<PBFILE>) {
3238 s/^(..):5:initdefault:$/$1:3:initdefault:/;
3239 print PBOUT $_;
3240 }
3241 close(PBFILE);
3242 close(PBOUT);
3243 rename("$file.new",$file);
3244 chmod 0640,$file;
3245}
3246
3247# pb has to be added to portage group on gentoo
3248
3249# We need to have that pb_distro_get_context function
3250# Get it from Project-Builder::Distribution
3251# And we now need the conf file required for this to work created above
3252
3253my $pbos = pb_distro_get_context();
3254print "distro tuple: ".Dumper($pbos)."\n";
3255
3256# Adapt sudoers
3257# sudo is not default on Solaris and needs to be installed first
3258# from http://www.sunfreeware.com/programlistsparc10.html#sudo
3259if ($pbos->{'type'} eq "pkg") {
3260 $file="/usr/local/etc/sudoers";
3261} else {
3262 $file="/etc/sudoers";
3263}
3264open(PBFILE,$file) || die "Unable to open $file";
3265open(PBOUT,"> $file.new") || die "Unable to open $file.new";
3266while (<PBFILE>) {
3267EOF
3268 # Skip what will be generated
3269 print SCRIPT << "EOF";
3270 next if (/^$pbac->{$ENV{'PBPROJ'}}\\s+/);
3271 next if (/^Defaults:$pbac->{$ENV{'PBPROJ'}}\\s+/);
3272 next if (/^Defaults:root\\s+\!requiretty/);
3273 next if (/^Defaults:root\\s+env_keep/);
3274EOF
3275 print SCRIPT << 'EOF';
3276 s/Defaults\s+requiretty/# disable Defaults: requiretty/;
3277 print PBOUT $_;
3278}
3279close(PBFILE);
3280EOF
3281 print SCRIPT << "EOF";
3282# Some distro force requiretty at compile time, so disable here
3283print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} !requiretty\n";
3284print PBOUT "Defaults:root !requiretty\n";
3285# Keep proxy configuration while using sudo
3286print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} env_keep += \\\"http_proxy ftp_proxy\\\"\n";
3287print PBOUT "Defaults:root env_keep += \\\"http_proxy ftp_proxy\\\"\n";
3288EOF
3289 # Try to restrict security to what is really needed
3290 if ($vtype =~ /^vm/) {
3291 my $hpath = pb_distro_get_param($pbos,pb_conf_get("ospathcmd-halt"));
3292 my @sudocmds = pb_get_sudocmds($pbos,$ntpline,"sudo $hpath");
3293 print SCRIPT << "EOF";
3294# This is needed in order to be able on VM to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
3295# Build account $pbac->{$ENV{'PBPROJ'}} in VM also needs to setup date and install deps.
3296# Nothing else should be needed
3297EOF
3298 foreach my $c (@sudocmds) {
3299 print SCRIPT "print PBOUT \"$pbac->{$ENV{'PBPROJ'}} ALL = NOPASSWD: $c\\n\";\n";
3300 }
3301 } elsif ($vtype =~ /^rm/) {
3302 my @sudocmds = pb_get_sudocmds($pbos,$ntpline);
3303 print SCRIPT << "EOF";
3304# Build account $pbac->{$ENV{'PBPROJ'}} in RM only needs to setup date and install deps if needed each time
3305EOF
3306 foreach my $c (@sudocmds) {
3307 print SCRIPT "print PBOUT \"$pbac->{$ENV{'PBPROJ'}} ALL = NOPASSWD: $c\\n\";\n";
3308 }
3309 } else {
3310 print SCRIPT << "EOF";
3311# Build account $pbac->{$ENV{'PBPROJ'}} for VE needs to do a lot in the host (and chroot), so allow without restriction for now
3312print PBOUT "$pbac->{$ENV{'PBPROJ'}} ALL=(ALL) NOPASSWD:ALL\n";
3313EOF
3314}
3315 print SCRIPT << 'EOF';
3316close(PBOUT);
3317rename("$file.new",$file);
3318chmod 0440,$file;
3319
3320EOF
3321
3322 if ($vtype =~ /(v|r)m/) {
3323 # Sync date
3324 # do it after sudoers is setup
3325 print SCRIPT "pb_system(\"$ntpline\",\"Setting up time\",\"mayfail\");\n";
3326 }
3327 # We may need a proxy configuration. Get it from the local env
3328 my $ftp_proxy = pb_distro_get_param($pbos,pb_conf_get_if("ftp_proxy"));
3329 my $http_proxy = pb_distro_get_param($pbos,pb_conf_get_if("http_proxy"));
3330
3331 $ENV{'ftp_proxy'} ||= $ftp_proxy if ((defined $ftp_proxy) && ($ftp_proxy ne ""));
3332 $ENV{'http_proxy'} ||= $http_proxy if ((defined $http_proxy) && ($http_proxy ne ""));
3333
3334 if (defined $ENV{'http_proxy'}) {
3335 print SCRIPT "\$ENV\{'http_proxy'\}=\"$ENV{'http_proxy'}\";\n";
3336 }
3337
3338 if (defined $ENV{'ftp_proxy'}) {
3339 print SCRIPT "\$ENV\{'ftp_proxy'\}=\"$ENV{'ftp_proxy'}\";\n";
3340 }
3341
3342 print SCRIPT << 'EOF';
3343
3344# Suse wants sudoers as 640
3345if ((($pbos->{'name'} eq "sles") && (($pbos->{'version'} =~ /10/) || ($pbos->{'version'} =~ /9/))) || (($pbos->{'name'} eq "opensuse") && ($pbos->{'version'} =~ /10.[012]/))) {
3346 chmod 0640,$file;
3347}
3348
3349# First install all required packages
3350pb_system("yum clean all","Cleaning yum env") if (($pbos->{'name'} eq "fedora") || ($pbos->{'name'} eq "asianux") || ($pbos->{'name'} eq "rhel"));
3351my ($ospkgdep) = pb_conf_get_if("ospkgdep");
3352
3353my $pkgdep = pb_distro_get_param($pbos,$ospkgdep);
3354pb_distro_installdeps(undef,$pbos,pb_distro_only_deps_needed($pbos,join(' ',split(/,/,$pkgdep))));
3355
3356EOF
3357 my $itype = pb_distro_get_param($pbos,pb_conf_get("pbinstalltype"));
3358 # Install from sandbox mean a file base install
3359 $itype = "file" if (defined $sbx);
3360 if ($itype =~ /^file/) {
3361 my $cmdget;
3362 if (defined $sbx) {
3363 # Install from sandbox mean using the result of the just passed sbx2build command
3364 # Get content saved in cms2build
3365 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
3366 my $pbextdir = pb_get_extdir();
3367 die "Unable to get package list" if (not defined $pkg);
3368
3369 # We consider 2 specific packages
3370 my $vertag1 = $pkg->{"ProjectBuilder"};
3371 my $vertag2 = $pkg->{"project-builder"};
3372 # get the version of the current package - maybe different
3373 pb_log(2,"Vertag1: $vertag1\n");
3374 pb_log(2,"Vertag2: $vertag2\n");
3375 my ($pbver1,$tmp1) = split(/-/,$vertag1);
3376 my ($pbver2,$tmp2) = split(/-/,$vertag2);
3377 # Copy inside the VE
3378 if ($vtype eq "ve") {
3379 my ($vepath) = pb_conf_get("vepath");
3380 copy("$ENV{'PBDESTDIR'}/ProjectBuilder-$pbver1$pbextdir.tar.gz","$vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/tmp");
3381 copy("$ENV{'PBDESTDIR'}/project-builder-$pbver2$pbextdir.tar.gz","$vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/tmp");
3382 } else {
3383 pb_system("scp -i $keyfile -p -o UserKnownHostsFile=/dev/null -P $nport $ENV{'PBDESTDIR'}/ProjectBuilder-$pbver1$pbextdir.tar.gz $ENV{'PBDESTDIR'}/project-builder-$pbver2$pbextdir.tar.gz root\@$vmhost->{$ENV{'PBPROJ'}}:/tmp","Copying local project files to $vtype.");
3384 }
3385 $cmdget = "mv /tmp/ProjectBuilder-$pbver1$pbextdir.tar.gz ProjectBuilder-latest.tar.gz ; mv /tmp/project-builder-$pbver2$pbextdir.tar.gz project-builder-latest.tar.gz";
3386 } else {
3387 # TODO: shouldn't we have a variable to specify the source file?
3388 # And shouldn't we be getting the one that matches the current pb version?
3389 # For the moment, just take the latest stable one
3390 $cmdget = "wget --passive-ftp ftp://ftp.project-builder.org/src/ProjectBuilder-latest.tar.gz; wget --passive-ftp ftp://ftp.project-builder.org/src/project-builder-latest.tar.gz";
3391 }
3392 print SCRIPT << 'EOF';
3393# Then install manually the missing perl modules
3394my ($osperldep,$osperlver) = pb_conf_get_if("osperldep","osperlver");
3395
3396my $perldep = pb_distro_get_param($pbos,$osperldep);
3397foreach my $m (split(/,/,$perldep)) {
3398 # Skip empty deps
3399 next if ($m =~ /^\s*$/);
3400 my $dir = $m;
3401 $dir =~ s/-.*//;
3402 pb_system("echo \"rm -rf $m* ; wget http://search.cpan.org/CPAN/modules/by-module/$dir/$m-$osperlver->{$m}.tar.gz ; gzip -cd $m-$osperlver->{$m}.tar.gz | tar xf - ; cd $m* ; if [ -f Build.PL ]; then perl Build.PL; ./Build ; ./Build install ; else perl Makefile.PL; make ; make install ; fi; cd .. ; rm -rf $m*\" | bash -e" ,"Installing perl module $m-$osperlver->{$m}");
3403}
3404EOF
3405 print SCRIPT << 'EOF';
3406pb_system("rm -rf ProjectBuilder-* ; rm -rf project-builder-* ; rm -rf `perl -V:installvendorlib | awk -F\"'\" '{print \$2}'`/ProjectBuilder ;
3407EOF
3408 print SCRIPT " $cmdget ; ";
3409 print SCRIPT << 'EOF'
3410gzip -cd ProjectBuilder-latest.tar.gz | tar xf - ; cd ProjectBuilder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf ProjectBuilder-* ; gzip -cd project-builder-latest.tar.gz | tar xf - ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf project-builder-* ;","Building Project-Builder");
3411EOF
3412 } elsif ($itype =~ /^pkg/) {
3413 # pkg based install. We need to point to the project-builder.org repository
3414 print SCRIPT << 'EOF';
3415my $pkgforpb = pb_distro_get_param($pbos,pb_conf_get_if("ospkg"));
3416pb_distro_setuposrepo($pbos);
3417pb_distro_installdeps(undef,$pbos,pb_distro_only_deps_needed($pbos,join(' ',split(/,/,$pkgforpb))));
3418EOF
3419 } else {
3420 # Unknown install type
3421 die("Unknown install type $itype->{$ENV{'PBPROJ'}} for param pbinstalltype");
3422 }
3423 print SCRIPT << 'EOF';
3424pb_system("pb 2>&1 | head -5",undef,"verbose");
3425pb_system("pbdistrocheck",undef,"verbose");
3426EOF
3427 if ($vtype eq "ve") {
3428 print SCRIPT << 'EOF';
3429# For VE we need to umount some FS at the end
3430
3431pb_system("umount /proc");
3432
3433# Create a basic network file if not already there
3434
3435my $nf="/etc/sysconfig/network";
3436if ((! -f $nf) && ($pbos->{'type'} eq "rpm") && ($pbos->{'family'} ne "novell")) {
3437 open(NF,"> $nf") || die "Unable to create $nf";
3438 print NF "NETWORKING=yes\n";
3439 print NF "HOSTNAME=localhost\n";
3440 close(NF);
3441}
3442chmod 0755,$nf;
3443EOF
3444 }
3445
3446 # Adds pb_distro_get_context and all functions needed from ProjectBuilder::Distribution, Conf and Base
3447 foreach my $m ("ProjectBuilder/Base.pm","ProjectBuilder/Distribution.pm","ProjectBuilder/Conf.pm") {
3448 foreach my $d (@INC) {
3449 my $f = "$d/$m";
3450 if (-f "$f") {
3451 open(PBD,"$f") || die "Unable to open $f: $!";
3452 while (<PBD>) {
3453 next if (/^package/);
3454 next if (/^use Exporter/);
3455 next if (/^use ProjectBuilder::/);
3456 next if (/^our /);
3457 print SCRIPT $_;
3458 }
3459 close(PBD);
3460 # We just need the first one of each file wrt @INC - hopefully that's the right one.
3461 last;
3462 }
3463 }
3464 }
3465 # Use a fake pb_version_init version here
3466 print SCRIPT << "EOF";
3467sub pb_version_init {
3468
3469return("$projectbuilderver","$projectbuilderrev");
3470}
34711;
3472EOF
3473 close(SCRIPT);
3474 chmod 0755,"$pbscript";
3475
3476 # That build script needs to be run as root and force stop of VM at end
3477 $pbaccount = "root";
3478
3479 # Force shutdown of VM except if it was already launched
3480 my $pbforce = 0;
3481 if ((! $vmexist) && ($vtype eq "vm")) {
3482 $pbforce = 1;
3483 }
3484
3485 pb_script2v($pbscript,$vtype,$pbforce,$v);
3486 $pm->finish if (defined $pbparallel);
3487}
3488$pm->wait_all_children if (defined $pbparallel);
3489die "Aborting, one or more of the children failed." if ((not $all_ok) && ($Global::pb_stop_on_error));
3490return;
3491}
3492
3493# Function to create a snapshot named 'pb' for VMs and a compressed tar for VEs
3494sub pb_snap2v {
3495
3496my $vtype = shift;
3497
3498my ($vm,$all) = pb_get2v($vtype);
3499
3500# Script generated
3501my $pbscript = "$ENV{'PBDESTDIR'}/snapv";
3502
3503my ($pbac) = pb_conf_get($vtype."login");
3504
3505foreach my $v (@$vm) {
3506 if ($vtype eq "ve") {
3507 # Get distro context
3508 my $pbos = pb_distro_get_context($v);
3509 my ($vepath) = pb_conf_get("vepath");
3510
3511 # Test if an existing snapshot exists and remove it if there is a VE
3512 if ((-f "$vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz") &&
3513 (! -d "$vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}")) {
3514 pb_system("sudo rm -f $vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz","Removing previous snapshot $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz");
3515 }
3516 }
3517
3518 # Prepare the script to be executed on the VM/VE
3519 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
3520 print SCRIPT << 'EOF';
3521 #!/bin/bash
3522 sleep 2
3523EOF
3524 close(SCRIPT);
3525 chmod 0755,"$pbscript";
3526
3527 # Force shutdown of VM/VE
3528 # Force snapshot of VM/VE
3529 pb_script2v($pbscript,$vtype,1,$v,1);
3530}
3531return;
3532}
3533
3534# Function to update VMs/VEs/RMs with the latest distribution content
3535sub pb_update2v {
3536
3537my $vtype = shift;
3538
3539my ($vm,$all) = pb_get2v($vtype);
3540
3541# Script generated
3542my $pbscript = "$ENV{'PBDESTDIR'}/updatev";
3543
3544my ($pbac) = pb_conf_get($vtype."login");
3545
3546foreach my $v (@$vm) {
3547 # Get distro context
3548 my $pbos = pb_distro_get_context($v);
3549
3550 # Prepare the script to be executed on the VM/VE/RM
3551 # in $ENV{'PBDESTDIR'}/updatev
3552 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
3553
3554 print SCRIPT << 'EOF';
3555 #!/bin/bash
3556 sleep 2
3557EOF
3558 # VE needs a good /proc
3559 if ($vtype eq "ve") {
3560 print SCRIPT "sudo /bin/mount -t proc /proc /proc\n";
3561 }
3562 print SCRIPT "$pbos->{'update'}\n";
3563 if ($vtype eq "ve") {
3564 print SCRIPT "sudo /bin/umount /proc\n";
3565 }
3566 close(SCRIPT);
3567 chmod 0755,"$pbscript";
3568
3569 # Force shutdown of VM except
3570 pb_script2v($pbscript,$vtype,1,$v);
3571}
3572return;
3573}
3574
3575sub pb_announce {
3576
3577 my $antype = shift;
3578
3579 # Get all required parameters
3580 my ($pbpackager,$pbrepo,$pbml,$pbsmtp) = pb_conf_get("pbpackager","pbrepo","pbml","pbsmtp");
3581 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
3582 if (((not defined $testver) || (not defined $testver->{$ENV{'PBPROJ'}}) || ($testver->{$ENV{'PBPROJ'}} !~ /true/i)) && ($antype eq "Clean")) {
3583 # We just clean for test versions
3584 pb_log(0,"Unable to clean SSH repository for non testver version\n");
3585 return;
3586 }
3587 my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
3588 my @pkgs = @$pkg;
3589 my %pkgs;
3590 my $first = 0;
3591
3592 # Get all distros concerned
3593 my $pbos = pb_distro_get_context();
3594 my $distrolist = pb_get_distros($pbos,undef);
3595 my %dl;
3596 my %theorlist;
3597 my %archlist;
3598 foreach my $d (split(/,/,$distrolist)) {
3599 my ($d1,$d2,$d3) = split(/-/,$d);
3600 $dl{$d1}++;
3601 }
3602
3603 # Command to find packages on repo
3604 my $findstr = "find ".join(" ",keys %dl)." ";
3605 my $srcstr = "";
3606 # Generated announce files
3607 my @files;
3608
3609 foreach my $pbpkg (@pkgs) {
3610 if ($first != 0) {
3611 $findstr .= "-o ";
3612 }
3613 $first++;
3614 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
3615 $pbver = $pkgv->{$pbpkg};
3616 } else {
3617 $pbver = $ENV{'PBPROJVER'};
3618 }
3619 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
3620 $pbtag = $pkgt->{$pbpkg};
3621 } else {
3622 $pbtag = $ENV{'PBPROJTAG'};
3623 }
3624
3625 # TODO: use virtual/real names here now
3626 my $pbrealpkg = $pbpkg;
3627 my $pbrealpkgrpm = pb_cms_get_real_pkg($pbpkg,"rpm");
3628 my $pbrealpkgdeb = pb_cms_get_real_pkg($pbpkg,"deb");
3629 if ($antype eq "Clean") {
3630 # We only clean test versions anyway
3631 $pbtag = "0";
3632 my $nver = $pbver;
3633 my $ntag = "[2-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]";
3634 $pbver .= $ntag;
3635 $findstr .= "-name \'$pbrealpkgrpm-$pbver-$pbtag\.*.rpm\' -o -name \'$pbrealpkgrpm-debug-$pbver-$pbtag\.*.rpm\' -o -name \'$pbrealpkgdeb"."_$pbver-$pbtag"."_*\.deb\' -o -name \'$pbrealpkgdeb"."_$pbver-$pbtag.dsc\' -o -name \'$pbrealpkgdeb"."_$pbver-$pbtag.tar.gz\' -o -name \'$pbrealpkg-$nver"."_p$ntag\.ebuild\' -o -name \'$pbrealpkg-$pbver-$pbtag*\.pkg\' -o -name \'$pbrealpkg-$pbver-$pbtag*\.sd\' ";
3636 $srcstr .= "src/$pbrealpkg-$pbver.tar.gz src/$pbrealpkg-$pbver.pbconf.tar.gz ";
3637 } else {
3638 my @date=pb_get_date();
3639 # the matching is only done on packages made the same day for test version. Hopefully this is enough
3640 my $nver = $pbver;
3641 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i) && ($antype eq "Check")) {
3642 $pbtag = "0";
3643 my $ntag .= strftime("%Y%m%d*", @date);
3644 $nver = $pbver."_p$ntag";
3645 $pbver .= $ntag;
3646 }
3647 $findstr .= "-name \'$pbrealpkgrpm-$pbver-$pbtag\.*.rpm\' -o -name \'$pbrealpkgdeb"."_$pbver*\.deb\' -o -name \'$pbrealpkg-$nver*\.ebuild\' -o -name \'$pbrealpkg-$pbver*\.pkg\' -o -name \'$pbrealpkg-$pbver*\.sd\' ";
3648 }
3649
3650 if ($antype eq "Announce") {
3651 my $chglog;
3652
3653 pb_cms_init($pbinit);
3654 # Get project info on log file and generate tmp files used later on
3655 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
3656 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
3657 $chglog = undef if (! -f $chglog);
3658
3659 open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
3660 my $pb;
3661 $pb->{'realpkg'} = $pbrealpkg;
3662 $pb->{'ver'} = $pbver;
3663 $pb->{'tag'} = $pbtag;
3664 $pb->{'date'} = $pbdate;
3665 $pb->{'extdir'} = pb_get_extdir();
3666 $pb->{'chglog'} = $chglog;
3667 $pb->{'packager'} = $pbpackager;
3668 $pb->{'proj'} = $ENV{'PBPROJ'};
3669 $pb->{'repo'} = $pbrepo;
3670 $pb->{'pbos'}->{'type'} = "announce";
3671 $pb->{'pbos'}->{'suffix'} = "none";
3672 pb_changelog($pb,\*OUT,"yes");
3673 close(OUT);
3674 push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
3675 } elsif ($antype eq "Check") {
3676 # For the check we also build the theoritical complete list we should get
3677 foreach my $d (split(/,/,$distrolist)) {
3678 $pbos = pb_distro_get_context($d);
3679 if ($pbos->{'type'} eq "rpm") {
3680 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/$pbrealpkgrpm-$pbver-$pbtag$pbos->{'suffix'}"} = 0;
3681 } elsif ($pbos->{'type'} eq "deb") {
3682 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkgdeb"."_$pbver-$pbtag"} = 0;
3683 # TODO are we always using the last arch ?
3684 $archlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkgdeb"."_$pbver-$pbtag"} = "$pbos->{'arch'}";
3685 } elsif ($pbos->{'type'} eq "ebuild") {
3686 my $prefix = "-r";
3687 $prefix = "_p" if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i));
3688 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkg-$pbver$prefix$pbtag.ebuild"} = 0;
3689 $archlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkg-$pbver$prefix$pbtag.ebuild"} = "$pbos->{'arch'}";
3690 } elsif ($pbos->{'type'} eq "pkg") {
3691 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/$pbrealpkg-$pbver-$pbtag.pkg"} = 0;
3692 } else {
3693 pb_log(1,"No theoritical list possible for type $pbos->{'type'}\n");
3694 }
3695 }
3696 }
3697 pb_log(2,"theorlist : ".Dumper(%theorlist)."\n");
3698 }
3699 if ($antype eq "Announce") {
3700 $findstr .= " | grep -Ev \'src.rpm\'";
3701 } elsif ($antype eq "Clean") {
3702 $findstr .= " | xargs rm -f -v $srcstr ";
3703 }
3704
3705 # Prepare the command to run and execute it
3706 open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
3707 print PBS "#!/bin/bash\n";
3708 print PBS "set -x\n" if ($pbdebug gt 1);
3709 print PBS "$findstr | sort 2> /dev/null\n";
3710 close(PBS);
3711 chmod 0755,"$ENV{'PBTMP'}/pbscript";
3712 pb_send2target("Announce");
3713
3714 my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
3715 if ($antype eq "Announce") {
3716 # Get subject line
3717 pb_log(0,"Please enter the title of your announce\n");
3718 pb_log(0,"(By default: $sl)\n");
3719 my $sl2 = <STDIN>;
3720 $sl = $sl2 if ($sl2 !~ /^$/);
3721
3722 # Prepare a template of announce
3723 open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
3724 print ANN << "EOF";
3725$sl</p>
3726
3727<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
3728<p>
3729Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
3730</p>
3731<p>
3732EOF
3733 }
3734
3735 open(LOG,"$ENV{'PBTMP'}/system.$$.log") || die "Unable to read $ENV{'PBTMP'}/system.$$.log: $!";
3736 if ($antype eq "Announce") {
3737 my $col = 2;
3738 my $i = 1;
3739 print ANN << 'EOF';
3740<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0">
3741<TR>
3742EOF
3743 while (<LOG>) {
3744 print ANN "<TD><A HREF=\"$pbrepo->{$ENV{'PBPROJ'}}/$_\">$_</A></TD>";
3745 $i++;
3746 if ($i > $col) {
3747 print ANN "</TR>\n<TR>";
3748 $i = 1;
3749 }
3750 }
3751 } elsif ($antype eq "Clean") {
3752 while (<LOG>) {
3753 # skip errors
3754 next if ($_ !~ /^removed /);
3755 pb_log(0,"$_");
3756 }
3757 } else {
3758 # In Check mode we need to compare the 2 lists (real and theoritical)
3759 while (<LOG>) {
3760 # Get package name and remove what is in extra for the theoritical list (arch at the end)
3761 chomp();
3762 # skip find errors
3763 next if (/^find:/);
3764 my $p = $_;
3765 $p =~ s/\.(i[3456]86|x86_64|noarch|src)\.rpm$//;
3766 $p =~ s/_(i[3456]86|amd64|all).deb$//;
3767 $p =~ s/(-0\.[0-9]{8})[0-9]{6}/$1*/ if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i));
3768 $p =~ s/(-r|_p[0-9]+)\.ebuild/$1*/ if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i));
3769 $theorlist{$p} = -2 if (not defined $theorlist{$p});
3770 $theorlist{$p} = $theorlist{$p} + 1;
3771 }
3772 pb_log(2,"theorlist : ".Dumper(%theorlist)."\n");
3773 }
3774 close(LOG);
3775
3776 # Nothing more for the cleanssh case
3777 return if ($antype eq "Clean");
3778
3779 if ($antype eq "Check") {
3780 my ($chkex) = pb_conf_get_if("checkexclude");
3781 my $vmbuildlist = "";
3782 my $vebuildlist = "";
3783 my $rmbuildlist = "";
3784 my @pt = pb_conf_get_if("vmlist","velist","rmlist");
3785 foreach my $t (sort keys %theorlist) {
3786 if (defined $theorlist{$t} and $theorlist{$t} >= 1) {
3787 pb_log(1,"Packages found for $t\n");
3788 } elsif (defined $theorlist{$t} and $theorlist{$t} < 0) {
3789 pb_log(0,"Extra Package found for $t\n");
3790 } else {
3791 pb_log(2,"Analyzing $t\n");
3792 my ($os,$ver,$arch,$package) = split(/\//,$t);
3793 # Some distro have no arch subdir
3794 if (not defined $package) {
3795 $package = $arch;
3796 # TODO: If both arch have failed, we just make the last one
3797 $arch = $archlist{$t};
3798 }
3799 my $pbos = pb_distro_get_context("$os-$ver-$arch");
3800 my $pkgn = $package;
3801 if ($pbos->{'type'} ne "deb") {
3802 # package name is more easily found from the end for non deb
3803 # as '-' is the separator, but it can also be used in names
3804 $pkgn = reverse($package);
3805 # search the second '-' and isolate the now last part which is the full name
3806 $pkgn =~ s/([^-]+)-([^-]+)-([\S])+$/$3/;
3807 } else {
3808 $pkgn =~ s/([^_]+)_([\S])+$/$2/;
3809 }
3810 my $found = 0;
3811 # Handle the exclusion of OSes
3812 my $excl = "";
3813 $excl .= $chkex->{$pkgn} if (defined $chkex->{$pkgn});
3814 $excl .= $chkex->{"all"} if (defined $chkex->{"all"});
3815 foreach my $ex (split(/,/,$excl)) {
3816 $found = 1 if ("$os-$ver-$arch" =~ /^$ex/);
3817 }
3818 # Skip as excluded
3819 next if ($found == 1);
3820 pb_log(0,"Package NOT found for $t\n");
3821 # Avoid duplicates in list
3822 next if ($vmbuildlist =~ /$os-$ver-$arch/);
3823 next if ($vebuildlist =~ /$os-$ver-$arch/);
3824 next if ($rmbuildlist =~ /$os-$ver-$arch/);
3825 # check with which method we need to build
3826 if ((defined $pt[0]->{$ENV{'PBPROJ'}}) and ($pt[0]->{$ENV{'PBPROJ'}} =~ /$os-$ver-$arch/)) {
3827 $vmbuildlist = "$os-$ver-$arch" if ($vmbuildlist eq "");
3828 $vmbuildlist .= ",$os-$ver-$arch" if ($vmbuildlist !~ /$os-$ver-$arch/);
3829 next;
3830 }
3831 if ((defined $pt[1]->{$ENV{'PBPROJ'}}) and ($pt[1]->{$ENV{'PBPROJ'}} =~ /$os-$ver-$arch/)) {
3832 $vebuildlist = "$os-$ver-$arch" if ($vebuildlist eq "");
3833 $vebuildlist .= ",$os-$ver-$arch" if ($vebuildlist !~ /$os-$ver-$arch/);
3834 next;
3835 }
3836 if ((defined $pt[2]->{$ENV{'PBPROJ'}}) and ($pt[2]->{$ENV{'PBPROJ'}} =~ /$os-$ver-$arch/)) {
3837 $rmbuildlist = "$os-$ver-$arch" if ($rmbuildlist eq "");
3838 $rmbuildlist .= ",$os-$ver-$arch" if ($rmbuildlist !~ /$os-$ver-$arch/);
3839 }
3840 }
3841 }
3842 # If we want to rebuild automatically, let's do it
3843 if (defined $opts{'rebuild'}) {
3844 # SandBox or CMS
3845 pb_log(0,"Rebuilding from SandBox\n");
3846 pb_log(0,"for VMs: $vmbuildlist\n") if ($vmbuildlist ne "");
3847 pb_log(0,"for VEs: $vebuildlist\n") if ($vebuildlist ne "");
3848 pb_log(0,"for RMs: $rmbuildlist\n") if ($rmbuildlist ne "");
3849 pb_cms2build("SandBox");
3850 # Which mode
3851 $ENV{'PBV'} = $vmbuildlist;
3852 pb_build2v("vm","build") if ($vmbuildlist ne "");
3853 $ENV{'PBV'} = $vebuildlist;
3854 pb_build2v("ve","build") if ($vebuildlist ne "");
3855 $ENV{'PBV'} = $rmbuildlist;
3856 pb_build2v("rm","build") if ($rmbuildlist ne "");
3857 }
3858 # For the check part this is now finished
3859 return;
3860 }
3861
3862 print ANN << "EOF";
3863</TR>
3864</TABLE>
3865</p>
3866
3867<p>As usual source packages are also available in the same directory.</p>
3868
3869<p>
3870Changes are :
3871</p>
3872<p>
3873EOF
3874 # Get each package changelog content
3875 foreach my $f (sort(@files)) {
3876 open(IN,"$f") || die "Unable to read $f:$!";
3877 while (<IN>) {
3878 print ANN $_;
3879 }
3880 close(IN);
3881 print ANN "</p><p>\n";
3882 }
3883 print ANN "</p>\n";
3884 close(ANN);
3885
3886 # Allow for modification
3887 my $editor = "vi";
3888 $editor = $ENV{'EDITOR'} if (defined $ENV{'EDITOR'});
3889 pb_system("$editor $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
3890
3891 # Store it in DB for external usage (Web pages generation)
3892 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
3893
3894 my $precmd = "";
3895 if (! -f $db) {
3896 $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
3897 }
3898
3899 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
3900 { RaiseError => 1, AutoCommit => 1 })
3901 || die "Unable to connect to $db";
3902
3903 if ($precmd ne "") {
3904 my $sth = $dbh->prepare(qq{$precmd})
3905 || die "Unable to create table into $db";
3906 $sth->execute();
3907 }
3908
3909 # To read whole file
3910 local $/;
3911 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
3912 my $announce = <ANN>;
3913 close(ANN);
3914
3915 pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
3916 my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
3917 || die "Unable to insert into $db";
3918 $sth->execute($pbdate, $announce);
3919 $sth->finish();
3920 $dbh->disconnect;
3921
3922 # Then deliver it on the Web
3923 # $TOOLHOME/livwww www
3924
3925 # Mail it to project's ML
3926 open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
3927 print ML << 'EOF';
3928<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
3929
3930<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
3931 <head>
3932 </head>
3933 <body>
3934 <p>
3935EOF
3936 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
3937 while(<ANN>) {
3938 print ML $_;
3939 }
3940 print ML << 'EOF';
3941</body>
3942</html>
3943EOF
3944 close(ML);
3945
3946 # To read whole file
3947 local $/;
3948 open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
3949 my $msg = <ANN>;
3950 close(ANN);
3951
3952 # Preparation of headers
3953 eval
3954 {
3955 require Mail::Sendmail;
3956 Mail::Sendmail->import();
3957 };
3958 if ($@) {
3959 # Mail::Sendmail not found not sending mail !
3960 pb_log(0,"No Mail::Sendmail module found so not sending any mail !\n");
3961 } else {
3962 my %mail = (
3963 To => $pbml->{$ENV{'PBPROJ'}},
3964 From => $pbpackager->{$ENV{'PBPROJ'}},
3965 Smtp => $pbsmtp->{$ENV{'PBPROJ'}},
3966 Body => $msg,
3967 Subject => "[ANNOUNCE] $sl",
3968 );
3969
3970 # Send mail
3971 if (! sendmail(%mail)) {
3972 if ((defined $Mail::Sendmail::error) and (defined $Mail::Sendmail::log)) {
3973 die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
3974 }
3975 }
3976 }
3977}
3978
3979#
3980# Creates a set of HTML file containing the news for the project
3981# based on what has been generated by the pb_announce function
3982#
3983sub pb_web_news2html {
3984
3985 my $dest = shift || $ENV{'PBTMP'};
3986
3987 # Get all required parameters
3988 my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
3989
3990 # DB of announces for external usage (Web pages generation)
3991 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
3992
3993 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
3994 { RaiseError => 1, AutoCommit => 1 })
3995 || die "Unable to connect to $db";
3996 # For date handling
3997 $ENV{LANGUAGE}="C";
3998 my $firstjan = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year(), 0, 0, -1);
3999 my $oldfirst = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year()-1, 0, 0, -1);
4000 pb_log(2,"firstjan: $firstjan, oldfirst: $oldfirst, pbdate:$pbdate\n");
4001 my $all = $dbh->selectall_arrayref("SELECT id,date,announce FROM announces ORDER BY date DESC");
4002 my %news;
4003 $news{"cy"} = ""; # current year's news
4004 $news{"ly"} = ""; # last year news
4005 $news{"py"} = ""; # previous years news
4006 $news{"fp"} = ""; # first page news
4007 my $cpt = 4; # how many news for first page
4008 # Extract info from DB
4009 foreach my $row (@$all) {
4010 my ($id, $date, $announce) = @$row;
4011 $news{"cy"} = $news{"cy"}."<p><B>$date</B> $announce\n" if ((($date cmp $pbdate) le 0) && (($firstjan cmp $date) le 0));
4012 $news{"ly"} = $news{"ly"}."<p><B>$date</B> $announce\n" if ((($date cmp $firstjan) le 0) && (($oldfirst cmp $date) le 0));
4013 $news{"py"} = $news{"py"}."<p><B>$date</B> $announce\n" if (($date cmp $oldfirst) le 0);
4014 $news{"fp"} = $news{"fp"}."<p><B>$date</B> $announce\n" if ($cpt > 0);
4015 $cpt--;
4016 }
4017 pb_log(1,"news{fp}: ".$news{"fp"}."\n");
4018 $dbh->disconnect;
4019
4020 # Generate the HTML content
4021 foreach my $pref (keys %news) {
4022 open(NEWS,"> $dest/pb_web_$pref"."news.html") || die "Unable to create $dest/pb_web_$pref"."news.html: $!";
4023 print NEWS "$news{$pref}";
4024 close(NEWS);
4025 }
4026}
4027
4028
4029# Return the SSH key file to use
4030# Potentially create it if needed
4031
4032sub pb_ssh_get {
4033
4034my $create = shift || 0; # Do not create keys by default
4035
4036my ($pbagent) = pb_conf_get_if("pbusesshagent");
4037# use ssh-agent if asked so.
4038return(undef) if (($create == 0) && (defined $pbagent->{$ENV{'PBPROJ'}}) && ($pbagent->{$ENV{'PBPROJ'}} =~ /true/io));
4039
4040# Check the SSH environment
4041my $keyfile = undef;
4042
4043# We have specific keys by default
4044$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa";
4045if (!(-e $keyfile) && ($create eq 1)) {
4046 pb_system("ssh-keygen -q -b 1024 -N '' -f $keyfile -t dsa","Generating SSH keys for pb");
4047}
4048
4049$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
4050$keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if (-s "$ENV{'HOME'}/.ssh/id_dsa");
4051$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
4052die "Unable to find your public ssh key under $ENV{'HOME'}/.ssh" if (not defined $keyfile);
4053return($keyfile);
4054}
4055
4056
4057# Returns the pid of a running VM command using a specific VM file
4058sub pb_check_ps {
4059 my $vmcmd = shift;
4060 my $vmm = shift;
4061 my $vmexist = 0; # FALSE by default
4062
4063 open(PS, "ps auxhww|") || die "Unable to call ps";
4064 while (<PS>) {
4065 next if (! /$vmcmd/);
4066 next if (! /$vmm/);
4067 my ($void1, $void2);
4068 ($void1, $vmexist, $void2) = split(/ +/);
4069 last;
4070 }
4071 return($vmexist);
4072}
4073
4074
4075sub pb_extract_build_files {
4076
4077my $src=shift;
4078my $dir=shift;
4079my $ddir=shift;
4080my $mandatory=shift || "spec";
4081my @files;
4082
4083my $flag = "mayfail" if (($mandatory eq "patch") || ($mandatory eq "src"));
4084my $res;
4085
4086if ($src =~ /tar\.gz$/) {
4087 $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
4088} elsif ($src =~ /tar\.bz2$/) {
4089 $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
4090} else {
4091 die "Unknown compression algorithm for $src";
4092}
4093# If not mandatory return now
4094return() if (($res != 0) and (($mandatory eq "patch") || ($mandatory eq "src")));
4095opendir(DIR,"$dir") || die "Unable to open directory $dir: $!";
4096foreach my $f (readdir(DIR)) {
4097 next if ($f =~ /^\./);
4098 # Skip potential patch dir
4099 next if ($f =~ /^pbpatch/);
4100 # Skip potential source dir
4101 next if ($f =~ /^pbsrc/);
4102 # Skip potential backup files
4103 next if ($f =~ /~$/);
4104 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
4105 pb_log(2,"mv $dir/$f $ddir\n");
4106 push @files,"$ddir/$f";
4107}
4108closedir(DIR);
4109# Not enough but still a first cleanup
4110pb_rm_rf("$dir");
4111return(@files);
4112}
4113
4114sub pb_list_bfiles {
4115
4116my $dir = shift;
4117my $pbpkg = shift;
4118my $bfiles = shift;
4119my $pkgfiles = shift;
4120my $supfiles = shift;
4121# subdir to keep if recursive mode, empty by default
4122my $subdir = shift || "";
4123# In a recursive function , we need a local var as DIR handle
4124my $bdir;
4125
4126pb_log(2,"DEBUG: entering pb_list_bfiles in $dir: ".Dumper($bfiles)."\n");
4127opendir($bdir,"$dir") || die "Unable to open dir $dir: $!";
4128foreach my $f (readdir($bdir)) {
4129 pb_log(3,"DEBUG: pb_list_bfiles found $f\n");
4130 next if ($f =~ /^\./);
4131 if (-d "$dir/$f") {
4132 # Recurse for directories (Debian 3.0 format e.g.)
4133 pb_log(2,"DEBUG: pb_list_bfiles recurse in $dir/$f\n");
4134 pb_list_bfiles("$dir/$f",$pbpkg,$bfiles,$pkgfiles,$supfiles,$f);
4135 next;
4136 }
4137
4138 my $key = $f;
4139 # if recursive then store also the subdir
4140 $key = "$subdir/$f" if ($subdir ne "");
4141 $bfiles->{$key} = "$dir/$f";
4142 $bfiles->{$key} =~ s~$ENV{'PBROOTDIR'}~~;
4143 if (defined $supfiles->{$pbpkg}) {
4144 $pkgfiles->{$key} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
4145 }
4146}
4147closedir($bdir);
4148pb_log(2,"DEBUG: exiting pb_list_bfiles: ".Dumper($bfiles)."\n");
4149}
4150
4151sub pb_add_comma {
4152
4153my $str = shift;
4154my $addstr = shift;
4155
4156$str .= "," if (defined $str);
4157$str .= $addstr;
4158return($str);
4159}
4160
4161sub pb_list_sfiles {
4162
4163my $sdir = shift;
4164my $ptr = shift;
4165my $pbos = shift;
4166my $extdir = shift;
4167
4168pb_log(2,"DEBUG: entering pb_list_sfiles: ".Dumper($ptr)."\n");
4169my $key = "$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}";
4170
4171# Prepare local sources for this distro - They are always applied first - May be a problem one day
4172# This function works for both patches and additional sources
4173foreach my $p (sort(<$sdir/*>)) {
4174 $ptr->{$key} = pb_add_comma($ptr->{$key},"file://$p") if (($p =~ /\.all$/) || ($p =~ /\.$pbos->{'os'}$/) || ($p =~ /\.$pbos->{'type'}$/) || ($p =~ /\.$pbos->{'family'}$/) || ($p =~ /\.$pbos->{'name'}$/) || ($p =~ /\.$pbos->{'name'}-$pbos->{'version'}$/) ||($p =~ /\.$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}$/));
4175}
4176
4177# Prepare also remote sources to be included - Applied after the local ones
4178foreach my $p ("all","$pbos->{'os'}","$pbos->{'type'}","$pbos->{'family'}","$pbos->{'name'}","$pbos->{'name'}-$pbos->{'version'}","$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}") {
4179 my $f = "$extdir.".".$p";
4180 next if (not -f $f);
4181 if (not open(PATCH,$f)) {
4182 pb_display("Unable to open existing external source file content $f\n");
4183 next;
4184 }
4185 while (<PATCH>) {
4186 chomp();
4187 $ptr->{$key} = pb_add_comma($ptr->{$key},"$_");
4188 }
4189 close(PATCH);
4190}
4191pb_log(2,"DEBUG: exiting pb_list_sfiles: ".Dumper($ptr)."\n");
4192return($ptr);
4193}
4194
4195#
4196# Return the list of packages we are working on in a non CMS action
4197#
4198sub pb_get_pkg {
4199
4200my @pkgs = ();
4201
4202my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
4203@pkgs = keys %$var;
4204
4205pb_log(0,"Packages: ".join(',',@pkgs)."\n");
4206return(\@pkgs);
4207}
4208
4209# Manages VM/RM SSH port communication
4210sub pb_get_port {
4211
4212my $port = shift;
4213my $pbos = shift;
4214my $cmt = shift;
4215my $nport;
4216
4217die "No port passed in parameter. Report to dev team\n" if (not defined $port);
4218# key is project on VM, but machine tuple for RM
4219if ($cmt =~ /^RM/i) {
4220 $nport = $port->{"$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}"};
4221} else {
4222 $nport = $port->{$ENV{'PBPROJ'}};
4223}
4224pb_log(2,"pb_get_port with $nport\n");
4225# Maybe a port was given as parameter so overwrite
4226$nport = "$pbport" if (defined $pbport);
4227# Maybe in // mode so use the env var set up as an offset to the base port, except when called from send2target for Packages or for RM
4228if (($cmt ne "Packages") && ($cmt !~ /^RM/i)) {
4229 $nport += $ENV{'PBVMPORT'} if ((defined $pbparallel) && (defined $ENV{'PBVMPORT'}));
4230}
4231pb_log(2,"pb_get_port returns $nport\n");
4232return($nport);
4233}
4234
4235sub pb_set_port {
4236
4237my ($pid,$ident) = @_;
4238pb_log(2,"pb_set_port for VM ($pid), id $ident\n");
4239$ENV{'PBVMPORT'} = $ident;
4240pb_log(2,"pb_set_port sets PBVMPORT in env to $ENV{'PBVMPORT'}\n");
4241}
4242
4243sub pb_set_parallel {
4244
4245my $vtype = shift;
4246
4247pb_log(2,"pb_set_parallel vtype: $vtype\n");
4248# Take care of memory size if VM, parallel mode and more than 1 action
4249if ((defined $pbparallel) && ($pbparallel ne 1) && ($vtype eq "vm")) {
4250 eval
4251 {
4252 require Linux::SysInfo;
4253 Linux::SysInfo->import();
4254 };
4255 if ($@) {
4256 # Linux::SysInfo not found
4257 pb_log(1,"ADVISE: Install Linux::SysInfo to benefit from automatic parallelism optimization.\nOr optimize manually pbparallel in your pb.conf file\nUsing $pbparallel processes max at a time for the moment\nWARNING: This may consume too much memory for your system\n");
4258 } else {
4259 # Using the memory size
4260 my $si = Linux::SysInfo::sysinfo();
4261 if (not defined $si) {
4262 pb_log(1,"ADVISE: Install Linux::SysInfo to benefit from automatic parallelism optimization.\nOr optimize manually pbparallel in your pb.conf file\nUsing $pbparallel processes max at a time for the moment\nWARNING: This may consume too much memory for your system\n");
4263 } else {
4264 # Keep the number of VM whose memory can be allocated
4265 my $ram = $si->{"totalram"}-$si->{"sharedram"}-$si->{"bufferram"};
4266 my $ram2;
4267 my ($vmmem) = pb_conf_get_if("vmmem");
4268
4269 my $v = "default";
4270 if ((defined $vmmem) and (defined $vmmem->{$v})) {
4271 $ram2 = $vmmem->{$v};
4272 } else {
4273 # Default for KVM/QEMU
4274 $ram2 = 128;
4275 }
4276 $pbparallel = sprintf("%d",$ram/$ram2);
4277 }
4278 pb_log(1,"Using $pbparallel processes at a time\n");
4279 }
4280}
4281pb_log(2,"pb_set_parallel returns: $pbparallel\n") if (defined $pbparallel);
4282return($pbparallel);
4283}
4284
4285sub pb_get_sudocmds {
4286
4287my $pbos = shift;
4288my %sudocmds;
4289
4290pb_log(2,"pb_get_sudocmds entering with lines:".Dumper(@_)."\n");
4291foreach my $c (split(/;/,$pbos->{'update'}),split(/;/,$pbos->{'install'}),@_) {
4292 pb_log(2,"pb_get_sudocmds analyses $c\n");
4293 next if ($c !~ /^\s*sudo/);
4294 # remove sudo and leading spaces
4295 $c =~ s/^\s*sudo\s+//;
4296 # keep only the command, not the params
4297 $c =~ s/([^\s]+)\s.*$/$1/;
4298 $sudocmds{$c} = "";
4299}
4300pb_log(2,"pb_get_sudocmds returns ".Dumper(keys %sudocmds)."\n");
4301return(keys %sudocmds);
4302}
4303
4304sub pb_sign_pkgs {
4305
4306my $pbos = shift;
4307my $made = shift;
4308
4309pb_log(2,"entering pb_sign_pkg: $made ".Dumper($pbos)."\n");
4310my ($passfile, $passphrase, $passpath) = pb_conf_get_if("pbpassfile","pbpassphrase","pbpasspath");
4311$ENV{'PBPASSPHRASE'} = $passphrase->{$ENV{'PBPROJ'}} if ((not defined $ENV{'PBPASSPHRASE'}) && (defined $passphrase->{$ENV{'PBPROJ'}}));
4312$ENV{'PBPASSFILE'} = $passfile->{$ENV{'PBPROJ'}} if ((not defined $ENV{'PBPASSFILE'})&& (defined $passfile->{$ENV{'PBPROJ'}})) ;
4313$ENV{'PBPASSPATH'} = $passpath->{$ENV{'PBPROJ'}} if ((not defined $ENV{'PBPASSPATH'})&& (defined $passpath->{$ENV{'PBPROJ'}})) ;
4314
4315# Remove extra spaces
4316$made =~ s/\s+/ /g;
4317$made =~ s/^\s//g;
4318$made =~ s/\s$//g;
4319
4320if ($pbos->{'type'} eq "rpm") {
4321 eval
4322 {
4323 require RPM4::Sign;
4324 RPM4::Sign->import();
4325 };
4326 if ($@) {
4327 # RPM4::Sign not found
4328 pb_log(1,"WARNING: Install RPM4::Sign to benefit from automatic package signing.\n");
4329 } else {
4330 return if ((not defined $ENV{'PBPASSPHRASE'}) and (not defined $ENV{'PBPASSFILE'}));
4331 my $sign = RPM4::Sign->new(
4332 passphrase => $ENV{'PBPASSPHRASE'},
4333 name => $ENV{'PBPACKAGER'},
4334 path => $ENV{'PBPASSPATH'},
4335 password_file => $ENV{'PBPASSFILE'},
4336 );
4337
4338 pb_log(0,"Signing RPM packages...\n");
4339 pb_log(2,"pb_sign_pkg: pkgs:".Dumper(split(/ /,$made))."\n");
4340 $sign->rpmssign(split(/ /,$made));
4341 }
4342} elsif ($pbos->{'type'} eq "deb") {
4343 my $changes = "";
4344 foreach my $c (split(/ /,$made)) {
4345 $changes .= " $ENV{'PBBUILDDIR'}/$c" if (($c =~ /\.changes$/) && (-f "$ENV{PBBUILDDIR}/$c"));
4346 }
4347 my $debsigncmd = pb_check_req("debsign",1);
4348 pb_system("$debsigncmd -m\'$ENV{'PBPACKAGER'}\' $changes","Signing DEB packages","mayfail") if (($changes ne "") && (defined $debsigncmd));
4349} else {
4350 pb_log(0,"I don't know yet how to sign packages for type $pbos->{'type'}.\nPlease give feedback to dev team\n");
4351}
4352pb_log(2,"exiting pb_sign_pkg\n");
4353}
4354
4355# return list of all distributins supported, comma separated
4356sub pb_get_distros {
4357
4358my $pbos = shift;
4359my $pbtarget = shift;
4360
4361my @dists = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}");
4362
4363# Get list of distributions for which we need to generate build files if no target
4364if (not defined $pbtarget) {
4365 my @pt = pb_conf_get_if("vmlist","velist","rmlist");
4366 foreach my $pt (@pt) {
4367 push(@dists, split(/,/, $pt->{$ENV{PBPROJ}})) if defined $pt->{$ENV{PBPROJ}};
4368 }
4369 # remove any whitespace
4370 grep(s/\s+//go, @dists);
4371}
4372return(join(",",@dists));
4373}
4374
4375sub pb_get_extdir () {
4376
4377 # the pbrc file should contain it and whatever the key, we take it
4378 my ($ed) = pb_conf_read("$ENV{'PBDESTDIR'}/pbrc","pbextdir");
4379 pb_log(2,"ed: ".Dumper($ed)."\n");
4380 my $pbextdir = "";
4381 foreach my $k (keys %$ed) {
4382 $pbextdir = $ed->{$k};
4383 # In case we have an empty field, empty it completely
4384 pb_log(2,"pbextdir: ***$pbextdir***\n");
4385 $pbextdir =~ s/^\s*$//;
4386 }
4387 pb_log(2,"pbextdir: ***$pbextdir***\n");
4388 return($pbextdir);
4389}
4390
43911;
Note: See TracBrowser for help on using the repository browser.