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

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

Fix usage of labels in env var (normalize with 'LABEL'

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