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

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