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

Last change on this file since 1792 was 1792, checked in by Bruno Cornec, 11 years ago

pb_check_ps now returns a list of 2 params systematically so API changed accordingly everywhere

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