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

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