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

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