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

Last change on this file since 1909 was 1909, checked in by Bruno Cornec, 10 years ago
  • Some improvements for build2ve for docker. Remains to handle the correct launch of the script (pb of multiple commands for now) and of

the volumes due to different UIDs between host and guest. I may need 4
volumes for that

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