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

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