source: devel/pb/bin/pb @ 545

Revision 545, 71.1 KB checked in by bruno, 5 years ago (diff)

New Debian fixes

  • Property svn:executable set to *
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';
13use Getopt::Long qw(:config auto_abbrev no_ignore_case);
14use Data::Dumper;
15use English;
16use File::Basename;
17use File::Copy;
18use File::stat;
19use File::Temp qw(tempdir);
20use POSIX qw(strftime);
21use lib qw (lib);
22use ProjectBuilder::Version;
23use ProjectBuilder::Base;
24use ProjectBuilder::Display;
25use ProjectBuilder::Conf;
26use ProjectBuilder::Distribution;
27use ProjectBuilder::CMS;
28use ProjectBuilder::Env;
29use ProjectBuilder::Filter;
30use ProjectBuilder::Changelog;
31use Mail::Sendmail;
32
33# Global variables
34my %opts;                                       # CLI Options
35my $action;                                     # action to realize
36my $test = "FALSE";                     # Not used
37my $force = 0;                          # Force VE/VM rebuild
38my $option = "";                        # Not used
39my @pkgs;                                       # list of packages
40my $pbtag;                                      # Global Tag variable
41my $pbver;                                      # Global Version variable
42my $pbscript;                           # Name of the script
43my %pbver;                                      # per package
44my %pbtag;                                      # per package
45my $pbrev;                                      # Global REVISION variable
46my $pbaccount;                          # Login to use to connect to the VM
47my $pbport;                                     # Port to use to connect to the VM
48my $newver;                                     # New version to create
49my $iso;                                        # ISO image for the VM to create
50
51my @date = pb_get_date();
52my $pbdate = strftime("%Y-%m-%d", @date);
53
54=pod
55
56=head1 NAME
57
58pb, aka project-builder.org - builds packages for your projects
59
60=head1 DESCRIPTION
61
62pb helps you build various packages directly from your project sources.
63Those sources could be handled by a CMS (Configuration Management System)
64such as Subversion, CVS, ... or being a simple reference to a compressed tar file.
65It's based on a set of configuration files, a set of provided macros to help
66you keeping build files as generic as possible. For example, a single .spec
67file should be required to generate for all rpm based distributions, even
68if you could also have multiple .spec files if required.
69
70=head1 SYNOPSIS
71
72pb [-vhq][-r pbroot][-p project][[-s script -a account -P port][-m mach-1[,...]]][-i iso] <action> [<pkg1> ...]
73
74pb [--verbose][--help][--man][--quiet][--revision pbroot][--project project][[--script script --account account --port port][--machine mach-1[,...]]][--iso iso] <action> [<pkg1> ...]
75
76=head1 OPTIONS
77
78=over 4
79
80=item B<-v|--verbose>
81
82Print a brief help message and exits.
83
84=item B<-q|--quiet>
85
86Do not print any output.
87
88=item B<-h|--help>
89
90Print a brief help message and exits.
91
92=item B<--man>
93
94Prints the manual page and exits.
95
96=item B<-m|--machine machine1[,machine2,...]>
97
98Name of the Virtual Machines (VM) or Virtual Environments (VE) you want to build on (coma separated).
99All if none precised (or use the env variable PBV).
100
101=item B<-s|--script script>
102
103Name of the script you want to execute on the related VMs or VEs.
104
105=item B<-i|--iso iso_image>
106
107Name of the ISO image of the distribution you want to install on the related VMs.
108
109=item B<-a|--account account>
110
111Name of the account to use to connect on the related VMs.
112
113=item B<-P|--port port_number>
114
115Port number to use to connect on the related VMs.\n";
116
117=item B<-p|--project project_name>
118
119Name of the project you're working on (or use the env variable PBPROJ)
120
121=item B<-r|--revision revision>
122
123Path Name of the project revision under the CMS (or use the env variable PBROOT)
124
125=item B<-V|--version new_version>
126
127New version of the project to create based on the current one.
128
129=back
130
131=head1 ARGUMENTS
132
133<action> can be:
134
135=over 4
136
137=item B<cms2build>
138
139Create tar files for the project under your CMS.
140CMS supported are SVN and CVS
141parameters are packages to build
142if not using default list
143
144=item B<build2pkg>
145
146Create packages for your running distribution
147
148=item B<cms2pkg>
149
150cms2build + build2pkg
151
152=item B<build2ssh>
153
154Send the tar files to a SSH host
155
156=item B<cms2ssh>
157
158cms2build + build2ssh
159
160=item B<pkg2ssh>
161
162Send the packages built to a SSH host
163
164=item B<build2vm>
165
166Create packages in VMs, launching them if needed
167and send those packages to a SSH host once built
168VM type supported are QEMU
169
170=item B<build2ve>
171
172Create packages in VEs, creating it if needed
173and send those packages to a SSH host once built
174
175=item B<cms2vm>
176
177cms2build + build2vm
178
179=item B<cms2ve>
180
181cms2build + build2ve
182
183=item B<launchvm>
184
185Launch one virtual machine
186
187=item B<launchve>
188
189Launch one virtual environment
190
191=item B<script2vm>
192
193Launch one virtual machine if needed
194and executes a script on it
195
196=item B<script2ve>
197
198Execute a script in a virtual environment
199
200=item B<newvm>
201
202Create a new virtual machine
203
204=item B<newve>
205
206Create a new virtual environment
207
208=item B<setupvm>
209
210Setup a virtual machine for pb usage
211
212=item B<setupve>
213
214Setup a virtual environment for pb usage
215
216=item B<newver>
217
218Create a new version of the project derived
219from the current one
220
221=item B<newproj>
222
223Create a new project and a template set of
224configuration files under pbconf
225
226=item B<announce>
227
228Announce the availability of the project through various means
229
230=back
231
232<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).
233
234=head1 WEB SITES
235
236The 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/>.
237
238=head1 USER MAILING LIST
239
240None exists for the moment.
241
242=head1 CONFIGURATION FILES
243
244Each pb user may have a configuration in F<$HOME/.pbrc>. The values in this file may overwrite any other configuration file value.
245
246Here is an example of such a configuration file:
247
248 #
249 # Define for each project the URL of its pbconf repository
250 # No default option allowed here as they need to be all different
251 #
252 # URL of the pbconf content
253 # This is the format of a classical URL with the extension of additional schema such as
254 # svn+ssh, cvs+ssh, ...
255 #
256 pbconfurl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe/pbconf
257
258 # This is normaly defined in the project's configuration file
259 # Url of the project
260 #
261 pburl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe
262 
263 # All these URLs needs to be defined here as the are the entry point
264 # for how to build packages for the project
265 #
266 pbconfurl pb = svn+ssh://svn.project-builder.org/mondo/svn/pb/pbconf
267 pbconfurl mondorescue = svn+ssh://svn.project-builder.org/mondo/svn/project-builder/mondorescue/pbconf
268 pbconfurl collectl = svn+ssh://bruno@svn.mondorescue.org/mondo/svn/project-builder/collectl/pbconf
269 pbconfurl netperf = svn+ssh://svn.mondorescue.org/mondo/svn/project-builder/netperf/pbconf
270 
271 # Under that dir will take place everything related to pb
272 # If you want to use VMs/chroot/..., then use $ENV{'HOME'} to make it portable
273 # to your VMs/chroot/...
274 # if not defined then /var/cache
275 pbdefdir default = $ENV{'HOME'}/project-builder
276 pbdefdir pb = $ENV{'HOME'}
277 pbdefdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
278 pbdefdir mondorescue = $ENV{'HOME'}/mondo/svn
279 
280 # pbconfdir points to the directory where the CMS content of the pbconfurl is checked out
281 # If not defined, pbconfdir is under pbdefdir/pbproj/pbconf
282 pbconfdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs/pbconf
283 pbconfdir mondorescue = $ENV{'HOME'}/mondo/svn/pbconf
284 
285 # pbdir points to the directory where the CMS content of the pburl is checked out
286 # If not defined, pbdir is under pbdefdir/pbproj
287 # Only defined if we have access to the dev of the project
288 pbdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
289 pbdir mondorescue = $ENV{'HOME'}/mondo/svn
290 
291 # -daemonize doesn't work with qemu 0.8.2
292 vmopt default = -m 384
293
294=head1 AUTHORS
295
296The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
297
298=head1 COPYRIGHT
299
300Project-Builder.org is distributed under the GPL v2.0 license
301described in the file C<COPYING> included with the distribution.
302
303=cut
304
305# ---------------------------------------------------------------------------
306
307# Old syntax
308#getopts('a:fhi:l:m:P:p:qr:s:vV:',\%opts);
309
310my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
311
312# Initialize the syntax string
313
314pb_syntax_init("pb (aka project-builder.org) Version $projectbuilderver-$projectbuilderrev\n");
315
316GetOptions("help|?|h" => \$opts{'h'}, 
317                "man" => \$opts{'man'},
318                "verbose|v+" => \$opts{'v'},
319                "quiet|q" => \$opts{'q'},
320                "log-files|l=s" => \$opts{'l'},
321                "force|f" => \$opts{'f'},
322                "account|a=s" => \$opts{'a'},
323                "revision|r=s" => \$opts{'r'},
324                "script|s=s" => \$opts{'s'},
325                "machines|mock|m=s" => \$opts{'m'},
326                "port|P=i" => \$opts{'P'},
327                "project|p=s" => \$opts{'p'},
328                "iso|i=s" => \$opts{'i'},
329                "version|V=s" => \$opts{'V'},
330) || pb_syntax(-1,0);
331
332if (defined $opts{'h'}) {
333        pb_syntax(0,1);
334}
335if (defined $opts{'man'}) {
336        pb_syntax(0,2);
337}
338if (defined $opts{'v'}) {
339        $pbdebug = $opts{'v'};
340}
341if (defined $opts{'f'}) {
342        $force=1;
343}
344if (defined $opts{'q'}) {
345        $pbdebug=-1;
346}
347if (defined $opts{'l'}) {
348        open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
349        $pbLOG = \*pbLOG;
350        $pbdebug = 0  if ($pbdebug == -1);
351        }
352pb_log_init($pbdebug, $pbLOG);
353pb_display_init("text","");
354
355# Handle root of the project if defined
356if (defined $opts{'r'}) {
357        $ENV{'PBROOTDIR'} = $opts{'r'};
358}
359# Handle virtual machines if any
360if (defined $opts{'m'}) {
361        $ENV{'PBV'} = $opts{'m'};
362}
363if (defined $opts{'s'}) {
364        $pbscript = $opts{'s'};
365}
366if (defined $opts{'a'}) {
367        $pbaccount = $opts{'a'};
368        die "option -a requires a -s script option" if (not defined $pbscript);
369}
370if (defined $opts{'P'}) {
371        $pbport = $opts{'P'};
372}
373if (defined $opts{'V'}) {
374        $newver = $opts{'V'};
375}
376if (defined $opts{'i'}) {
377        $iso = $opts{'i'};
378}
379
380# Get Action
381$action = shift @ARGV;
382die pb_syntax(-1,1) if (not defined $action);
383
384my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
385my $pbinit = undef;
386$pbinit = 1 if ($action =~ /^newproj$/);
387
388# Handles project name if any
389# And get global params
390($filteredfiles, $supfiles, $defpkgdir, $extpkgdir) = pb_env_init($opts{'p'},$pbinit,$action);
391
392pb_log(0,"Project: $ENV{'PBPROJ'}\n");
393pb_log(0,"Action: $action\n");
394
395# Act depending on action
396if ($action =~ /^cms2build$/) {
397        pb_cms2build();
398} elsif ($action =~ /^build2pkg$/) {
399        pb_build2pkg();
400} elsif ($action =~ /^cms2pkg$/) {
401        pb_cms2build();
402        pb_build2pkg();
403} elsif ($action =~ /^build2ssh$/) {
404        pb_build2ssh();
405} elsif ($action =~ /^cms2ssh$/) {
406        pb_cms2build();
407        pb_build2ssh();
408} elsif ($action =~ /^pkg2ssh$/) {
409        pb_pkg2ssh();
410} elsif ($action =~ /^build2ve$/) {
411        pb_build2v("ve");
412} elsif ($action =~ /^build2vm$/) {
413        pb_build2v("vm");
414} elsif ($action =~ /^cms2ve$/) {
415        pb_cms2build();
416        pb_build2v("ve");
417} elsif ($action =~ /^cms2vm$/) {
418        pb_cms2build();
419        pb_build2v("vm");
420} elsif ($action =~ /^launchvm$/) {
421        pb_launchv("vm",$ENV{'PBV'},0);
422} elsif ($action =~ /^launchve$/) {
423        pb_launchv("ve",$ENV{'PBV'},0);
424} elsif ($action =~ /^script2vm$/) {
425        pb_script2v($pbscript,"vm");
426} elsif ($action =~ /^script2ve$/) {
427        pb_script2v($pbscript,"ve");
428} elsif ($action =~ /^newver$/) {
429        pb_newver();
430} elsif ($action =~ /^newve$/) {
431        pb_launchv("ve",$ENV{'PBV'},1);
432} elsif ($action =~ /^newvm$/) {
433        pb_launchv("vm",$ENV{'PBV'},1);
434} elsif ($action =~ /^setupve$/) {
435        pb_setup_v("ve");
436} elsif ($action =~ /^setupvm$/) {
437        pb_setup_v("vm");
438} elsif ($action =~ /^newproj$/) {
439        # Nothing to do - already done in pb_env_init
440} elsif ($action =~ /^clean$/) {
441        # TBC
442} elsif ($action =~ /^announce$/) {
443        # For announce only. Require avoids the systematic load of these modules
444        require DBI;
445
446        pb_announce();
447} else {
448        pb_log(0,"\'$action\' is not available\n");
449        pb_syntax(-2,1);
450}
451
452sub pb_cms2build {
453
454        my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
455        my @pkgs = @$pkg;
456        my %pkgs;
457        my %pb;                         # Structure to store conf info
458
459        my ($scheme, $uri) = pb_cms_init($pbinit);
460
461        my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
462
463        # declare packager and repo for filtering
464        my ($tmp1, $tmp2) = pb_conf_get("pbpackager","pbrepo");
465        $ENV{'PBPACKAGER'} = $tmp1->{$ENV{'PBPROJ'}};
466        $ENV{'PBREPO'} = $tmp2->{$ENV{'PBPROJ'}};
467
468        foreach my $pbpkg (@pkgs) {
469                $ENV{'PBPKG'} = $pbpkg;
470                if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
471                        $pbver = $pkgv->{$pbpkg};
472                } else {
473                        $pbver = $ENV{'PBPROJVER'};
474                }
475                if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
476                        $pbtag = $pkgt->{$pbpkg};
477                } else {
478                        $pbtag = $ENV{'PBPROJTAG'};
479                }
480
481                $pbrev = $ENV{'PBREVISION'};
482                pb_log(0,"\n");
483                pb_log(0,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
484                die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
485
486                # Clean up dest if necessary. The export will recreate it
487                my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
488                pb_rm_rf($dest) if (-d $dest);
489
490                # Export CMS tree for the concerned package to dest
491                # And generate some additional files
492                $OUTPUT_AUTOFLUSH=1;
493
494                # computes in which dir we have to work
495                my $dir = $defpkgdir->{$pbpkg};
496                $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
497                pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");
498
499                # Exporting content from CMS
500                my $preserve = pb_cms_export($uri,"$ENV{'PBDIR'}/$dir",$dest);
501
502                # Generated fake content for test versions to speed up stuff
503                my ($testver) = pb_conf_get_if("testver");
504                my $chglog;
505
506                # Get project info on authors and log file
507                $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
508                $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
509                $chglog = undef if (! -f $chglog);
510
511                my $authors = "$ENV{'PBROOTDIR'}/$pbpkg/pbauthors";
512                $authors = "$ENV{'PBROOTDIR'}/pbauthors" if (! -f $authors);
513                $authors = "/dev/null" if (! -f $authors);
514
515                # Extract cms log history and store it
516                if ((defined $chglog) && (! -f "$dest/NEWS")) {
517                        pb_log(2,"Generating NEWS file from $chglog\n");
518                        copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
519                }
520                pb_cms_log($scheme,"$ENV{'PBDIR'}/$dir",$dest,$chglog,$authors,$testver);
521
522                my %build;
523                my @pt;
524                my $tmpl = "";
525                my %patches;
526
527                @pt = pb_conf_get_if("vmlist","velist");
528                if (defined $pt[0]->{$ENV{'PBPROJ'}}) {
529                        $tmpl .= $pt[0]->{$ENV{'PBPROJ'}};
530                }
531                if (defined $pt[1]->{$ENV{'PBPROJ'}}) {
532                        # the 2 lists needs to be grouped with a ',' separated them
533                        if ($tmpl ne "") {
534                                $tmpl .= ",";
535                        }
536                        $tmpl .= $pt[1]->{$ENV{'PBPROJ'}} 
537                }
538
539                # Setup %pb structure to allow filtering later on, on files using that structure
540                $pb{'tag'} = $pbtag;
541                $pb{'rev'} = $pbrev;
542                $pb{'ver'} = $pbver;
543                $pb{'pkg'} = $pbpkg;
544                $pb{'date'} = $pbdate;
545                $pb{'defpkgdir'} = $defpkgdir;
546                $pb{'extpkgdir'} = $extpkgdir;
547                $pb{'chglog'} = $chglog;
548                $pb{'packager'} = $ENV{'PBPACKAGER'};
549                $pb{'proj'} = $ENV{'PBPROJ'};
550                $pb{'repo'} = $ENV{'PBREPO'};
551                $pb{'patches'} = \%patches;
552                pb_log(2,"DEBUG: pb: ".Dumper(%pb)."\n");
553
554                foreach my $d (split(/,/,$tmpl)) {
555                        my ($name,$ver,$arch) = split(/-/,$d);
556                        chomp($arch);
557                        my ($ddir, $dver, $dfam);
558                        ($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'}) = pb_distro_init($name,$ver);
559                        pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'})."\n");
560                        pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
561
562                        # We need to compute the real name of the package
563                        my $pbrealpkg = pb_cms_get_real_pkg($pbpkg,$pb{'dtype'});
564                        $pb{'realpkg'} = $pbrealpkg;
565                        pb_log(1,"Virtual package $pbpkg has a real package name of $pbrealpkg on $ddir-$dver\n") if ($pbrealpkg ne $pbpkg);
566
567                        # Filter build files from the less precise up to the most with overloading
568                        # Filter all files found, keeping the name, and generating in dest
569
570                        # Find all build files first relatively to PBROOTDIR
571                        # Find also all specific files referenced in the .pb conf file
572                        my %bfiles = ();
573                        my %pkgfiles = ();
574                        $build{"$ddir-$dver-$arch"} = "yes";
575
576                        if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}") {
577                                pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
578                        } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$dfam") {
579                                pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
580                        } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir") {
581                                pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
582                        } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver") {
583                                pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
584                        } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch") {
585                                pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
586                        } else {
587                                $build{"$ddir-$dver-$arch"} = "no";
588                                next;
589                        }
590                        pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");
591
592                        # Get all filters to apply
593                        my $ptr = pb_get_filters($pbpkg, $pb{'dtype'}, $dfam, $ddir, $dver);
594
595                        # Prepare local patches for this distro - They are always applied first - May be a problem one day
596                        foreach my $p (sort(<$ENV{'PBROOTDIR'}/$pbpkg/pbpatch/*>)) {
597                                $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.all$/));
598                                $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.all$/);
599                                $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$pb{'dtype'}$/));
600                                $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$pb{'dtype'}$/);
601                                $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$dfam$/));
602                                $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$dfam$/);
603                                $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir$/));
604                                $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir$/);
605                                $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver$/));
606                                $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver$/);
607                                $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver-$arch$/));
608                                $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver-$arch$/);
609                        }
610
611                        # Prepare also remote patches to be included - Applied after the local ones
612                        foreach my $p ("all","$pb{'dtype'}","$dfam","$ddir","$ddir-$dver","$ddir-$dver-$arch") {
613                                my $f = "$ENV{'PBROOTDIR'}/$pbpkg/pbextpatch.$p";
614                                next if (not -f $f);
615                                if (not open(PATCH,$f)) {
616                                        pb_display("Unable to open existing external patch file content $f\n");
617                                        next;
618                                }
619                                while (<PATCH>) {
620                                        chomp();
621                                        $patches{"$ddir-$dver-$arch"} .= "," if (defined $patches{"$ddir-$dver-$arch"});
622                                        $patches{"$ddir-$dver-$arch"} .= "$_";
623                                }
624                                close(PATCH);
625                        }
626                        pb_log(2,"DEBUG: pb->patches: ".Dumper($pb{'patches'})."\n");
627
628                        # Apply now all the filters on all the files concerned
629                        # destination dir depends on the type of file
630                        if (defined $ptr) {
631                                # For patch support
632                                $pb{'tuple'} = "$ddir-$dver-$arch";
633                                foreach my $f (values %bfiles,values %pkgfiles) {
634                                        pb_filter_file("$ENV{'PBROOTDIR'}/$f",$ptr,"$dest/pbconf/$ddir-$dver-$arch/".basename($f),\%pb);
635                                }
636                        }
637                }
638                my @found;
639                my @notfound;
640                foreach my $b (keys %build) {
641                        push @found,$b if ($build{$b} =~ /yes/);
642                        push @notfound,$b if ($build{$b} =~ /no/);
643                }
644                pb_log(0,"Build files generated for ".join(',',sort(@found))."\n");
645                pb_log(0,"No Build files found for ".join(',',sort(@notfound))."\n") if (@notfound);
646                pb_log(2,"DEBUG: patches: ".Dumper(%patches)."\n");
647
648                # Get the generic filter (all.pbf) and
649                # apply those to the non-build files including those
650                # generated by pbinit if applicable
651
652                # Get only all.pbf filter
653                my $ptr = pb_get_filters($pbpkg);
654
655                my $liste ="";
656                if (defined $filteredfiles->{$pbpkg}) {
657                        foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
658                                pb_filter_file_inplace($ptr,"$dest/$f",\%pb);
659                                $liste = "$f $liste";
660                        }
661                }
662                pb_log(2,"Files ".$liste."have been filtered\n");
663
664                # Filter potential patches (local + remote)
665                pb_log(0,"Delivering and compressing patches ");
666                foreach my $v (keys %patches) {
667                        pb_mkdir_p("$dest/pbconf/$v/pbpatch");
668                        foreach my $pf (split(/,/,$patches{$v})) {
669                                my $pp = basename($pf);
670                                pb_cms_export($pf,undef,"$dest/pbconf/$v/pbpatch");
671                                pb_filter_file_inplace($ptr,"$dest/pbconf/$v/pbpatch/$pp",\%pb);
672                                pb_system("gzip -9f $dest/pbconf/$v/pbpatch/$pp","","quiet");
673                        }
674                        pb_log(0,"$patches{$v} ");
675                }
676                pb_log(0,"\n");
677
678                # Prepare the dest directory for archive
679                if (-x "$ENV{'PBROOTDIR'}/$pbpkg/pbinit") {
680                        pb_filter_file("$ENV{'PBROOTDIR'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",\%pb);
681                        chmod 0755,"$ENV{'PBTMP'}/pbinit";
682                        pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOTDIR'}/$pbpkg/pbinit","verbose");
683                }
684
685                # Archive dest dir
686                chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
687                if (defined $preserve) {
688                        # In that case we want to preserve the original tar file for checksum purposes
689                        # The one created is btw equivalent in that case to this one
690                        # Maybe check basename of both to be sure they are the same ?
691                        pb_log(0,"Preserving original tar file ");
692                        move("$preserve","$pbpkg-$pbver.tar.gz");
693                } else {
694                        # Possibility to look at PBSRC to guess more the filename
695                        pb_system("tar cfz $pbpkg-$pbver.tar.gz --exclude=$pbpkg-$pbver/pbconf $pbpkg-$pbver","Creating $pbpkg tar files compressed");
696                }
697                pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");
698                pb_system("tar cfz $pbpkg-$pbver.pbconf.tar.gz $pbpkg-$pbver/pbconf","Creating pbconf tar files compressed");
699                pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz\n");
700
701                # Keep track of version-tag per pkg
702                $pkgs{$pbpkg} = "$pbver-$pbtag";
703
704                # Final cleanup
705                pb_rm_rf($dest) if (-d $dest);
706        }
707
708        # Keep track of per package version
709        pb_log(2,"DEBUG pkgs: ".Dumper(%pkgs)."\n");
710        open(PKG,"> $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb";
711        foreach my $pbpkg (keys %pkgs) {
712                print PKG "pbpkg $pbpkg = $pkgs{$pbpkg}\n";
713        }
714        close(PKG);
715
716        # Keep track of what is generated by default
717        # We need to store the dir and info on version-tag
718        # Base our content on the existing .pb file
719        copy("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb","$ENV{'PBDESTDIR'}/pbrc");
720        open(LAST,">> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
721        print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOTDIR'}\n";
722        print LAST "pbprojver $ENV{'PBPROJ'} = $ENV{'PBPROJVER'}\n";
723        print LAST "pbprojtag $ENV{'PBPROJ'} = $ENV{'PBPROJTAG'}\n";
724        print LAST "pbpackager $ENV{'PBPROJ'} = $ENV{'PBPACKAGER'}\n";
725        close(LAST);
726}
727
728sub pb_build2pkg {
729
730        # Get the running distro to build on
731        my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
732        pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
733
734        # Get list of packages to build
735        # Get content saved in cms2build
736        my $ptr = pb_get_pkg();
737        @pkgs = @$ptr;
738
739        my $arch = pb_get_arch();
740
741        my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
742        $pkg = { } if (not defined $pkg);
743
744        chdir "$ENV{'PBBUILDDIR'}";
745        my $made = ""; # pkgs made during build
746        foreach my $pbpkg (@pkgs) {
747                my $vertag = $pkg->{$pbpkg};
748                # get the version of the current package - maybe different
749                ($pbver,$pbtag) = split(/-/,$vertag);
750
751                my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
752                my $src2="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
753                pb_log(2,"Source file: $src\n");
754                pb_log(2,"Pbconf file: $src2\n");
755
756                pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
757                if ($dtype eq "rpm") {
758                        foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
759                                if (! -d "$ENV{'PBBUILDDIR'}/$d") {
760                                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";
761                                }
762                        }
763
764                        # Remove in case a previous link/file was there
765                        unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
766                        symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
767                        # We need to first extract the spec file
768                        my @specfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$ENV{'PBBUILDDIR'}/SPECS","spec");
769
770                        # We need to handle potential patches to upstream sources
771                        pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/pbpatch/","$ENV{'PBBUILDDIR'}/SOURCES","patch");
772
773                        pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
774                        # set LANGUAGE to check for correct log messages
775                        $ENV{'LANGUAGE'}="C";
776                        # Older Redhat use _target_platform in %configure incorrectly
777                        my $specialdef = "";
778                        if (($ddir eq "redhat") || (($ddir eq "rhel") && ($dver eq "2.1"))) {
779                                $specialdef = "--define \'_target_platform \"\"\'";
780                        }
781                        foreach my $f (@specfile) {
782                                if ($f =~ /\.spec$/) {
783                                        pb_system("rpmbuild $specialdef --define \'packager $ENV{'PBPACKAGER'}\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}","verbose");
784                                        last;
785                                }
786                        }
787                        # Get the name of the generated packages
788                        open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
789                        while (<LOG>) {
790                                chomp();
791                                next if ($_ !~ /^Wrote:/);
792                                s|.*/([S]*RPMS.*)|$1|;
793                                $made="$made $_";
794                        }
795                        close(LOG);
796
797                } elsif ($dtype eq "deb") {
798                        chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
799                        pb_system("tar xfz $src","Extracting sources");
800                        pb_system("tar xfz $src2","Extracting pbconf");
801
802                        chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
803                        pb_rm_rf("debian");
804                        symlink "pbconf/$ddir-$dver-$arch","debian" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
805                        chmod 0755,"debian/rules";
806
807                        pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package","verbose");
808                        # Get the name of the generated packages
809                        open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
810                        while (<LOG>) {
811                                chomp();
812                                my $tmp = $_;
813                                next if ($tmp !~ /^dpkg-deb.:/);
814                                $tmp =~ s|.*../(.*)_(.*).deb.*|$1|;
815                                $made="$made $tmp.dsc $tmp.tar.gz $tmp"."_*.deb $tmp"."_*.changes";
816                        }
817                        close(LOG);
818                } elsif ($dtype eq "ebuild") {
819                        my @ebuildfile;
820                        # For gentoo we need to take pb as subsystem name
821                        # We put every apps here under sys-apps. hope it's correct
822                        # We use pb's home dir in order to have a single OVERLAY line
823                        my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
824                        pb_mkdir_p($tmpd) if (! -d "$tmpd");
825                        pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
826
827                        # We need to first extract the ebuild file
828                        @ebuildfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$tmpd","ebuild");
829
830                        # Prepare the build env for gentoo
831                        my $found = 0;
832                        my $pbbd = $ENV{'HOME'};
833                        $pbbd =~ s|/|\\/|g;
834                        if (-r "/etc/make.conf") {
835                                open(MAKE,"/etc/make.conf");
836                                while (<MAKE>) {
837                                        $found = 1 if (/$pbbd\/portage/);
838                                }
839                                close(MAKE);
840                        }
841                        if ($found == 0) {
842                                pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
843                        }
844                        #$found = 0;
845                        #if (-r "/etc/portage/package.keywords") {
846                        #open(KEYW,"/etc/portage/package.keywords");
847                        #while (<KEYW>) {
848                        #$found = 1 if (/portage\/pb/);
849                        #}
850                        #close(KEYW);
851                        #}
852                        #if ($found == 0) {
853                        #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
854                        #}
855
856                        # Build
857                        foreach my $f (@ebuildfile) {
858                                if ($f =~ /\.ebuild$/) {
859                                        move($f,"$tmpd/$pbpkg-$pbver.ebuild");
860                                        pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package","verbose");
861                                        # Now move it where pb expects it
862                                        pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
863                                        move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
864                                }
865                        }
866
867                        $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver.ebuild";
868                } elsif ($dtype eq "tgz") {
869                        # Slackware family
870                        $made="$made $pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
871
872                        chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
873                        pb_system("tar xfz $src","Extracting sources");
874                        pb_system("tar xfz $src2","Extracting pbconf");
875                        chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
876                        symlink "pbconf/$ddir-$dver-$arch","install" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
877                        if (-x "install/pbslack") {
878                                pb_system("./install/pbslack","Building package");
879                                pb_system("sudo /sbin/makepkg -p -l y -c y $pbpkg","Packaging $pbpkg","verbose");
880                        }
881                } else {
882                        die "Unknown dtype format $dtype";
883                }
884        }
885        # Packages check if needed
886        if ($dtype eq "rpm") {
887                if (-f "/usr/bin/rpmlint") {
888                        pb_system("rpmlint $made","Checking validity of rpms with rpmlint","verbose");
889                }
890        } elsif ($dtype eq "deb") {
891                if (-f "/usr/bin/lintian") {
892                        my $made2 = "";
893                        foreach my $f (split(/ /,$made)) {
894                                $made2 .= "$f " if ($f =~ /\.changes$/);
895                        }
896                        pb_system("lintian $made2","Checking validity of debs with lintian","verbose");
897                }
898        } else {
899                pb_log(0, "No check done for $dtype yet");
900        }
901
902        # Keep track of what is generated so that we can get them back from VMs
903        open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
904        print KEEP "$made\n";
905        close(KEEP);
906}
907
908sub pb_build2ssh {
909        pb_send2target("Sources");
910}
911
912sub pb_pkg2ssh {
913        pb_send2target("Packages");
914}
915
916# By default deliver to the the public site hosting the
917# ftp structure (or whatever) or a VM/VE
918sub pb_send2target {
919
920        my $cmt = shift;
921        my $v = shift || undef;
922        my $vmexist = shift || 0;                       # 0 is FALSE
923        my $vmpid = shift || 0;                         # 0 is FALSE
924
925        pb_log(2,"DEBUG: pb_send2target($cmt,".Dumper($v).",$vmexist,$vmpid)\n");
926        my $host = "sshhost";
927        my $login = "sshlogin";
928        my $dir = "sshdir";
929        my $port = "sshport";
930        my $conf = "sshconf";
931        my $rebuild = "sshrebuild";
932        my $tmout = "vmtmout";
933        my $path = "vmpath";
934        if (($cmt eq "vm") || ($cmt eq "Script")) {
935                $login = "vmlogin";
936                $dir = "pbdefdir";
937                $tmout = "vmtmout";
938                $rebuild = "vmrebuild";
939                # Specific VM
940                $host = "vmhost";
941                $port = "vmport";
942        } elsif ($cmt eq "ve") {
943                $login = "velogin";
944                $dir = "pbdefdir";
945                $tmout = "vetmout";
946                # Specific VE
947                $path = "vepath";
948                $conf = "veconf";
949                $rebuild = "verebuild";
950        }
951        my $cmd = "";
952        my $src = "";
953        my ($odir,$over,$oarch) = (undef, undef, undef);
954        my ($ddir, $dver, $dfam, $dtype, $pbsuf);
955
956        if ($cmt ne "Announce") {
957                my $ptr = pb_get_pkg();
958                @pkgs = @$ptr;
959
960                # Get the running distro to consider
961                if (defined $v) {
962                        ($odir,$over,$oarch) = split(/-/,$v);
963                }
964                ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
965                pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
966
967                # Get list of packages to build
968                # Get content saved in cms2build
969                my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
970                $pkg = { } if (not defined $pkg);
971
972                chdir "$ENV{'PBBUILDDIR'}";
973                foreach my $pbpkg (@pkgs) {
974                        my $vertag = $pkg->{$pbpkg};
975                        # get the version of the current package - maybe different
976                        ($pbver,$pbtag) = split(/-/,$vertag);
977
978                        if (($cmt eq "Sources") || ($cmt eq "vm") || ($cmt eq "ve")) {
979                                $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
980                                if ($cmd eq "") {
981                                        $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
982                                } else {
983                                        $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
984                                }
985                        }
986                }
987                # Adds conf file for availability of conf elements
988                pb_conf_add("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb");
989        }
990
991        if (($cmt eq "vm") || ($cmt eq "ve")) {
992                $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript";
993        } elsif ($cmt eq "Script") {
994                $src="$src $ENV{'PBDESTDIR'}/pbscript";
995        } elsif ($cmt eq "Announce") {
996                $src="$src $ENV{'PBTMP'}/pbscript";
997        } elsif ($cmt eq "Packages") {
998                # Get package list from file made during build2pkg
999                open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1000                $src = <KEEP>;
1001                chomp($src);
1002                close(KEEP);
1003                $src="$src $ENV{'PBBUILDDIR'}/pbscript" if ($cmt ne "Sources");
1004        }
1005        # Remove potential leading spaces (cause problem with basename)
1006        $src =~ s/^ *//;
1007        my $basesrc = "";
1008        foreach my $i (split(/ +/,$src)) {
1009                $basesrc .= " ".basename($i);
1010        }
1011
1012        pb_log(0,"Sources handled ($cmt): $src\n");
1013        pb_log(2,"values: ".Dumper(($host,$login,$dir,$port,$tmout,$rebuild,$path,$conf))."\n");
1014        my ($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vepath) = pb_conf_get($host,$login,$dir,$port,$tmout,$path);
1015        my ($vrebuild,$veconf) = pb_conf_get_if($rebuild,$conf);
1016        pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf))."\n");
1017        # Not mandatory
1018        my ($testver) = pb_conf_get_if("testver");
1019
1020        my $mac;
1021        # Useless for VE
1022        if ($cmt ne "ve") {
1023                $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
1024                # Overwrite account value if passed as parameter
1025                $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
1026                pb_log(2, "DEBUG: pbaccount: $pbaccount => mac: $mac\n") if (defined $pbaccount);
1027        }
1028
1029        my $tdir;
1030        my $bdir;
1031        if (($cmt eq "Sources") || ($cmt eq "Script")) {
1032                $tdir = $sshdir->{$ENV{'PBPROJ'}}."/src";
1033                if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1034                        # This is a test pkg => target dir is under test
1035                        $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/src";
1036                }
1037        } elsif (($cmt eq "vm") || ($cmt eq "ve")) {
1038                $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery";
1039                $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build";
1040                # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
1041                $bdir =~ s|\$ENV.+\}/||;
1042        } elsif ($cmt eq "Announce") {
1043                $tdir = "$sshdir->{$ENV{'PBPROJ'}}";
1044                if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1045                        # This is a test pkg => target dir is under test
1046                        $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test";
1047                }
1048        } elsif ($cmt eq "Packages") {
1049                $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ddir/$dver";
1050
1051                if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1052                        # This is a test pkg => target dir is under test
1053                        $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/$ddir/$dver";
1054                }
1055
1056                my $repodir = $tdir;
1057                $repodir =~ s|^$sshdir->{$ENV{'PBPROJ'}}/||;
1058
1059                my ($pbrepo) = pb_conf_get("pbrepo");
1060
1061                # Repository management
1062                open(PBS,"> $ENV{'PBBUILDDIR'}/pbscript") || die "Unable to create $ENV{'PBBUILDDIR'}/pbscript";
1063                if ($dtype eq "rpm") {
1064                        # Also make a pbscript to generate yum/urpmi bases
1065                        print PBS << "EOF";
1066#!/bin/bash
1067# Prepare a script to ease yum setup
1068cat > $ENV{'PBPROJ'}.repo << EOT
1069[$ENV{'PBPROJ'}]
1070name=$ddir $dver - $ENV{'PBPROJ'} Vanilla Packages
1071baseurl=$pbrepo->{$ENV{'PBPROJ'}}/$repodir
1072enabled=1
1073gpgcheck=0
1074EOT
1075chmod 644 $ENV{'PBPROJ'}.repo
1076
1077# Clean up old repo content
1078rm -rf headers/ repodata/
1079# Create yum repo
1080yum-arch .
1081# Create repodata
1082createrepo .
1083EOF
1084                        if ($dfam eq "md") {
1085                                # For Mandriva add urpmi management
1086                                print PBS << "EOF";
1087# Prepare a script to ease urpmi setup
1088cat > $ENV{'PBPROJ'}.addmedia << EOT
1089urpmi.addmedia $ENV{'PBPROJ'} $pbrepo->{$ENV{'PBPROJ'}}/$repodir with hdlist.cz
1090EOT
1091chmod 755 $ENV{'PBPROJ'}.addmedia
1092
1093# Clean up old repo content
1094rm -f hdlist.cz synthesis.hdlist.cz
1095# Create urpmi repo
1096genhdlist .
1097EOF
1098                        }
1099                        if ($ddir eq "fedora") {
1100                                # Extract the spec file to please Fedora maintainers :-(
1101                                print PBS << "EOF";
1102for p in $basesrc; do
1103        echo \$p | grep -q 'src.rpm'
1104        if [ \$\? -eq 0 ]; then
1105                rpm2cpio \$p | cpio -ivdum --quiet '*.spec'
1106        fi
1107done
1108EOF
1109                        }
1110                } elsif ($dtype eq "deb") {
1111                        # Also make a pbscript to generate apt bases
1112                        # Cf: http://www.debian.org/doc/manuals/repository-howto/repository-howto.fr.html
1113                        my $rpd = dirname("$pbrepo->{$ENV{'PBPROJ'}}/$repodir");
1114                        print PBS << "EOF";
1115#!/bin/bash
1116# Prepare a script to ease apt setup
1117cat > $ENV{'PBPROJ'}.sources.list << EOT
1118deb $rpd $dver contrib
1119deb-src $rpd $dver contrib
1120EOT
1121chmod 644 $ENV{'PBPROJ'}.sources.list
1122
1123# Prepare a script to create apt info file
1124(cd .. ; for a in i386 amd64 ia64; do mkdir -p dists/$dver/contrib/binary-\$a; dpkg-scanpackages -a\$a $dver /dev/null | gzip -c9 > dists/$dver/contrib/binary-\$a/Packages.gz; done; mkdir -p dists/$dver/contrib/source; dpkg-scansources $dver /dev/null | gzip -c9 > dists/$dver/contrib/source/Sources.gz)
1125#(cd .. ; rm -f dists/$dver/Release ; apt-ftparchive release dists/$dver > dists/$dver/Release; gpg --sign -ba -o dists/$dver/Release.gpg dists/$dver/Release)
1126EOF
1127                }
1128                close(PBS);
1129                chmod 0755,"$ENV{'PBBUILDDIR'}/pbscript";
1130
1131        } else {
1132                return;
1133        }
1134
1135        # Useless for VE
1136        my $nport;
1137        if ($cmt ne "ve") {
1138                $nport = $sshport->{$ENV{'PBPROJ'}};
1139                $nport = "$pbport" if (defined $pbport);
1140        }
1141
1142        # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
1143        $tdir =~ s|\$ENV.+\}/||;
1144
1145        my $tm = $vtmout->{$ENV{'PBPROJ'}};
1146
1147        # ssh communication if not VE
1148        # should use a hash instead...
1149        my ($shcmd,$cpcmd,$cptarget,$cp2target);
1150        if ($cmt ne "ve") {
1151                my $keyfile = pb_ssh_get(0);
1152                $shcmd = "ssh -i $keyfile -q -o UserKnownHostsFile=/dev/null -p $nport $mac";
1153                $cpcmd = "scp -i $keyfile -p -o UserKnownHostsFile=/dev/null -P $nport";
1154                $cptarget = "$mac:$tdir";
1155                if ($cmt eq "vm") {
1156                        $cp2target = "$mac:$bdir";
1157                }
1158        } else {
1159                my $tp = $vepath->{$ENV{'PBPROJ'}};
1160                $shcmd = "sudo chroot $tp/$v /bin/su - $sshlogin->{$ENV{'PBPROJ'}} -c ";
1161                $cpcmd = "cp -a ";
1162                $cptarget = "$tp/$tdir";
1163                $cp2target = "$tp/$bdir";
1164        }
1165
1166        my $logres = "";
1167        # Do not touch when just announcing
1168        if ($cmt ne "Announce") {
1169                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");
1170        } else {
1171                $logres = "> ";
1172        }
1173        pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
1174
1175        # For VE we need to change the owner manually - To be tested if needed
1176        #if ($cmt eq "ve") {
1177        #pb_system("cd $cptarget ; sudo chown -R $sshlogin->{$ENV{'PBPROJ'}} .","$cmt chown in $cptarget to $sshlogin->{$ENV{'PBPROJ'}}");
1178        #}
1179        pb_system("$shcmd \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi ; rm -f ./pbscript\' | bash\"","Executing pbscript on $cptarget if needed","verbose");
1180        if (($cmt eq "vm") || ($cmt eq "ve")) {
1181                # Get back info on pkg produced, compute their name and get them from the VM
1182                pb_system("$cpcmd $cp2target/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'} $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target");
1183                open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1184                my $src = <KEEP>;
1185                chomp($src);
1186                close(KEEP);
1187                $src =~ s/^ *//;
1188                pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
1189                # Change pgben to make the next send2target happy
1190                my $made = "";
1191                open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1192                foreach my $p (split(/ +/,$src)) {
1193                        my $j = basename($p);
1194                        pb_system("$cpcmd $cp2target/\'$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $cp2target");
1195                        $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
1196                }
1197                print KEEP "$made\n";
1198                close(KEEP);
1199                pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
1200
1201                # We want to send them to the ssh account so overwrite what has been done before
1202                undef $pbaccount;
1203                pb_log(2,"Before sending pkgs, vmexist: $vmexist, vmpid: $vmpid\n");
1204                pb_send2target("Packages",$odir."-".$over."-".$oarch,$vmexist,$vmpid);
1205                pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
1206        }
1207        pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
1208        if ((! $vmexist) && (($cmt eq "vm") || ($cmt eq "Script"))) {
1209                pb_system("$shcmd \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $v halt (pid $vmpid)");
1210        }
1211}
1212
1213sub pb_script2v {
1214        my $pbscript=shift;
1215        my $vtype=shift;
1216        my $force=shift || 0;   # Force stop of VM. Default not
1217        my $vm1=shift || undef; # Only that VM to treat
1218        my $vm;
1219        my $all;
1220
1221        pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$force,$vm1)\n");
1222        # Prepare the script to be executed on the VM
1223        # in $ENV{'PBDESTDIR'}/pbscript
1224        if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
1225                copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1226                chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1227        }
1228
1229        if (not defined $vm1) {
1230                ($vm,$all) = pb_get_v($vtype);
1231        } else {
1232                @$vm = ($vm1);
1233        }
1234        my ($vmexist,$vmpid) = (undef,undef);
1235
1236        foreach my $v (@$vm) {
1237                # Launch the VM/VE
1238                if ($vtype eq "vm") {
1239                        ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
1240                        pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
1241
1242                        # Skip that VM if something went wrong
1243                        next if (($vmpid == 0) && ($vmexist == 0));
1244
1245                        # If force stopping the VM then reset vmexist
1246                        if ($force == 1) {
1247                                $vmpid = $vmexist;
1248                                $vmexist = 0;
1249                        }
1250                }
1251
1252                # Gather all required files to send them to the VM
1253                # and launch the build through pbscript
1254                pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
1255                pb_send2target("Script","$v",$vmexist,$vmpid);
1256
1257        }
1258}
1259
1260sub pb_launchv {
1261        my $vtype = shift;
1262        my $v = shift;
1263        my $create = shift || 0;                # By default do not create a VM
1264
1265        pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create)\n");
1266        die "No VM/VE defined, unable to launch" if (not defined $v);
1267        # Keep only the first VM in case many were given
1268        $v =~ s/,.*//;
1269
1270        my $arch = pb_get_arch();
1271
1272        # Launch the VMs/VEs
1273        if ($vtype eq "vm") {
1274                die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
1275
1276                my ($ptr,$vmopt,$vmpath,$vmport,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmpath","vmport","vmtmout","vmsize");
1277
1278                my $vmtype = $ptr->{$ENV{'PBPROJ'}};
1279                if (not defined $ENV{'PBVMOPT'}) {
1280                        $ENV{'PBVMOPT'} = "";
1281                }
1282                # Set a default timeout of 2 minutes
1283                if (not defined $ENV{'PBVMTMOUT'}) {
1284                        $ENV{'PBVMTMOUT'} = "120";
1285                }
1286                if (defined $vmopt->{$v}) {
1287                        $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
1288                } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
1289                        $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
1290                }
1291                if (defined $vmtmout->{$v}) {
1292                        $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
1293                } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
1294                        $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
1295                }
1296                my $nport = $vmport->{$ENV{'PBPROJ'}};
1297                $nport = "$pbport" if (defined $pbport);
1298       
1299                my $cmd;
1300                my $vmcmd;              # has to be used for pb_check_ps
1301                my $vmm;                # has to be used for pb_check_ps
1302                if ($vmtype eq "qemu") {
1303                        my $qemucmd32;
1304                        my $qemucmd64;
1305                        if ($arch eq "x86_64") {
1306                                $qemucmd32 = "/usr/bin/qemu-system-i386";
1307                                $qemucmd64 = "/usr/bin/qemu";
1308                        } else {
1309                                $qemucmd32 = "/usr/bin/qemu";
1310                                $qemucmd64 = "/usr/bin/qemu-system-x86_64";
1311                        }
1312                if ($v =~ /x86_64/) {
1313                                $vmcmd = "$qemucmd64 -no-kqemu";
1314                        } else {
1315                                $vmcmd = "$qemucmd32";
1316                        }
1317                        $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
1318                        if ($create != 0) {
1319                                $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
1320                        }
1321                        $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
1322                } elsif ($vmtype eq "xen") {
1323                } elsif ($vmtype eq "vmware") {
1324                } else {
1325                        die "VM of type $vmtype not supported. Report to the dev team";
1326                }
1327                my ($tmpcmd,$void) = split(/ +/,$cmd);
1328                my $vmexist = pb_check_ps($tmpcmd,$vmm);
1329                my $vmpid = 0;
1330                if (! $vmexist) {
1331                        if ($create != 0) {
1332                                if (($vmtype eq "qemu") || ($vmtype eq "xen")) {
1333                                        pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
1334                                } elsif ($vmtype eq "vmware") {
1335                                } else {
1336                                }
1337                        }
1338                        if (! -f "$vmm") {
1339                                pb_log(0,"Unable to find VM $vmm\n");
1340                        } else {
1341                                pb_system("$cmd &","Launching the VM $vmm");
1342                                pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up");
1343                                $vmpid = pb_check_ps($tmpcmd,$vmm);
1344                                pb_log(0,"VM $vmm launched (pid $vmpid)\n");
1345                        }
1346                } else {
1347                        pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
1348                }
1349                pb_log(2,"DEBUG: pb_launchv returns ($vmexist,$vmpid)\n");
1350                return($vmexist,$vmpid);
1351        # VE here
1352        } else {
1353                # Get VE context
1354                my ($ptr,$vetmout,$vepath,$verebuild,$veconf) = pb_conf_get("vetype","vetmout","vepath","verebuild","veconf");
1355                my $vetype = $ptr->{$ENV{'PBPROJ'}};
1356
1357                # Get distro context
1358                my ($name,$ver,$darch) = split(/-/,$v);
1359                chomp($darch);
1360                my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
1361
1362                if ($vetype eq "chroot") {
1363                        # Architecture consistency
1364                        if ($arch ne $darch) {
1365                                die "Unable to launch a VE of architecture $darch on a $arch platform" if (not (($darch eq "x86_64") && ($arch =~ /i?86/)));
1366                        }
1367
1368                        if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($force == 1)) {
1369                                # We have to rebuild the chroot
1370                                if ($dtype eq "rpm") {
1371                                        pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
1372                                        # Once setup we need to install some packages, the pb account, ...
1373                                        pb_system("sudo /usr/sbin/mock --install --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
1374                                        #pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" --basedir=\"$vepath->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
1375                                } elsif ($dtype eq "deb") {
1376                                        pb_system("","Creating the pbuilder VE");
1377                                } elsif ($dtype eq "ebuild") {
1378                                        die "Please teach the dev team how to build gentoo chroot";
1379                                } else {
1380                                        die "Unknown distribution type $dtype. Report to dev team";
1381                                }
1382                        }
1383                        # Nothing more to do for VE. No real launch
1384                } else {
1385                        die "VE of type $vetype not supported. Report to the dev team";
1386                }
1387        }
1388}
1389
1390sub pb_build2v {
1391
1392my $vtype = shift;
1393
1394# Prepare the script to be executed on the VM/VE
1395# in $ENV{'PBDESTDIR'}/pbscript
1396#my ($ntp) = pb_conf_get($vtype."ntp");
1397#my $vntp = $ntp->{$ENV{'PBPROJ'}};
1398
1399open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1400print SCRIPT "#!/bin/bash\n";
1401print SCRIPT "echo ... Execution needed\n";
1402print SCRIPT "# This is in directory delivery\n";
1403print SCRIPT "# Setup the variables required for building\n";
1404print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
1405print SCRIPT "# Preparation for pb\n";
1406print SCRIPT "mv .pbrc \$HOME\n";
1407print SCRIPT "cd ..\n";
1408# Force new date to be in the future compared to the date of the tar file by adding 1 minute
1409my @date=pb_get_date();
1410$date[1]++;
1411my $upddate = strftime("%m%d%H%M%Y", @date);
1412#print SCRIPT "echo Setting up date on $vntp...\n";
1413# Or use ntpdate if available TBC
1414print SCRIPT "sudo date $upddate\n";
1415# Get list of packages to build and get some ENV vars as well
1416my $ptr = pb_get_pkg();
1417@pkgs = @$ptr;
1418my $p = join(' ',@pkgs) if (@pkgs);
1419print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
1420print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
1421print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
1422print SCRIPT "# Build\n";
1423print SCRIPT "echo Building packages on $vtype...\n";
1424print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
1425close(SCRIPT);
1426chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1427
1428my ($v,$all) = pb_get_v($vtype);
1429
1430# Send tar files when we do a global generation
1431pb_build2ssh() if ($all == 1);
1432
1433my ($vmexist,$vmpid) = (undef,undef);
1434
1435foreach my $v (@$v) {
1436        if ($vtype eq "vm") {
1437                # Launch the VM
1438                ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
1439
1440                # Skip that VM if it something went wrong
1441                next if (($vmpid == 0) && ($vmexist == 0));
1442        }
1443        # Gather all required files to send them to the VM/VE
1444        # and launch the build through pbscript
1445        pb_log(2,"Calling send2target $vtype,$v,$vmexist,$vmpid\n");
1446        pb_send2target($vtype,"$v",$vmexist,$vmpid);
1447}
1448}
1449
1450
1451sub pb_newver {
1452
1453        die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
1454
1455        # Need this call for PBDIR
1456        my ($scheme2,$uri) = pb_cms_init($pbinit);
1457
1458        my ($pbconf) = pb_conf_get("pbconfurl");
1459        $uri = $pbconf->{$ENV{'PBPROJ'}};
1460        my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
1461
1462        # Checking CMS repositories status
1463        my ($pburl) = pb_conf_get("pburl");
1464        ($scheme2, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
1465
1466        if ($scheme !~ /^svn/) {
1467                die "Only SVN is supported at the moment";
1468        }
1469
1470        my $res = pb_cms_isdiff($scheme,$ENV{'PBROOTDIR'});
1471        die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
1472
1473        $res = pb_cms_isdiff($scheme2,$ENV{'PBDIR'});
1474        die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
1475
1476        # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
1477        # we want to get for the root of the new URL
1478
1479        my $tmp = $ENV{'PBROOTDIR'};
1480        $tmp =~ s|^$ENV{'PBCONFDIR'}||;
1481
1482        my $newurl = "$uri/".dirname($tmp)."/$newver";
1483        # Should probably use projver in the old file
1484        my $oldver= basename($tmp);
1485
1486        # Duplicate and extract project-builder part
1487        pb_log(2,"Copying $uri/$tmp to $newurl\n");
1488        pb_cms_copy($scheme,"$uri/$tmp",$newurl);
1489        pb_log(2,"Checkout $newurl to $ENV{'PBROOTDIR'}/../$newver\n");
1490        pb_cms_up($scheme,"$ENV{'PBCONFDIR'}/..");
1491
1492        # Duplicate and extract project
1493        my $newurl2 = "$pburl->{$ENV{'PBPROJ'}}/".dirname($tmp)."/$newver";
1494
1495        pb_log(2,"Copying $pburl->{$ENV{'PBPROJ'}}/$tmp to $newurl2\n");
1496        pb_cms_copy($scheme,"$pburl->{$ENV{'PBPROJ'}}/$tmp",$newurl2);
1497        pb_log(2,"Checkout $newurl2 to $ENV{'PBDIR'}/../$newver\n");
1498        pb_cms_up($scheme,"$ENV{'PBDIR'}/..");
1499
1500        # Update the .pb file
1501        open(FILE,"$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb";
1502        open(OUT,"> $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new";
1503        while(<FILE>) {
1504                s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
1505                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/);
1506                s/^testver/#testver/;
1507                pb_log(0,"Commenting testver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^testver/);
1508                print OUT $_;
1509        }
1510        close(FILE);
1511        close(OUT);
1512        rename("$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb");
1513
1514        # Checking pbcl files
1515        foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
1516                open(PBCL,$f) || die "Unable to open $f";
1517                my $foundnew = 0;
1518                while (<PBCL>) {
1519                        $foundnew = 1 if (/^$newver \(/);
1520                }
1521                close(PBCL);
1522                open(OUT,"> $f.new") || die "Unable to write to $f.new: $!";
1523                open(PBCL,$f) || die "Unable to open $f";
1524                while (<PBCL>) {
1525                        print OUT "$_" if (not /^$oldver \(/);
1526                        if ((/^$oldver \(/) && ($foundnew == 0)) {
1527                                print OUT "$newver ($pbdate)\n";
1528                                print OUT "- TBD\n";
1529                                print OUT "\n";
1530                                pb_log(0,"WARNING: version $newver not found in $f so added...") if ($foundnew == 0);
1531                        }
1532                }
1533                close(OUT);
1534                close(PBCL);
1535                rename("$f.new","$f");
1536        }
1537
1538        pb_log(2,"Checkin $ENV{'PBROOTDIR'}/../$newver\n");
1539        pb_cms_checkin($scheme,"$ENV{'PBROOTDIR'}/../$newver",undef);
1540}
1541
1542#
1543# Return the list of VMs/VEs we are working on
1544# $all is a flag to know if we return all of them
1545# or only some (if all we publish also tar files in addition to pkgs
1546#
1547sub pb_get_v {
1548
1549my $vtype = shift;
1550my @v;
1551my $all = 0;
1552my $vlist;
1553my $pbv = 'PBV';
1554
1555if ($vtype eq "vm") {
1556        $vlist = "vmlist";
1557} elsif ($vtype eq "ve") {
1558        $vlist = "velist";
1559}
1560# Get VM/VE list
1561if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
1562        my ($ptr) = pb_conf_get($vlist);
1563        $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
1564        $all = 1;
1565}
1566pb_log(2,"$vtype: $ENV{$pbv}\n");
1567@v = split(/,/,$ENV{$pbv});
1568return(\@v,$all);
1569}
1570
1571# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
1572# Needs to use root account to connect to the VM/VE
1573# pb will take your local public SSH key to access
1574# the pb account in the VM later on if needed
1575sub pb_setup_v {
1576
1577my $vtype = shift;
1578
1579my ($vm,$all) = pb_get_v($vtype);
1580
1581# Script generated
1582my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
1583
1584foreach my $v (@$vm) {
1585        # Name of the account to deal with for VM/VE
1586        # Do not use the one passed potentially with -a
1587        my ($pbac) = pb_conf_get($vtype."login");
1588        my ($key,$zero0,$zero1,$zero2);
1589        my ($vmexist,$vmpid,$ntps);
1590
1591        if ($vtype eq "vm") {
1592                # Prepare the key to be used and transfered remotely
1593                my $keyfile = pb_ssh_get(1);
1594               
1595                my ($vmhost,$vmport,$vmntp) = pb_conf_get("vmhost","vmport","vmntp");
1596                my $nport = $vmport->{$ENV{'PBPROJ'}};
1597                $ntps = $vmntp->{$ENV{'PBPROJ'}};
1598                $nport = "$pbport" if (defined $pbport);
1599       
1600                # Launch the VM
1601                ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
1602
1603                # Skip that VM if something went wrong
1604                next if (($vmpid == 0) && ($vmexist == 0));
1605       
1606                # Store the pub key part in a variable
1607                open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
1608                ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
1609                close(FILE);
1610
1611                $key = "\Q$zero1";
1612
1613                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\"","Copying local keys to $vtype. This may require the root password");
1614                # once this is done, we can do what we want on the VM remotely
1615        }
1616       
1617        # Prepare the script to be executed on the VM/VE
1618        # in $ENV{'PBDESTDIR'}/setupv
1619       
1620        open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
1621        print SCRIPT << 'EOF';
1622#!/usr/bin/perl -w
1623
1624use strict;
1625use File::Copy;
1626
1627# We should not need in this script more functions than what is provided
1628# by Base and Distribution to avoid problems at exec time.
1629# They are appended at the end.
1630
1631our $pbdebug;
1632our $pbLOG;
1633our $pbsynmsg = "pbscript";
1634our $pbdisplaytype = "text";
1635our $pblocale = "";
1636pb_log_init($pbdebug, $pbLOG);
1637pb_temp_init();
1638
1639EOF
1640        if ($vtype eq "vm") {
1641                print SCRIPT << 'EOF';
1642# Removes duplicate in .ssh/authorized_keys of our key if needed
1643#
1644my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
1645open(PBFILE,$file1) || die "Unable to open $file1";
1646open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
1647my $count = 0;
1648while (<PBFILE>) {
1649
1650EOF
1651                print SCRIPT << "EOF";
1652        if (/ $key /) {
1653                \$count++;
1654        }
1655print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
1656}
1657close(PBFILE);
1658close(PBOUT);
1659rename("\$file1.new",\$file1);
1660chmod 0600,\$file1;
1661
1662# Sync date
1663pb_system("/usr/sbin/ntpdate $ntps","Syncing date to $ntps");
1664
1665EOF
1666        }
1667        print SCRIPT << 'EOF';
1668
1669# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
1670#
1671my $file="/etc/passwd";
1672open(PBFILE,$file) || die "Unable to open $file";
1673my $found = 0;
1674while (<PBFILE>) {
1675EOF
1676        print SCRIPT << "EOF";
1677        \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
1678EOF
1679        print SCRIPT << 'EOF';
1680}
1681close(PBFILE);
1682
1683if ( $found == 0 ) {
1684        if ( ! -d "/home" ) {
1685                pb_mkdir("/home");
1686        }
1687EOF
1688        print SCRIPT << "EOF";
1689pb_system("groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
1690pb_system("useradd $pbac->{$ENV{'PBPROJ'}} -g $pbac->{$ENV{'PBPROJ'}} -m -d /home/$pbac->{$ENV{'PBPROJ'}}","Adding user $pbac->{$ENV{'PBPROJ'}} (group $pbac->{$ENV{'PBPROJ'}} - home /home/$pbac->{$ENV{'PBPROJ'}}");
1691}
1692
1693# allow ssh entry to build
1694#
1695mkdir "/home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
1696# Allow those accessing root to access the build account
1697copy("\$ENV{'HOME'}/.ssh/authorized_keys","/home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
1698chmod 0600,".ssh/authorized_keys";
1699pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} /home/$pbac->{$ENV{'PBPROJ'}}/.ssh","Finish setting up the SSH env for $pbac->{$ENV{'PBPROJ'}}");
1700
1701EOF
1702        print SCRIPT << 'EOF';
1703# No passwd for build account only keys
1704$file="/etc/shadow";
1705open(PBFILE,$file) || die "Unable to open $file";
1706open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1707while (<PBFILE>) {
1708EOF
1709        print SCRIPT << "EOF";
1710        s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
1711        s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;     #SLES 9 e.g.
1712EOF
1713        print SCRIPT << 'EOF';
1714        print PBOUT $_;
1715}
1716close(PBFILE);
1717close(PBOUT);
1718rename("$file.new",$file);
1719chmod 0640,$file;
1720
1721# Keep the VM in text mode
1722$file="/etc/inittab";
1723if (-f $file) {
1724        open(PBFILE,$file) || die "Unable to open $file";
1725        open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1726        while (<PBFILE>) {
1727                s/^(..):5:initdefault:$/$1:3:initdefault:/;
1728                print PBOUT $_;
1729        }
1730        close(PBFILE);
1731        close(PBOUT);
1732        rename("$file.new",$file);
1733        chmod 0640,$file;
1734}
1735
1736# pb has to be added to portage group on gentoo
1737
1738# Adapt sudoers
1739$file="/etc/sudoers";
1740open(PBFILE,$file) || die "Unable to open $file";
1741open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1742while (<PBFILE>) {
1743EOF
1744        print SCRIPT << "EOF";
1745        next if (/^$pbac->{$ENV{'PBPROJ'}}   /);
1746EOF
1747        print SCRIPT << 'EOF';
1748        s/Defaults[ \t]+requiretty//;
1749        print PBOUT $_;
1750}
1751close(PBFILE);
1752EOF
1753        print SCRIPT << "EOF";
1754# This is needed in order to be able to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
1755print PBOUT "$pbac->{$ENV{'PBPROJ'}}   ALL=(ALL) NOPASSWD:ALL\n";
1756EOF
1757        print SCRIPT << 'EOF';
1758close(PBOUT);
1759rename("$file.new",$file);
1760chmod 0440,$file;
1761
1762EOF
1763               
1764        my $SCRIPT = \*SCRIPT;
1765       
1766        pb_install_deps($SCRIPT);
1767       
1768        print SCRIPT << 'EOF';
1769# Suse wants sudoers as 640
1770if (($ddir eq "sles") || (($ddir eq "suse")) && ($dver =~ /10.[012]/)) {
1771        chmod 0640,$file;
1772}
1773
1774pb_system("rm -rf perl-ProjectBuilder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/perl-ProjectBuilder-latest.tar.gz ; tar xvfz perl-ProjectBuilder-latest.tar.gz ; cd perl-ProjectBuilder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf perl-ProjectBuilder-* ; rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf project-builder-* ;","Building Project-Builder");
1775system "pb 2>&1 | head -5";
1776EOF
1777        # Adds pb_distro_init from ProjectBuilder::Distribution and Base
1778        foreach my $d (@INC) {
1779                my @f = ("$d/ProjectBuilder/Base.pm","$d/ProjectBuilder/Distribution.pm");
1780                foreach my $f (@f) {
1781                        if (-f "$f") {
1782                                open(PBD,"$f") || die "Unable to open $f";
1783                                while (<PBD>) {
1784                                                next if (/^package/);
1785                                                next if (/^use Exporter/);
1786                                                next if (/^use ProjectBuilder::/);
1787                                                next if (/^our /);
1788                                        print SCRIPT $_;
1789                                }
1790                                close(PBD);
1791                        }
1792                }
1793        }
1794        close(SCRIPT);
1795        chmod 0755,"$pbscript";
1796
1797        # That build script needs to be run as root and force stop of VM at end
1798        $pbaccount = "root";
1799
1800        # Force shutdown of VM exept if it was already launched
1801        my $force = 0;
1802        if ((! $vmexist) && ($vtype eq "vm")) {
1803                $force = 1;
1804        }
1805       
1806        pb_script2v($pbscript,$vtype,$force,$v);
1807}
1808return;
1809}
1810
1811sub pb_install_deps {
1812
1813my $SCRIPT = shift;
1814
1815print {$SCRIPT} << 'EOF';
1816# We need to have that pb_distro_init function
1817# Get it from Project-Builder::Distribution
1818my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init(); 
1819print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n";
1820
1821# Get and install pb
1822my $insdm = "rm -rf Date-Manip* ; wget http://search.cpan.org/CPAN/authors/id/S/SB/SBECK/Date-Manip-5.54.tar.gz ; tar xvfz Date-Manip-5.54.tar.gz ; cd Date-Manip* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf Date-Manip*";
1823my $insmb = "rm -rf Module-Build* ; wget http://search.cpan.org/CPAN/authors/id/K/KW/KWILLIAMS/Module-Build-0.2808.tar.gz ; tar xvfz Module-Build-0.2808.tar.gz ; cd Module-Build* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf Module-Build*";
1824my $insfm = "rm -rf File-MimeInfo* ; wget http://search.cpan.org/CPAN/authors/id/P/PA/PARDUS/File-MimeInfo/File-MimeInfo-0.15.tar.gz ; tar xvfz File-MimeInfo-0.15.tar.gz ; cd File-MimeInfo* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf File-MimeInfo*";
1825my $insfb = "rm -rf File-Basedir* ; wget http://search.cpan.org/CPAN/authors/id/P/PA/PARDUS/File-BaseDir-0.03.tar.gz ; tar xvfz File-BaseDir-0.03.tar.gz ; cd File-BaseDir* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf File-BaseDir*";
1826my $insms = "rm -rf Mail-Sendmail* ; wget http://search.cpan.org/CPAN/authors/id/M/MI/MIVKOVIC/Mail-Sendmail-0.79.tar.gz ; tar xvfz Mail-Sendmail-0.79.tar.gz ; cd Mail-Sendmail* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf Mail-Sendmail*";
1827my $inslg = "rm -rf gettext* ; wget http://search.cpan.org/CPAN/authors/id/P/PV/PVANDRY/gettext-1.05.tar.gz ; tar xvfz gettext-1.05.tar.gz ; cd gettext* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf gettext*";
1828my $cmtdm = "Installing Date-Manip perl module";
1829my $cmtmb = "Installing Module-Build perl module";
1830my $cmtfm = "Installing File-MimeInfo perl module";
1831my $cmtfb = "Installing File-Basedir perl module";
1832my $cmtms = "Installing Perl-Sendmail perl module";
1833my $cmtlg = "Installing Perl-Locale-gettext perl module";
1834my $cmtall = "Installing required modules";
1835
1836if ( $ddir eq "fedora" ) {
1837        pb_system("yum clean all","Cleaning yum env");
1838        #system "yum update -y";
1839        my $arch=`uname -m`;
1840        my $opt = "";
1841        chomp($arch);
1842        if ($arch eq "x86_64") {
1843                $opt="--exclude=*.i?86";
1844        }
1845
1846        if ($dver eq 4) {
1847                pb_system("yum -y $opt install rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker",$cmtall);
1848                pb_system("$insmb","$cmtmb");
1849                pb_system("$insfm","$cmtfm");
1850                pb_system("$insfb","$cmtfb");
1851                pb_system("$insms","$cmtms");
1852                pb_system("$inslg","$cmtlg");
1853        } else {
1854                pb_system("yum -y $opt install rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker perl-File-MimeInfo perl-Mail-Sendmail",$cmtall);
1855                pb_system("$inslg","$cmtlg");
1856        }
1857} elsif (( $dfam eq "rh" ) || ($ddir eq "sles") || (($ddir eq "suse") && (($dver eq "10.1") || ($dver eq "10.0"))) || ($ddir eq "slackware")) {
1858        # Suppose pkg are installed already as no online mirror available
1859        pb_system("rpm -e lsb 2>&1 > /dev/null","Removing lsb package");
1860        pb_system("$insdm","$cmtdm");
1861        pb_system("$insmb","$cmtmb");
1862        pb_system("$insfm","$cmtfm");
1863        pb_system("$insfb","$cmtfb");
1864        pb_system("$insms","$cmtms");
1865        pb_system("$inslg","$cmtlg");
1866} elsif ($ddir eq "suse") { 
1867        # New OpenSuSE
1868        pb_system("$insmb","$cmtmb");
1869        pb_system("$insfm","$cmtfm");
1870        pb_system("$insfb","$cmtfb");
1871        pb_system("$insms","$cmtms");
1872        pb_system("export TERM=linux ; liste=\"\" ; for i in make wget patch sudo perl-DateManip perl-File-HomeDir perl-Mail-Sendmail ntp; do rpm -q \$i 1> /dev/null 2> /dev/null ; if [ \$\? != 0 ]; then liste=\"\$liste \$i\"; fi; done; echo \"Liste: \$liste\" ; if [ \"\$liste\" != \"\" ]; then yast2 -i \$liste ; fi","$cmtall");
1873} elsif ( $dfam eq "md" ) {
1874                pb_system("urpmi.update -a ; urpmi --auto rpm-build wget sudo patch ntp-client perl-File-MimeInfo perl-Mail-Sendmail perl-Locale-gettext","$cmtall");
1875                if (($ddir eq "mandrake") && ($dver eq "10.1")) {
1876                        pb_system("$insdm","$cmtdm");
1877                        pb_system("$inslg","$cmtlg");
1878                } else {
1879                        pb_system("urpmi --auto perl-DateManip","$cmtdm");
1880                        pb_system("urpmi --auto perl-Locale-gettext","$cmtdm");
1881                }
1882} elsif ( $dfam eq "du" ) {
1883        if (( $dver eq "3.1" ) && ($ddir eq "debian")) {
1884                #system "apt-get update";
1885                pb_system("$insfb","$cmtfb");
1886                pb_system("$insfm","$cmtfm");
1887                pb_system("apt-get -y install wget patch ssh sudo debian-builder dh-make fakeroot ntpdate libmodule-build-perl libdate-manip-perl libmail-sendmail-perl liblocale-gettext-perl","$cmtall");
1888        } else  {
1889                pb_system("apt-get update; apt-get -y install wget patch openssh-server dpkg-dev sudo debian-builder dh-make fakeroot ntpdate libfile-mimeinfo-perl libmodule-build-perl libdate-manip-perl libmail-sendmail-perl liblocale-gettext-perl","$cmtall");
1890        }
1891} elsif ( $dfam eq "gen" ) {
1892                #system "emerge -u system";
1893                pb_system("emerge wget sudo ntp DateManip File-MimeInfo Mail-Sendmail Locale-gettext","$cmtall");
1894} else {
1895        pb_log(0,"No pkg to install\n");
1896}
1897EOF
1898}
1899
1900sub pb_announce {
1901
1902        # Get all required parameters
1903        my ($pbpackager,$pbrepo,$pbml,$pbsmtp) = pb_conf_get("pbpackager","pbrepo","pbml","pbsmtp");
1904        my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
1905        my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
1906        my @pkgs = @$pkg;
1907        my %pkgs;
1908        my $first = 0;
1909
1910        # Command to find packages on repo
1911        my $findstr = "find . ";
1912        # Generated announce files
1913        my @files;
1914
1915        foreach my $pbpkg (@pkgs) {
1916                if ($first != 0) {
1917                        $findstr .= "-o ";
1918                }
1919                $first++;
1920                if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
1921                        $pbver = $pkgv->{$pbpkg};
1922                } else {
1923                        $pbver = $ENV{'PBPROJVER'};
1924                }
1925                if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
1926                        $pbtag = $pkgt->{$pbpkg};
1927                } else {
1928                        $pbtag = $ENV{'PBPROJTAG'};
1929                }
1930
1931                $findstr .= "-name \'$pbpkg-$pbver-$pbtag\.*.rpm\' -o -name \'$pbpkg"."_$pbver*\.deb\' -o -name \'$pbpkg-$pbver\.ebuild\' ";
1932
1933                my $chglog;
1934
1935                # Get project info on log file and generate tmp files used later on
1936                pb_cms_init($pbinit);
1937                $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
1938                $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
1939                $chglog = undef if (! -f $chglog);
1940
1941                open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
1942                pb_changelog("announce",$pbpkg,$pbver,"N/A","N/A","N/A",\*OUT,"yes",$chglog);
1943                close(OUT);
1944                push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
1945        }
1946        $findstr .= " | grep -Ev \'src.rpm\'";
1947        if ((not defined $testver) || (not defined $testver->{$ENV{'PBPROJ'}}) || ($testver->{$ENV{'PBPROJ'}} !~ /true/i)) {
1948                $findstr .= " | grep -v ./test/";
1949        }
1950
1951        # Prepare the command to run and execute it
1952        open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
1953        print PBS "$findstr\n";
1954        close(PBS);
1955        chmod 0755,"$ENV{'PBTMP'}/pbscript";
1956        pb_send2target("Announce");
1957
1958        # Get subject line
1959        my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
1960        pb_log(0,"Please enter the title of your announce\n");
1961        pb_log(0,"(By default: $sl)\n");
1962        my $sl2 = <STDIN>;
1963        $sl = $sl2 if ($sl2 !~ /^$/);
1964
1965        # Prepare a template of announce
1966        open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
1967        print ANN << "EOF";
1968$sl</p>
1969
1970<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
1971<p>
1972Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
1973</p>
1974<p>
1975EOF
1976        open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to read $ENV{'PBTMP'}/system.log: $!";
1977        my $col = 2;
1978        my $i = 1;
1979        print ANN << 'EOF';
1980<TABLE WIDTH="700" CELLPADDING="0" CELLSPACING="0" BORDER="0">
1981<TR>
1982EOF
1983        while (<LOG>) {
1984                print ANN "<TD>$_</TD>";
1985                $i++;
1986                if ($i > $col) {
1987                        print ANN "</TR>\n<TR>";
1988                        $i = 1;
1989                }
1990        }
1991        close(LOG);
1992        print ANN << "EOF";
1993</TR>
1994</TABLE>
1995</p>
1996
1997<p>As usual source packages are also available in the same directory.</p>
1998
1999<p>
2000Changes are :
2001</p>
2002<p>
2003EOF
2004        # Get each package changelog content
2005        foreach my $f (sort(@files)) {
2006                open(IN,"$f") || die "Unable to read $f:$!";
2007                while (<IN>) {
2008                        print ANN $_;
2009                }
2010                close(IN);
2011                print ANN "</p><p>\n";
2012        }
2013        print ANN "</p>\n";
2014        close(ANN);
2015
2016        # Allow for modification
2017        pb_system("vi $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
2018
2019        # Store it in DB for external usage (Web pages generation)
2020        my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
2021
2022        my $precmd = "";
2023        if (! -f $db) {
2024                $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
2025        }
2026
2027        my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
2028                        { RaiseError => 1, AutoCommit => 1 })
2029                        || die "Unable to connect to $db";
2030
2031        if ($precmd ne "") {
2032                my $sth = $dbh->prepare(qq{$precmd})
2033                        || die "Unable to create table into $db";
2034                $sth->execute();
2035        }
2036
2037        # To read whole file
2038        local $/;
2039        open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2040        my $announce = <ANN>;
2041        close(ANN);
2042       
2043        pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
2044        my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
2045                        || die "Unable to insert into $db";
2046        $sth->execute($pbdate, $announce);
2047        $dbh->disconnect;
2048
2049        # Then deliver it on the Web
2050        # $TOOLHOME/livwww www
2051
2052        # Mail it to project's ML
2053        open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
2054        print ML << 'EOF';
2055<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
2056
2057<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
2058  <head>
2059  </head>
2060  <body>
2061  <p>
2062EOF
2063        open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2064        while(<ANN>) {
2065                print ML $_;
2066        }
2067        print ML << 'EOF';
2068</body>
2069</html>
2070EOF
2071        close(ML);
2072
2073        # To read whole file
2074        local $/;
2075        open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
2076        my $msg = <ANN>;
2077        close(ANN);
2078       
2079        # Preparation of headers
2080
2081        my %mail = (   
2082                        To                      =>      $pbml->{$ENV{'PBPROJ'}},
2083                        From            =>      $pbpackager->{$ENV{'PBPROJ'}},
2084                        Smtp            =>      $pbsmtp->{$ENV{'PBPROJ'}},
2085                        Body            =>      $msg,
2086                        Subject         =>      "[ANNOUNCE] $sl",
2087                );
2088                       
2089        # Send mail
2090        sendmail(%mail) or die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
2091}
2092
2093# Return the SSH key file to use
2094# Potentially create it if needed
2095
2096sub pb_ssh_get {
2097
2098my $create = shift || 0;        # Do not create keys by default
2099
2100# Check the SSH environment
2101my $keyfile = undef;
2102
2103# We have specific keys by default
2104$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa";
2105if (!(-e $keyfile) && ($create eq 1)) {
2106        pb_system("ssh-keygen -q -b 1024 -N '' -f $keyfile -t dsa","Generating SSH keys for pb");
2107}
2108
2109$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
2110$keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if (-s "$ENV{'HOME'}/.ssh/id_dsa");
2111$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
2112die "Unable to find your public ssh key under $keyfile" if (not defined $keyfile);
2113return($keyfile);
2114}
2115
2116
2117# Returns the pid of a running VM command using a specific VM file
2118sub pb_check_ps {
2119        my $vmcmd = shift;
2120        my $vmm = shift;
2121        my $vmexist = 0;                # FALSE by default
2122
2123        open(PS, "ps auxhww|") || die "Unable to call ps";
2124        while (<PS>) {
2125                next if (! /$vmcmd/);
2126                next if (! /$vmm/);
2127                my ($void1, $void2);
2128                ($void1, $vmexist, $void2) = split(/ +/);
2129                last;
2130        }
2131        return($vmexist);
2132}
2133
2134
2135sub pb_extract_build_files {
2136
2137my $src=shift;
2138my $dir=shift;
2139my $ddir=shift;
2140my $mandatory=shift || "spec";
2141my @files;
2142
2143my $flag = "mayfail" if ($mandatory eq "patch");
2144my $res;
2145
2146if ($src =~ /tar\.gz$/) {
2147        $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
2148} elsif ($src =~ /tar\.bz2$/) {
2149        $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
2150} else {
2151        die "Unknown compression algorithm for $src";
2152}
2153# If not mandatory return now
2154return() if (($res != 0) and ($mandatory eq "patch"));
2155opendir(DIR,"$dir") || die "Unable to open directory $dir";
2156foreach my $f (readdir(DIR)) {
2157        next if ($f =~ /^\./);
2158        # Skip potential patch dir
2159        next if ($f =~ /^pbpatch/);
2160        move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
2161        pb_log(2,"mv $dir/$f $ddir\n");
2162        push @files,"$ddir/$f";
2163}
2164closedir(DIR);
2165# Not enough but still a first cleanup
2166pb_rm_rf("$dir");
2167return(@files);
2168}
2169
2170sub pb_list_bfiles {
2171
2172my $dir = shift;
2173my $pbpkg = shift;
2174my $bfiles = shift;
2175my $pkgfiles = shift;
2176my $supfiles = shift;
2177
2178opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
2179foreach my $f (readdir(BDIR)) {
2180        next if ($f =~ /^\./);
2181        $bfiles->{$f} = "$dir/$f";
2182        $bfiles->{$f} =~ s~$ENV{'PBROOTDIR'}~~;
2183        if (defined $supfiles->{$pbpkg}) {
2184                $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
2185        }
2186}
2187closedir(BDIR);
2188}
2189
2190
2191#
2192# Return the list of packages we are working on in a non CMS action
2193#
2194sub pb_get_pkg {
2195
2196my @pkgs = ();
2197
2198my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
2199@pkgs = keys %$var;
2200
2201pb_log(0,"Packages: ".join(',',@pkgs)."\n");
2202return(\@pkgs);
2203}
2204
2205# Which is our local arch ? (standardize on i386 for those platforms)
2206sub pb_get_arch {
2207
2208my $arch = `uname -m`;
2209chomp($arch);
2210$arch =~ s/i.86/i386/;
2211return($arch);
2212}
2213
22141;
Note: See TracBrowser for help on using the repository browser.