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

Last change on this file since 2427 was 2427, checked in by Bruno Cornec, 5 years ago

Improve install2pkg when repo already configured and use the new pb_distro_installpkgs function

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