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

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