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

Last change on this file since 1140 was 1140, checked in by Bruno Cornec, 13 years ago
  • Adds 2 new functions sbx2setupve|vm to update an in VE|VM incompatible version of pb to latest needed to dialog correctly.
  • Fix a now incorrect call to pbscript in pb_setup2v
  • Property svn:executable set to *
File size: 110.2 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;
1672 if ($cmt !~ /^VE/) {
1673 $nport = $sshport->{$ENV{'PBPROJ'}};
1674 $nport = "$pbport" if (defined $pbport);
1675 }
1676
1677 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
1678 $tdir =~ s|\$ENV.+\}/||;
1679
1680 my $tm = undef;
1681 if ($cmt =~ /^VM/) {
1682 $tm = $vtmout->{$ENV{'PBPROJ'}};
1683 }
1684
1685 # ssh communication if not VE
1686 # should use a hash instead...
1687 my ($shcmd,$cpcmd,$cptarget,$cp2target);
1688 if ($cmt !~ /^VE/) {
1689 my $keyfile = pb_ssh_get(0);
1690 $shcmd = "ssh -i $keyfile -q -o UserKnownHostsFile=/dev/null -p $nport $mac";
1691 $cpcmd = "scp -i $keyfile -p -o UserKnownHostsFile=/dev/null -P $nport";
1692 $cptarget = "$mac:$tdir";
1693 if ($cmt =~ /^VMbuild/) {
1694 $cp2target = "$mac:$bdir";
1695 }
1696 } else {
1697 my $tp = $vepath->{$ENV{'PBPROJ'}};
1698 ($odir,$over,$oarch) = split(/-/,$v);
1699 my $tpdir = "$tp/$odir/$over/$oarch";
1700 my ($ptr) = pb_conf_get("vetype");
1701 my $vetype = $ptr->{$ENV{'PBPROJ'}};
1702 if ($vetype eq "chroot") {
1703 $shcmd = "sudo chroot $tpdir /bin/su - $mac -c ";
1704 } elsif ($vetype eq "schroot") {
1705 $shcmd = "schroot $tp -u $mac -- ";
1706 }
1707 $cpcmd = "sudo cp -r ";
1708 # We need to get the home dir of the target account to deliver in the right place
1709 open(PASS,"$tpdir/etc/passwd") || die "Unable to open $tpdir/etc/passwd";
1710 my $homedir = "";
1711 while (<PASS>) {
1712 my ($c1,$c2,$c3,$c4,$c5,$c6,$c7) = split(/:/);
1713 $homedir = $c6 if ($c1 =~ /^$mac$/);
1714 pb_log(3,"Homedir: $homedir - account: $c6\n");
1715 }
1716 close(PASS);
1717 $cptarget = "$tpdir/$homedir/$tdir";
1718 if ($cmt eq "VEbuild") {
1719 $cp2target = "$tpdir/$homedir/$bdir";
1720 }
1721 pb_log(2,"On VE using $cptarget as target dir to copy to\n");
1722 }
1723
1724 my $logres = "";
1725 # Do not touch when just announcing
1726 if ($cmt ne "Announce") {
1727 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");
1728 } else {
1729 $logres = "> ";
1730 }
1731 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
1732
1733 # For VE we need to change the owner manually
1734 if ($cmt =~ /^VE/) {
1735 pb_system("$shcmd \"sudo chown -R $mac $tdir\"","Adapt owner in $tdir to $mac");
1736 }
1737
1738 # Use the right script name depending on context
1739 my $pbscript;
1740 if ($cmt =~ /^V[EM]/) {
1741 $pbscript = "pbscript.$$";
1742 } else {
1743 $pbscript = "pbscript";
1744 }
1745
1746 pb_system("$shcmd \"echo \'cd $tdir ; if [ -x $pbscript ]; then ./$pbscript; fi ; rm -f ./$pbscript\' | bash\"","Executing pbscript on $cptarget if needed","verbose");
1747 if ($cmt =~ /^V[EM]build/) {
1748 # Get back info on pkg produced, compute their name and get them from the VM
1749 pb_system("$cpcmd $cp2target/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'} $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$ 2> /dev/null","Get package names in $cp2target");
1750 # For VE we need to change the owner manually
1751 if ($cmt eq "VEbuild") {
1752 pb_system("sudo chown $UID $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$","Adapt owner in $tdir to $UID");
1753 }
1754 # return here to avoid breaking the load on VMs/VEs in case of a bad one
1755 if (not -f "$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$") {
1756 pb_log(0,"Problem with VM $v on $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$");
1757 return;
1758 }
1759 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$";
1760 my $src = <KEEP>;
1761 chomp($src);
1762 close(KEEP);
1763 unlink("$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.$$");
1764
1765 $src =~ s/^ *//;
1766 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
1767 # Change pgben to make the next send2target happy
1768 my $made = "";
1769
1770 # For VM we don't want shell expansion to hapen locally but remotely
1771 my $delim = '\'';
1772 if ($cmt =~ /^VEbuild/) {
1773 # For VE we need to support shell expansion locally
1774 $delim = "";
1775 }
1776
1777 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1778 foreach my $p (split(/ +/,$src)) {
1779 my $j = basename($p);
1780 pb_system("$cpcmd $cp2target/$delim$p$delim $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Recovery of package $j in $ENV{'PBBUILDDIR'}/$odir/$over");
1781 $made="$made $odir/$over/$j"; # if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
1782 }
1783 print KEEP "$made\n";
1784 close(KEEP);
1785 pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
1786
1787 # Sign packages locally
1788 if ($dtype eq "rpm") {
1789 #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");
1790 } elsif ($dtype eq "deb") {
1791 #pb_system("debsign $made","Signing DEB packages");
1792 } else {
1793 pb_log(0,"I don't know yet how to sign packages for type $dtype.\nPlease give feedback to dev team\n");
1794 }
1795
1796 # We want to send them to the ssh account so overwrite what has been done before
1797 undef $pbaccount;
1798 pb_log(2,"Before sending pkgs, vmexist: $vmexist, vmpid: $vmpid\n");
1799 pb_send2target("Packages",$odir."-".$over."-".$oarch,$vmexist,$vmpid);
1800 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
1801 }
1802 unlink("$ENV{'PBDESTDIR'}/pbscript.$$") if ($cmt =~ /^V[ME]/);
1803
1804 pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
1805 if ((! $vmexist) && ($cmt =~ /^VM/)) {
1806 # If in setupvm then takes a snapshot just before halting
1807 if ($snapme != 0) {
1808 my ($vmmonport,$vmtype) = pb_conf_get("vmmonport","vmtype");
1809 # For monitoring control
1810 if ((($vmtype->{$ENV{'PBPROJ'}}) eq "kvm") || (($vmtype->{$ENV{'PBPROJ'}}) eq "qemu")) {
1811 require Net::Telnet;
1812 my $t = new Net::Telnet (Timeout => 120, Host => "localhost", Port => $vmmonport->{$ENV{'PBPROJ'}}) || die "Unable to dialog on the monitor";
1813 # move to monitor mode
1814 my @lines = $t->cmd("c");
1815 # Create a snapshot named pb
1816 @lines = $t->cmd("savevm pb");
1817 # Write the new status in the VM
1818 @lines = $t->cmd("commit all");
1819 # End
1820 @lines = $t->cmd("quit");
1821 }
1822 }
1823 my $hoption = "-p";
1824 my $hpath = "/sbin";
1825 # Solaris doesn't support -h and has halt elsewhere
1826 if ($dtype eq "pkg") {
1827 $hoption = "" ;
1828 $hpath = "/usr/sbin";
1829 }
1830 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)");
1831 }
1832 if (($cmt =~ /^VE/) && ($snapme != 0)) {
1833 ($odir,$over,$oarch) = split(/-/,$v);
1834 my $tpdir = "$vepath->{$ENV{'PBPROJ'}}/$odir/$over/$oarch";
1835 pb_system("sudo tar cz -f $vepath->{$ENV{'PBPROJ'}}/$odir-$over-$oarch.tar.gz -C $tpdir .","Creating a snapshot of $tpdir");
1836 }
1837}
1838
1839sub pb_script2v {
1840 my $pbscript=shift;
1841 my $vtype=shift;
1842 my $pbforce=shift || 0; # Force stop of VM. Default not.
1843 my $vm1=shift || undef; # Only that VM to treat. Default all.
1844 my $snapme=shift || 0; # Do we have to create a snapshot. Default not.
1845 my $vm;
1846 my $all;
1847
1848 pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$pbforce,".Dumper($vm1).",$snapme)\n");
1849 # Prepare the script to be executed on the VM
1850 # in $ENV{'PBDESTDIR'}/pbscript.$$
1851 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript.$$")) {
1852 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript.$$") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript.$$";
1853 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript.$$";
1854 }
1855
1856 if (not defined $vm1) {
1857 ($vm,$all) = pb_get2v($vtype);
1858 } else {
1859 @$vm = ($vm1);
1860 }
1861 my ($vmexist,$vmpid) = (undef,undef);
1862
1863 foreach my $v (@$vm) {
1864 # Launch VM/VE
1865 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,$snapme,$pbsnap);
1866
1867 if ($vtype eq "vm") {
1868 pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
1869
1870 # Skip that VM if something went wrong
1871 next if (($vmpid == 0) && ($vmexist == 0));
1872
1873 # If force stopping the VM then reset vmexist
1874 if ($pbforce == 1) {
1875 $vmpid = $vmexist;
1876 $vmexist = 0;
1877 }
1878 } else {
1879 #VE
1880 $vmexist = 0;
1881 $vmpid = 0;
1882 }
1883
1884 # Gather all required files to send them to the VM
1885 # and launch the build through pbscript
1886 pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
1887 pb_send2target(uc($vtype)."Script","$v",$vmexist,$vmpid,$snapme);
1888
1889 }
1890}
1891
1892sub pb_launchv {
1893 my $vtype = shift;
1894 my $v = shift;
1895 my $create = shift || 0; # By default do not create a VM/VE
1896 my $snapme = shift || 0; # By default do not snap a VM/VE
1897 my $usesnap = shift || 1; # By default study the usage of the snapshot feature of VM/VE
1898
1899 # If creation or snapshot creation mode, no snapshot usable
1900 if (($create == 1) || ($snapme == 1)) {
1901 $usesnap = 0;
1902 }
1903
1904 pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create,$snapme,$usesnap)\n");
1905 die "No VM/VE defined, unable to launch" if (not defined $v);
1906 # Keep only the first VM in case many were given
1907 $v =~ s/,.*//;
1908
1909 my $arch = pb_get_arch();
1910
1911 # Launch the VMs/VEs
1912 if ($vtype eq "vm") {
1913 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
1914
1915 # TODO: vmmonport should be optional
1916 my ($ptr,$ptr2,$vmpath,$vmport,$vmsize,$vmmonport) = pb_conf_get("vmtype","vmcmd","vmpath","vmport","vmsize","vmmonport");
1917 my ($vmopt,$vmtmout,$vmsnap) = pb_conf_get_if("vmopt","vmtmout","vmsnap");
1918
1919 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
1920 my $vmcmd = $ptr2->{$ENV{'PBPROJ'}};
1921
1922 if (defined $opts{'g'}) {
1923 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
1924 $ENV{'PBVMOPT'} = "--nographics";
1925 }
1926 }
1927 if (not defined $ENV{'PBVMOPT'}) {
1928 $ENV{'PBVMOPT'} = "";
1929 }
1930 # Save the current status for later restoration
1931 $ENV{'PBOLDVMOPT'} = $ENV{'PBVMOPT'};
1932 # Set a default timeout of 2 minutes
1933 if (not defined $ENV{'PBVMTMOUT'}) {
1934 $ENV{'PBVMTMOUT'} = "120";
1935 }
1936 if (defined $vmopt->{$v}) {
1937 $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
1938 } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
1939 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
1940 }
1941
1942 # Are we allowed to use snapshot feature
1943 if ($usesnap == 1) {
1944 if ((defined $vmsnap->{$v}) && ($vmsnap->{$v} =~ /true/i)) {
1945 $ENV{'PBVMOPT'} .= " -snapshot";
1946 } elsif ((defined $vmsnap->{$ENV{'PBPROJ'}}) && ($vmsnap->{$ENV{'PBPROJ'}} =~ /true/i)) {
1947 $ENV{'PBVMOPT'} .= " -snapshot";
1948 } elsif ($pbsnap eq 1) {
1949 $ENV{'PBVMOPT'} .= " -snapshot";
1950 }
1951 }
1952 if ($snapme != 0) {
1953 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
1954 # Configure the monitoring to automate the creation of the 'pb' snapshot
1955 $ENV{'PBVMOPT'} .= " -serial mon:telnet::$vmmonport->{$ENV{'PBPROJ'}},server,nowait";
1956 # In that case no snapshot call needed
1957 $ENV{'PBVMOPT'} =~ s/ -snapshot//;
1958 }
1959 }
1960 if (defined $vmtmout->{$v}) {
1961 $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
1962 } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
1963 $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
1964 }
1965 my $nport = $vmport->{$ENV{'PBPROJ'}};
1966 $nport = "$pbport" if (defined $pbport);
1967
1968 my $cmd;
1969 my $vmm; # has to be used for pb_check_ps
1970 if (($vmtype eq "qemu") || ($vmtype eq "kvm")) {
1971 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
1972 if (($create != 0) || (defined $iso)) {
1973 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
1974 }
1975 # Always redirect the network and always try to use a 'pb' snapshot
1976 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 -loadvm pb $vmm"
1977 } elsif ($vmtype eq "xen") {
1978 } elsif ($vmtype eq "vmware") {
1979 } else {
1980 die "VM of type $vmtype not supported. Report to the dev team";
1981 }
1982 # Restore the ENV VAR Value
1983 $ENV{'PBVMOPT'} = $ENV{'PBOLDVMOPT'};
1984
1985 my ($tmpcmd,$void) = split(/ +/,$cmd);
1986 my $vmexist = pb_check_ps($tmpcmd,$vmm);
1987 my $vmpid = 0;
1988 if (! $vmexist) {
1989 if ($create != 0) {
1990 die("Found an existing Virtual machine $vmm. Won't overwrite") if (-r $vmm);
1991 if (($vmtype eq "qemu") || ($vmtype eq "xen") || ($vmtype eq "kvm")) {
1992 my $command = pb_check_req("qemu-img",0);
1993 pb_system("$command create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
1994 } elsif ($vmtype eq "vmware") {
1995 } else {
1996 }
1997 }
1998 if (! -f "$vmm") {
1999 pb_log(0,"Unable to find VM $vmm\n");
2000 } else {
2001 pb_system("$cmd &","Launching the VM $vmm");
2002 pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up");
2003 $vmpid = pb_check_ps($tmpcmd,$vmm);
2004 pb_log(0,"VM $vmm launched (pid $vmpid)\n");
2005 }
2006 } else {
2007 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
2008 }
2009 pb_log(2,"DEBUG: pb_launchv returns ($vmexist,$vmpid)\n");
2010 return($vmexist,$vmpid);
2011 # VE here
2012 } else {
2013 # Get distro context
2014 my ($name,$ver,$darch) = split(/-/,$v);
2015 chomp($darch);
2016 my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf) = pb_distro_init($name,$ver,$darch);
2017
2018 # Get VE context
2019 my ($ptr,$vepath) = pb_conf_get("vetype","vepath");
2020 my $vetype = $ptr->{$ENV{'PBPROJ'}};
2021
2022 # We can probably only get those params now we have the distro context
2023 my ($rbsb4pi,$rbspi,$vesnap,$oscodename,$osmindep,$verebuild,$rbsmirrorsrv) = pb_conf_get_if("rbsb4pi","rbspi","vesnap","oscodename","osmindep","verebuild","rbsmirrorsrv");
2024
2025 # We need to avoid umask propagation to the VE
2026 umask 0022;
2027
2028 if (($vetype eq "chroot") || ($vetype eq "schroot")) {
2029 # Architecture consistency
2030 if ($arch ne $darch) {
2031 die "Unable to launch a VE of architecture $darch on a $arch platform" if (($darch eq "x86_64") && ($arch =~ /i?86/));
2032 }
2033
2034 my ($verpmtype,$vedebtype) = pb_conf_get("verpmtype","vedebtype");
2035 if (($create != 0) || ((defined $verebuild) && ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i)) || ($pbforce == 1)) {
2036 my ($rbsopt1) = pb_conf_get_if("rbsopt");
2037
2038 # We have to rebuild the chroot
2039 if ($dtype eq "rpm") {
2040
2041 # Which tool is used
2042 my $verpmstyle = $verpmtype->{$ENV{'PBPROJ'}};
2043
2044 # Get potential rbs option
2045 my $rbsopt = "";
2046 if (defined $rbsopt1) {
2047 if (defined $rbsopt1->{$verpmstyle}) {
2048 $rbsopt = $rbsopt1->{$verpmstyle};
2049 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
2050 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
2051 } else {
2052 $rbsopt = "";
2053 }
2054 }
2055
2056 my $postinstall = pb_get_postinstall($ddir,$dver,$darch,$rbspi,$verpmstyle);
2057 if ($verpmstyle eq "rinse") {
2058 # Need to reshape the mirrors generated with local before-post-install script
2059 my $b4post = "--before-post-install ";
2060 my $postparam = pb_distro_get_param($ddir,$dver,$darch,$rbsb4pi);
2061 if ($postparam eq "") {
2062 $b4post = "";
2063 } else {
2064 $b4post .= $postparam;
2065 }
2066
2067 # Need to reshape the package list for pb
2068 my $addpkgs;
2069 $postparam = "";
2070 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
2071 if ($postparam eq "") {
2072 $addpkgs = "";
2073 } else {
2074 my $pkgfile = "$ENV{'PBTMP'}/addpkgs.lis";
2075 open(PKG,"> $pkgfile") || die "Unable to create $pkgfile";
2076 foreach my $p (split(/,/,$postparam)) {
2077 print PKG "$p\n";
2078 }
2079 close(PKG);
2080 $addpkgs = "--add-pkg-list $pkgfile";
2081 }
2082
2083 my $rinseverb = "";
2084 $rinseverb = "--verbose" if ($pbdebug gt 0);
2085 my ($rbsconf) = pb_conf_get("rbsconf");
2086
2087 my $command = pb_check_req("rinse",0);
2088 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");
2089 } elsif ($verpmstyle eq "rpmbootstrap") {
2090 my $rbsverb = "";
2091 foreach my $i (1..$pbdebug) {
2092 $rbsverb .= " -v";
2093 }
2094 my $addpkgs = "";
2095 my $postparam = "";
2096 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
2097 if ($postparam eq "") {
2098 $addpkgs = "";
2099 } else {
2100 $addpkgs = "-a $postparam";
2101 }
2102 my $command = pb_check_req("rpmbootstrap",0);
2103 pb_system("sudo $command $rbsopt $postinstall $addpkgs $ddir-$dver-$darch $rbsverb","Creating the rpmbootstrap VE for $ddir-$dver ($darch)", "verbose");
2104 } elsif ($verpmstyle eq "mock") {
2105 my ($rbsconf) = pb_conf_get("rbsconf");
2106 my $command = pb_check_req("mock",0);
2107 pb_system("sudo $command --init --resultdir=\"/tmp\" --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v $rbsopt","Creating the mock VE for $ddir-$dver ($darch)");
2108 # Once setup we need to install some packages, the pb account, ...
2109 pb_system("sudo $command --install --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
2110 } else {
2111 die "Unknown verpmtype type $verpmstyle. Report to dev team";
2112 }
2113 } elsif ($dtype eq "deb") {
2114 my $vedebstyle = $vedebtype->{$ENV{'PBPROJ'}};
2115
2116 my $codename = pb_distro_get_param($ddir,$dver,$darch,$oscodename);
2117 my $postparam = "";
2118 my $addpkgs;
2119 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
2120 if ($postparam eq "") {
2121 $addpkgs = "";
2122 } else {
2123 $addpkgs = "--include $postparam";
2124 }
2125 my $debmir = "";
2126 $debmir .= pb_distro_get_param($ddir,$dver,$darch,$rbsmirrorsrv);
2127
2128 # Get potential rbs option
2129 my $rbsopt = "";
2130 if (defined $rbsopt1) {
2131 if (defined $rbsopt1->{$vedebstyle}) {
2132 $rbsopt = $rbsopt1->{$vedebstyle};
2133 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
2134 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
2135 } else {
2136 $rbsopt = "";
2137 }
2138 }
2139
2140 # debootstrap works with amd64 not x86_64
2141 my $debarch = $darch;
2142 $debarch = "amd64" if ($darch eq "x86_64");
2143 if ($vedebstyle eq "debootstrap") {
2144 my $dbsverb = "";
2145 $dbsverb = "--verbose" if ($pbdebug gt 0);
2146
2147 # Some perl modules are in Universe on Ubuntu
2148 $rbsopt .= " --components=main,universe" if ($ddir eq "ubuntu");
2149
2150 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");
2151 # debootstrap doesn't create an /etc/hosts file
2152 if (! -f "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/etc/hosts" ) {
2153 pb_system("sudo cp /etc/hosts $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/etc/hosts");
2154 }
2155 } else {
2156 die "Unknown vedebtype type $vedebstyle. Report to dev team";
2157 }
2158 } elsif ($dtype eq "ebuild") {
2159 die "Please teach the dev team how to build gentoo chroot";
2160 } else {
2161 die "Unknown distribution type $dtype. Report to dev team";
2162 }
2163 }
2164 # Fix modes to allow access to the VE for pb user
2165 pb_system("sudo chmod 755 $vepath->{$ENV{'PBPROJ'}}/$ddir $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch","Fixing permissions");
2166
2167 # Test if an existing snapshot exists and use it if appropriate
2168 # And also use it of no local extracted VE is present
2169 if ((-f "$vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz") &&
2170 (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
2171 ((defined $vesnap->{$ENV{'PBPROJ'}}) && ($vesnap->{$ENV{'PBPROJ'}} =~ /true/i)) ||
2172 ($pbsnap eq 1) ||
2173 (! -d "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch"))) {
2174 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");
2175 }
2176 # Nothing more to do for VE. No real launch
2177 } else {
2178 die "VE of type $vetype not supported. Report to the dev team";
2179 }
2180 }
2181}
2182
2183# Return string for date synchro
2184sub pb_date2v {
2185
2186my $vtype = shift;
2187my $v = shift;
2188
2189my ($ntp) = pb_conf_get_if($vtype."ntp");
2190my $vntp = $ntp->{$ENV{'PBPROJ'}} if (defined $ntp);
2191my $ntpline;
2192
2193if (defined $vntp) {
2194 my ($ntpcmd) = pb_conf_get($vtype."ntpcmd");
2195 my $vntpcmd;
2196 if (defined $ntpcmd->{$v}) {
2197 $vntpcmd = $ntpcmd->{$v};
2198 } elsif (defined $ntpcmd->{$ENV{'PBPROJ'}}) {
2199 $vntpcmd = $ntpcmd->{$ENV{'PBPROJ'}};
2200 } else {
2201 $vntpcmd = "/bin/true";
2202 }
2203 $ntpline = "sudo $vntpcmd $vntp";
2204} else {
2205 $ntpline = undef;
2206}
2207# Force new date to be in the future compared to the date
2208# of the host by adding 1 minute
2209my @date=pb_get_date();
2210$date[1]++;
2211my $upddate = strftime("%m%d%H%M%Y", @date);
2212my $dateline = "sudo date $upddate";
2213return($ntpline,$dateline);
2214}
2215
2216sub pb_build2v {
2217
2218my $vtype = shift;
2219my $action = shift || "build";
2220
2221my ($v,$all) = pb_get2v($vtype);
2222
2223# Send tar files when we do a global generation
2224pb_build2ssh() if (($all == 1) && ($action eq "build"));
2225
2226my ($vmexist,$vmpid) = (undef,undef);
2227my $pm = new Parallel::ForkManager($pbparallel) if (defined $pbparallel);
2228
2229foreach my $v (@$v) {
2230 $pm->start and next if (defined $pbparallel);
2231 # Prepare the script to be executed on the VM/VE
2232 # in $ENV{'PBDESTDIR'}/pbscript.$$
2233 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript.$$") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript.$$";
2234 print SCRIPT "#!/bin/bash\n";
2235
2236 # Transmit the verbosity level to the virtual env/mach.
2237 my $verbose = "";
2238 my $i = 0; # minimal debug level
2239 while ($i lt $pbdebug) {
2240 $verbose .= "-v ";
2241 $i++;
2242 }
2243 # Activate script verbosity if at least 2 for pbdebug
2244 print SCRIPT "set -x\n" if ($i gt 1);
2245 # Quiet if asked to be so on the original system
2246 $verbose = "-q" if ($pbdebug eq -1);
2247
2248 print SCRIPT "echo ... Execution needed\n";
2249 print SCRIPT "# This is in directory delivery\n";
2250 print SCRIPT "# Setup the variables required for building\n";
2251 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
2252
2253 if ($action eq "build") {
2254 print SCRIPT "# Preparation for pb\n";
2255 print SCRIPT "mv .pbrc \$HOME\n";
2256 print SCRIPT "cd ..\n";
2257 }
2258
2259 # VE needs a good /proc
2260 if ($vtype eq "ve") {
2261 print SCRIPT "sudo mount -t proc /proc /proc\n";
2262 }
2263
2264 my ($ntpline,$dateline) = pb_date2v($vtype,$v);
2265 print SCRIPT "# Time sync\n";
2266 print SCRIPT "echo 'setting up date with '";
2267 if (defined $ntpline) {
2268 print SCRIPT "echo $ntpline\n";
2269 print SCRIPT "$ntpline\n";
2270 } else {
2271 print SCRIPT "echo $dateline\n";
2272 print SCRIPT "$dateline\n";
2273 }
2274 # Use potential local proxy declaration in case we need it to download repo, pkgs, ...
2275 if (defined $ENV{'http_proxy'}) {
2276 print SCRIPT "export http_proxy=\"$ENV{'http_proxy'}\"\n";
2277 }
2278
2279 if (defined $ENV{'ftp_proxy'}) {
2280 print SCRIPT "export ftp_proxy=\"$ENV{'ftp_proxy'}\"\n";
2281 }
2282
2283 # Get list of packages to build/test and get some ENV vars as well
2284 my $ptr = pb_get_pkg();
2285 @pkgs = @$ptr;
2286 my $p = join(' ',@pkgs) if (@pkgs);
2287 print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
2288 print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
2289 print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
2290
2291 # We may need to do some other tasks before building. Read a script here to finish setup
2292 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".pre") {
2293 print SCRIPT "# Special pre-instructions to be launched\n";
2294 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".pre");
2295 }
2296
2297 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre") {
2298 print SCRIPT "# Special pre-$action instructions to be launched\n";
2299 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre");
2300 }
2301
2302 print SCRIPT "# $action\n";
2303 print SCRIPT "echo $action"."ing packages on $vtype...\n";
2304
2305 if (($action eq "test") && (! -x "$ENV{'PBDESTDIR'}/pbtest")) {
2306 die "No test script ($ENV{'PBDESTDIR'}/pbtest) found when in test mode. Aborting ...";
2307 }
2308 print SCRIPT "pb $verbose -p $ENV{'PBPROJ'} $action"."2pkg $p\n";
2309
2310 if ($vtype eq "ve") {
2311 print SCRIPT "sudo umount /proc\n";
2312 }
2313
2314 # We may need to do some other tasks after building. Read a script here to exit properly
2315 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.post") {
2316 print SCRIPT "# Special post-$action instructions to be launched\n";
2317 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.post");
2318 }
2319
2320 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".post") {
2321 print SCRIPT "# Special post-instructions to be launched\n";
2322 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".post");
2323 }
2324
2325 close(SCRIPT);
2326 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript.$$";
2327
2328 # Launch the VM/VE
2329 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
2330
2331
2332 if ($vtype eq "vm") {
2333 # Skip that VM if it something went wrong
2334 if (($vmpid == 0) && ($vmexist == 0)) {
2335 $pm->finish if (defined $pbparallel);
2336 next;
2337 }
2338 } else {
2339 # VE
2340 $vmexist = 0;
2341 $vmpid = 0;
2342 }
2343 # Gather all required files to send them to the VM/VE
2344 # and launch the build through pbscript
2345 pb_log(2,"Calling send2target $vtype,$v,$vmexist,$vmpid\n");
2346 pb_send2target(uc($vtype).$action,"$v",$vmexist,$vmpid);
2347 $pm->finish if (defined $pbparallel);
2348}
2349if (defined $pbparallel) {
2350 $pm->wait_all_children;
2351}
2352}
2353
2354
2355sub pb_clean {
2356
2357 my $sleep=10;
2358 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
2359 die "Unable to get env var PBBUILDDIR" if (not defined $ENV{'PBBUILDDIR'});
2360 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");
2361 sleep $sleep;
2362 pb_rm_rf($ENV{'PBDESTDIR'});
2363 pb_rm_rf($ENV{'PBBUILDDIR'});
2364}
2365
2366sub pb_newver {
2367
2368 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
2369
2370 # Need this call for PBDIR
2371 my ($scheme2,$uri) = pb_cms_init($pbinit);
2372
2373 my ($pbconf,$pburl) = pb_conf_get("pbconfurl","pburl");
2374 $uri = $pbconf->{$ENV{'PBPROJ'}};
2375 my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
2376
2377 # Checking CMS repositories status
2378 ($scheme2, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
2379
2380 if ($scheme !~ /^svn/) {
2381 die "Only SVN is supported at the moment";
2382 }
2383
2384 my $res = pb_cms_isdiff($scheme,$ENV{'PBROOTDIR'});
2385 die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
2386
2387 $res = pb_cms_isdiff($scheme2,$ENV{'PBDIR'});
2388 die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
2389
2390 # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
2391 # we want to get for the root of the new URL
2392
2393 my $tmp = $ENV{'PBROOTDIR'};
2394 $tmp =~ s|^$ENV{'PBCONFDIR'}||;
2395
2396 my $newurl = "$uri/".dirname($tmp)."/$newver";
2397 # Should probably use projver in the old file
2398 my $oldver= basename($tmp);
2399
2400 # Duplicate and extract project-builder part
2401 pb_log(2,"Copying $uri/$tmp to $newurl\n");
2402 pb_cms_copy($scheme,"$uri/$tmp",$newurl);
2403 pb_log(2,"Checkout $newurl to $ENV{'PBROOTDIR'}/../$newver\n");
2404 pb_cms_up($scheme,"$ENV{'PBCONFDIR'}/..");
2405
2406 # Duplicate and extract project
2407 my $newurl2 = "$pburl->{$ENV{'PBPROJ'}}/".dirname($tmp)."/$newver";
2408
2409 pb_log(2,"Copying $pburl->{$ENV{'PBPROJ'}}/$tmp to $newurl2\n");
2410 pb_cms_copy($scheme2,"$pburl->{$ENV{'PBPROJ'}}/$tmp",$newurl2);
2411 pb_log(2,"Checkout $newurl2 to $ENV{'PBDIR'}/../$newver\n");
2412 pb_cms_up($scheme2,"$ENV{'PBDIR'}/..");
2413
2414 # Update the .pb file
2415 open(FILE,"$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb";
2416 open(OUT,"> $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new";
2417 while(<FILE>) {
2418 s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
2419 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/);
2420 pb_log(0,"Commenting testver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^testver/);
2421 s/^testver/#testver/;
2422 print OUT $_;
2423 pb_log(0,"Please check delivery ($_) in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^delivery/);
2424 }
2425 close(FILE);
2426 close(OUT);
2427 rename("$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb");
2428
2429 # Checking pbcl files
2430 foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
2431 # Compute new pbcl file
2432 my $f2 = $f;
2433 $f2 =~ s|$ENV{'PBROOTDIR'}|$ENV{'PBROOTDIR'}/../$newver/|;
2434 open(PBCL,$f) || die "Unable to open $f";
2435 my $foundnew = 0;
2436 while (<PBCL>) {
2437 $foundnew = 1 if (/^$newver \(/);
2438 }
2439 close(PBCL);
2440 open(OUT,"> $f2") || die "Unable to write to $f2: $!";
2441 open(PBCL,$f) || die "Unable to open $f";
2442 while (<PBCL>) {
2443 print OUT "$_" if (not /^$oldver \(/);
2444 if ((/^$oldver \(/) && ($foundnew == 0)) {
2445 print OUT "$newver ($pbdate)\n";
2446 print OUT "- TBD\n";
2447 print OUT "\n";
2448 pb_log(0,"WARNING: version $newver not found in $f so added to $f2...\n") if ($foundnew == 0);
2449 }
2450 }
2451 close(OUT);
2452 close(PBCL);
2453 }
2454
2455 pb_log(2,"Checkin $ENV{'PBROOTDIR'}/../$newver\n");
2456 pb_cms_checkin($scheme,"$ENV{'PBROOTDIR'}/../$newver",undef);
2457}
2458
2459#
2460# Return the list of VMs/VEs we are working on
2461# $all is a flag to know if we return all of them
2462# or only some (if all we publish also tar files in addition to pkgs
2463#
2464sub pb_get2v {
2465
2466my $vtype = shift;
2467my @v;
2468my $all = 0;
2469my $vlist;
2470my $pbv = 'PBV';
2471
2472if ($vtype eq "vm") {
2473 $vlist = "vmlist";
2474} elsif ($vtype eq "ve") {
2475 $vlist = "velist";
2476}
2477# Get VM/VE list
2478if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
2479 my ($ptr) = pb_conf_get($vlist);
2480 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
2481 $all = 1;
2482}
2483pb_log(2,"$vtype: $ENV{$pbv}\n");
2484@v = split(/,/,$ENV{$pbv});
2485return(\@v,$all);
2486}
2487
2488# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
2489# Needs to use root account to connect to the VM/VE
2490# pb will take your local public SSH key to access
2491# the pb account in the VM later on if needed
2492sub pb_setup2v {
2493
2494my $vtype = shift;
2495my $sbx = shift || undef;
2496
2497my ($vm,$all) = pb_get2v($vtype);
2498
2499# Script generated
2500my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
2501
2502foreach my $v (@$vm) {
2503 # Deal with date sync.
2504 my ($ntpline,$dateline) = pb_date2v($vtype,$v);
2505
2506 # Get distro context
2507 my ($name,$ver,$darch) = split(/-/,$v);
2508 chomp($darch);
2509 my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $pbins) = pb_distro_init($name,$ver,$darch);
2510
2511 # Name of the account to deal with for VM/VE
2512 # Do not use the one passed potentially with -a
2513 my ($pbac) = pb_conf_get($vtype."login");
2514 my ($key,$zero0,$zero1,$zero2);
2515 my ($vmexist,$vmpid);
2516
2517 # Prepare the script to be executed on the VM/VE
2518 # in $ENV{'PBDESTDIR'}/setupv
2519 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2520
2521 print SCRIPT << 'EOF';
2522#!/usr/bin/perl -w
2523
2524use strict;
2525use File::Copy;
2526
2527# We should not need in this script more functions than what is provided
2528# by Base, Conf and Distribution to avoid problems at exec time.
2529# They are appended at the end.
2530
2531our $pbdebug;
2532our $pbLOG;
2533our $pbsynmsg = "pbscript";
2534our $pbdisplaytype = "text";
2535our $pblocale = "";
2536pb_log_init($pbdebug, $pbLOG);
2537pb_temp_init();
2538
2539EOF
2540
2541 # Launch the VM/VE - Usage of snapshot disabled
2542 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,0,0);
2543
2544 if ($vtype eq "vm") {
2545 # Prepare the key to be used and transfered remotely
2546 my $keyfile = pb_ssh_get(1);
2547
2548 my ($vmhost,$vmport,$vmntp) = pb_conf_get("vmhost","vmport","vmntp");
2549 my $nport = $vmport->{$ENV{'PBPROJ'}};
2550 $nport = "$pbport" if (defined $pbport);
2551
2552 # Skip that VM if something went wrong
2553 next if (($vmpid == 0) && ($vmexist == 0));
2554
2555 # Store the pub key part in a variable
2556 open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
2557 ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
2558 close(FILE);
2559
2560 $key = "\Q$zero1";
2561
2562 # We call true to avoid problems if SELinux is not activated, but chcon is present and returns in that case 1
2563 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");
2564 # once this is done, we can do what we want on the VM remotely
2565 } elsif ($vtype eq "ve") {
2566 print SCRIPT << "EOF";
2567# For VE we need a good null dev
2568pb_system("rm -f /dev/null; mknod /dev/null c 1 3; chmod 777 /dev/null");
2569EOF
2570 print SCRIPT << "EOF";
2571# For VE we first need to mount some FS
2572pb_system("mount -t proc /proc /proc");
2573
2574EOF
2575 }
2576
2577if ($vtype eq "vm") {
2578 print SCRIPT << 'EOF';
2579# Removes duplicate in .ssh/authorized_keys of our key if needed
2580#
2581my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
2582open(PBFILE,$file1) || die "Unable to open $file1";
2583open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
2584my $count = 0;
2585while (<PBFILE>) {
2586
2587EOF
2588 print SCRIPT << "EOF";
2589 if (/ $key /) {
2590 \$count++;
2591 }
2592print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
2593}
2594close(PBFILE);
2595close(PBOUT);
2596rename("\$file1.new",\$file1);
2597chmod 0600,\$file1;
2598
2599# Sync date
2600EOF
2601 if (defined $ntpline) {
2602 print SCRIPT "pb_system(\"$ntpline\");\n";
2603 } else {
2604 print SCRIPT "pb_system(\"$dateline\");\n";
2605 }
2606 }
2607 print SCRIPT << 'EOF';
2608
2609# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
2610#
2611my $file="/etc/passwd";
2612open(PBFILE,$file) || die "Unable to open $file";
2613my $found = 0;
2614while (<PBFILE>) {
2615EOF
2616 print SCRIPT << "EOF";
2617 \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
2618EOF
2619
2620my $home = "/home";
2621# Solaris doesn't like that we use /home
2622$home = "/export/home" if ($dtype eq "pkg");
2623
2624 print SCRIPT << "EOF";
2625}
2626close(PBFILE);
2627
2628if ( \$found == 0 ) {
2629 if ( ! -d "$home" ) {
2630 pb_mkdir_p("$home");
2631 }
2632EOF
2633 print SCRIPT << "EOF";
2634pb_system("/usr/sbin/groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
2635pb_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'}})");
2636}
2637EOF
2638
2639 # Copy the content of our local conf file to the VM/VE
2640 my $content = pb_get_content(pb_distro_conffile());
2641 print SCRIPT << "EOF";
2642 #
2643 # Create a temporary local conf file for distribution support
2644 # This is created here before its use later. Its place is hardcoded, so no choice for the path
2645 #
2646 my \$tempconf = pb_distro_conffile();
2647 pb_mkdir_p(dirname(\$tempconf));
2648 open(CONF,"> \$tempconf") || die "Unable to create \$tempconf";
2649 print CONF q{$content};
2650 close(CONF);
2651EOF
2652
2653 if ($vtype eq "vm") {
2654 print SCRIPT << "EOF";
2655# allow ssh entry to build
2656#
2657mkdir "$home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
2658# Allow those accessing root to access the build account
2659copy("\$ENV{'HOME'}/.ssh/authorized_keys","$home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
2660chmod 0600,".ssh/authorized_keys";
2661pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} $home/$pbac->{$ENV{'PBPROJ'}}","Finish setting up the account env for $pbac->{$ENV{'PBPROJ'}}");
2662
2663EOF
2664}
2665 print SCRIPT << 'EOF';
2666# No passwd for build account only keys
2667$file="/etc/shadow";
2668if (-f $file) {
2669 open(PBFILE,$file) || die "Unable to open $file";
2670 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2671 while (<PBFILE>) {
2672EOF
2673 print SCRIPT << "EOF";
2674 s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
2675 s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/; #SLES 9 e.g.
2676 s/^$pbac->{$ENV{'PBPROJ'}}:\\*LK\\*:/$pbac->{$ENV{'PBPROJ'}}:NP:/; #Solaris e.g.
2677EOF
2678 print SCRIPT << 'EOF';
2679 print PBOUT $_;
2680 }
2681 close(PBFILE);
2682 close(PBOUT);
2683 rename("$file.new",$file);
2684 chmod 0640,$file;
2685 }
2686
2687# Keep the VM in text mode
2688$file="/etc/inittab";
2689if (-f $file) {
2690 open(PBFILE,$file) || die "Unable to open $file";
2691 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2692 while (<PBFILE>) {
2693 s/^(..):5:initdefault:$/$1:3:initdefault:/;
2694 print PBOUT $_;
2695 }
2696 close(PBFILE);
2697 close(PBOUT);
2698 rename("$file.new",$file);
2699 chmod 0640,$file;
2700}
2701
2702# pb has to be added to portage group on gentoo
2703
2704# We need to have that pb_distro_init function
2705# Get it from Project-Builder::Distribution
2706# And we now need the conf file required for this to work created above
2707
2708my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $pbins, $darch) = pb_distro_init();
2709print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $pbins, $darch))."\n";
2710
2711# Adapt sudoers
2712# sudo is not default on Solaris and needs to be installed first
2713# from http://www.sunfreeware.com/programlistsparc10.html#sudo
2714if ($dtype eq "pkg") {
2715 $file="/usr/local/etc/sudoers";
2716} else {
2717 $file="/etc/sudoers";
2718}
2719open(PBFILE,$file) || die "Unable to open $file";
2720open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2721while (<PBFILE>) {
2722EOF
2723 print SCRIPT << "EOF";
2724 next if (/^$pbac->{$ENV{'PBPROJ'}} /);
2725EOF
2726 print SCRIPT << 'EOF';
2727 s/Defaults[ \t]+requiretty//;
2728 print PBOUT $_;
2729}
2730close(PBFILE);
2731EOF
2732 print SCRIPT << "EOF";
2733# Some distro force requiretty at compile time, so disable here
2734print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} !requiretty\n";
2735print PBOUT "Defaults:root !requiretty\n";
2736# This is needed in order to be able to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
2737print PBOUT "Defaults:$pbac->{$ENV{'PBPROJ'}} env_keep += \\\"http_proxy ftp_proxy\\\"\n";
2738print PBOUT "$pbac->{$ENV{'PBPROJ'}} ALL=(ALL) NOPASSWD:ALL\n";
2739EOF
2740 print SCRIPT << 'EOF';
2741close(PBOUT);
2742rename("$file.new",$file);
2743chmod 0440,$file;
2744
2745EOF
2746
2747 # We may need a proxy configuration. Get it from the local env
2748
2749 if (defined $ENV{'http_proxy'}) {
2750 print SCRIPT "\$ENV\{'http_proxy'\}=\"$ENV{'http_proxy'}\";\n";
2751 }
2752
2753 if (defined $ENV{'ftp_proxy'}) {
2754 print SCRIPT "\$ENV\{'ftp_proxy'\}=\"$ENV{'ftp_proxy'}\";\n";
2755 }
2756
2757 print SCRIPT << 'EOF';
2758
2759# Suse wants sudoers as 640
2760if ((($ddir eq "sles") && (($dver =~ /10/) || ($dver =~ /9/))) || (($ddir eq "opensuse") && ($dver =~ /10.[012]/))) {
2761 chmod 0640,$file;
2762}
2763
2764# First install all required packages
2765pb_system("yum clean all","Cleaning yum env") if (($ddir eq "fedora") || ($ddir eq "asianux") || ($ddir eq "rhel"));
2766my ($ospkgdep) = pb_conf_get_if("ospkgdep");
2767
2768my $pkgdep = pb_distro_get_param($ddir,$dver,$darch,$ospkgdep,$dfam,$dtype,$dos);
2769pb_distro_installdeps(undef,$dtype,$pbins,pb_distro_only_deps_needed($dtype,join(' ',split(/,/,$pkgdep))));
2770
2771EOF
2772 my ($instype) = pb_conf_get("pbinstalltype");
2773 my $itype = pb_distro_get_param($ddir,$dver,$darch,$instype,$dfam,$dtype,$dos);
2774 if (defined $sbx) {
2775 # Install from sandbox mean a file base install
2776 $itype = "file";
2777 }
2778 if ($itype =~ /^file/) {
2779 my $cmdget;
2780 if (defined $sbx) {
2781 # Install from sandbox mean using the result of the just passed sbx2build command
2782 # Get content saved in cms2build
2783 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
2784 die "Unable to get package list" if (not defined $pkg);
2785 my ($vepath) = pb_conf_get("vepath");
2786
2787 # We consider 2 specific packages
2788 my $vertag1 = $pkg->{"ProjectBuilder"};
2789 my $vertag2 = $pkg->{"project-builder"};
2790 # get the version of the current package - maybe different
2791 my ($pbver1,$tmp1) = split(/-/,$vertag1);
2792 my ($pbver2,$tmp2) = split(/-/,$vertag2);
2793 # Copy inside the VM
2794 copy("$ENV{'PBDESTDIR'}/ProjectBuilder-$pbver1.tar.gz","$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/tmp");
2795 copy("$ENV{'PBDESTDIR'}/project-builder-$pbver2.tar.gz","$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/tmp");
2796 $cmdget = "mv /tmp/ProjectBuilder-$pbver1.tar.gz ProjectBuilder-latest.tar.gz ; mv /tmp/project-builder-$pbver2.tar.gz project-builder-latest.tar.gz";
2797 } else {
2798 $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";
2799 }
2800 print SCRIPT << 'EOF';
2801# Then install manually the missing perl modules
2802my ($osperldep,$osperlver) = pb_conf_get_if("osperldep","osperlver");
2803
2804my $perldep = pb_distro_get_param($ddir,$dver,$darch,$osperldep,$dfam,$dtype,$dos);
2805foreach my $m (split(/,/,$perldep)) {
2806 # Skip empty deps
2807 next if ($m =~ /^\s*$/);
2808 my $dir = $m;
2809 $dir =~ s/-.*//;
2810 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}");
2811}
2812EOF
2813
2814 print SCRIPT << "EOF";
2815pb_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");
2816EOF
2817 } elsif ($itype =~ /^pkg/) {
2818 # pkg based install. We need to point to the project-builder.org repository
2819 print SCRIPT << 'EOF';
2820my ($ospkg) = pb_conf_get_if("ospkg");
2821
2822my $pkgforpb = pb_distro_get_param($ddir,$dver,$darch,$ospkg,$dfam,$dtype,$dos);
2823pb_distro_setuposrepo($ddir,$dver,$darch,$dtype,$dfam,$dos);
2824pb_distro_installdeps(undef,$dtype,$pbins,pb_distro_only_deps_needed($dtype,join(' ',split(/,/,$pkgforpb))));
2825EOF
2826 } else {
2827 # Unknown install type
2828 die("Unknown install type $itype->{$ENV{'PBPROJ'}} for param pbinstalltype");
2829 }
2830 print SCRIPT << 'EOF';
2831pb_system("pb 2>&1 | head -5",undef,"verbose");
2832EOF
2833 if ($vtype eq "ve") {
2834 print SCRIPT << 'EOF';
2835# For VE we need to umount some FS at the end
2836
2837pb_system("umount /proc");
2838
2839# Create a basic network file if not already there
2840
2841my $nf="/etc/sysconfig/network";
2842if ((! -f $nf) && ($dtype eq "rpm")) {
2843 open(NF,"> $nf") || die "Unable to create $nf";
2844 print NF "NETWORKING=yes\n";
2845 print NF "HOSTNAME=localhost\n";
2846 close(NF);
2847}
2848chmod 0755,$nf;
2849EOF
2850 }
2851
2852 # Adds pb_distro_init and all functions needed from ProjectBuilder::Distribution and Base
2853 foreach my $d (@INC) {
2854 my @f = ("$d/ProjectBuilder/Base.pm","$d/ProjectBuilder/Distribution.pm","$d/ProjectBuilder/Conf.pm");
2855 foreach my $f (@f) {
2856 if (-f "$f") {
2857 open(PBD,"$f") || die "Unable to open $f";
2858 while (<PBD>) {
2859 next if (/^package/);
2860 next if (/^use Exporter/);
2861 next if (/^use ProjectBuilder::/);
2862 next if (/^our /);
2863 print SCRIPT $_;
2864 }
2865 close(PBD);
2866 }
2867 }
2868 }
2869 close(SCRIPT);
2870 chmod 0755,"$pbscript";
2871
2872 # That build script needs to be run as root and force stop of VM at end
2873 $pbaccount = "root";
2874
2875 # Force shutdown of VM except if it was already launched
2876 my $pbforce = 0;
2877 if ((! $vmexist) && ($vtype eq "vm")) {
2878 $pbforce = 1;
2879 }
2880
2881 pb_script2v($pbscript,$vtype,$pbforce,$v);
2882}
2883return;
2884}
2885
2886# Function to create a snapshot named 'pb' for VMs and a compressed tar for VEs
2887sub pb_snap2v {
2888
2889my $vtype = shift;
2890
2891my ($vm,$all) = pb_get2v($vtype);
2892
2893# Script generated
2894my $pbscript = "$ENV{'PBDESTDIR'}/snapv";
2895
2896my ($pbac) = pb_conf_get($vtype."login");
2897
2898foreach my $v (@$vm) {
2899 if ($vtype eq "ve") {
2900 # Get distro context
2901 my ($name,$ver,$darch) = split(/-/,$v);
2902 chomp($darch);
2903 my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf) = pb_distro_init($name,$ver,$darch);
2904 my ($vepath) = pb_conf_get("vepath");
2905
2906 # Test if an existing snapshot exists and remove it if there is a VE
2907 if ((-f "$vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz") &&
2908 (! -d "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch")) {
2909 pb_system("sudo rm -f $vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz","Removing previous snapshot $ddir-$dver-$darch.tar.gz");
2910 }
2911 }
2912
2913 # Prepare the script to be executed on the VM/VE
2914 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2915 print SCRIPT << 'EOF';
2916 #!/bin/bash
2917 sleep 2
2918EOF
2919 close(SCRIPT);
2920 chmod 0755,"$pbscript";
2921
2922 # Force shutdown of VM/VE
2923 # Force snapshot of VM/VE
2924 pb_script2v($pbscript,$vtype,1,$v,1);
2925}
2926return;
2927}
2928
2929# Function to update a VMs or VEs with the latest distribution content
2930sub pb_update2v {
2931
2932my $vtype = shift;
2933
2934my ($vm,$all) = pb_get2v($vtype);
2935
2936# Script generated
2937my $pbscript = "$ENV{'PBDESTDIR'}/updatev";
2938
2939my ($pbac) = pb_conf_get($vtype."login");
2940
2941foreach my $v (@$vm) {
2942 # Get distro context
2943 my ($name,$ver,$darch) = split(/-/,$v);
2944 chomp($darch);
2945 my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $pbins) = pb_distro_init($name,$ver,$darch);
2946
2947 # Prepare the script to be executed on the VM/VE
2948 # in $ENV{'PBDESTDIR'}/updatev
2949 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2950
2951 print SCRIPT << 'EOF';
2952 #!/bin/bash
2953 sleep 2
2954EOF
2955 # VE needs a good /proc
2956 if ($vtype eq "ve") {
2957 print SCRIPT "sudo mount -t proc /proc /proc\n";
2958 }
2959 print SCRIPT "$pbupd\n";
2960 if ($vtype eq "ve") {
2961 print SCRIPT "sudo umount /proc\n";
2962 }
2963 close(SCRIPT);
2964 chmod 0755,"$pbscript";
2965
2966 # Force shutdown of VM except
2967 pb_script2v($pbscript,$vtype,1,$v);
2968}
2969return;
2970}
2971
2972sub pb_announce {
2973
2974 # Get all required parameters
2975 my ($pbpackager,$pbrepo,$pbml,$pbsmtp) = pb_conf_get("pbpackager","pbrepo","pbml","pbsmtp");
2976 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
2977 my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
2978 my @pkgs = @$pkg;
2979 my %pkgs;
2980 my $first = 0;
2981
2982 # Command to find packages on repo
2983 my $findstr = "find . ";
2984 # Generated announce files
2985 my @files;
2986
2987 foreach my $pbpkg (@pkgs) {
2988 if ($first != 0) {
2989 $findstr .= "-o ";
2990 }
2991 $first++;
2992 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
2993 $pbver = $pkgv->{$pbpkg};
2994 } else {
2995 $pbver = $ENV{'PBPROJVER'};
2996 }
2997 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
2998 $pbtag = $pkgt->{$pbpkg};
2999 } else {
3000 $pbtag = $ENV{'PBPROJTAG'};
3001 }
3002
3003 # TODO: use virtual/real names here now
3004 $findstr .= "-name \'$pbpkg-$pbver-$pbtag\.*.rpm\' -o -name \'$pbpkg"."_$pbver*\.deb\' -o -name \'$pbpkg-$pbver*\.ebuild\' ";
3005
3006 my $chglog;
3007
3008 # Get project info on log file and generate tmp files used later on
3009 pb_cms_init($pbinit);
3010 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
3011 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
3012 $chglog = undef if (! -f $chglog);
3013
3014 open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
3015 my %pb;
3016 $pb{'dtype'} = "announce";
3017 $pb{'realpkg'} = $pbpkg;
3018 $pb{'ver'} = $pbver;
3019 $pb{'tag'} = $pbtag;
3020 $pb{'suf'} = "N/A"; # Should not be empty even if unused
3021 $pb{'date'} = $pbdate;
3022 $pb{'chglog'} = $chglog;
3023 $pb{'packager'} = $pbpackager;
3024 $pb{'proj'} = $ENV{'PBPROJ'};
3025 $pb{'repo'} = $pbrepo;
3026 pb_changelog(\%pb,\*OUT,"yes");
3027 close(OUT);
3028 push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
3029 }
3030 $findstr .= " | grep -Ev \'src.rpm\'";
3031
3032 # Prepare the command to run and execute it
3033 open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
3034 print PBS "$findstr\n";
3035 close(PBS);
3036 chmod 0755,"$ENV{'PBTMP'}/pbscript";
3037 pb_send2target("Announce");
3038
3039 # Get subject line
3040 my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
3041 pb_log(0,"Please enter the title of your announce\n");
3042 pb_log(0,"(By default: $sl)\n");
3043 my $sl2 = <STDIN>;
3044 $sl = $sl2 if ($sl2 !~ /^$/);
3045
3046 # Prepare a template of announce
3047 open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
3048 print ANN << "EOF";
3049$sl</p>
3050
3051<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
3052<p>
3053Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
3054</p>
3055<p>
3056EOF
3057 open(LOG,"$ENV{'PBTMP'}/system.$$.log") || die "Unable to read $ENV{'PBTMP'}/system.$$.log: $!";
3058 my $col = 2;
3059 my $i = 1;
3060 print ANN << 'EOF';
3061<TABLE WIDTH="700" CELLPADDING="0" CELLSPACING="0" BORDER="0">
3062<TR>
3063EOF
3064 while (<LOG>) {
3065 print ANN "<TD><A HREF=\"$pbrepo->{$ENV{'PBPROJ'}}/$_\">$_</A></TD>";
3066 $i++;
3067 if ($i > $col) {
3068 print ANN "</TR>\n<TR>";
3069 $i = 1;
3070 }
3071 }
3072 close(LOG);
3073 print ANN << "EOF";
3074</TR>
3075</TABLE>
3076</p>
3077
3078<p>As usual source packages are also available in the same directory.</p>
3079
3080<p>
3081Changes are :
3082</p>
3083<p>
3084EOF
3085 # Get each package changelog content
3086 foreach my $f (sort(@files)) {
3087 open(IN,"$f") || die "Unable to read $f:$!";
3088 while (<IN>) {
3089 print ANN $_;
3090 }
3091 close(IN);
3092 print ANN "</p><p>\n";
3093 }
3094 print ANN "</p>\n";
3095 close(ANN);
3096
3097 # Allow for modification
3098 my $editor = "vi";
3099 $editor = $ENV{'EDITOR'} if (defined $ENV{'EDITOR'});
3100 pb_system("$editor $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
3101
3102 # Store it in DB for external usage (Web pages generation)
3103 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
3104
3105 my $precmd = "";
3106 if (! -f $db) {
3107 $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
3108 }
3109
3110 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
3111 { RaiseError => 1, AutoCommit => 1 })
3112 || die "Unable to connect to $db";
3113
3114 if ($precmd ne "") {
3115 my $sth = $dbh->prepare(qq{$precmd})
3116 || die "Unable to create table into $db";
3117 $sth->execute();
3118 }
3119
3120 # To read whole file
3121 local $/;
3122 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
3123 my $announce = <ANN>;
3124 close(ANN);
3125
3126 pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
3127 my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
3128 || die "Unable to insert into $db";
3129 $sth->execute($pbdate, $announce);
3130 $sth->finish();
3131 $dbh->disconnect;
3132
3133 # Then deliver it on the Web
3134 # $TOOLHOME/livwww www
3135
3136 # Mail it to project's ML
3137 open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
3138 print ML << 'EOF';
3139<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
3140
3141<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
3142 <head>
3143 </head>
3144 <body>
3145 <p>
3146EOF
3147 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
3148 while(<ANN>) {
3149 print ML $_;
3150 }
3151 print ML << 'EOF';
3152</body>
3153</html>
3154EOF
3155 close(ML);
3156
3157 # To read whole file
3158 local $/;
3159 open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
3160 my $msg = <ANN>;
3161 close(ANN);
3162
3163 # Preparation of headers
3164 eval
3165 {
3166 require Mail::Sendmail;
3167 Mail::Sendmail->import();
3168 };
3169 if ($@) {
3170 # Mail::Sendmail not found not sending mail !
3171 pb_log(0,"No Mail::Sendmail module found so not sending any mail !\n");
3172 } else {
3173 my %mail = (
3174 To => $pbml->{$ENV{'PBPROJ'}},
3175 From => $pbpackager->{$ENV{'PBPROJ'}},
3176 Smtp => $pbsmtp->{$ENV{'PBPROJ'}},
3177 Body => $msg,
3178 Subject => "[ANNOUNCE] $sl",
3179 );
3180
3181 # Send mail
3182 if (! sendmail(%mail)) {
3183 if ((defined $Mail::Sendmail::error) and (defined $Mail::Sendmail::log)) {
3184 die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
3185 }
3186 }
3187 }
3188}
3189
3190#
3191# Creates a set of HTML file containing the news for the project
3192# based on what has been generated by the pb_announce function
3193#
3194sub pb_web_news2html {
3195
3196 my $dest = shift || $ENV{'PBTMP'};
3197
3198 # Get all required parameters
3199 my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
3200
3201 # DB of announces for external usage (Web pages generation)
3202 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
3203
3204 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
3205 { RaiseError => 1, AutoCommit => 1 })
3206 || die "Unable to connect to $db";
3207 # For date handling
3208 $ENV{LANGUAGE}="C";
3209 my $firstjan = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year(), 0, 0, -1);
3210 my $oldfirst = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year()-1, 0, 0, -1);
3211 pb_log(2,"firstjan: $firstjan, oldfirst: $oldfirst, pbdate:$pbdate\n");
3212 my $all = $dbh->selectall_arrayref("SELECT id,date,announce FROM announces ORDER BY date DESC");
3213 my %news;
3214 $news{"cy"} = ""; # current year's news
3215 $news{"ly"} = ""; # last year news
3216 $news{"py"} = ""; # previous years news
3217 $news{"fp"} = ""; # first page news
3218 my $cpt = 4; # how many news for first page
3219 # Extract info from DB
3220 foreach my $row (@$all) {
3221 my ($id, $date, $announce) = @$row;
3222 $news{"cy"} = $news{"cy"}."<p><B>$date</B> $announce\n" if ((($date cmp $pbdate) le 0) && (($firstjan cmp $date) le 0));
3223 $news{"ly"} = $news{"ly"}."<p><B>$date</B> $announce\n" if ((($date cmp $firstjan) le 0) && (($oldfirst cmp $date) le 0));
3224 $news{"py"} = $news{"py"}."<p><B>$date</B> $announce\n" if (($date cmp $oldfirst) le 0);
3225 $news{"fp"} = $news{"fp"}."<p><B>$date</B> $announce\n" if ($cpt > 0);
3226 $cpt--;
3227 }
3228 pb_log(1,"news{fp}: ".$news{"fp"}."\n");
3229 $dbh->disconnect;
3230
3231 # Generate the HTML content
3232 foreach my $pref (keys %news) {
3233 open(NEWS,"> $dest/pb_web_$pref"."news.html") || die "Unable to create $dest/pb_web_$pref"."news.html: $!";
3234 print NEWS "$news{$pref}";
3235 close(NEWS);
3236 }
3237}
3238
3239
3240# Return the SSH key file to use
3241# Potentially create it if needed
3242
3243sub pb_ssh_get {
3244
3245my $create = shift || 0; # Do not create keys by default
3246
3247# Check the SSH environment
3248my $keyfile = undef;
3249
3250# We have specific keys by default
3251$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa";
3252if (!(-e $keyfile) && ($create eq 1)) {
3253 pb_system("ssh-keygen -q -b 1024 -N '' -f $keyfile -t dsa","Generating SSH keys for pb");
3254}
3255
3256$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
3257$keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if (-s "$ENV{'HOME'}/.ssh/id_dsa");
3258$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
3259die "Unable to find your public ssh key under $keyfile" if (not defined $keyfile);
3260return($keyfile);
3261}
3262
3263
3264# Returns the pid of a running VM command using a specific VM file
3265sub pb_check_ps {
3266 my $vmcmd = shift;
3267 my $vmm = shift;
3268 my $vmexist = 0; # FALSE by default
3269
3270 open(PS, "ps auxhww|") || die "Unable to call ps";
3271 while (<PS>) {
3272 next if (! /$vmcmd/);
3273 next if (! /$vmm/);
3274 my ($void1, $void2);
3275 ($void1, $vmexist, $void2) = split(/ +/);
3276 last;
3277 }
3278 return($vmexist);
3279}
3280
3281
3282sub pb_extract_build_files {
3283
3284my $src=shift;
3285my $dir=shift;
3286my $ddir=shift;
3287my $mandatory=shift || "spec";
3288my @files;
3289
3290my $flag = "mayfail" if ($mandatory eq "patch");
3291my $res;
3292
3293if ($src =~ /tar\.gz$/) {
3294 $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
3295} elsif ($src =~ /tar\.bz2$/) {
3296 $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
3297} else {
3298 die "Unknown compression algorithm for $src";
3299}
3300# If not mandatory return now
3301return() if (($res != 0) and ($mandatory eq "patch"));
3302opendir(DIR,"$dir") || die "Unable to open directory $dir";
3303foreach my $f (readdir(DIR)) {
3304 next if ($f =~ /^\./);
3305 # Skip potential patch dir
3306 next if ($f =~ /^pbpatch/);
3307 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
3308 pb_log(2,"mv $dir/$f $ddir\n");
3309 push @files,"$ddir/$f";
3310}
3311closedir(DIR);
3312# Not enough but still a first cleanup
3313pb_rm_rf("$dir");
3314return(@files);
3315}
3316
3317sub pb_list_bfiles {
3318
3319my $dir = shift;
3320my $pbpkg = shift;
3321my $bfiles = shift;
3322my $pkgfiles = shift;
3323my $supfiles = shift;
3324
3325opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
3326foreach my $f (readdir(BDIR)) {
3327 next if ($f =~ /^\./);
3328 if (-d $f) {
3329 # Recurse for directories (Debian 3.0 format e.g.)
3330 pb_list_bfiles($f,$pbpkg,$bfiles,$pkgfiles,$supfiles);
3331 next;
3332 }
3333 $bfiles->{$f} = "$dir/$f";
3334 $bfiles->{$f} =~ s~$ENV{'PBROOTDIR'}~~;
3335 if (defined $supfiles->{$pbpkg}) {
3336 $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
3337 }
3338}
3339closedir(BDIR);
3340}
3341
3342sub pb_list_sfiles {
3343
3344my $sdir = shift;
3345my $sources = shift;
3346my $dtype = shift;
3347my $dfam = shift;
3348my $ddir = shift;
3349my $dver = shift;
3350my $arch = shift;
3351my $extdir = shift;
3352
3353# Prepare local sources for this distro - They are always applied first - May be a problem one day
3354# This function works for both patches and additional sources
3355foreach my $p (sort(<$sdir/*>)) {
3356 $sources->{"$ddir-$dver-$arch"} .= "," if ((defined $sources->{"$ddir-$dver-$arch"}) and ($p =~ /\.all$/));
3357 $sources->{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.all$/);
3358 $sources->{"$ddir-$dver-$arch"} .= "," if ((defined $sources->{"$ddir-$dver-$arch"}) and ($p =~ /\.$dtype$/));
3359 $sources->{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$dtype$/);
3360 $sources->{"$ddir-$dver-$arch"} .= "," if ((defined $sources->{"$ddir-$dver-$arch"}) and ($p =~ /\.$dfam$/));
3361 $sources->{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$dfam$/);
3362 $sources->{"$ddir-$dver-$arch"} .= "," if ((defined $sources->{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir$/));
3363 $sources->{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir$/);
3364 $sources->{"$ddir-$dver-$arch"} .= "," if ((defined $sources->{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver$/));
3365 $sources->{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver$/);
3366 $sources->{"$ddir-$dver-$arch"} .= "," if ((defined $sources->{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver-$arch$/));
3367 $sources->{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver-$arch$/);
3368}
3369
3370# Prepare also remote sources to be included - Applied after the local ones
3371foreach my $p ("all","$dtype","$dfam","$ddir","$ddir-$dver","$ddir-$dver-$arch") {
3372 my $f = "$extdir.".".$p";
3373 next if (not -f $f);
3374 if (not open(PATCH,$f)) {
3375 pb_display("Unable to open existing external source file content $f\n");
3376 next;
3377 }
3378 while (<PATCH>) {
3379 chomp();
3380 $sources->{"$ddir-$dver-$arch"} .= "," if (defined $sources->{"$ddir-$dver-$arch"});
3381 $sources->{"$ddir-$dver-$arch"} .= "$_";
3382 }
3383 close(PATCH);
3384}
3385pb_log(2,"DEBUG: sources: ".Dumper($sources)."\n");
3386}
3387
3388#
3389# Return the list of packages we are working on in a non CMS action
3390#
3391sub pb_get_pkg {
3392
3393my @pkgs = ();
3394
3395my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
3396@pkgs = keys %$var;
3397
3398pb_log(0,"Packages: ".join(',',@pkgs)."\n");
3399return(\@pkgs);
3400}
3401
3402#
3403# Return the postinstall line if needed
3404#
3405
3406sub pb_get_postinstall {
3407
3408my $ddir = shift;
3409my $dver = shift;
3410my $darch = shift;
3411my $rbspi = shift;
3412my $vestyle = shift;
3413my $post = "";
3414
3415# Do we have a local post-install script
3416if ($vestyle eq "rinse") {
3417 $post = "--post-install ";
3418} elsif ($vestyle eq "rpmbootstrap") {
3419 $post = "-s ";
3420}
3421
3422my $postparam = pb_distro_get_param($ddir,$dver,$darch,$rbspi);
3423if ($postparam eq "") {
3424 $post = "";
3425} else {
3426 $post .= $postparam;
3427}
3428return($post);
3429}
3430
34311;
Note: See TracBrowser for help on using the repository browser.