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

Last change on this file since 1124 was 1124, checked in by Bruno Cornec, 13 years ago

i- Adds new potion -g to support non-graphical modes on the CLI and not only in conf files and env var - Fix #68

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