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

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

Avoid parallelism issues with confess in subfunction

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