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

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