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

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