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

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