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

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

PBVMTMOUT is now takeing precedence over params in conf files

  • Property svn:executable set to *
File size: 179.7 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_cms_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 pb_system("$shcmdbase \"chmod 644 $debdir/$f.gpg\"","Set mode to 644 for $debdir");
2891 }
2892 }
2893
2894 if ($cmt =~ /^(V[EM]|RM)build/) {
2895 # Get back info on pkg produced, compute their name and get them from the VM/RM
2896 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");
2897 if (not -f "$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$") {
2898 pb_log(0,"ERROR with VM/RM $v on getting $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$\n");
2899 } else {
2900 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$";
2901 my $src = <KEEP>;
2902 chomp($src);
2903 close(KEEP);
2904 unlink("$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$");
2905
2906 $src =~ s/^ *//;
2907 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}");
2908 # Change pgben to make the next send2target happy
2909 my $made = "";
2910
2911 # For VM/RM we don't want shell expansion to hapen locally but remotely
2912 my $delim = '\'';
2913 if ($cmt =~ /^VEbuild/) {
2914 # For VE we need to support shell expansion locally
2915 $delim = "";
2916 }
2917
2918 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'}";
2919 foreach my $p (split(/ +/,$src)) {
2920 my $j = basename($p);
2921 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'}");
2922 $made="$made $pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/$j"; # if (($pbos->{'type'} ne "rpm") || ($j !~ /.src.rpm$/));
2923 }
2924
2925 print KEEP "$made\n";
2926 close(KEEP);
2927 if (($cmt =~ /^VE/) && ($vetype eq "docker") && ($pbstep >= 3)) {
2928 pb_rm_rf("$cp2target");
2929 pb_system("$cmd1 rm $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}-$cmt","","quiet");
2930 } else {
2931 pb_system("$shcmdbase \"rm -rf $tdir $bdir\"","$cmt cleanup");
2932 }
2933
2934 # Sign packages locally
2935 pb_sign_pkgs($pbos,$made);
2936
2937 # We want to send them to the ssh account so overwrite what has been done before
2938 pb_log(2,"Before sending pkgs, vexist: $vexist, vpid: $vpid\n");
2939 pb_send2target("Packages","$ENV{'PBDESTDIR'}/pbscript.$$",$pbos->{'name'}."-".$pbos->{'version'}."-".$pbos->{'arch'},$vexist,$vpid);
2940 pb_rm_rf("$ENV{'PBBUILDDIR'}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}");
2941 }
2942 }
2943 unlink("$pbscript{$v}") if ((($cmt =~ /^(V[ME]|RM)/) || ($cmt =~ /Packages/)) && ($pbkeep eq 0) && ($cmt !~ /Script/));
2944
2945 pb_log(2,"Before halt, vexist: $vexist, vpid: $vpid\n");
2946 if ((! $vexist) && ($cmt =~ /^VM/)) {
2947 # If in setupvm then takes a snapshot just before halting
2948 if ($snapme != 0) {
2949 my ($vmmonport,$vmtype) = pb_conf_get("vmmonport","vmtype");
2950 # For monitoring control
2951 if ((($vmtype->{$ENV{'PBPROJ'}}) eq "kvm") || (($vmtype->{$ENV{'PBPROJ'}}) eq "qemu")) {
2952 eval
2953 {
2954 require Net::Telnet;
2955 Net::Telnet->import();
2956 };
2957 if ($@) {
2958 # Net::Telnet not found
2959 pb_log(1,"ADVISE: Install Net::Telnet to benefit from monitoring control and snapshot feature.\nWARNING: No snapshot created");
2960 } else {
2961 my $t = new Net::Telnet (Timeout => 120, Host => "localhost", Port => $vmmonport->{$ENV{'PBPROJ'}}) || die "Unable to dialog on the monitor";
2962 # move to monitor mode
2963 my @lines = $t->cmd("c");
2964 # Create a snapshot named pb
2965 @lines = $t->cmd("savevm pb");
2966 # Write the new status in the VM
2967 @lines = $t->cmd("commit all");
2968 # End
2969 @lines = $t->cmd("quit");
2970 }
2971 }
2972 }
2973 my $hoption = "-p";
2974 my $hpath = pb_distro_get_param($pbos,pb_conf_get("ospathcmd-halt"));
2975 # Solaris doesn't support -p of halt
2976 if ($pbos->{'type'} eq "pkg") {
2977 $hoption = "" ;
2978 }
2979 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)");
2980 }
2981 if (($cmt =~ /^VE/) && ($snapme != 0)) {
2982 if ($vetype eq "docker") {
2983 foreach my $f (split(/ +/,$src)) {
2984 unlink("$context/$f");
2985 }
2986 } else {
2987 pb_ve_snap($pbos,$tp);
2988 }
2989 }
2990}
2991
2992sub pb_script2v {
2993
2994my $pbscript=shift;
2995my $vtype=shift;
2996my $pbstep=shift; # Which step are we in (0: create, 1: setup pb, 2: prep pbproj, 3: use to build)
2997my $pbforce=shift; # Force stop of VM. Default not.
2998my $snapme=shift; # Do we have to create a snapshot. Default not.
2999my $pbimage=shift; # Which image to use to start the VM/VE
3000
3001$pbforce = 0 if (not defined $pbforce);
3002$snapme = 0 if (not defined $snapme);
3003$pbstep = 3 if (not defined $pbstep);
3004
3005pb_log(2,"DEBUG: pb_script2v($vtype,$pbstep,$pbforce,$snapme)\n");
3006pb_log(2,"DEBUG: pb_script2v(pbscript: ".Dumper($pbscript)."\n") if (defined $pbscript);
3007pb_parallel_launchv($pbscript,$vtype,uc($vtype)."Script",$pbstep,$pbimage,$pbforce,$snapme,$pbsnap);
3008}
3009
3010sub pb_parallel_launchv {
3011
3012my $pbscript=shift;
3013my $vtype = shift;
3014my $action = shift; # It an action is defined then use send2target
3015my $pbstep=shift; # Which step are we in (0: create, 1: setup, 2: build 3: use)
3016my $pbimage=shift; # Which image to use to start the VM/VE
3017my $pbforce=shift; # Force stop of VM. Default not.
3018my $snapme = shift; # By default do not snap a VM/VE/RM
3019my $usesnap = shift; # By default study the usage of the snapshot feature of VM/VE/RM
3020my $vm;
3021my $all;
3022
3023$pbforce = 0 if (not defined $pbforce);
3024$snapme = 0 if (not defined $snapme);
3025$usesnap = 1 if (not defined $usesnap);
3026
3027# Adapt // mode to memory size
3028$pbparallel = pb_set_parallel($vtype);
3029
3030pb_log(2,"DEBUG: pb_parallel_launch(vtype,step,force,snapme,usesnap: $vtype,$pbstep,$pbforce,$snapme,$usesnap)\n");
3031pb_log(2,"DEBUG: pb_parallel_launch(pbscript: ".Dumper($pbscript)."\n");
3032
3033my ($vexist,$vpid) = (undef,undef);
3034my $pm;
3035my $all_ok = 1;
3036if (defined $pbparallel) {
3037 $pm = new Parallel::ForkManager($pbparallel);
3038
3039 # Set which port the VM/RM will use to communicate
3040 $pm->run_on_start(\&pb_set_port);
3041 $pm->run_on_finish(sub { my ($pid, $code, $id, $signal, $dump) = @_;
3042 unless ($code == 0 && $signal == 0 && $dump == 0) {
3043 $all_ok = 0;
3044 pb_log(0,"ERROR: pid $pid failed\n");
3045 }
3046 });
3047}
3048
3049# Determine on which V to work
3050if (defined $ENV{'PBV'}) {
3051 @$vm = split(/,/,$ENV{'PBV'});
3052} else {
3053 ($vm,$all) = pb_get2v($vtype);
3054}
3055
3056my $counter = 0;
3057foreach my $v (@$vm) {
3058 $counter++;
3059 # Modulo 2 * pbparallel (to avoid synchronization problems)
3060 $counter = 1 if ((defined $pbparallel) && ($counter > 2 * $pbparallel));
3061 $pm->start($counter) and next if (defined $pbparallel);
3062
3063 # If launched via script2v then get the script passed as the one for the that VM
3064 if (not defined $pbscript->{$v}) {
3065 $pbscript->{$v} = $pbscript->{'default'};
3066 }
3067 #
3068 # Launch a single operation
3069 ($vexist,$vpid) = pb_launchv($pbscript->{$v},$vtype,$v,$action,$pbstep,$pbforce,$snapme,$pbsnap,$pbimage);
3070
3071 # Skip that VM/RM if something went wrong
3072 if (($vtype =~ /(v|r)m/) && ($vpid == 0) && ($vexist == 0)) {
3073 $pm->finish if (defined $pbparallel);
3074 next;
3075 }
3076
3077
3078 $pm->finish if (defined $pbparallel);
3079}
3080$pm->wait_all_children if (defined $pbparallel);
3081die "Aborting, one or more of the children failed." if ((not $all_ok) && ($Global::pb_stop_on_error));
3082}
3083
3084sub pb_launchv {
3085
3086my $pbscript1=shift; # Just the script name to execute, not the hash
3087my $vtype = shift;
3088my $v = shift; # Only 1 VM/VE/RM treated here
3089my $action = shift; # It an action is defined then use send2target
3090my $pbstep=shift; # Which step are we in (0: create, 1: setup, 2: build 3: use)
3091my $pbforce=shift; # Force stop of VM. Default not.
3092my $snapme = shift; # By default do not snap a VM/VE/RM
3093my $usesnap = shift; # By default study the usage of the snapshot feature of VM/VE/RM
3094my $pbimage=shift; # Which image to use to start the VM/VE
3095my $vexist = undef;
3096my $vpid = undef;
3097
3098$pbforce = 0 if (not defined $pbforce);
3099$snapme = 0 if (not defined $snapme);
3100$usesnap = 1 if (not defined $usesnap);
3101
3102# Check that v exists
3103die "No VM/VE/RM defined, unable to launch" if (not defined $v);
3104
3105# If creation or snapshot creation mode, no snapshot usable
3106if (($pbstep == 0) || ($snapme == 1)) {
3107 $usesnap = 0;
3108}
3109
3110pb_log(2,"DEBUG: pb_launchv(vtype,v,step,force,snapme,usesnap: $vtype,$v,$pbstep,$pbforce,$snapme,$usesnap)\n");
3111pb_log(2,"DEBUG: pb_launchv(pbimage: $pbimage)\n") if (defined $pbimage);
3112pb_log(2,"DEBUG: pb_launchv(pbscript $pbscript1)\n") if (defined $pbscript1);
3113pb_log(2,"DEBUG: pb_launchv(action: $action)\n") if (defined $action);
3114# Keep only the first VM in case many were given
3115if ($v =~ /,/) {
3116 pb_log(0,"WARNING: pruning to just the first of several VM/VE/RMs listed ($v)\n");
3117 $v =~ s/,.*//;
3118}
3119
3120my $pbos = pb_distro_get_context($v);
3121
3122pb_apply_conf_proxy($pbos);
3123
3124# Launch the VMs/VEs
3125if ($vtype eq "vm") {
3126 confess "-i image parameter needed" if (((not defined $pbimage) || ($pbimage eq "")) && ($pbstep == 0));
3127
3128 my ($ptr,$ptr2,$vmpath,$vmport,$vms) = pb_conf_get("vmtype","vmcmd","vmpath","vmport","vmsize");
3129 my ($vmopt,$vmmm,$vmtmout,$vmsnap,$vmbuildtm,$vmmonport) = pb_conf_get_if("vmopt","vmmem","vmtmout","vmsnap","vmbuildtm","vmmonport");
3130 my $vmsize = pb_distro_get_param($pbos,$vms);
3131
3132 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
3133 my $vmcmd = $ptr2->{$ENV{'PBPROJ'}};
3134
3135 if (defined $opts{'g'}) {
3136 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
3137 $ENV{'PBVMOPT'} = "--nographic";
3138 }
3139 }
3140 if (not defined $ENV{'PBVMOPT'}) {
3141 $ENV{'PBVMOPT'} = "";
3142 }
3143 # Save the current status for later restoration
3144 $ENV{'PBOLDVMOPT'} = $ENV{'PBVMOPT'};
3145
3146 if (defined $vmopt->{$v}) {
3147 $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
3148 } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
3149 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
3150 }
3151
3152 # How much memory to allocate for VMs
3153 if (defined $vmmm) {
3154 my $vmmem = pb_distro_get_param($pbos,$vmmm);
3155 if (defined $vmmem) {
3156 $ENV{'PBVMOPT'} .= " -m $vmmem";
3157 }
3158 }
3159
3160 # Are we allowed to use snapshot feature
3161 if ($usesnap == 1) {
3162 if ((defined $vmsnap->{$v}) && ($vmsnap->{$v} =~ /true/i)) {
3163 $ENV{'PBVMOPT'} .= " -snapshot";
3164 } elsif ((defined $vmsnap->{$ENV{'PBPROJ'}}) && ($vmsnap->{$ENV{'PBPROJ'}} =~ /true/i)) {
3165 $ENV{'PBVMOPT'} .= " -snapshot";
3166 } elsif ($pbsnap eq 1) {
3167 $ENV{'PBVMOPT'} .= " -snapshot";
3168 }
3169 }
3170 if ($snapme != 0) {
3171 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
3172 # Configure the monitoring to automate the creation of the 'pb' snapshot
3173 $ENV{'PBVMOPT'} .= " -serial mon:telnet::$vmmonport->{$ENV{'PBPROJ'}},server,nowait" if ((defined $vmmonport) && (defined $vmmonport->{$ENV{'PBPROJ'}}));
3174 # In that case no snapshot call needed
3175 $ENV{'PBVMOPT'} =~ s/ -snapshot//;
3176 }
3177 }
3178
3179 # Set a default timeout of 2 minutes if none defined externally or in conf files
3180 if (not defined $ENV{'PBVMTMOUT'}) {
3181 if (defined $vmtmout->{$v}) {
3182 $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
3183 } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
3184 $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
3185 }
3186 }
3187 if (not defined $ENV{'PBVMTMOUT'}) {
3188 $ENV{'PBVMTMOUT'} = "120";
3189 }
3190
3191 my $nport = pb_get_port($vmport,$pbos,$vtype);
3192
3193 my $cmd;
3194 my $vmm; # has to be used for pb_check_ps
3195 if (($vmtype eq "qemu") || ($vmtype eq "kvm")) {
3196 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
3197 if (($pbstep == 0) || (defined $pbimage)) {
3198 $ENV{'PBVMOPT'} .= " -cdrom $pbimage -boot d";
3199 }
3200 # Always redirect the network and always try to use a 'pb' snapshot
3201 #$cmd = "$vmcmd $ENV{'PBVMOPT'} -net nic,model=virtio -net user,hostfwd=tcp::$nport:22 -loadvm pb $vmm"
3202 #$cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
3203 $cmd = "$vmcmd $ENV{'PBVMOPT'} -net nic -net user,hostfwd=tcp:127.0.0.1:$nport-:22 $vmm"
3204 } elsif ($vmtype eq "xen") {
3205 } elsif ($vmtype eq "vmware") {
3206 } else {
3207 die "VM of type $vmtype not supported. Report to the dev team";
3208 }
3209 # Restore the ENV VAR Value
3210 $ENV{'PBVMOPT'} = $ENV{'PBOLDVMOPT'};
3211
3212 my ($tmpcmd,$void) = split(/ +/,$cmd);
3213 my $vmmport = undef;
3214 ($vexist,$vmmport) = pb_check_ps($tmpcmd,$vmm);
3215 $vpid = 0;
3216
3217 if (! $vexist) {
3218 if ($pbstep == 0) {
3219 die("Found an existing Virtual machine $vmm. Won't overwrite") if (-r $vmm);
3220 if (($vmtype eq "qemu") || ($vmtype eq "xen") || ($vmtype eq "kvm")) {
3221 my $command = pb_check_req("qemu-img",0);
3222 pb_system("$command create -f qcow2 $vmm $vmsize","Creating the QEMU VM");
3223 } elsif ($vmtype eq "vmware") {
3224 } else {
3225 }
3226 }
3227 if (! -f "$vmm") {
3228 pb_log(0,"Unable to find VM $vmm\n");
3229 } else {
3230 # Is the SSH port free? if not kill the existing process using it after a build timeout period
3231 my ($vmssh,$void) = pb_check_ps($tmpcmd,$vmm);
3232 if ($vmssh) {
3233 my $buildtm = $ENV{'PBVMTMOUT'};
3234 if (defined $vmbuildtm->{$v}) {
3235 $buildtm = $vmbuildtm->{$v};
3236 } elsif (defined $vmbuildtm->{$ENV{'PBPROJ'}}) {
3237 $buildtm = $vmbuildtm->{$ENV{'PBPROJ'}};
3238 }
3239
3240 sleep $buildtm;
3241 pb_log(0,"WARNING: Killing the process ($vmssh) using port $void (previous failed VM ?)\n");
3242 kill 15,$vmssh;
3243 # Let it time to exit
3244 sleep 5;
3245 }
3246 pb_system("$cmd &","Launching the VM $vmm");
3247 # Using system allows to kill it externaly if needed,sosupport that in the call
3248 pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up","mayfail");
3249 ($vpid,$void) = pb_check_ps($tmpcmd,$vmm);
3250 pb_log(0,"VM $vmm launched (pid $vpid)\n");
3251 }
3252 } else {
3253 pb_log(0,"Found an existing VM $vmm (pid $vexist - Port $vmmport)\n");
3254 # Set the correct port here based on what is done
3255 $pbport = $vmmport;
3256 $vpid = $vexist;
3257 }
3258 pb_log(2,"DEBUG: pb_launchv returns ($vexist,$vpid)\n");
3259
3260 # After the VM is launched, then setup the SSH access if not in creation phase
3261 if ($pbstep != 0) {
3262 if (defined $pbport) {
3263 pb_ssh_setup($vtype,$pbport);
3264 } else {
3265 pb_ssh_setup($vtype,$nport);
3266 }
3267 }
3268
3269 # Skip that VM if something went wrong
3270 return($vexist,$vpid) if (($vpid == 0) && ($vexist == 0));
3271
3272 # If force stopping the VM then reset vexist
3273 if ($pbforce == 1) {
3274 $vpid = $vexist;
3275 $vexist = 0;
3276 }
3277} elsif ($vtype eq "ve") {
3278 # Force the creation of the VE and no snapshot usable
3279 $vetype = pb_ve_get_type($vetype);
3280 pb_ve_launch($v,$pbstep,$usesnap,$vetype,$pbimage);
3281 $vexist = 0;
3282 $vpid = 0;
3283} else {
3284 # Get RM context
3285 my ($ptr,$rmpath,$rmport) = pb_conf_get("rmtype","rmpath","rmport");
3286
3287 # Nothing more to do for RM. No real launch
3288 # For the moment we support the RM is already running
3289 # For ProLiant may be able to power them on if needed later on as an example.
3290 $vexist = 0;
3291 $vpid = 0;
3292
3293 # After the RM is launched, then setup the SSH access
3294 my $nport = pb_get_port($rmport,$pbos,$vtype);
3295 pb_ssh_setup($vtype,$nport);
3296}
3297
3298# Gather all required files to send them to the VM/VE/RM
3299# and launch the build through pbscript
3300if (defined $action) {
3301 pb_log(2,"DEBUG: Before send2target, vexist: $vexist, vpid: $vpid, vtype: $vtype, v: $v, action: $action\n");
3302 pb_send2target($action,$pbscript1,$v,$vexist,$vpid,$snapme,$pbstep);
3303}
3304return($vexist,$vpid);
3305}
3306
3307# Return string for date synchro
3308sub pb_date2v {
3309
3310my $vtype = shift;
3311my $pbos = shift;
3312my $nontp = shift || 0;
3313
3314pb_log(1,"Entering pb_date2v\n");
3315# VE gets time from parent OS.
3316return "/bin/true" if ($vtype) =~ /^ve/o;
3317
3318my ($ntp) = pb_conf_get_if($vtype."ntp");
3319my $vntp = undef;
3320my $ntpline = undef;
3321$vntp = $ntp->{$ENV{'PBPROJ'}} if (defined $ntp);
3322
3323if ((defined $vntp) && ($vntp !~ /^\s*$/)) {
3324 pb_log(2,"ntp server is $vntp\n");
3325 # ntp command depends on pbos
3326 my $vntpcmd = pb_distro_get_param($pbos,pb_conf_get("oscmdntp"));
3327 $ntpline = "sudo $vntpcmd $vntp";
3328}
3329# Force new date to be in the future compared to the date
3330# of the host by adding 1 minute
3331my @date=pb_get_date();
3332$date[1]++;
3333my $upddate = strftime("%m%d%H%M%Y", @date);
3334my $dateline = "sudo /bin/date $upddate";
3335if ((defined $ntpline) && ($nontp == 0)) {
3336 pb_log(1,"pb_date2v returns $ntpline\n");
3337 return($ntpline);
3338} else {
3339 pb_log(1,"pb_date2v returns $dateline\n");
3340 return($dateline);
3341}
3342}
3343
3344sub pb_build2v {
3345
3346my $vtype = shift;
3347my $action = shift; #build, test or prep
3348my $pbforce=shift; # Force stop of VM. Default not.
3349
3350$action = "build" if (not defined $action);
3351$pbforce = 0 if (not defined $pbforce);
3352
3353my $pbstep;
3354
3355$vetype = pb_ve_get_type($vetype);
3356if ($action eq "prep") {
3357 $pbstep = 2;
3358} else {
3359 $pbstep = 3;
3360 # Need to be traversed by others if using containers
3361 if (($vtype eq "ve") && ($vetype eq "docker")) {
3362 chmod 0751,"$ENV{'PBBUILDDIR'}";
3363 }
3364}
3365
3366pb_log(1,"Entering in pb_build2v ($action in $vtype) \n");
3367
3368# We prepare scripts for all V of this vtype to allow sharing that part
3369my ($v,$all) = pb_get2v($vtype);
3370
3371# Send tar files when we do a global generation
3372pb_build2ssh() if (($all == 1) && ($action eq "build"));
3373
3374# Adapt // mode to memory size
3375$pbparallel = pb_set_parallel($vtype);
3376
3377my ($vexist,$vpid) = (undef,undef);
3378my $pm;
3379my $all_ok = 1;
3380if (defined $pbparallel) {
3381 $pm = new Parallel::ForkManager($pbparallel);
3382
3383 # Set which port the VM/RM will use to communicate
3384 $pm->run_on_start(\&pb_set_port);
3385 $pm->run_on_finish(sub { my ($pid, $code, $id, $signal, $dump) = @_;
3386 unless ($code == 0 && $signal == 0 && $dump == 0) {
3387 $all_ok = 0;
3388 pb_log(0,"ERROR: pid $pid failed\n");
3389 }
3390 });
3391}
3392
3393my $counter = 0;
3394foreach my $v (@$v) {
3395 $counter++;
3396 $pbscript{$v} = "$ENV{'PBDESTDIR'}/pb$action-$v";
3397 # Modulo 2 * pbparallel (to avoid synchronization problems)
3398 $counter = 1 if ((defined $pbparallel) && ($counter > 2 * $pbparallel));
3399 $pm->start($counter) and next if (defined $pbparallel);
3400 #
3401 # Prepare the script to be executed on the VM/VE/RM
3402 open(SCRIPT,"> $pbscript{$v}") || die "Unable to create $pbscript{$v}";
3403 print SCRIPT "#!/bin/bash\n";
3404
3405 # Transmit the verbosity level to the virtual env/mach.
3406 my $verbose = "";
3407 my $i = 0; # minimal debug level
3408 while ($i lt $pbdebug) {
3409 $verbose .= "-v ";
3410 $i++;
3411 }
3412 print SCRIPT "set -e\n" if ($Global::pb_stop_on_error);
3413 # Activate script verbosity if at least 2 for pbdebug
3414 print SCRIPT "set -x\n" if ($i gt 1);
3415 # Quiet if asked to be so on the original system
3416 $verbose = "-q" if ($pbdebug eq -1);
3417
3418 print SCRIPT "echo ... Execution needed\n";
3419 print SCRIPT "echo ==== Start of script for $vtype $v =====\n";
3420 print SCRIPT "# This is in directory pbdelivery\n";
3421 print SCRIPT "# Setup the variables required for building\n";
3422 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
3423
3424 if (($action eq "build") || ($action eq "prep")) {
3425 print SCRIPT "# Preparation for pb\n";
3426 print SCRIPT "rm -f \$HOME/.pbrc.yml\n";
3427 print SCRIPT "rm -f \$HOME/.pbrc\n";
3428 print SCRIPT "cp .pbrc.yml \$HOME\n";
3429 # TODO: Why ?
3430 print SCRIPT "cd ..\n";
3431 }
3432
3433 # VE needs a good /proc, tolerate one being potentially left around after a failure
3434 if (($vtype eq "ve") && ($vetype ne "docker")) {
3435 print SCRIPT "[ -d /proc/1 ] || sudo /bin/mount -t proc /proc /proc\n";
3436 }
3437
3438 # Get distro context
3439 my $pbos = pb_distro_get_context($v);
3440
3441 my $ntpline = pb_date2v($vtype,$pbos);
3442 print SCRIPT "# Time sync\n";
3443 print SCRIPT "echo setting up date with $ntpline\n";
3444 print SCRIPT "$ntpline\n";
3445 print SCRIPT "if [ \$\? -ne 0 ]; then\n";
3446 # Force if bad status a trick using date
3447 $ntpline = pb_date2v($vtype,$pbos, 1);
3448 print SCRIPT " $ntpline\n";
3449 print SCRIPT "fi\n";
3450 # Use potential local proxy declaration in case we need it to download repo, pkgs, ...
3451 if (defined $ENV{'http_proxy'}) {
3452 print SCRIPT "export http_proxy=\"$ENV{'http_proxy'}\"\n";
3453 }
3454 if (defined $ENV{'https_proxy'}) {
3455 print SCRIPT "export https_proxy=\"$ENV{'https_proxy'}\"\n";
3456 }
3457 if (defined $ENV{'ftp_proxy'}) {
3458 print SCRIPT "export ftp_proxy=\"$ENV{'ftp_proxy'}\"\n";
3459 }
3460
3461 # Get list of packages to build/test and get some ENV vars as well
3462 my $ptr = pb_get_pkg();
3463 @pkgs = @$ptr;
3464 my $p = join(' ',@pkgs) if (@pkgs);
3465 print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
3466 print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
3467 print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
3468
3469 # We may need to do some other tasks before building. Read a script here to finish setup
3470 if (-x "$ENV{'PBTMP'}/pb$vtype".".pre") {
3471 print SCRIPT "# Special pre-instructions to be launched\n";
3472 print SCRIPT pb_get_content("$ENV{'PBTMP'}/pb$vtype".".pre");
3473 }
3474
3475 if (-x "$ENV{'PBTMP'}/pb$vtype"."$action.pre") {
3476 print SCRIPT "# Special pre-$action instructions to be launched\n";
3477 print SCRIPT pb_get_content("$ENV{'PBTMP'}/pb$vtype"."$action.pre");
3478 }
3479
3480 print SCRIPT "# $action\n";
3481 print SCRIPT "echo $action"."ing packages on $vtype...\n";
3482
3483 if (($action eq "test") && (! -x "$ENV{'PBDESTDIR'}/pbtest")) {
3484 confess "No test script ($ENV{'PBDESTDIR'}/pbtest) found when in test mode. Aborting ...";
3485 }
3486 print SCRIPT "pb --version\n";
3487 my $act = "$action"."2pkg";
3488 $act = "build2prep" if ($action eq "prep");
3489 print SCRIPT "pb $verbose -p $ENV{'PBPROJ'} $act $p\n";
3490
3491 if (($vtype eq "ve") && ($vetype ne "docker")) {
3492 print SCRIPT "sudo /bin/umount /proc\n";
3493 }
3494
3495 # We may need to do some other tasks after building. Read a script here to exit properly
3496 if (-x "$ENV{'PBTMP'}/pb$vtype"."$action.post") {
3497 print SCRIPT "# Special post-$action instructions to be launched\n";
3498 print SCRIPT pb_get_content("$ENV{'PBTMP'}/pb$vtype"."$action.post");
3499 }
3500
3501 if (-x "$ENV{'PBTMP'}/pb$vtype".".post") {
3502 print SCRIPT "# Special post-instructions to be launched\n";
3503 print SCRIPT pb_get_content("$ENV{'PBTMP'}/pb$vtype".".post");
3504 }
3505
3506 print SCRIPT "echo ==== End of script $$ for $vtype $v =====\n";
3507 close(SCRIPT);
3508 chmod 0755,"$pbscript{$v}";
3509
3510 $pm->finish if (defined $pbparallel);
3511}
3512$pm->wait_all_children if (defined $pbparallel);
3513die "Aborting, one or more of the children failed." if ((not $all_ok) && ($Global::pb_stop_on_error));
3514
3515# Launch the VM/VE/RM
3516# docker image to use if needed
3517# Gather all required files to send them to the VM/VE
3518# and launch the build through pbscript
3519pb_parallel_launchv(\%pbscript,$vtype,uc($vtype).$action,$pbstep,$pbimage,$pbforce);
3520}
3521
3522
3523sub pb_clean {
3524
3525 my $sleep=10;
3526 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
3527 die "Unable to get env var PBBUILDDIR" if (not defined $ENV{'PBBUILDDIR'});
3528 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");
3529 sleep $sleep;
3530 pb_rm_rf($ENV{'PBDESTDIR'});
3531 pb_rm_rf($ENV{'PBBUILDDIR'});
3532}
3533
3534sub pb_newver {
3535
3536 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
3537
3538 # Need this call for PBDIR
3539 my ($scheme2,$uri) = pb_cms_init($pbinit);
3540
3541 my ($pbconf,$pbprojurl) = pb_conf_get("pbconfurl","pbprojurl");
3542 $uri = $pbconf->{$ENV{'PBPROJ'}};
3543 my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
3544
3545 # Checking CMS repositories status
3546 ($scheme2, $account, $host, $port, $path) = pb_get_uri($pbprojurl->{$ENV{'PBPROJ'}});
3547
3548 if ($scheme !~ /^svn/) {
3549 die "Only SVN is supported at the moment";
3550 }
3551
3552 my $res = pb_vcs_isdiff($scheme,$ENV{'PBROOTDIR'});
3553 die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
3554
3555 $res = pb_vcs_isdiff($scheme2,$ENV{'PBDIR'});
3556 die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
3557
3558 # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
3559 # we want to get for the root of the new URL
3560
3561 my $oldver = $ENV{'PBROOTDIR'};
3562 $oldver =~ s|^$ENV{'PBCONFDIR'}||;
3563
3564 pb_log(2, "PBCONFDIR: $ENV{'PBCONFDIR'}\nPBROOTDIR: $ENV{'PBROOTDIR'}\n");
3565
3566 my $newurl = "$uri/$newver";
3567 # Should probably use projver in the old file
3568 my $oldvertxt= basename($oldver);
3569 my $newvertxt = basename($newver);
3570
3571 # Duplicate and extract project-builder part
3572 pb_log(2,"Copying $uri/$oldver to $newurl\n");
3573 pb_vcs_copy($scheme,"$uri/$oldver",$newurl);
3574 pb_log(2,"Checkout $newurl to $ENV{'PBCONFDIR'}/$newver\n");
3575 pb_vcs_up($scheme,"$ENV{'PBCONFDIR'}");
3576
3577 # Duplicate and extract project
3578 my $newurl2 = "$pbprojurl->{$ENV{'PBPROJ'}}/$newver";
3579
3580 pb_log(2,"Copying $pbprojurl->{$ENV{'PBPROJ'}}/$oldver to $newurl2\n");
3581 pb_vcs_copy($scheme2,"$pbprojurl->{$ENV{'PBPROJ'}}/$oldver",$newurl2);
3582
3583 my $tmp = $ENV{'PBDIR'};
3584 $tmp =~ s|$oldver$||;
3585 pb_log(2,"Checkout $newurl2 to $tmp/$newver\n");
3586 pb_vcs_up($scheme2,"$tmp");
3587
3588 # Update the .yml file
3589 open(FILE,"$ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml") || die "Unable to open $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml";
3590 open(OUT,"> $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml.new") || die "Unable to write to $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml.new";
3591 while(<FILE>) {
3592 if (/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldvertxt$/) {
3593 s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldvertxt$/projver $ENV{'PBPROJ'} = $newvertxt/;
3594 pb_log(0,"Changing projver from $oldvertxt to $newvertxt in $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml\n");
3595 }
3596 if (/^testver/) {
3597 s/^testver/#testver/;
3598 pb_log(0,"Commenting testver in $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml\n") if (/^testver/);
3599 }
3600 if (/^delivery/) {
3601 my $txt = $_;
3602 chomp($txt);
3603 pb_log(0,"Please check delivery ($txt) in $ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml\n");
3604 }
3605 print OUT $_;
3606 }
3607 close(FILE);
3608 close(OUT);
3609 rename("$ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml.new","$ENV{'PBCONFDIR'}/$newver/$ENV{'PBPROJ'}.yml");
3610
3611 # Checking pbcl files
3612 foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
3613 # Compute new pbcl file
3614 my $f2 = $f;
3615 $f2 =~ s|$ENV{'PBROOTDIR'}|$ENV{'PBCONFDIR'}/$newver/|;
3616 open(PBCL,$f) || die "Unable to open $f";
3617 my $foundnew = 0;
3618 while (<PBCL>) {
3619 $foundnew = 1 if (/^$newvertxt \(/);
3620 }
3621 close(PBCL);
3622 open(OUT,"> $f2") || die "Unable to write to $f2: $!";
3623 open(PBCL,$f) || die "Unable to open $f";
3624 # We skip as long as we do not reach the new version in the log (case of devel)
3625 my $skip = 1;
3626 # We let the 4 first lines
3627 my $cnt = 0;
3628 while (<PBCL>) {
3629 $skip = 0 if (/^$newvertxt \(/);
3630 if ((/^$oldvertxt \(/) && ($foundnew == 0)) {
3631 print OUT "$newvertxt ($pbdate)\n";
3632 print OUT "- TBD\n";
3633 print OUT "\n";
3634 pb_log(0,"WARNING: version $newvertxt not found in $f so added to $f2...\n") if ($foundnew == 0);
3635 }
3636 if ($cnt <= 4) {
3637 print OUT "$_" if (not /^$oldvertxt \(/);
3638 } else {
3639 print OUT "$_" if ($skip == 0);
3640 }
3641 $cnt++;
3642 }
3643 close(OUT);
3644 close(PBCL);
3645 }
3646
3647 pb_log(2,"Checkin $ENV{'PBCONFDIR'}/$newver\n");
3648 pb_vcs_checkin($scheme,"$ENV{'PBCONFDIR'}/$newver","updated to $ENV{'PBCONFDIR'}");
3649}
3650
3651#
3652# Return the list of VMs/VEs/RMs we are working on
3653# $all is a flag to know if we return all of them
3654# or only some (if all we publish also tar files in addition to pkgs
3655#
3656sub pb_get2v {
3657
3658my $vtype = shift;
3659my @v;
3660my $all = 0;
3661my $pbv = 'PBV';
3662my $vlist = $vtype."list";
3663
3664# Get VM/VE list
3665if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
3666 my ($ptr) = pb_conf_get($vlist);
3667 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
3668 $all = 1;
3669}
3670pb_log(2,"$vtype: $ENV{$pbv}\n");
3671@v = split(/,/,$ENV{$pbv});
3672return(\@v,$all);
3673}
3674
3675# This function creates a giant script to configure a particular VM/VE/RM, it then copies the
3676# script to the target.
3677
3678# Function to create a potentialy missing pb account on the VM/VE/RM, and adds it to sudo
3679# Needs to use root account to connect to the VM/VE/RM
3680# pb will take your local public SSH key to access
3681# the pb account in the VM/VE/RM later on if needed
3682sub pb_setup2v {
3683
3684my $vtype = shift;
3685my $sbx = shift;
3686my $pbstep = 1;
3687
3688my ($vm,$all) = pb_get2v($vtype);
3689
3690# Adapt // mode to memory size
3691$pbparallel = pb_set_parallel($vtype);
3692
3693my $pm;
3694my $all_ok = 1;
3695if (defined $pbparallel) {
3696 $pm = new Parallel::ForkManager($pbparallel);
3697
3698 # Set which port the VM/RM will use to communicate
3699 $pm->run_on_start(\&pb_set_port);
3700 $pm->run_on_finish(sub { my ($pid, $code, $id, $signal, $dump) = @_;
3701 $all_ok = 0 unless (($code == 0) && ($signal == 0) && ($dump == 0));
3702 });
3703}
3704
3705my $counter = 0;
3706foreach my $v (@$vm) {
3707 # Script generated - should be before the fork - after the %pbscript hash isn't updated correctly
3708 $pbscript{$v} = "$ENV{'PBDESTDIR'}/setupv-$v";
3709
3710 $counter++;
3711 # Modulo 2 * pbparallel (to avoid synchronization problems)
3712 $counter = 1 if ((defined $pbparallel) && ($counter > 2 * $pbparallel));
3713 $pm->start($counter) and next if (defined $pbparallel);
3714
3715 # Get distro context
3716 my $pbos = pb_distro_get_context($v);
3717
3718 # Deal with date sync.
3719 my $ntpline = pb_date2v($vtype,$pbos);
3720
3721 # Name of the account to deal with for VM/VE/RM
3722 # Do not use the one passed potentially with -a
3723 my ($pbac) = pb_conf_get($vtype."login");
3724 my $key = "";
3725
3726 # Prepare the script to be executed on the VM/VE/RM
3727 # in $ENV{'PBDESTDIR'}/setupv
3728
3729 pb_log(2,"Preparing setup script for $v in $pbscript{$v}\n");
3730 open(SCRIPT,"> $pbscript{$v}") || die "Unable to create $pbscript{$v}";
3731
3732 print SCRIPT << 'EOF';
3733#!/usr/bin/perl -w
3734
3735use strict;
3736use File::Copy;
3737
3738# We should not need in this script more functions than what is provided
3739# by Base, Conf and Distribution to avoid problems at exec time.
3740# They are appended at the end.
3741
3742# Define mandatory global vars
3743EOF
3744 my $ppref = "our";
3745 $ppref = "my" if (($pbos->{'name'} =~ /^redhat/) && ($pbos->{'version'} =~ /^6/));
3746 print SCRIPT << "EOF";
3747$ppref \$pbdebug = $pbdebug;
3748$ppref \$pbLOG;
3749$ppref \$pbsynmsg = "pbscript";
3750$ppref \$pbdisplaytype = "text";
3751$ppref \$pblocale = "";
3752$ppref \$PBCONFVER = $projectbuilderconfver;
3753EOF
3754 print SCRIPT << 'EOF';
3755pb_log_init($pbdebug, $pbLOG);
3756EOF
3757 print SCRIPT << "EOF";
3758\$Global::pb_stop_on_error = $Global::pb_stop_on_error;
3759pb_temp_init($pbkeep);
3760pb_conf_init("$ENV{'PBPROJ'}");
3761
3762EOF
3763
3764 if ($vtype eq "ve") {
3765 $vetype = pb_ve_get_type($vetype);
3766 if ($vetype ne "docker") {
3767 print SCRIPT << "EOF";
3768# For VE we first need to mount some FS
3769pb_system("mount -t proc /proc /proc") unless (-d "/proc/$$");
3770EOF
3771 }
3772 print SCRIPT << "EOF";
3773# For VE we need a good null dev
3774# Except for RedHat 6.2 where it's good and doesn't like being recreated
3775EOF
3776 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/));
3777 } elsif ($vtype =~ /(v|r)m/) {
3778 # Nothing to do now.
3779 my $useless = 0;
3780 } else {
3781 die "Unknown virtual type $vtype";
3782 }
3783
3784 print SCRIPT << 'EOF';
3785
3786# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
3787#
3788my $file="/etc/passwd";
3789open(PBFILE,$file) || die "Unable to open $file";
3790my $found = 0;
3791while (<PBFILE>) {
3792EOF
3793 print SCRIPT << "EOF";
3794 \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
3795EOF
3796
3797# TODO: use an external parameter
3798my $home = "/home";
3799# Solaris doesn't like that we use /home
3800$home = "/export/home" if ($pbos->{'type'} eq "pkg");
3801
3802 print SCRIPT << "EOF";
3803}
3804close(PBFILE);
3805
3806if ( \$found == 0 ) {
3807 if ( ! -d "$home" ) {
3808 pb_mkdir_p("$home");
3809 }
3810EOF
3811 # TODO: Level of portability of these cmds ? Critical now for RM
3812 # TODO: Check existence before adding to avoid errors
3813 print SCRIPT << "EOF";
3814pb_system("/usr/sbin/groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
3815pb_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'}})");
3816}
3817EOF
3818
3819 # Copy the content of our local conf file to the VM/VE/RM
3820 my $content = pb_get_content(pb_distro_conffile());
3821 print SCRIPT << "EOF";
3822 #
3823 # Create a temporary local conf file for distribution support
3824 # This is created here before its use later. Its place is hardcoded, so no choice for the path
3825 #
3826 my \$tempconf = pb_distro_conffile();
3827 pb_mkdir_p(dirname(\$tempconf));
3828 open(CONF,"> \$tempconf") || die "Unable to create \$tempconf";
3829 print CONF q{$content};
3830 close(CONF);
3831EOF
3832
3833 if ($vtype =~ /(v|r)m/) {
3834 print SCRIPT << "EOF";
3835# allow ssh entry to build
3836#
3837mkdir "$home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
3838# Allow those accessing root to access the build account
3839copy("\$ENV{'HOME'}/.ssh/authorized_keys","$home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
3840chmod 0600,".ssh/authorized_keys";
3841pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} $home/$pbac->{$ENV{'PBPROJ'}}","Finish setting up the account env for $pbac->{$ENV{'PBPROJ'}}");
3842
3843EOF
3844}
3845 print SCRIPT << 'EOF';
3846# No passwd for build account only keys
3847$file="/etc/shadow";
3848if (-f $file) {
3849 open(PBFILE,$file) || die "Unable to open $file";
3850 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
3851 while (<PBFILE>) {
3852EOF
3853 print SCRIPT << "EOF";
3854 s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
3855 s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/; #SLES 9 e.g.
3856 s/^$pbac->{$ENV{'PBPROJ'}}:\\*LK\\*:/$pbac->{$ENV{'PBPROJ'}}:NP:/; #Solaris e.g.
3857EOF
3858 print SCRIPT << 'EOF';
3859 print PBOUT $_;
3860 }
3861 close(PBFILE);
3862 close(PBOUT);
3863 rename("$file.new",$file);
3864 chmod 0640,$file;
3865 }
3866
3867# TODO: systemd support
3868# Keep the VM in text mode
3869$file="/etc/inittab";
3870if (-f $file) {
3871 open(PBFILE,$file) || die "Unable to open $file";
3872 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
3873 while (<PBFILE>) {
3874 s/^(..):5:initdefault:$/$1:3:initdefault:/;
3875 print PBOUT $_;
3876 }
3877 close(PBFILE);
3878 close(PBOUT);
3879 rename("$file.new",$file);
3880 chmod 0640,$file;
3881}
3882
3883# pb has to be added to portage group on gentoo
3884
3885# We need to have that pb_distro_get_context function
3886# Get it from ProjectBuilder::Distribution
3887# And we now need the conf file required for this to work created above
3888
3889my $pbos = pb_distro_get_context();
3890print "distro tuple: ".Dumper($pbos)."\n";
3891
3892# Adapt sudoers
3893# sudo is not default on Solaris and needs to be installed first
3894# from http://www.sunfreeware.com/programlistsparc10.html#sudo
3895if ($pbos->{'type'} eq "pkg") {
3896 $file="/usr/local/etc/sudoers";
3897} else {
3898 $file="/etc/sudoers";
3899}
3900open(PBFILE,$file) || die "Unable to open $file";
3901open(PBOUT,"> $file.new") || die "Unable to open $file.new";
3902while (<PBFILE>) {
3903EOF
3904 # Skip what will be generated
3905 print SCRIPT << "EOF";
3906 next if (/^$pbac->{$ENV{'PBPROJ'}}\\s+/);
3907 next if (/^Defaults:$pbac->{$ENV{'PBPROJ'}}\\s+/);
3908 next if (/^Defaults:root\\s+\!requiretty/);
3909 next if (/^Defaults:root\\s+env_keep/);
3910EOF
3911 print SCRIPT << 'EOF';
3912 s/Defaults\s+requiretty/# disable Defaults: requiretty/;
3913 print PBOUT $_;
3914}
3915close(PBFILE);
3916EOF
3917 print SCRIPT << "EOF";
3918# Some distro force requiretty at compile time, so disable here
3919print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} !requiretty\\n";
3920print PBOUT "Defaults:root !requiretty\\n";
3921EOF
3922 # RH 6.2 sudo doesn't support env_keep
3923 if (($pbos->{'name'} ne "redhat") && ($pbos->{'version'} ne "6.2")) {
3924 print SCRIPT << "EOF";
3925# Keep proxy configuration while using sudo
3926print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} env_keep += \\\"http_proxy https_proxy ftp_proxy\\\"\\n";
3927print PBOUT "Defaults:root env_keep += \\\"http_proxy https_proxy ftp_proxy\\\"\\n";
3928EOF
3929 }
3930 # Try to restrict security to what is really needed
3931 if ($vtype =~ /^vm/) {
3932 my $hpath = pb_distro_get_param($pbos,pb_conf_get("ospathcmd-halt"));
3933 my @sudocmds = pb_get_sudocmds($pbos,$ntpline,"sudo $hpath","sudo /bin/date");
3934 print SCRIPT << "EOF";
3935# This is needed in order to be able on VM to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
3936# Build account $pbac->{$ENV{'PBPROJ'}} in VM also needs to setup date and install deps.
3937# Nothing else should be needed
3938EOF
3939 foreach my $c (@sudocmds) {
3940 print SCRIPT "print PBOUT \"$pbac->{$ENV{'PBPROJ'}} ALL = NOPASSWD: $c\\n\";\n";
3941 }
3942 } elsif ($vtype =~ /^rm/) {
3943 my @sudocmds = pb_get_sudocmds($pbos,$ntpline);
3944 print SCRIPT << "EOF";
3945# Build account $pbac->{$ENV{'PBPROJ'}} in RM only needs to setup date and install deps if needed each time
3946EOF
3947 foreach my $c (@sudocmds) {
3948 print SCRIPT "print PBOUT \"$pbac->{$ENV{'PBPROJ'}} ALL = NOPASSWD: $c\\n\";\n";
3949 }
3950 } else {
3951 print SCRIPT << "EOF";
3952# Build account $pbac->{$ENV{'PBPROJ'}} for VE needs to do a lot in the host (and chroot), so allow without restriction for now
3953print PBOUT "$pbac->{$ENV{'PBPROJ'}} ALL=(ALL) NOPASSWD:ALL\n";
3954EOF
3955 }
3956 my $sudomode = pb_distro_get_param($pbos,pb_conf_get("ossudoersmode"));
3957 print SCRIPT << 'EOF';
3958close(PBOUT);
3959rename("$file.new",$file);
3960EOF
3961 print SCRIPT << "EOF";
3962chmod 0$sudomode,\$file;
3963
3964EOF
3965
3966 if ($vtype =~ /(v|r)m/) {
3967 # Sync date
3968 # do it after sudoers is setup
3969 print SCRIPT "my \$res = pb_system(\"$ntpline\",\"Updating time with $ntpline\",\"mayfailverbose\");\n";
3970 print SCRIPT "if (\$res != 0) {\n";
3971 # try again with another method
3972 $ntpline = pb_date2v($vtype,$pbos,1);
3973 print SCRIPT " pb_system(\"$ntpline\",\"Updating time with $ntpline\",\"mayfailverbose\");\n";
3974 print SCRIPT "}\n";
3975 }
3976
3977 my ($addrepo) = pb_conf_read("$ENV{'PBDESTDIR'}/pbrc.yml","addrepo");
3978 my $pbrepo = pb_distro_get_param($pbos,$addrepo);
3979 print SCRIPT << 'EOF';
3980
3981# We may need a proxy configuration. Get it from the local env
3982pb_apply_conf_proxy($pbos);
3983
3984# First install all required packages
3985pb_system("dnf clean all","Cleaning dnf env","mayfail") if (($pbos->{'name'} eq "fedora") && ($pbos->{'version'} >= 22));
3986pb_system("yum clean all","Cleaning yum env","mayfail") if ((($pbos->{'name'} eq "fedora") && ($pbos->{'version'} < 22)) || ($pbos->{'name'} eq "asianux") || ($pbos->{'name'} eq "rhel"));
3987my ($ospkgdep) = pb_conf_get_if("ospkgdep");
3988
3989my $pkgdep = pb_distro_get_param($pbos,$ospkgdep);
3990my $forcerepo;
3991EOF
3992 print SCRIPT "\$forcerepo = pb_distro_setuprepo_gen(\$pbos,\"$pbrepo\");\n" if ((defined $pbrepo) && ($pbrepo ne ""));
3993 print SCRIPT << 'EOF';
3994pb_distro_installdeps(undef,$pbos,$forcerepo,pb_distro_only_deps_needed($pbos,join(' ',split(/,/,$pkgdep))));
3995
3996EOF
3997 my $itype = pb_distro_get_param($pbos,pb_conf_get("pbinstalltype"));
3998 # Install from sandbox mean a file base install
3999 $itype = "file" if (defined $sbx);
4000 if ($itype =~ /^file/) {
4001 my $cmdget;
4002 my ($pbver1,$pbver2,$pbextdir);
4003 my ($tmp1,$tmp2);
4004 if (defined $sbx) {
4005 # Install from sandbox mean using the result of the just passed sbx2build command
4006 # Get content saved in cms2build
4007 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.yml","pbpkg");
4008 $pbextdir = pb_get_extdir();
4009 die "Unable to get package list" if (not defined $pkg);
4010
4011 # We consider 2 specific packages
4012 my $vertag1 = $pkg->{"ProjectBuilder"};
4013 my $vertag2 = $pkg->{"project-builder"};
4014 # get the version of the current package - maybe different
4015 pb_log(2,"Vertag1: $vertag1\n");
4016 pb_log(2,"Vertag2: $vertag2\n");
4017 ($pbver1,$tmp1) = split(/-/,$vertag1);
4018 ($pbver2,$tmp2) = split(/-/,$vertag2);
4019 $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";
4020 } else {
4021 # TODO: shouldn't we have a variable to specify the source file?
4022 # And shouldn't we be getting the one that matches the current pb version?
4023 # For the moment, just take the latest stable one
4024 my $wget = pb_check_req("wget",0);
4025 $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";
4026 }
4027 print SCRIPT << 'EOF';
4028# Then install manually the missing perl modules
4029my ($osperldep,$osperlver) = pb_conf_get_if("osperldep","osperlver");
4030
4031my $perldep = pb_distro_get_param($pbos,$osperldep);
4032my $bashopt = "";
4033$bashopt = "-x" if ($pbdebug ge 1);
4034my $verbopt = "mayfail";
4035$verbopt = "mayfailverbose" if ($pbdebug ge 1);
4036foreach my $m (split(/,/,$perldep)) {
4037 # Skip empty deps
4038 next if ($m =~ /^\s*$/);
4039 my $dir = $m;
4040 $dir =~ s/-.*//;
4041 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);
4042}
4043EOF
4044 print SCRIPT << 'EOF';
4045pb_system("rm -rf ProjectBuilder-* ; rm -rf project-builder-* ; rm -rf `perl -V:installvendorlib | awk -F\"'\" '{print \$2}'`/ProjectBuilder ;
4046EOF
4047 print SCRIPT " $cmdget ; ";
4048 print SCRIPT << "EOF"
4049gzip -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");
4050EOF
4051 } elsif ($itype =~ /^pkg/) {
4052 # pkg based install. We need to point to the project-builder.org repository
4053 print SCRIPT << 'EOF';
4054my $pkgforpb = pb_distro_get_param($pbos,pb_conf_get_if("ospkg"));
4055my $forcerepo = pb_distro_setuposrepo($pbos);
4056pb_log(1,"Forcing full package install with forcerepo\n") if (defined $forcerepo);
4057pb_distro_installdeps(undef,$pbos,$forcerepo,pb_distro_only_deps_needed($pbos,join(' ',split(/,/,$pkgforpb))));
4058EOF
4059 } else {
4060 # Unknown install type
4061 die("Unknown install type $itype->{$ENV{'PBPROJ'}} for param pbinstalltype");
4062 }
4063 print SCRIPT << 'EOF';
4064pb_system("pbdistrocheck",undef,"verbose");
4065pb_system("pb --version",undef,"verbose");
4066EOF
4067 if ($vtype eq "ve"){
4068 if ($vetype ne "docker") {
4069 print SCRIPT << 'EOF';
4070# For VE we need to umount some FS at the end
4071
4072pb_system("umount /proc","Unmounting /proc","mayfail");
4073EOF
4074 }
4075 print SCRIPT << 'EOF';
4076# Create a basic network file if not already there
4077
4078my $nf="/etc/sysconfig/network";
4079if ((! -f $nf) && ($pbos->{'type'} eq "rpm") && ($pbos->{'family'} ne "novell")) {
4080 open(NF,"> $nf") || die "Unable to create $nf";
4081 print NF "NETWORKING=yes\n";
4082 print NF "HOSTNAME=localhost\n";
4083 close(NF);
4084}
4085chmod 0755,$nf;
4086EOF
4087 }
4088
4089 # Adds pb_distro_get_context and all functions needed from ProjectBuilder::Distribution, Conf and Base
4090 foreach my $m ("ProjectBuilder/Base.pm","ProjectBuilder/Distribution.pm","ProjectBuilder/Conf.pm") {
4091 foreach my $d (@INC) {
4092 my $f = "$d/$m";
4093 if (-f "$f") {
4094 open(PBD,"$f") || die "Unable to open $f: $!";
4095 while (<PBD>) {
4096 next if (/^package/);
4097 next if (/^use Exporter/);
4098 next if (/^use ProjectBuilder::/);
4099 next if (/^our /);
4100 print SCRIPT $_;
4101 }
4102 close(PBD);
4103 # We just need the first one of each file wrt @INC - hopefully that's the right one.
4104 last;
4105 }
4106 }
4107 }
4108 # Use a fake pb_version_init version here
4109 print SCRIPT << "EOF";
4110sub pb_version_init {
4111
4112return("$projectbuilderver","$projectbuilderrev","$projectbuilderconfver");
4113}
41141;
4115EOF
4116 close(SCRIPT);
4117 chmod 0755,"$pbscript{$v}";
4118
4119 $pm->finish if (defined $pbparallel);
4120}
4121$pm->wait_all_children if (defined $pbparallel);
4122die "Aborting, one or more of the children failed." if ((not $all_ok) && ($Global::pb_stop_on_error));
4123
4124# These build scripts need to be run as root and force stop of VM at end
4125$ENV{'PBACCOUNT'} = "root";
4126
4127$sbx = "" if (not defined $sbx);
4128# Launch the VM/VE/RM
4129pb_log(2,"DEBUG: before parallel launch, pbscript hash is:".Dumper(%pbscript)."\n");
4130pb_parallel_launchv(\%pbscript,$vtype,uc($vtype)."Script".$sbx,$pbstep,$pbimage,$pbforce,0,$pbsnap);
4131
4132return;
4133}
4134
4135# Function to create a snapshot named 'pb' for VMs and a compressed tar for VEs
4136sub pb_snap2v {
4137
4138my $vtype = shift;
4139my $pbstep = 2;
4140
4141my ($vm,$all) = pb_get2v($vtype);
4142
4143# Script generated
4144my $pbscript = "$ENV{'PBDESTDIR'}/snapv.$$";
4145
4146my ($pbac) = pb_conf_get($vtype."login");
4147
4148foreach my $v (@$vm) {
4149 if ($vtype eq "ve") {
4150 # Get distro context
4151 my $pbos = pb_distro_get_context($v);
4152 my ($vepath) = pb_conf_get("vepath");
4153
4154 # Test if an existing snapshot exists and remove it if there is a VE
4155 if ((-f "$vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz") &&
4156 (! -d "$vepath->{$ENV{'PBPROJ'}}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}")) {
4157 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");
4158 }
4159 }
4160
4161 # Prepare the script to be executed on the VM/VE
4162 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
4163 print SCRIPT << 'EOF';
4164 #!/bin/bash
4165 sleep 2
4166EOF
4167 close(SCRIPT);
4168 chmod 0755,"$pbscript";
4169
4170 # Force shutdown of VM/VE
4171 # Force snapshot of VM/VE
4172 pb_script2v($pbscript,$vtype,$pbstep,1,$v,1);
4173}
4174return;
4175}
4176
4177# Function to update VMs/VEs/RMs with the latest distribution content
4178sub pb_update2v {
4179
4180my $vtype = shift;
4181my $pbstep = 2;
4182
4183my ($vm,$all) = pb_get2v($vtype);
4184
4185# Script generated
4186my $pbscript = "$ENV{'PBDESTDIR'}/updatev.$$";
4187
4188my ($pbac) = pb_conf_get($vtype."login");
4189
4190foreach my $v (@$vm) {
4191 # Get distro context
4192 my $pbos = pb_distro_get_context($v);
4193
4194 # Prepare the script to be executed on the VM/VE/RM
4195 # in $ENV{'PBDESTDIR'}/updatev
4196 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
4197
4198 print SCRIPT << 'EOF';
4199 #!/bin/bash
4200 sleep 2
4201EOF
4202 # VE needs a good /proc
4203 $vetype = pb_ve_get_type($vetype);
4204 if (($vtype eq "ve") && ($vetype ne "docker")) {
4205 print SCRIPT "sudo /bin/mount -t proc /proc /proc\n";
4206 }
4207 print SCRIPT "$pbos->{'update'}\n";
4208 if (($vtype eq "ve") && ($vetype ne "docker")) {
4209 print SCRIPT "sudo /bin/umount /proc\n";
4210 }
4211 close(SCRIPT);
4212 chmod 0755,"$pbscript";
4213
4214 # Force shutdown of VM except
4215 pb_script2v($pbscript,$vtype,$pbstep,1,$v);
4216}
4217return;
4218}
4219
4220sub pb_announce {
4221
4222 my $antype = shift;
4223
4224 # Get all required parameters
4225 my ($pbpackager,$pbrepo) = pb_conf_get("pbpackager","pbrepo");
4226 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
4227 if (((not defined $testver) || (not defined $testver->{$ENV{'PBPROJ'}}) || ($testver->{$ENV{'PBPROJ'}} !~ /true/i)) && ($antype eq "Clean")) {
4228 # We just clean for test versions
4229 pb_log(0,"Unable to clean SSH repository for non testver version\n");
4230 return;
4231 }
4232 my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
4233 my @pkgs = @$pkg;
4234 my %pkgs;
4235 my $first = 0;
4236
4237 # Get all distros concerned
4238 my $pbos = pb_distro_get_context();
4239 my $distrolist = pb_get_distros($pbos,undef);
4240 my %dl;
4241 my %theorlist;
4242 my %archlist;
4243 foreach my $d (split(/,/,$distrolist)) {
4244 my ($d1,$d2,$d3) = split(/-/,$d);
4245 $dl{$d1}++;
4246 }
4247
4248 # Command to find packages on repo
4249 my $findstr = "find ".join(" ",keys %dl)." ";
4250 my $srcstr = "";
4251 # Generated announce files
4252 my @files;
4253
4254 foreach my $pbpkg (@pkgs) {
4255 if ($first != 0) {
4256 $findstr .= "-o ";
4257 }
4258 $first++;
4259 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
4260 $pbver = $pkgv->{$pbpkg};
4261 } else {
4262 $pbver = $ENV{'PBPROJVER'};
4263 }
4264 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
4265 $pbtag = $pkgt->{$pbpkg};
4266 } else {
4267 $pbtag = $ENV{'PBPROJTAG'};
4268 }
4269
4270 # TODO: use virtual/real names here now
4271 my $pbrealpkg = $pbpkg;
4272 my $pbrealpkgrpm = pb_cms_get_real_pkg($pbpkg,"rpm");
4273 my $pbrealpkgdeb = pb_cms_get_real_pkg($pbpkg,"deb");
4274 if ($antype eq "Clean") {
4275 # We only clean test versions anyway
4276 $pbtag = "0";
4277 my $nver = $pbver;
4278 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]";
4279 $pbver .= $ntag;
4280 $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\' ";
4281 $srcstr .= "src/$pbrealpkg-$pbver.tar.gz src/$pbrealpkg-$pbver.pbconf.tar.gz ";
4282 } else {
4283 # "Announce" and "Check"
4284 my @date=pb_get_date();
4285 # the matching is only done on packages made the same day for test version. Hopefully this is enough
4286 my $nver;
4287 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
4288 $pbtag = "0";
4289 my $ntag .= strftime("%Y%m%d*", @date);
4290 $nver = $pbver."_p$ntag*";
4291 $pbver .= "$ntag*";
4292 } else {
4293 $nver = $pbver."-r$pbtag";
4294 }
4295 $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\' ";
4296 }
4297
4298 if ($antype eq "Announce") {
4299 my $chglog;
4300
4301 pb_cms_init($pbinit);
4302 # Get project info on log file and generate tmp files used later on
4303 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
4304 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
4305 $chglog = undef if (! -f $chglog);
4306
4307 open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
4308 my $pb;
4309 $pb->{'realpkg'} = $pbrealpkg;
4310 $pb->{'ver'} = $pbver;
4311 $pb->{'tag'} = $pbtag;
4312 $pb->{'date'} = $pbdate;
4313 $pb->{'extdir'} = pb_get_extdir();
4314 $pb->{'chglog'} = $chglog;
4315 $pb->{'packager'} = $pbpackager;
4316 $pb->{'proj'} = $ENV{'PBPROJ'};
4317 $pb->{'repo'} = $pbrepo;
4318 $pb->{'pbos'}->{'type'} = "announce";
4319 $pb->{'pbos'}->{'suffix'} = "none";
4320 pb_changelog($pb,\*OUT,"yes");
4321 close(OUT);
4322 push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
4323 } elsif ($antype eq "Check") {
4324 # For the check we also build the theoritical complete list we should get
4325 foreach my $d (split(/,/,$distrolist)) {
4326 $pbos = pb_distro_get_context($d);
4327 if ($pbos->{'type'} eq "rpm") {
4328 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/$pbrealpkgrpm-$pbver-$pbtag$pbos->{'suffix'}"} = 0;
4329 } elsif ($pbos->{'type'} eq "deb") {
4330 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkgdeb"."_$pbver-$pbtag"} = 0;
4331 # TODO are we always using the last arch ?
4332 $archlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkgdeb"."_$pbver-$pbtag"} = "$pbos->{'arch'}";
4333 } elsif ($pbos->{'type'} eq "ebuild") {
4334 my $prefix = "-r";
4335 $prefix = "_p" if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i));
4336 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkg-$pbver$prefix$pbtag.ebuild"} = 0;
4337 $archlist{"$pbos->{'name'}/$pbos->{'version'}/$pbrealpkg-$pbver$prefix$pbtag.ebuild"} = "$pbos->{'arch'}";
4338 } elsif ($pbos->{'type'} eq "pkg") {
4339 $theorlist{"$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/$pbrealpkg-$pbver-$pbtag.pkg.gz"} = 0;
4340 } else {
4341 pb_log(1,"No theoritical list possible for type $pbos->{'type'}\n");
4342 }
4343 }
4344 }
4345 pb_log(2,"theorlist initial: ".Dumper(%theorlist)."\n");
4346 pb_log(2,"archlist: ".Dumper(%archlist)."\n");
4347 }
4348 if ($antype eq "Announce") {
4349 $findstr .= " | grep -Ev \'src.rpm\'";
4350 } elsif ($antype eq "Clean") {
4351 $findstr .= " | xargs rm -f -v $srcstr ";
4352 }
4353
4354 # Prepare the command to run and execute it
4355 open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
4356 print PBS "#!/bin/bash\n";
4357 print PBS "set -x\n" if ($pbdebug gt 1);
4358 print PBS "$findstr | sort 2> /dev/null\n";
4359 close(PBS);
4360 chmod 0755,"$ENV{'PBTMP'}/pbscript";
4361 pb_send2target("Announce","$ENV{'PBTMP'}/pbscript");
4362
4363 my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
4364 if ($antype eq "Announce") {
4365 # Get subject line
4366 pb_log(0,"Please enter the title of your announce\n");
4367 pb_log(0,"(By default: $sl)\n");
4368 my $sl2 = <STDIN>;
4369 $sl = $sl2 if ($sl2 !~ /^$/);
4370
4371 # Prepare a template of announce
4372 open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
4373 print ANN << "EOF";
4374$sl</p>
4375
4376<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
4377<p>
4378Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
4379</p>
4380<p>
4381EOF
4382 }
4383
4384 open(LOG,"$ENV{'PBTMP'}/system.$$.log") || die "Unable to read $ENV{'PBTMP'}/system.$$.log: $!";
4385 if ($antype eq "Announce") {
4386 my $col = 2;
4387 my $i = 1;
4388 print ANN << 'EOF';
4389<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0">
4390<TR>
4391EOF
4392 while (<LOG>) {
4393 print ANN "<TD><A HREF=\"$pbrepo->{$ENV{'PBPROJ'}}/$_\">$_</A></TD>";
4394 $i++;
4395 if ($i > $col) {
4396 print ANN "</TR>\n<TR>";
4397 $i = 1;
4398 }
4399 }
4400 } elsif ($antype eq "Clean") {
4401 while (<LOG>) {
4402 # skip errors
4403 next if ($_ !~ /^removed /);
4404 pb_log(0,"$_");
4405 }
4406 } else {
4407 # In "Check" mode we need to compare the 2 lists (real and theoritical)
4408 while (<LOG>) {
4409 # Get package name and remove what is in extra for the theoritical list (arch at the end)
4410 chomp();
4411 # skip find errors
4412 next if (/^find:/);
4413 my $p = $_;
4414 $p =~ s/\.(i[3456]86|x86_64|noarch|src)\.rpm$//;
4415 $p =~ s/_(i[3456]86|amd64|all)\.deb$//;
4416 $p =~ s/(-0\.[0-9]{8})[0-9]{6}/$1*/ if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i));
4417 $p =~ s/(-r|_p[0-9]+)\.ebuild/$1*/ if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i));
4418 $theorlist{$p} = -2 if (not defined $theorlist{$p});
4419 $theorlist{$p} = $theorlist{$p} + 1;
4420 }
4421 pb_log(2,"theorlist after check: ".Dumper(%theorlist)."\n");
4422 }
4423 close(LOG);
4424
4425 # Nothing more for the cleanssh case
4426 return if ($antype eq "Clean");
4427
4428 if ($antype eq "Check") {
4429 my ($chkex) = pb_conf_get_if("checkexclude");
4430 my $vmbuildlist = "";
4431 my $vebuildlist = "";
4432 my $rmbuildlist = "";
4433 my @pt = pb_conf_get_if("vmlist","velist","rmlist");
4434 foreach my $t (sort keys %theorlist) {
4435 if (defined $theorlist{$t} and $theorlist{$t} >= 1) {
4436 pb_log(1,"Packages found for $t\n");
4437 } elsif (defined $theorlist{$t} and $theorlist{$t} < 0) {
4438 pb_log(0,"Extra Package found for $t\n");
4439 } else {
4440 pb_log(2,"Analyzing $t\n");
4441 my ($os,$ver,$arch,$package) = split(/\//,$t);
4442 # Some distro have no arch subdir
4443 if (not defined $package) {
4444 $package = $arch;
4445 # TODO: If both arch have failed, we just make the last one
4446 $arch = $archlist{$t} if (defined $archlist{$t});
4447 }
4448 my $pbos = pb_distro_get_context("$os-$ver-$arch");
4449 my $pkgn = $package;
4450 if ($pbos->{'type'} ne "deb") {
4451 # package name is more easily found from the end for non deb
4452 # as '-' is the separator, but it can also be used in names
4453 $pkgn = reverse($package);
4454 # search the second '-' and isolate the now last part which is the full name
4455 $pkgn =~ s/([^-]+)-([^-]+)-([\S])+$/$3/;
4456 } else {
4457 $pkgn =~ s/([^_]+)_([\S])+$/$2/;
4458 }
4459 my $found = 0;
4460 # Handle the exclusion of OSes
4461 my $excl = "";
4462 $excl .= $chkex->{$pkgn} if (defined $chkex->{$pkgn});
4463 $excl .= $chkex->{"all"} if (defined $chkex->{"all"});
4464
4465 pb_log(2,"Searching in excl $excl\n");
4466 foreach my $ex (split(/,/,$excl)) {
4467 $found = 1 if ("$os-$ver-$arch" =~ /^$ex/);
4468 }
4469 # Skip as excluded
4470 next if ($found == 1);
4471 pb_log(0,"Package NOT found for $t\n");
4472 # Avoid duplicates in list
4473 next if ($vmbuildlist =~ /$os-$ver-$arch/);
4474 next if ($vebuildlist =~ /$os-$ver-$arch/);
4475 next if ($rmbuildlist =~ /$os-$ver-$arch/);
4476 # check with which method we need to build
4477 if ((defined $pt[0]->{$ENV{'PBPROJ'}}) and ($pt[0]->{$ENV{'PBPROJ'}} =~ /$os-$ver-$arch/)) {
4478 $vmbuildlist = "$os-$ver-$arch" if ($vmbuildlist eq "");
4479 $vmbuildlist .= ",$os-$ver-$arch" if ($vmbuildlist !~ /$os-$ver-$arch/);
4480 next;
4481 }
4482 if ((defined $pt[1]->{$ENV{'PBPROJ'}}) and ($pt[1]->{$ENV{'PBPROJ'}} =~ /$os-$ver-$arch/)) {
4483 $vebuildlist = "$os-$ver-$arch" if ($vebuildlist eq "");
4484 $vebuildlist .= ",$os-$ver-$arch" if ($vebuildlist !~ /$os-$ver-$arch/);
4485 next;
4486 }
4487 if ((defined $pt[2]->{$ENV{'PBPROJ'}}) and ($pt[2]->{$ENV{'PBPROJ'}} =~ /$os-$ver-$arch/)) {
4488 $rmbuildlist = "$os-$ver-$arch" if ($rmbuildlist eq "");
4489 $rmbuildlist .= ",$os-$ver-$arch" if ($rmbuildlist !~ /$os-$ver-$arch/);
4490 }
4491 }
4492 }
4493 # If we want to rebuild automatically, let's do it
4494 if (defined $opts{'rebuild'}) {
4495 # SandBox or CMS
4496 pb_log(0,"Rebuilding from SandBox\n");
4497 pb_log(0,"for VMs: $vmbuildlist\n") if ($vmbuildlist ne "");
4498 pb_log(0,"for VEs: $vebuildlist\n") if ($vebuildlist ne "");
4499 pb_log(0,"for RMs: $rmbuildlist\n") if ($rmbuildlist ne "");
4500 pb_cms2build("SandBox");
4501 # Which mode
4502 $ENV{'PBV'} = $vmbuildlist;
4503 pb_build2v("vm","build") if ($vmbuildlist ne "");
4504 $ENV{'PBV'} = $vebuildlist;
4505 pb_build2v("ve","build") if ($vebuildlist ne "");
4506 $ENV{'PBV'} = $rmbuildlist;
4507 pb_build2v("rm","build") if ($rmbuildlist ne "");
4508 }
4509 # For the check part this is now finished
4510 return;
4511 }
4512
4513 print ANN << "EOF";
4514</TR>
4515</TABLE>
4516</p>
4517
4518<p>As usual source packages are also available in the same directory.</p>
4519
4520<p>
4521Changes are :
4522</p>
4523<p>
4524EOF
4525 # Get each package changelog content
4526 foreach my $f (sort(@files)) {
4527 open(IN,"$f") || die "Unable to read $f:$!";
4528 while (<IN>) {
4529 print ANN $_;
4530 }
4531 close(IN);
4532 print ANN "</p><p>\n";
4533 }
4534 print ANN "</p>\n";
4535 close(ANN);
4536
4537 # Allow for modification
4538 my $editor = "vi";
4539 $editor = $ENV{'EDITOR'} if (defined $ENV{'EDITOR'});
4540 pb_system("$editor $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
4541
4542 # Store it in DB for external usage (Web pages generation)
4543 my $dbh = pb_connect_db();
4544
4545 # To read whole file
4546 local $/;
4547 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
4548 my $announce = <ANN>;
4549 close(ANN);
4550
4551 pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
4552 my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
4553 || die "Unable to insert into db";
4554 $sth->execute($pbdate, $announce);
4555 $sth->finish();
4556 $dbh->disconnect;
4557
4558 # Then deliver it on the Web
4559 # $TOOLHOME/livwww www
4560
4561 # Mail it to project's ML
4562 open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
4563 print ML << 'EOF';
4564<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
4565
4566<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
4567 <head>
4568 </head>
4569 <body>
4570 <p>
4571EOF
4572 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
4573 while(<ANN>) {
4574 print ML $_;
4575 }
4576 print ML << 'EOF';
4577</body>
4578</html>
4579EOF
4580 close(ML);
4581
4582 # To read whole file
4583 local $/;
4584 open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
4585 my $msg = <ANN>;
4586 close(ANN);
4587
4588 # Preparation of headers
4589 eval
4590 {
4591 require Mail::Sendmail;
4592 Mail::Sendmail->import();
4593 };
4594 if ($@) {
4595 # Mail::Sendmail not found not sending mail !
4596 pb_log(0,"No Mail::Sendmail module found so not sending any mail !\n");
4597 } else {
4598 my ($pbml,$pbsmtp) = pb_conf_get("pbml","pbsmtp");
4599 my %mail = (
4600 To => $pbml->{$ENV{'PBPROJ'}},
4601 From => $pbpackager->{$ENV{'PBPROJ'}},
4602 Smtp => $pbsmtp->{$ENV{'PBPROJ'}},
4603 Body => $msg,
4604 Subject => "[ANNOUNCE] $sl",
4605 );
4606
4607 # Send mail
4608 if (! sendmail(%mail)) {
4609 if ((defined $Mail::Sendmail::error) and (defined $Mail::Sendmail::log)) {
4610 die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
4611 }
4612 }
4613 }
4614}
4615
4616#
4617# Creates a set of HTML file containing the news for the project
4618# based on what has been generated by the pb_announce function
4619#
4620sub pb_web_news2html {
4621
4622 my $dest = shift || $ENV{'PBTMP'};
4623
4624 # Get all required parameters
4625 my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
4626
4627
4628 my $dbh = pb_connect_db();
4629 # For date handling
4630 $ENV{'LANGUAGE'}="C";
4631 my $firstjan = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year(), 0, 0, -1);
4632 my $oldfirst = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year()-1, 0, 0, -1);
4633 pb_log(2,"firstjan: $firstjan, oldfirst: $oldfirst, pbdate:$pbdate\n");
4634 my $all = $dbh->selectall_arrayref("SELECT id,date,announce FROM announces ORDER BY date DESC");
4635 my %news;
4636 $news{"cy"} = ""; # current year's news
4637 $news{"ly"} = ""; # last year news
4638 $news{"py"} = ""; # previous years news
4639 $news{"fp"} = ""; # first page news
4640 my $cpt = 4; # how many news for first page
4641 # Extract info from DB
4642 foreach my $row (@$all) {
4643 my ($id, $date, $announce) = @$row;
4644 $news{"cy"} = $news{"cy"}."<p><B>$date</B> $announce\n" if ((($date cmp $pbdate) le 0) && (($firstjan cmp $date) le 0));
4645 $news{"ly"} = $news{"ly"}."<p><B>$date</B> $announce\n" if ((($date cmp $firstjan) le 0) && (($oldfirst cmp $date) le 0));
4646 $news{"py"} = $news{"py"}."<p><B>$date</B> $announce\n" if (($date cmp $oldfirst) le 0);
4647 $news{"fp"} = $news{"fp"}."<p><B>$date</B> $announce\n" if ($cpt > 0);
4648 $cpt--;
4649 }
4650 pb_log(1,"news{fp}: ".$news{"fp"}."\n");
4651 $dbh->disconnect;
4652
4653 # Generate the HTML content
4654 foreach my $pref (keys %news) {
4655 open(NEWS,"> $dest/pb_web_$pref"."news.html") || die "Unable to create $dest/pb_web_$pref"."news.html: $!";
4656 print NEWS "$news{$pref}";
4657 close(NEWS);
4658 }
4659}
4660
4661sub pb_connect_db {
4662
4663 # Store in DB for external usage (Web pages generation)
4664 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
4665
4666 my $precmd = "";
4667 if (! -f $db) {
4668 $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
4669 # we should add the $db to the VCS as well here
4670 }
4671
4672 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
4673 { RaiseError => 1, AutoCommit => 1 })
4674 || die "Unable to connect to $db";
4675
4676 if ($precmd ne "") {
4677 my $sth = $dbh->prepare(qq{$precmd})
4678 || die "Unable to create table into $db";
4679 $sth->execute();
4680 }
4681
4682 return($dbh);
4683}
4684
4685
4686# Return the SSH key file to use
4687# Potentially create it if needed
4688
4689sub pb_ssh_get {
4690
4691my $create = shift || 0; # Do not create keys by default. In that case prefer native keys, not pb's ones
4692my $target = shift || "localhost";
4693my $nport = shift || "22";
4694
4695my ($pbagent) = pb_conf_get_if("pbusesshagent");
4696# use ssh-agent if asked so.
4697return(undef) if (($create == 0) && (defined $pbagent->{$ENV{'PBPROJ'}}) && ($pbagent->{$ENV{'PBPROJ'}} =~ /true/io));
4698
4699# Check the SSH environment
4700my $keyfile = undef;
4701
4702# Check whether ecdsa is accepted by the remote host
4703my $ecdsa = 0;
4704my $dsa = 0;
4705open(SCAN,"ssh-keyscan -p $nport -t ecdsa $target 2>/dev/null |") || pb_log(0,"Unable to check ECDSA support for $target");
4706while(<SCAN>) {
4707 $ecdsa = 1 if ($_ =~ /ecdsa-/);
4708 pb_log(2,"Found ECDSA support on $target ($nport)\n");
4709}
4710close(SCAN);
4711
4712open(SCAN,"ssh-keyscan -p $nport -t dsa $target 2>/dev/null |") || pb_log(0,"Unable to check DSA support for $target");
4713while(<SCAN>) {
4714 $dsa = 1 if ($_ =~ /ssh-dss/);
4715 pb_log(2,"Found DSA support on $target ($nport)\n");
4716}
4717close(SCAN);
4718
4719# We have specific keys by default - create them if needed
4720if (!(-e "$ENV{'HOME'}/.ssh/pb_ecdsa") && ($create eq 1)) {
4721 my $res = pb_system("ssh-keygen -q -b 521 -N '' -f $ENV{'HOME'}/.ssh/pb_ecdsa -t ecdsa","Generating ECDSA SSH keys for pb");
4722 if (($res != 0) && !(-e "$ENV{'HOME'}/.ssh/pb_dsa") && ($create eq 1)) {
4723 # ecdsa may not be supported if old ssh
4724 pb_system("ssh-keygen -q -b 1024 -N '' -f $ENV{'HOME'}/.ssh/pb_dsa -t dsa","Generating DSA SSH keys for pb");
4725 }
4726}
4727
4728#$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
4729# We still favoud DSA and fall back to ECDSA if not available
4730if ($dsa != 0) {
4731 # And we favour a specific key over a generic one except if in delivery to ftp server
4732 $keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if ((-s "$ENV{'HOME'}/.ssh/id_dsa") && ($create eq 0));
4733 $keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
4734} else {
4735 # And we favour a specific key over a generic one except if in delivery to ftp server
4736 $keyfile = "$ENV{'HOME'}/.ssh/id_ecdsa" if ((-s "$ENV{'HOME'}/.ssh/id_ecdsa") && ($ecdsa != 0) && ($create eq 0));
4737 $keyfile = "$ENV{'HOME'}/.ssh/pb_ecdsa" if ((-s "$ENV{'HOME'}/.ssh/pb_ecdsa") && ($ecdsa != 0));
4738}
4739if (defined $keyfile) {
4740 pb_log(2,"Using ssh key file $keyfile\n");
4741} else {
4742 pb_log(0, "Unable to find your public ssh key under $ENV{'HOME'}/.ssh\n");
4743}
4744return($keyfile);
4745}
4746
4747
4748# Returns the pid of a running VM command using a specific VM file, and the port used if the VM exists
4749sub pb_check_ps {
4750 my $vmcmd = shift;
4751 my $vmm = shift;
4752 my $vexist = 0; # FALSE by default
4753 my $vmport = undef; # NONE by default
4754
4755 open(PS, "ps auxhww|") || die "Unable to call ps";
4756 while (<PS>) {
4757 next if (! /$vmcmd/);
4758 next if (! /$vmm/);
4759 my $void1;
4760 ($void1, $vexist, $vmport) = split(/ +/,$_,3);
4761 pb_log(2,"pb_check_ps $vmport\n");
4762 $vmport =~ s/.*=tcp:127\.0\.0\.1:([0-9]+)-:22.*/$1/;
4763 chomp($vmport);
4764 pb_log(2,"pb_check_ps found a VM $vexist using port $vmport\n");
4765 last;
4766 }
4767 return($vexist,$vmport);
4768}
4769
4770
4771sub pb_extract_build_files {
4772
4773my $src=shift;
4774my $dir=shift;
4775my $ddir=shift;
4776my $mandatory=shift || "build";
4777
4778my $flag = "mayfail" if (($mandatory eq "patch") || ($mandatory eq "src"));
4779my $res;
4780
4781if ($src =~ /tar\.gz$/) {
4782 $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
4783} elsif ($src =~ /tar\.bz2$/) {
4784 $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
4785} else {
4786 die "Unknown compression algorithm for $src";
4787}
4788# If not mandatory return now
4789return() if (($res != 0) and (($mandatory eq "patch") || ($mandatory eq "src")));
4790return(pb_move_extracted_files($dir,$ddir));
4791}
4792
4793sub pb_move_extracted_files {
4794
4795my $dir = shift;
4796my $ddir = shift;
4797my @files;
4798
4799# In a recursive function , we need a local var as DIR handle
4800my $bdir;
4801
4802opendir($bdir,"$dir") || confess "Unable to open directory $dir: $!";
4803foreach my $f (readdir($bdir)) {
4804 next if ($f =~ /^\./);
4805 # Skip potential patch dir
4806 next if ($f =~ /^pbpatch/);
4807 # Skip potential source dir
4808 next if ($f =~ /^pbsrc/);
4809 # Skip potential backup files
4810 next if ($f =~ /~$/);
4811 if ((-d "$dir/$f") && (! -l "$dir/$f")) {
4812 # Case of the bug debian subdir
4813 pb_mkdir_p("$ddir/$f");
4814 push @files,pb_move_extracted_files("$dir/$f","$ddir/$f");
4815 } else {
4816 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
4817 pb_log(2,"mv $dir/$f $ddir\n");
4818 push @files,"$ddir/$f";
4819 }
4820}
4821closedir($bdir);
4822# Not enough but still a first cleanup
4823pb_rm_rf("$dir");
4824return(@files);
4825}
4826
4827sub pb_list_bfiles {
4828
4829my $dir = shift;
4830my $pbpkg = shift;
4831my $bfiles = shift;
4832my $pkgfiles = shift;
4833my $supfiles = shift;
4834# subdir to keep if recursive mode, empty by default
4835my $subdir = shift || "";
4836# In a recursive function , we need a local var as DIR handle
4837my $bdir;
4838
4839pb_log(2,"DEBUG: entering pb_list_bfiles in $dir: ".Dumper($bfiles)."\n");
4840opendir($bdir,"$dir") || die "Unable to open dir $dir: $!";
4841foreach my $f (readdir($bdir)) {
4842 pb_log(3,"DEBUG: pb_list_bfiles found $f\n");
4843 next if ($f =~ /^\./);
4844 if (-d "$dir/$f") {
4845 # Recurse for directories (Debian 3.0 format e.g.)
4846 pb_log(2,"DEBUG: pb_list_bfiles recurse in $dir/$f\n");
4847 pb_list_bfiles("$dir/$f",$pbpkg,$bfiles,$pkgfiles,$supfiles,$f);
4848 next;
4849 }
4850
4851 my $key = $f;
4852 # if recursive then store also the subdir
4853 $key = "$subdir/$f" if ($subdir ne "");
4854 $bfiles->{$key} = "$dir/$f";
4855 $bfiles->{$key} =~ s~$ENV{'PBROOTDIR'}~~;
4856 if (defined $supfiles->{$pbpkg}) {
4857 $pkgfiles->{$key} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
4858 }
4859}
4860closedir($bdir);
4861pb_log(2,"DEBUG: exiting pb_list_bfiles: ".Dumper($bfiles)."\n");
4862}
4863
4864sub pb_add_comma {
4865
4866my $str = shift;
4867my $addstr = shift;
4868
4869$str .= "," if (defined $str);
4870$str .= $addstr;
4871return($str);
4872}
4873
4874sub pb_list_sfiles {
4875
4876my $sdir = shift;
4877my $ptr = shift;
4878my $pbos = shift;
4879my $extdir = shift;
4880
4881pb_log(2,"DEBUG: entering pb_list_sfiles: ".Dumper($ptr)."\n");
4882my $key = "$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}";
4883
4884# Prepare local sources for this distro - They are always applied first - May be a problem one day
4885# This function works for both patches and additional sources
4886foreach my $p (sort(<$sdir/*>)) {
4887 $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'}$/));
4888}
4889
4890# Prepare also remote sources to be included - Applied after the local ones
4891foreach my $p ("all","$pbos->{'os'}","$pbos->{'type'}","$pbos->{'family'}","$pbos->{'name'}","$pbos->{'name'}-$pbos->{'version'}","$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}") {
4892 my $f = "$extdir.".".$p";
4893 next if (not -f $f);
4894 if (not open(PATCH,$f)) {
4895 pb_display("Unable to open existing external source file content $f\n");
4896 next;
4897 }
4898 while (<PATCH>) {
4899 chomp();
4900 $ptr->{$key} = pb_add_comma($ptr->{$key},"$_");
4901 }
4902 close(PATCH);
4903}
4904pb_log(2,"DEBUG: exiting pb_list_sfiles: ".Dumper($ptr)."\n");
4905return($ptr);
4906}
4907
4908#
4909# Return the list of packages we are working on in a non CMS action
4910#
4911sub pb_get_pkg {
4912
4913my @pkgs = ();
4914
4915# In script actions that file may not exist.
4916return(\@pkgs) if ((not defined $ENV{'PBDESTDIR'}) || (not defined $ENV{'PBPROJVER'}) || (not defined $ENV{'PBPROJTAG'}) || (! -f "$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.yml"));
4917
4918my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.yml","pbpkg");
4919@pkgs = keys %$var if (defined $var);
4920
4921pb_log(0,"Packages: ".join(',',@pkgs)."\n") if (@pkgs);
4922return(\@pkgs);
4923}
4924
4925# Manages VM/RM SSH port communication
4926sub pb_get_port {
4927
4928my $port = shift;
4929my $pbos = shift;
4930my $cmt = shift;
4931my $nport;
4932
4933die "No port passed in parameter. Report to dev team\n" if (not defined $port);
4934# key is project on VM, but machine tuple for RM
4935if ($cmt =~ /^RM/i) {
4936 $nport = $port->{"$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}"};
4937} else {
4938 $nport = $port->{$ENV{'PBPROJ'}};
4939}
4940pb_log(2,"pb_get_port with $nport\n");
4941if ($cmt =~ /^VM/i) {
4942 # Maybe a port was given as parameter so overwrite
4943 $nport = "$pbport" if (defined $pbport);
4944 # Maybe in // mode so use the env var set up as an offset to the base port
4945 $nport = $nport + $ENV{'PBVMPORT'} -1 if ((defined $pbparallel) && (defined $ENV{'PBVMPORT'}));
4946}
4947pb_log(2,"pb_get_port returns $nport\n");
4948return($nport);
4949}
4950
4951sub pb_set_port {
4952
4953my ($pid,$ident) = @_;
4954pb_log(2,"pb_set_port for VM ($pid), id $ident\n");
4955$ENV{'PBVMPORT'} = $ident;
4956pb_log(2,"pb_set_port sets PBVMPORT in env to $ENV{'PBVMPORT'}\n");
4957}
4958
4959sub pb_set_parallel {
4960
4961my $vtype = shift;
4962
4963pb_log(2,"pb_set_parallel vtype: $vtype\n");
4964# Take care of memory size if VM, parallel mode and more than 1 action
4965if ((defined $pbparallel) && ($pbparallel ne 1) && ($vtype eq "vm")) {
4966 eval
4967 {
4968 require Linux::SysInfo;
4969 Linux::SysInfo->import();
4970 };
4971 if ($@) {
4972 # Linux::SysInfo not found
4973 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");
4974 } else {
4975 # Using the memory size
4976 my $si = Linux::SysInfo::sysinfo();
4977 if (not defined $si) {
4978 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");
4979 } else {
4980 # Keep the number of VM whose memory can be allocated
4981 my $ram = $si->{"totalram"}-$si->{"sharedram"}-$si->{"bufferram"};
4982 my $ram2;
4983 my ($vmmem) = pb_conf_get_if("vmmem");
4984
4985 my $v = "default";
4986 if ((defined $vmmem) and (defined $vmmem->{$v})) {
4987 $ram2 = $vmmem->{$v};
4988 } else {
4989 # Default for KVM/QEMU
4990 $ram2 = 128;
4991 }
4992 $pbparallel = sprintf("%d",$ram/$ram2);
4993 }
4994 pb_log(1,"Using $pbparallel processes at a time\n");
4995 }
4996}
4997pb_log(2,"pb_set_parallel returns: $pbparallel\n") if (defined $pbparallel);
4998return($pbparallel);
4999}
5000
5001sub pb_get_sudocmds {
5002
5003my $pbos = shift;
5004my @cmds = @_;
5005my %sudocmds;
5006
5007pb_log(2,"pb_get_sudocmds entering with lines:".Dumper(@cmds)."\n");
5008foreach my $c (split(/;/,$pbos->{'update'}),split(/;/,$pbos->{'install'}),@cmds) {
5009 pb_log(2,"pb_get_sudocmds analyses $c\n");
5010 next if ($c !~ /^\s*sudo/);
5011 # remove sudo and leading spaces
5012 $c =~ s/^\s*sudo\s+//;
5013 # keep only the command, not the params
5014 $c =~ s/([^\s]+)\s.*$/$1/;
5015 $sudocmds{$c} = "";
5016}
5017pb_log(2,"pb_get_sudocmds returns ".Dumper(keys %sudocmds)."\n");
5018return(keys %sudocmds);
5019}
5020
5021sub pb_sign_setenv {
5022
5023my ($passfile, $passphrase, $passpath) = pb_conf_get_if("pbpassfile","pbpassphrase","pbpasspath");
5024$ENV{'PBPASSPHRASE'} = $passphrase->{$ENV{'PBPROJ'}} if ((not defined $ENV{'PBPASSPHRASE'}) && (defined $passphrase->{$ENV{'PBPROJ'}}));
5025$ENV{'PBPASSFILE'} = $passfile->{$ENV{'PBPROJ'}} if ((not defined $ENV{'PBPASSFILE'})&& (defined $passfile->{$ENV{'PBPROJ'}})) ;
5026$ENV{'PBPASSPATH'} = $passpath->{$ENV{'PBPROJ'}} if ((not defined $ENV{'PBPASSPATH'})&& (defined $passpath->{$ENV{'PBPROJ'}})) ;
5027}
5028
5029sub pb_sign_setenv_rpm {
5030
5031pb_sign_setenv();
5032
5033eval
5034{
5035 require RPM4::Sign;
5036 RPM4::Sign->import();
5037};
5038if ($@) {
5039 # RPM4::Sign not found
5040 pb_log(1,"WARNING: Install RPM4::Sign to benefit from automatic package signing.\n");
5041 return(undef);
5042} else {
5043 return(undef) if (((not defined $ENV{'PBPASSPHRASE'}) and (not defined $ENV{'PBPASSFILE'})) || (not defined $ENV{'PBPACKAGER'}) || (not defined $ENV{'PBPASSPATH'}));
5044 my $sign = RPM4::Sign->new(
5045 passphrase => $ENV{'PBPASSPHRASE'},
5046 name => $ENV{'PBPACKAGER'},
5047 path => $ENV{'PBPASSPATH'},
5048 password_file => $ENV{'PBPASSFILE'},
5049 );
5050 return(undef) if (not defined $sign);
5051 return(undef) if (not defined $sign->{name});
5052 return(undef) if (not defined $sign->{path});
5053 return(undef) if ((not defined $sign->{passphrase}) && (not defined $sign->{password_file}));
5054 return(undef) if (not defined $sign->{keyid});
5055
5056 return($sign);
5057}
5058}
5059
5060# Cf: http://wiki.debian.org/SecureApt
5061
5062sub pb_sign_file {
5063
5064my $file = shift;
5065
5066pb_sign_setenv();
5067pb_log(0,"Signing Debian Release file as $ENV{'PBPACKAGER'}...\n");
5068
5069if (not defined $ENV{'PBPASSPHRASE'}) {
5070 if (not defined $ENV{'PBPASSFILE'}) {
5071 confess("No pbpassphrase nor pbpassfile defined, unable to handle GPG Release file\n");
5072 } else {
5073 open(PASS,"$ENV{'PBPASSFILE'}") || confess("Unable to open $ENV{'PBPASSFILE'}\n");
5074 my $pass = <PASS>;
5075 chomp($pass);
5076 $ENV{'PBPASSPHRASE'} = $pass;
5077 close(PASS);
5078 }
5079}
5080
5081unlink("$file.gpg");
5082pb_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");
5083}
5084
5085sub pb_sign_pkgs {
5086
5087my $pbos = shift;
5088my $made = shift;
5089
5090pb_log(2,"entering pb_sign_pkg: $made ".Dumper($pbos)."\n");
5091# Remove extra spaces
5092$made =~ s/\s+/ /g;
5093$made =~ s/^\s//g;
5094$made =~ s/\s$//g;
5095
5096if ($pbos->{'type'} eq "rpm") {
5097 my $sign = pb_sign_setenv_rpm();
5098 if (defined $sign) {
5099 pb_log(0,"Signing RPM packages as $ENV{'PBPACKAGER'}...\n");
5100 pb_log(2,"pb_sign_pkg: pkgs:".Dumper(split(/ /,$made))."\n");
5101 $sign->rpmssign(split(/ /,$made));
5102 }
5103} elsif ($pbos->{'type'} eq "deb") {
5104 my $changes = "";
5105 foreach my $c (split(/ /,$made)) {
5106 $changes .= " $ENV{'PBBUILDDIR'}/$c" if (($c =~ /\.changes$/) && (-f "$ENV{'PBBUILDDIR'}/$c"));
5107 }
5108 my $debsigncmd = pb_check_req("debsign",1);
5109 pb_system("$debsigncmd -m\'$ENV{'PBPACKAGER'}\' $changes","Signing DEB packages as $ENV{'PBPACKAGER'}...","mayfail") if (($changes ne "") && (defined $debsigncmd));
5110} else {
5111 pb_log(0,"I don't know yet how to sign packages for type $pbos->{'type'}.\nPlease give feedback to dev team\n");
5112}
5113pb_log(2,"exiting pb_sign_pkg\n");
5114}
5115
5116# return list of all distributions supported, comma separated
5117sub pb_get_distros {
5118
5119my $pbos = shift;
5120my $pbtarget = shift;
5121
5122my @dists = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}");
5123
5124# Get list of distributions for which we need to generate build files if no target
5125if (not defined $pbtarget) {
5126 my @pt = pb_conf_get_if("vmlist","velist","rmlist");
5127 foreach my $pt (@pt) {
5128 push(@dists, split(/,/, $pt->{$ENV{'PBPROJ'}})) if defined $pt->{$ENV{'PBPROJ'}};
5129 }
5130 # remove any whitespace
5131 grep(s/\s+//go, @dists);
5132}
5133return(join(",",@dists));
5134}
5135
5136sub pb_get_extdir {
5137
5138 # the pbrc.yml file should contain it and whatever the key, we take it
5139 my ($ed) = pb_conf_read("$ENV{'PBDESTDIR'}/pbrc.yml","pbextdir");
5140 pb_log(2,"ed: ".Dumper($ed)."\n");
5141 my $pbextdir = "";
5142 foreach my $k (keys %$ed) {
5143 $pbextdir = $ed->{$k};
5144 # In case we have an empty field, empty it completely
5145 pb_log(2,"pbextdir: ***$pbextdir***\n");
5146 $pbextdir =~ s/^\s*$//;
5147 }
5148 pb_log(2,"pbextdir: ***$pbextdir***\n");
5149 return($pbextdir);
5150}
5151
5152# Unused now
5153sub pb_keep_step {
5154
5155my $pbos = shift;
5156my $pbstep = shift;
5157
5158$pbstep = 3 if (not defined $pbstep);
5159
5160my %h;
5161my $h = \%h;
5162$h = pb_conf_cache("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.yml",$h);
5163$h->{'pbstep'}->{"$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}"} = $pbstep;
5164pb_conf_write("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.yml",$h);
5165}
5166
5167sub create_solaris_prototype {
5168
5169 my $uidgid = "bin bin";
5170 my $pkgdestdir = $ENV{'PBSOLDESTDIR'};
5171
5172 return if ($_ =~ /^$pkgdestdir$/);
5173 if (-d $_) {
5174 my $n = $File::Find::name;
5175 $n =~ s~$pkgdestdir/~~;
5176 print PROTO "d none $n 0755 $uidgid\n";
5177 } elsif (-x $_) {
5178 my $n = $File::Find::name;
5179 $n =~ s~$pkgdestdir/~~;
5180 print PROTO "f none $n 0755 $uidgid\n";
5181 } elsif (-f $_) {
5182 my $n = $File::Find::name;
5183 $n =~ s~$pkgdestdir/~~;
5184 print PROTO "f none $n 0644 $uidgid\n";
5185 }
5186}
5187
5188sub pb_ssh_setup {
5189
5190my $vtype = shift;
5191my $nport = shift;
5192
5193my $keyfile;
5194my $vmhost;
5195
5196pb_log(1,"Entering pb_ssh_setup\n");
5197($vmhost) = pb_conf_get($vtype."host");
5198# Prepare the key to be used and transfered remotely
5199$keyfile = pb_ssh_get(1,$vmhost->{$ENV{'PBPROJ'}},$nport);
5200return if (not defined $keyfile);
5201
5202# We call true to avoid problems if SELinux is not activated, but chcon is present and returns in that case 1
5203pb_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");
5204# once this is done, we can do what we need on the VM/RM remotely
5205
5206# in particular we can remove duplicate in .ssh/authorized_keys of our key if needed
5207# Store the pub key part in a variable
5208open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
5209my ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
5210close(FILE);
5211my $key = "\Q$zero1";
5212my $perls = "$ENV{'PBTMP'}/perls.$$";
5213
5214open(FILE,"> $perls") || die "Unable to open $perls";
5215print FILE << 'EOF';
5216my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
5217open(PBFILE,$file1) || die "Unable to open $file1";
5218open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
5219my $count = 0;
5220while (<PBFILE>) {
5221EOF
5222print FILE << "EOF";
5223 if (/ $key /) {
5224 \$count++;
5225 }
5226 print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
5227}
5228close(PBFILE);
5229close(PBOUT);
5230rename("\$file1.new",\$file1);
5231chmod 0600,\$file1;
5232EOF
5233close(FILE);
5234pb_system("cat $perls | ssh -q -o UserKnownHostsFile=/dev/null -p $nport -i $keyfile root\@$vmhost->{$ENV{'PBPROJ'}} perl","","quiet");
5235return;
5236}
5237
5238# Returns debian architecture
5239#
5240sub pb_get_debarch {
5241
5242my $pbos = shift;
5243
5244my $debarch = $pbos->{'arch'};
5245$debarch = "amd64" if ($pbos->{'arch'} eq "x86_64");
5246
5247return($debarch);
5248}
5249
5250sub pb_get_debpc {
5251
5252my ($projcomponent_map) = pb_conf_get_if("projcomponent");
5253pb_log(2,"projcomponent = ".Dumper($projcomponent_map)."\n");
5254my $projcomponent = $projcomponent_map->{$ENV{'PBPROJ'}};
5255$projcomponent ||= 'contrib';
5256
5257return($projcomponent);
5258}
5259
52601;
Note: See TracBrowser for help on using the repository browser.