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

Last change on this file since 2319 was 2319, checked in by Bruno Cornec, 7 years ago

Avoid parallelism issues with confess in subfunction

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