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

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