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

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