source: ProjectBuilder/devel/pb/bin/pb

Last change on this file was 2664, checked in by Bruno Cornec, 3 years ago

Disable CPAN delivery for now as it's not woring and blocking with default conf

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