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

Last change on this file since 2488 was 2488, checked in by Bruno Cornec, 4 years ago

Improve debug messages

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