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

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