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

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

Fix *2docker, tested with Fedora 30

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