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

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