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

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