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

Last change on this file since 1402 was 1402, checked in by Bruno Cornec, 12 years ago

r4487@cabanilles: bruno | 2012-02-01 16:21:48 +0100

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