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

Last change on this file since 537 was 537, checked in by Bruno Cornec, 16 years ago

Fix #31: pb now preserves by default original tar files got by http or ftp to allow for checksum consistency.

  • Property svn:executable set to *
File size: 70.3 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 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 # Clean up dest if necessary. The export will recreate it
486 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
487 pb_rm_rf($dest) if (-d $dest);
488
489 # Export CMS tree for the concerned package to dest
490 # And generate some additional files
491 $OUTPUT_AUTOFLUSH=1;
492
493 # computes in which dir we have to work
494 my $dir = $defpkgdir->{$pbpkg};
495 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
496 pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");
497
498 # Exporting content from CMS
499 my $preserve = pb_cms_export($uri,"$ENV{'PBDIR'}/$dir",$dest);
500
501 # Generated fake content for test versions to speed up stuff
502 my ($testver) = pb_conf_get_if("testver");
503 my $chglog;
504
505 # Get project info on authors and log file
506 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
507 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
508 $chglog = undef if (! -f $chglog);
509
510 my $authors = "$ENV{'PBROOTDIR'}/$pbpkg/pbauthors";
511 $authors = "$ENV{'PBROOTDIR'}/pbauthors" if (! -f $authors);
512 $authors = "/dev/null" if (! -f $authors);
513
514 # Extract cms log history and store it
515 if ((defined $chglog) && (! -f "$dest/NEWS")) {
516 pb_log(2,"Generating NEWS file from $chglog\n");
517 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
518 }
519 pb_cms_log($scheme,"$ENV{'PBDIR'}/$dir",$dest,$chglog,$authors,$testver);
520
521 my %build;
522 my @pt;
523 my $tmpl = "";
524 my %patches;
525
526 @pt = pb_conf_get_if("vmlist","velist");
527 if (defined $pt[0]->{$ENV{'PBPROJ'}}) {
528 $tmpl .= $pt[0]->{$ENV{'PBPROJ'}};
529 }
530 if (defined $pt[1]->{$ENV{'PBPROJ'}}) {
531 # the 2 lists needs to be grouped with a ',' separated them
532 if ($tmpl ne "") {
533 $tmpl .= ",";
534 }
535 $tmpl .= $pt[1]->{$ENV{'PBPROJ'}}
536 }
537
538 # Setup %pb structure to allow filtering later on on files using that structure
539 $pb{'tag'} = $pbtag;
540 $pb{'rev'} = $pbrev;
541 $pb{'pkg'} = $pbpkg;
542 $pb{'ver'} = $pbver;
543 $pb{'date'} = $pbdate;
544 $pb{'defpkgdir'} = $defpkgdir;
545 $pb{'extpkgdir'} = $extpkgdir;
546 $pb{'chglog'} = $chglog;
547 $pb{'packager'} = $ENV{'PBPACKAGER'};
548 $pb{'proj'} = $ENV{'PBPROJ'};
549 $pb{'repo'} = $ENV{'PBREPO'};
550 $pb{'patches'} = \%patches;
551 pb_log(2,"DEBUG: pb: ".Dumper(%pb)."\n");
552
553 foreach my $d (split(/,/,$tmpl)) {
554 my ($name,$ver,$arch) = split(/-/,$d);
555 chomp($arch);
556 my ($ddir, $dver, $dfam);
557 ($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'}) = pb_distro_init($name,$ver);
558 pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'})."\n");
559 pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
560
561 # Filter build files from the less precise up to the most with overloading
562 # Filter all files found, keeping the name, and generating in dest
563
564 # Find all build files first relatively to PBROOTDIR
565 # Find also all specific files referenced in the .pb conf file
566 my %bfiles = ();
567 my %pkgfiles = ();
568 $build{"$ddir-$dver-$arch"} = "yes";
569
570 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}") {
571 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
572 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$dfam") {
573 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
574 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir") {
575 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
576 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver") {
577 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
578 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch") {
579 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
580 } else {
581 $build{"$ddir-$dver-$arch"} = "no";
582 next;
583 }
584 pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");
585
586 # Get all filters to apply
587 my $ptr = pb_get_filters($pbpkg, $pb{'dtype'}, $dfam, $ddir, $dver);
588
589 # Prepare local patches for this distro - They are always applied first - May be a problem one day
590 foreach my $p (sort(<$ENV{'PBROOTDIR'}/$pbpkg/pbpatch/*>)) {
591 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.all$/));
592 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.all$/);
593 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$pb{'dtype'}$/));
594 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$pb{'dtype'}$/);
595 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$dfam$/));
596 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$dfam$/);
597 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir$/));
598 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir$/);
599 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver$/));
600 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver$/);
601 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver-$arch$/));
602 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver-$arch$/);
603 }
604
605 # Prepare also remote patches to be included - Applied after the local ones
606 foreach my $p ("all","$pb{'dtype'}","$dfam","$ddir","$ddir-$dver","$ddir-$dver-$arch") {
607 my $f = "$ENV{'PBROOTDIR'}/$pbpkg/pbextpatch.$p";
608 next if (not -f $f);
609 if (not open(PATCH,$f)) {
610 pb_display("Unable to open existing external patch file content $f\n");
611 next;
612 }
613 while (<PATCH>) {
614 chomp();
615 $patches{"$ddir-$dver-$arch"} .= "," if (defined $patches{"$ddir-$dver-$arch"});
616 $patches{"$ddir-$dver-$arch"} .= "$_";
617 }
618 close(PATCH);
619 }
620 pb_log(2,"DEBUG: pb->patches: ".Dumper($pb{'patches'})."\n");
621
622 # Apply now all the filters on all the files concerned
623 # destination dir depends on the type of file
624 if (defined $ptr) {
625 # For patch support
626 $pb{'tuple'} = "$ddir-$dver-$arch";
627 foreach my $f (values %bfiles,values %pkgfiles) {
628 pb_filter_file("$ENV{'PBROOTDIR'}/$f",$ptr,"$dest/pbconf/$ddir-$dver-$arch/".basename($f),\%pb);
629 }
630 }
631 }
632 my @found;
633 my @notfound;
634 foreach my $b (keys %build) {
635 push @found,$b if ($build{$b} =~ /yes/);
636 push @notfound,$b if ($build{$b} =~ /no/);
637 }
638 pb_log(0,"Build files generated for ".join(',',sort(@found))."\n");
639 pb_log(0,"No Build files found for ".join(',',sort(@notfound))."\n") if (@notfound);
640 pb_log(2,"DEBUG: patches: ".Dumper(%patches)."\n");
641
642 # Get the generic filter (all.pbf) and
643 # apply those to the non-build files including those
644 # generated by pbinit if applicable
645
646 # Get only all.pbf filter
647 my $ptr = pb_get_filters($pbpkg);
648
649 my $liste ="";
650 if (defined $filteredfiles->{$pbpkg}) {
651 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
652 pb_filter_file_inplace($ptr,"$dest/$f",\%pb);
653 $liste = "$f $liste";
654 }
655 }
656 pb_log(2,"Files ".$liste."have been filtered\n");
657
658 # Filter potential patches (local + remote)
659 pb_log(0,"Delivering and compressing patches ");
660 foreach my $v (keys %patches) {
661 pb_mkdir_p("$dest/pbconf/$v/pbpatch");
662 foreach my $pf (split(/,/,$patches{$v})) {
663 my $pp = basename($pf);
664 pb_cms_export($pf,undef,"$dest/pbconf/$v/pbpatch");
665 pb_filter_file_inplace($ptr,"$dest/pbconf/$v/pbpatch/$pp",\%pb);
666 pb_system("gzip -9f $dest/pbconf/$v/pbpatch/$pp","","quiet");
667 }
668 pb_log(0,"$patches{$v} ");
669 }
670 pb_log(0,"\n");
671
672 # Prepare the dest directory for archive
673 if (-x "$ENV{'PBROOTDIR'}/$pbpkg/pbinit") {
674 pb_filter_file("$ENV{'PBROOTDIR'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",\%pb);
675 chmod 0755,"$ENV{'PBTMP'}/pbinit";
676 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOTDIR'}/$pbpkg/pbinit","verbose");
677 }
678
679 # Archive dest dir
680 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
681 if (defined $preserve) {
682 # In that case we want to preserve the original tar file for checksum purposes
683 # The one created is btw equivalent in that case to this one
684 # Maybe check basename of both to be sure they are the same ?
685 pb_log(0,"Preserving original tar file ");
686 move("$preserve","$pbpkg-$pbver.tar.gz");
687 } else {
688 # Possibility to look at PBSRC to guess more the filename
689 pb_system("tar cfz $pbpkg-$pbver.tar.gz --exclude=$pbpkg-$pbver/pbconf $pbpkg-$pbver","Creating $pbpkg tar files compressed");
690 }
691 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");
692 pb_system("tar cfz $pbpkg-$pbver.pbconf.tar.gz $pbpkg-$pbver/pbconf","Creating pbconf tar files compressed");
693 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz\n");
694
695 # Keep track of version-tag per pkg
696 $pkgs{$pbpkg} = "$pbver-$pbtag";
697
698 # Final cleanup
699 pb_rm_rf($dest) if (-d $dest);
700 }
701
702 # Keep track of per package version
703 pb_log(2,"DEBUG pkgs: ".Dumper(%pkgs)."\n");
704 open(PKG,"> $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb";
705 foreach my $pbpkg (@pkgs) {
706 print PKG "pbpkg $pbpkg = $pkgs{$pbpkg}\n";
707 }
708 close(PKG);
709
710 # Keep track of what is generated by default
711 # We need to store the dir and info on version-tag
712 # Base our content on the existing .pb file
713 copy("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb","$ENV{'PBDESTDIR'}/pbrc");
714 open(LAST,">> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
715 print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOTDIR'}\n";
716 print LAST "pbprojver $ENV{'PBPROJ'} = $ENV{'PBPROJVER'}\n";
717 print LAST "pbprojtag $ENV{'PBPROJ'} = $ENV{'PBPROJTAG'}\n";
718 print LAST "pbpackager $ENV{'PBPROJ'} = $ENV{'PBPACKAGER'}\n";
719 close(LAST);
720}
721
722sub pb_build2pkg {
723
724 # Get the running distro to build on
725 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
726 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
727
728 # Get list of packages to build
729 # Get content saved in cms2build
730 my $ptr = pb_get_pkg();
731 @pkgs = @$ptr;
732
733 my $arch = pb_get_arch();
734
735 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
736 $pkg = { } if (not defined $pkg);
737
738 chdir "$ENV{'PBBUILDDIR'}";
739 my $made = ""; # pkgs made during build
740 foreach my $pbpkg (@pkgs) {
741 my $vertag = $pkg->{$pbpkg};
742 # get the version of the current package - maybe different
743 ($pbver,$pbtag) = split(/-/,$vertag);
744
745 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
746 my $src2="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
747 pb_log(2,"Source file: $src\n");
748 pb_log(2,"Pbconf file: $src2\n");
749
750 pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
751 if ($dtype eq "rpm") {
752 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
753 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
754 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";
755 }
756 }
757
758 # Remove in case a previous link/file was there
759 unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
760 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
761 # We need to first extract the spec file
762 my @specfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$ENV{'PBBUILDDIR'}/SPECS","spec");
763
764 # We need to handle potential patches to upstream sources
765 pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/pbpatch/","$ENV{'PBBUILDDIR'}/SOURCES","patch");
766
767 pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
768 # set LANGUAGE to check for correct log messages
769 $ENV{'LANGUAGE'}="C";
770 # Older Redhat use _target_platform in %configure incorrectly
771 my $specialdef = "";
772 if (($ddir eq "redhat") || (($ddir eq "rhel") && ($dver eq "2.1"))) {
773 $specialdef = "--define \'_target_platform \"\"\'";
774 }
775 foreach my $f (@specfile) {
776 if ($f =~ /\.spec$/) {
777 pb_system("rpmbuild $specialdef --define \'packager $ENV{'PBPACKAGER'}\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}","verbose");
778 last;
779 }
780 }
781 $made="$made RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm";
782 if (-f "/usr/bin/rpmlint") {
783 pb_system("rpmlint $made","Checking validity of rpms with rpmlint","verbose");
784 }
785 } elsif ($dtype eq "deb") {
786 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
787 pb_system("tar xfz $src","Extracting sources");
788 pb_system("tar xfz $src2","Extracting pbconf");
789
790 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
791 pb_rm_rf("debian");
792 symlink "pbconf/$ddir-$dver-$arch","debian" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
793 chmod 0755,"debian/rules";
794 if ($dver !~ /[0-9]/) {
795 # dpkg-deb doesn't accept non digit versions. removing checks
796 # dpkg-source checks upper case when generating perl modules
797 }
798 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package","verbose");
799 $made="$made $pbpkg"."_*.deb $pbpkg"."_*.dsc $pbpkg"."_*.tar.gz";
800 if (-f "/usr/bin/lintian") {
801 pb_system("lintian $made","Checking validity of debs with lintian","verbose");
802 }
803 } elsif ($dtype eq "ebuild") {
804 my @ebuildfile;
805 # For gentoo we need to take pb as subsystem name
806 # We put every apps here under sys-apps. hope it's correct
807 # We use pb's home dir in order to have a single OVERLAY line
808 my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
809 pb_mkdir_p($tmpd) if (! -d "$tmpd");
810 pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
811
812 # We need to first extract the ebuild file
813 @ebuildfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$tmpd","ebuild");
814
815 # Prepare the build env for gentoo
816 my $found = 0;
817 my $pbbd = $ENV{'HOME'};
818 $pbbd =~ s|/|\\/|g;
819 if (-r "/etc/make.conf") {
820 open(MAKE,"/etc/make.conf");
821 while (<MAKE>) {
822 $found = 1 if (/$pbbd\/portage/);
823 }
824 close(MAKE);
825 }
826 if ($found == 0) {
827 pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
828 }
829 #$found = 0;
830 #if (-r "/etc/portage/package.keywords") {
831 #open(KEYW,"/etc/portage/package.keywords");
832 #while (<KEYW>) {
833 #$found = 1 if (/portage\/pb/);
834 #}
835 #close(KEYW);
836 #}
837 #if ($found == 0) {
838 #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
839 #}
840
841 # Build
842 foreach my $f (@ebuildfile) {
843 if ($f =~ /\.ebuild$/) {
844 move($f,"$tmpd/$pbpkg-$pbver.ebuild");
845 pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package","verbose");
846 # Now move it where pb expects it
847 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
848 move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
849 }
850 }
851
852 $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver.ebuild";
853 } elsif ($dtype eq "tgz") {
854 # Slackware family
855 $made="$made $pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
856
857 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
858 pb_system("tar xfz $src","Extracting sources");
859 pb_system("tar xfz $src2","Extracting pbconf");
860 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
861 symlink "pbconf/$ddir-$dver-$arch","install" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
862 if (-x "install/pbslack") {
863 pb_system("./install/pbslack","Building package");
864 pb_system("sudo /sbin/makepkg -p -l y -c y $pbpkg","Packaging $pbpkg","verbose");
865 }
866 } else {
867 die "Unknown dtype format $dtype";
868 }
869 }
870 # Keep track of what is generated so that we can get them back from VMs
871 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
872 print KEEP "$made\n";
873 close(KEEP);
874}
875
876sub pb_build2ssh {
877 pb_send2target("Sources");
878}
879
880sub pb_pkg2ssh {
881 pb_send2target("Packages");
882}
883
884# By default deliver to the the public site hosting the
885# ftp structure (or whatever) or a VM/VE
886sub pb_send2target {
887
888 my $cmt = shift;
889 my $v = shift || undef;
890 my $vmexist = shift || 0; # 0 is FALSE
891 my $vmpid = shift || 0; # 0 is FALSE
892
893 pb_log(2,"DEBUG: pb_send2target($cmt,".Dumper($v).",$vmexist,$vmpid)\n");
894 my $host = "sshhost";
895 my $login = "sshlogin";
896 my $dir = "sshdir";
897 my $port = "sshport";
898 my $conf = "sshconf";
899 my $rebuild = "sshrebuild";
900 my $tmout = "vmtmout";
901 my $path = "vmpath";
902 if (($cmt eq "vm") || ($cmt eq "Script")) {
903 $login = "vmlogin";
904 $dir = "pbdefdir";
905 $tmout = "vmtmout";
906 $rebuild = "vmrebuild";
907 # Specific VM
908 $host = "vmhost";
909 $port = "vmport";
910 } elsif ($cmt eq "ve") {
911 $login = "velogin";
912 $dir = "pbdefdir";
913 $tmout = "vetmout";
914 # Specific VE
915 $path = "vepath";
916 $conf = "veconf";
917 $rebuild = "verebuild";
918 }
919 my $cmd = "";
920 my $src = "";
921 my ($odir,$over,$oarch) = (undef, undef, undef);
922 my ($ddir, $dver, $dfam, $dtype, $pbsuf);
923
924 if ($cmt ne "Announce") {
925 my $ptr = pb_get_pkg();
926 @pkgs = @$ptr;
927
928 # Get the running distro to consider
929 if (defined $v) {
930 ($odir,$over,$oarch) = split(/-/,$v);
931 }
932 ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
933 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
934
935 # Get list of packages to build
936 # Get content saved in cms2build
937 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
938 $pkg = { } if (not defined $pkg);
939
940 chdir "$ENV{'PBBUILDDIR'}";
941 foreach my $pbpkg (@pkgs) {
942 my $vertag = $pkg->{$pbpkg};
943 # get the version of the current package - maybe different
944 ($pbver,$pbtag) = split(/-/,$vertag);
945
946 if (($cmt eq "Sources") || ($cmt eq "vm") || ($cmt eq "ve")) {
947 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
948 if ($cmd eq "") {
949 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
950 } else {
951 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
952 }
953 }
954 }
955 # Adds conf file for availability of conf elements
956 pb_conf_add("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb");
957 }
958
959 if (($cmt eq "vm") || ($cmt eq "ve")) {
960 $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript";
961 } elsif ($cmt eq "Script") {
962 $src="$src $ENV{'PBDESTDIR'}/pbscript";
963 } elsif ($cmt eq "Announce") {
964 $src="$src $ENV{'PBTMP'}/pbscript";
965 } elsif ($cmt eq "Packages") {
966 # Get package list from file made during build2pkg
967 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
968 $src = <KEEP>;
969 chomp($src);
970 close(KEEP);
971 $src="$src $ENV{'PBBUILDDIR'}/pbscript" if ($cmt ne "Sources");
972 }
973 # Remove potential leading spaces (cause problem with basename)
974 $src =~ s/^ *//;
975 my $basesrc = "";
976 foreach my $i (split(/ +/,$src)) {
977 $basesrc .= " ".basename($i);
978 }
979
980 pb_log(0,"Sources handled ($cmt): $src\n");
981 pb_log(2,"values: ".Dumper(($host,$login,$dir,$port,$tmout,$rebuild,$path,$conf))."\n");
982 my ($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vepath) = pb_conf_get($host,$login,$dir,$port,$tmout,$path);
983 my ($vrebuild,$veconf) = pb_conf_get_if($rebuild,$conf);
984 pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf))."\n");
985 # Not mandatory
986 my ($testver) = pb_conf_get_if("testver");
987
988 my $mac;
989 # Useless for VE
990 if ($cmt ne "ve") {
991 $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
992 # Overwrite account value if passed as parameter
993 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
994 pb_log(2, "DEBUG: pbaccount: $pbaccount => mac: $mac\n") if (defined $pbaccount);
995 }
996
997 my $tdir;
998 my $bdir;
999 if (($cmt eq "Sources") || ($cmt eq "Script")) {
1000 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/src";
1001 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1002 # This is a test pkg => target dir is under test
1003 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/src";
1004 }
1005 } elsif (($cmt eq "vm") || ($cmt eq "ve")) {
1006 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery";
1007 $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build";
1008 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
1009 $bdir =~ s|\$ENV.+\}/||;
1010 } elsif ($cmt eq "Announce") {
1011 $tdir = "$sshdir->{$ENV{'PBPROJ'}}";
1012 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1013 # This is a test pkg => target dir is under test
1014 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test";
1015 }
1016 } elsif ($cmt eq "Packages") {
1017 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ddir/$dver";
1018
1019 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1020 # This is a test pkg => target dir is under test
1021 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/$ddir/$dver";
1022 }
1023
1024 my $repodir = $tdir;
1025 $repodir =~ s|^$sshdir->{$ENV{'PBPROJ'}}/||;
1026
1027 my ($pbrepo) = pb_conf_get("pbrepo");
1028
1029 # Repository management
1030 open(PBS,"> $ENV{'PBBUILDDIR'}/pbscript") || die "Unable to create $ENV{'PBBUILDDIR'}/pbscript";
1031 if ($dtype eq "rpm") {
1032 # Also make a pbscript to generate yum/urpmi bases
1033 print PBS << "EOF";
1034#!/bin/bash
1035# Prepare a script to ease yum setup
1036cat > $ENV{'PBPROJ'}.repo << EOT
1037[$ENV{'PBPROJ'}]
1038name=$ddir $dver - $ENV{'PBPROJ'} Vanilla Packages
1039baseurl=$pbrepo->{$ENV{'PBPROJ'}}/$repodir
1040enabled=1
1041gpgcheck=0
1042EOT
1043chmod 644 $ENV{'PBPROJ'}.repo
1044
1045# Clean up old repo content
1046rm -rf headers/ repodata/
1047# Create yum repo
1048yum-arch .
1049# Create repodata
1050createrepo .
1051EOF
1052 if ($dfam eq "md") {
1053 # For Mandriva add urpmi management
1054 print PBS << "EOF";
1055# Prepare a script to ease urpmi setup
1056cat > $ENV{'PBPROJ'}.addmedia << EOT
1057urpmi.addmedia $ENV{'PBPROJ'} $pbrepo->{$ENV{'PBPROJ'}}/$repodir with hdlist.cz
1058EOT
1059chmod 755 $ENV{'PBPROJ'}.addmedia
1060
1061# Clean up old repo content
1062rm -f hdlist.cz synthesis.hdlist.cz
1063# Create urpmi repo
1064genhdlist .
1065EOF
1066 }
1067 if ($ddir eq "fedora") {
1068 # Extract the spec file to please Fedora maintainers :-(
1069 print PBS << "EOF";
1070for p in $basesrc; do
1071 echo \$p | grep -q 'src.rpm'
1072 if [ \$\? -eq 0 ]; then
1073 rpm2cpio \$p | cpio -ivdum --quiet '*.spec'
1074 fi
1075done
1076EOF
1077 }
1078 } elsif ($dtype eq "deb") {
1079 # Also make a pbscript to generate apt bases
1080 # Cf: http://www.debian.org/doc/manuals/repository-howto/repository-howto.fr.html
1081 my $rpd = dirname("$pbrepo->{$ENV{'PBPROJ'}}/$repodir");
1082 print PBS << "EOF";
1083#!/bin/bash
1084# Prepare a script to ease apt setup
1085cat > $ENV{'PBPROJ'}.sources.list << EOT
1086deb $rpd $dver contrib
1087deb-src $rpd $dver contrib
1088EOT
1089chmod 644 $ENV{'PBPROJ'}.sources.list
1090
1091# Prepare a script to create apt info file
1092(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)
1093#(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)
1094EOF
1095 }
1096 close(PBS);
1097 chmod 0755,"$ENV{'PBBUILDDIR'}/pbscript";
1098
1099 } else {
1100 return;
1101 }
1102
1103 # Useless for VE
1104 my $nport;
1105 if ($cmt ne "ve") {
1106 $nport = $sshport->{$ENV{'PBPROJ'}};
1107 $nport = "$pbport" if (defined $pbport);
1108 }
1109
1110 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
1111 $tdir =~ s|\$ENV.+\}/||;
1112
1113 my $tm = $vtmout->{$ENV{'PBPROJ'}};
1114
1115 # ssh communication if not VE
1116 # should use a hash instead...
1117 my ($shcmd,$cpcmd,$cptarget,$cp2target);
1118 if ($cmt ne "ve") {
1119 my $keyfile = pb_ssh_get(0);
1120 $shcmd = "ssh -i $keyfile -q -o UserKnownHostsFile=/dev/null -p $nport $mac";
1121 $cpcmd = "scp -i $keyfile -p -o UserKnownHostsFile=/dev/null -P $nport";
1122 $cptarget = "$mac:$tdir";
1123 if ($cmt eq "vm") {
1124 $cp2target = "$mac:$bdir";
1125 }
1126 } else {
1127 my $tp = $vepath->{$ENV{'PBPROJ'}};
1128 $shcmd = "sudo chroot $tp/$v /bin/su - $sshlogin->{$ENV{'PBPROJ'}} -c ";
1129 $cpcmd = "cp -a ";
1130 $cptarget = "$tp/$tdir";
1131 $cp2target = "$tp/$bdir";
1132 }
1133
1134 my $logres = "";
1135 # Do not touch when just announcing
1136 if ($cmt ne "Announce") {
1137 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");
1138 } else {
1139 $logres = "> ";
1140 }
1141 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
1142
1143 # For VE we need to change the owner manually - To be tested if needed
1144 #if ($cmt eq "ve") {
1145 #pb_system("cd $cptarget ; sudo chown -R $sshlogin->{$ENV{'PBPROJ'}} .","$cmt chown in $cptarget to $sshlogin->{$ENV{'PBPROJ'}}");
1146 #}
1147 pb_system("$shcmd \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi ; rm -f ./pbscript\' | bash\"","Executing pbscript on $cptarget if needed","verbose");
1148 if (($cmt eq "vm") || ($cmt eq "ve")) {
1149 # Get back info on pkg produced, compute their name and get them from the VM
1150 pb_system("$cpcmd $cp2target/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'} $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target");
1151 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1152 my $src = <KEEP>;
1153 chomp($src);
1154 close(KEEP);
1155 $src =~ s/^ *//;
1156 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
1157 # Change pgben to make the next send2target happy
1158 my $made = "";
1159 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1160 foreach my $p (split(/ +/,$src)) {
1161 my $j = basename($p);
1162 pb_system("$cpcmd $cp2target/\'$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $cp2target");
1163 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
1164 }
1165 print KEEP "$made\n";
1166 close(KEEP);
1167 pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
1168
1169 # We want to send them to the ssh account so overwrite what has been done before
1170 undef $pbaccount;
1171 pb_log(2,"Before sending pkgs, vmexist: $vmexist, vmpid: $vmpid\n");
1172 pb_send2target("Packages",$odir."-".$over."-".$oarch,$vmexist,$vmpid);
1173 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
1174 }
1175 pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
1176 if ((! $vmexist) && (($cmt eq "vm") || ($cmt eq "Script"))) {
1177 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)");
1178 }
1179}
1180
1181sub pb_script2v {
1182 my $pbscript=shift;
1183 my $vtype=shift;
1184 my $force=shift || 0; # Force stop of VM. Default not
1185 my $vm1=shift || undef; # Only that VM to treat
1186 my $vm;
1187 my $all;
1188
1189 pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$force,$vm1)\n");
1190 # Prepare the script to be executed on the VM
1191 # in $ENV{'PBDESTDIR'}/pbscript
1192 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
1193 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1194 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1195 }
1196
1197 if (not defined $vm1) {
1198 ($vm,$all) = pb_get_v($vtype);
1199 } else {
1200 @$vm = ($vm1);
1201 }
1202 my ($vmexist,$vmpid) = (undef,undef);
1203
1204 foreach my $v (@$vm) {
1205 # Launch the VM/VE
1206 if ($vtype eq "vm") {
1207 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
1208 pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
1209
1210 # Skip that VM if something went wrong
1211 next if (($vmpid == 0) && ($vmexist == 0));
1212
1213 # If force stopping the VM then reset vmexist
1214 if ($force == 1) {
1215 $vmpid = $vmexist;
1216 $vmexist = 0;
1217 }
1218 }
1219
1220 # Gather all required files to send them to the VM
1221 # and launch the build through pbscript
1222 pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
1223 pb_send2target("Script","$v",$vmexist,$vmpid);
1224
1225 }
1226}
1227
1228sub pb_launchv {
1229 my $vtype = shift;
1230 my $v = shift;
1231 my $create = shift || 0; # By default do not create a VM
1232
1233 pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create)\n");
1234 die "No VM/VE defined, unable to launch" if (not defined $v);
1235 # Keep only the first VM in case many were given
1236 $v =~ s/,.*//;
1237
1238 my $arch = pb_get_arch();
1239
1240 # Launch the VMs/VEs
1241 if ($vtype eq "vm") {
1242 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
1243
1244 my ($ptr,$vmopt,$vmpath,$vmport,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmpath","vmport","vmtmout","vmsize");
1245
1246 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
1247 if (not defined $ENV{'PBVMOPT'}) {
1248 $ENV{'PBVMOPT'} = "";
1249 }
1250 # Set a default timeout of 2 minutes
1251 if (not defined $ENV{'PBVMTMOUT'}) {
1252 $ENV{'PBVMTMOUT'} = "120";
1253 }
1254 if (defined $vmopt->{$v}) {
1255 $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
1256 } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
1257 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
1258 }
1259 if (defined $vmtmout->{$v}) {
1260 $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
1261 } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
1262 $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
1263 }
1264 my $nport = $vmport->{$ENV{'PBPROJ'}};
1265 $nport = "$pbport" if (defined $pbport);
1266
1267 my $cmd;
1268 my $vmcmd; # has to be used for pb_check_ps
1269 my $vmm; # has to be used for pb_check_ps
1270 if ($vmtype eq "qemu") {
1271 my $qemucmd32;
1272 my $qemucmd64;
1273 if ($arch eq "x86_64") {
1274 $qemucmd32 = "/usr/bin/qemu-system-i386";
1275 $qemucmd64 = "/usr/bin/qemu";
1276 } else {
1277 $qemucmd32 = "/usr/bin/qemu";
1278 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
1279 }
1280 if ($v =~ /x86_64/) {
1281 $vmcmd = "$qemucmd64 -no-kqemu";
1282 } else {
1283 $vmcmd = "$qemucmd32";
1284 }
1285 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
1286 if ($create != 0) {
1287 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
1288 }
1289 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
1290 } elsif ($vmtype eq "xen") {
1291 } elsif ($vmtype eq "vmware") {
1292 } else {
1293 die "VM of type $vmtype not supported. Report to the dev team";
1294 }
1295 my ($tmpcmd,$void) = split(/ +/,$cmd);
1296 my $vmexist = pb_check_ps($tmpcmd,$vmm);
1297 my $vmpid = 0;
1298 if (! $vmexist) {
1299 if ($create != 0) {
1300 if (($vmtype eq "qemu") || ($vmtype eq "xen")) {
1301 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
1302 } elsif ($vmtype eq "vmware") {
1303 } else {
1304 }
1305 }
1306 if (! -f "$vmm") {
1307 pb_log(0,"Unable to find VM $vmm\n");
1308 } else {
1309 pb_system("$cmd &","Launching the VM $vmm");
1310 pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up");
1311 $vmpid = pb_check_ps($tmpcmd,$vmm);
1312 pb_log(0,"VM $vmm launched (pid $vmpid)\n");
1313 }
1314 } else {
1315 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
1316 }
1317 pb_log(2,"DEBUG: pb_launchv returns ($vmexist,$vmpid)\n");
1318 return($vmexist,$vmpid);
1319 # VE here
1320 } else {
1321 # Get VE context
1322 my ($ptr,$vetmout,$vepath,$verebuild,$veconf) = pb_conf_get("vetype","vetmout","vepath","verebuild","veconf");
1323 my $vetype = $ptr->{$ENV{'PBPROJ'}};
1324
1325 # Get distro context
1326 my ($name,$ver,$darch) = split(/-/,$v);
1327 chomp($darch);
1328 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
1329
1330 if ($vetype eq "chroot") {
1331 # Architecture consistency
1332 if ($arch ne $darch) {
1333 die "Unable to launch a VE of architecture $darch on a $arch platform" if (not (($darch eq "x86_64") && ($arch =~ /i?86/)));
1334 }
1335
1336 if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($force == 1)) {
1337 # We have to rebuild the chroot
1338 if ($dtype eq "rpm") {
1339 pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
1340 # Once setup we need to install some packages, the pb account, ...
1341 pb_system("sudo /usr/sbin/mock --install --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
1342 #pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" --basedir=\"$vepath->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
1343 } elsif ($dtype eq "deb") {
1344 pb_system("","Creating the pbuilder VE");
1345 } elsif ($dtype eq "ebuild") {
1346 die "Please teach the dev team how to build gentoo chroot";
1347 } else {
1348 die "Unknown distribution type $dtype. Report to dev team";
1349 }
1350 }
1351 # Nothing more to do for VE. No real launch
1352 } else {
1353 die "VE of type $vetype not supported. Report to the dev team";
1354 }
1355 }
1356}
1357
1358sub pb_build2v {
1359
1360my $vtype = shift;
1361
1362# Prepare the script to be executed on the VM/VE
1363# in $ENV{'PBDESTDIR'}/pbscript
1364#my ($ntp) = pb_conf_get($vtype."ntp");
1365#my $vntp = $ntp->{$ENV{'PBPROJ'}};
1366
1367open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1368print SCRIPT "#!/bin/bash\n";
1369print SCRIPT "echo ... Execution needed\n";
1370print SCRIPT "# This is in directory delivery\n";
1371print SCRIPT "# Setup the variables required for building\n";
1372print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
1373print SCRIPT "# Preparation for pb\n";
1374print SCRIPT "mv .pbrc \$HOME\n";
1375print SCRIPT "cd ..\n";
1376# Force new date to be in the future compared to the date of the tar file by adding 1 minute
1377my @date=pb_get_date();
1378$date[1]++;
1379my $upddate = strftime("%m%d%H%M%Y", @date);
1380#print SCRIPT "echo Setting up date on $vntp...\n";
1381# Or use ntpdate if available TBC
1382print SCRIPT "sudo date $upddate\n";
1383# Get list of packages to build and get some ENV vars as well
1384my $ptr = pb_get_pkg();
1385@pkgs = @$ptr;
1386my $p = join(' ',@pkgs) if (@pkgs);
1387print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
1388print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
1389print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
1390print SCRIPT "# Build\n";
1391print SCRIPT "echo Building packages on $vtype...\n";
1392print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
1393close(SCRIPT);
1394chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1395
1396my ($v,$all) = pb_get_v($vtype);
1397
1398# Send tar files when we do a global generation
1399pb_build2ssh() if ($all == 1);
1400
1401my ($vmexist,$vmpid) = (undef,undef);
1402
1403foreach my $v (@$v) {
1404 if ($vtype eq "vm") {
1405 # Launch the VM
1406 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
1407
1408 # Skip that VM if it something went wrong
1409 next if (($vmpid == 0) && ($vmexist == 0));
1410 }
1411 # Gather all required files to send them to the VM/VE
1412 # and launch the build through pbscript
1413 pb_log(2,"Calling send2target $vtype,$v,$vmexist,$vmpid\n");
1414 pb_send2target($vtype,"$v",$vmexist,$vmpid);
1415}
1416}
1417
1418
1419sub pb_newver {
1420
1421 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
1422
1423 # Need this call for PBDIR
1424 my ($scheme2,$uri) = pb_cms_init($pbinit);
1425
1426 my ($pbconf) = pb_conf_get("pbconfurl");
1427 $uri = $pbconf->{$ENV{'PBPROJ'}};
1428 my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
1429
1430 # Checking CMS repositories status
1431 my ($pburl) = pb_conf_get("pburl");
1432 ($scheme2, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
1433
1434 if ($scheme !~ /^svn/) {
1435 die "Only SVN is supported at the moment";
1436 }
1437
1438 my $res = pb_cms_isdiff($scheme,$ENV{'PBROOTDIR'});
1439 die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
1440
1441 $res = pb_cms_isdiff($scheme2,$ENV{'PBDIR'});
1442 die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
1443
1444 # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
1445 # we want to get for the root of the new URL
1446
1447 my $tmp = $ENV{'PBROOTDIR'};
1448 $tmp =~ s|^$ENV{'PBCONFDIR'}||;
1449
1450 my $newurl = "$uri/".dirname($tmp)."/$newver";
1451 # Should probably use projver in the old file
1452 my $oldver= basename($tmp);
1453
1454 # Duplicate and extract project-builder part
1455 pb_log(2,"Copying $uri/$tmp to $newurl\n");
1456 pb_cms_copy($scheme,"$uri/$tmp",$newurl);
1457 pb_log(2,"Checkout $newurl to $ENV{'PBROOTDIR'}/../$newver\n");
1458 pb_cms_up($scheme,"$ENV{'PBCONFDIR'}/..");
1459
1460 # Duplicate and extract project
1461 my $newurl2 = "$pburl->{$ENV{'PBPROJ'}}/".dirname($tmp)."/$newver";
1462
1463 pb_log(2,"Copying $pburl->{$ENV{'PBPROJ'}}/$tmp to $newurl2\n");
1464 pb_cms_copy($scheme,"$pburl->{$ENV{'PBPROJ'}}/$tmp",$newurl2);
1465 pb_log(2,"Checkout $newurl2 to $ENV{'PBDIR'}/../$newver\n");
1466 pb_cms_up($scheme,"$ENV{'PBDIR'}/..");
1467
1468 # Update the .pb file
1469 open(FILE,"$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb";
1470 open(OUT,"> $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new";
1471 while(<FILE>) {
1472 s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
1473 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/);
1474 s/^testver/#testver/;
1475 pb_log(0,"Commenting testver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^testver/);
1476 print OUT $_;
1477 }
1478 close(FILE);
1479 close(OUT);
1480 rename("$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb");
1481
1482 # Checking pbcl files
1483 foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
1484 open(PBCL,$f) || die "Unable to open $f";
1485 my $foundnew = 0;
1486 while (<PBCL>) {
1487 $foundnew = 1 if (/^$newver \(/);
1488 }
1489 close(PBCL);
1490 open(OUT,"> $f.new") || die "Unable to write to $f.new: $!";
1491 open(PBCL,$f) || die "Unable to open $f";
1492 while (<PBCL>) {
1493 print OUT "$_" if (not /^$oldver \(/);
1494 if ((/^$oldver \(/) && ($foundnew == 0)) {
1495 print OUT "$newver ($pbdate)\n";
1496 print OUT "- TBD\n";
1497 print OUT "\n";
1498 pb_log(0,"WARNING: version $newver not found in $f so added...") if ($foundnew == 0);
1499 }
1500 }
1501 close(OUT);
1502 close(PBCL);
1503 rename("$f.new","$f");
1504 }
1505
1506 pb_log(2,"Checkin $ENV{'PBROOTDIR'}/../$newver\n");
1507 pb_cms_checkin($scheme,"$ENV{'PBROOTDIR'}/../$newver",undef);
1508}
1509
1510#
1511# Return the list of VMs/VEs we are working on
1512# $all is a flag to know if we return all of them
1513# or only some (if all we publish also tar files in addition to pkgs
1514#
1515sub pb_get_v {
1516
1517my $vtype = shift;
1518my @v;
1519my $all = 0;
1520my $vlist;
1521my $pbv = 'PBV';
1522
1523if ($vtype eq "vm") {
1524 $vlist = "vmlist";
1525} elsif ($vtype eq "ve") {
1526 $vlist = "velist";
1527}
1528# Get VM/VE list
1529if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
1530 my ($ptr) = pb_conf_get($vlist);
1531 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
1532 $all = 1;
1533}
1534pb_log(2,"$vtype: $ENV{$pbv}\n");
1535@v = split(/,/,$ENV{$pbv});
1536return(\@v,$all);
1537}
1538
1539# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
1540# Needs to use root account to connect to the VM/VE
1541# pb will take your local public SSH key to access
1542# the pb account in the VM later on if needed
1543sub pb_setup_v {
1544
1545my $vtype = shift;
1546
1547my ($vm,$all) = pb_get_v($vtype);
1548
1549# Script generated
1550my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
1551
1552foreach my $v (@$vm) {
1553 # Name of the account to deal with for VM/VE
1554 # Do not use the one passed potentially with -a
1555 my ($pbac) = pb_conf_get($vtype."login");
1556 my ($key,$zero0,$zero1,$zero2);
1557 my ($vmexist,$vmpid,$ntps);
1558
1559 if ($vtype eq "vm") {
1560 # Prepare the key to be used and transfered remotely
1561 my $keyfile = pb_ssh_get(1);
1562
1563 my ($vmhost,$vmport,$vmntp) = pb_conf_get("vmhost","vmport","vmntp");
1564 my $nport = $vmport->{$ENV{'PBPROJ'}};
1565 $ntps = $vmntp->{$ENV{'PBPROJ'}};
1566 $nport = "$pbport" if (defined $pbport);
1567
1568 # Launch the VM
1569 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
1570
1571 # Skip that VM if something went wrong
1572 next if (($vmpid == 0) && ($vmexist == 0));
1573
1574 # Store the pub key part in a variable
1575 open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
1576 ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
1577 close(FILE);
1578
1579 $key = "\Q$zero1";
1580
1581 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");
1582 # once this is done, we can do what we want on the VM remotely
1583 }
1584
1585 # Prepare the script to be executed on the VM/VE
1586 # in $ENV{'PBDESTDIR'}/setupv
1587
1588 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
1589 print SCRIPT << 'EOF';
1590#!/usr/bin/perl -w
1591
1592use strict;
1593use File::Copy;
1594
1595# We should not need in this script more functions than what is provided
1596# by Base and Distribution to avoid problems at exec time.
1597# They are appended at the end.
1598
1599our $pbdebug;
1600our $pbLOG;
1601our $pbsynmsg = "pbscript";
1602our $pbdisplaytype = "text";
1603our $pblocale = "";
1604pb_log_init($pbdebug, $pbLOG);
1605pb_temp_init();
1606
1607EOF
1608 if ($vtype eq "vm") {
1609 print SCRIPT << 'EOF';
1610# Removes duplicate in .ssh/authorized_keys of our key if needed
1611#
1612my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
1613open(PBFILE,$file1) || die "Unable to open $file1";
1614open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
1615my $count = 0;
1616while (<PBFILE>) {
1617
1618EOF
1619 print SCRIPT << "EOF";
1620 if (/ $key /) {
1621 \$count++;
1622 }
1623print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
1624}
1625close(PBFILE);
1626close(PBOUT);
1627rename("\$file1.new",\$file1);
1628chmod 0600,\$file1;
1629
1630# Sync date
1631pb_system("/usr/sbin/ntpdate $ntps","Syncing date to $ntps");
1632
1633EOF
1634 }
1635 print SCRIPT << 'EOF';
1636
1637# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
1638#
1639my $file="/etc/passwd";
1640open(PBFILE,$file) || die "Unable to open $file";
1641my $found = 0;
1642while (<PBFILE>) {
1643EOF
1644 print SCRIPT << "EOF";
1645 \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
1646EOF
1647 print SCRIPT << 'EOF';
1648}
1649close(PBFILE);
1650
1651if ( $found == 0 ) {
1652 if ( ! -d "/home" ) {
1653 pb_mkdir("/home");
1654 }
1655EOF
1656 print SCRIPT << "EOF";
1657pb_system("groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
1658pb_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'}}");
1659}
1660
1661# allow ssh entry to build
1662#
1663mkdir "/home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
1664# Allow those accessing root to access the build account
1665copy("\$ENV{'HOME'}/.ssh/authorized_keys","/home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
1666chmod 0600,".ssh/authorized_keys";
1667pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} /home/$pbac->{$ENV{'PBPROJ'}}/.ssh","Finish setting up the SSH env for $pbac->{$ENV{'PBPROJ'}}");
1668
1669EOF
1670 print SCRIPT << 'EOF';
1671# No passwd for build account only keys
1672$file="/etc/shadow";
1673open(PBFILE,$file) || die "Unable to open $file";
1674open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1675while (<PBFILE>) {
1676EOF
1677 print SCRIPT << "EOF";
1678 s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
1679 s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/; #SLES 9 e.g.
1680EOF
1681 print SCRIPT << 'EOF';
1682 print PBOUT $_;
1683}
1684close(PBFILE);
1685close(PBOUT);
1686rename("$file.new",$file);
1687chmod 0640,$file;
1688
1689# Keep the VM in text mode
1690$file="/etc/inittab";
1691if (-f $file) {
1692 open(PBFILE,$file) || die "Unable to open $file";
1693 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1694 while (<PBFILE>) {
1695 s/^(..):5:initdefault:$/$1:3:initdefault:/;
1696 print PBOUT $_;
1697 }
1698 close(PBFILE);
1699 close(PBOUT);
1700 rename("$file.new",$file);
1701 chmod 0640,$file;
1702}
1703
1704# pb has to be added to portage group on gentoo
1705
1706# Adapt sudoers
1707$file="/etc/sudoers";
1708open(PBFILE,$file) || die "Unable to open $file";
1709open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1710while (<PBFILE>) {
1711EOF
1712 print SCRIPT << "EOF";
1713 next if (/^$pbac->{$ENV{'PBPROJ'}} /);
1714EOF
1715 print SCRIPT << 'EOF';
1716 s/Defaults[ \t]+requiretty//;
1717 print PBOUT $_;
1718}
1719close(PBFILE);
1720EOF
1721 print SCRIPT << "EOF";
1722# This is needed in order to be able to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
1723print PBOUT "$pbac->{$ENV{'PBPROJ'}} ALL=(ALL) NOPASSWD:ALL\n";
1724EOF
1725 print SCRIPT << 'EOF';
1726close(PBOUT);
1727rename("$file.new",$file);
1728chmod 0440,$file;
1729
1730EOF
1731
1732 my $SCRIPT = \*SCRIPT;
1733
1734 pb_install_deps($SCRIPT);
1735
1736 print SCRIPT << 'EOF';
1737# Suse wants sudoers as 640
1738if (($ddir eq "sles") || (($ddir eq "suse")) && ($dver =~ /10.[012]/)) {
1739 chmod 0640,$file;
1740}
1741
1742pb_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");
1743system "pb 2>&1 | head -5";
1744EOF
1745 # Adds pb_distro_init from ProjectBuilder::Distribution and Base
1746 foreach my $d (@INC) {
1747 my @f = ("$d/ProjectBuilder/Base.pm","$d/ProjectBuilder/Distribution.pm");
1748 foreach my $f (@f) {
1749 if (-f "$f") {
1750 open(PBD,"$f") || die "Unable to open $f";
1751 while (<PBD>) {
1752 next if (/^package/);
1753 next if (/^use Exporter/);
1754 next if (/^use ProjectBuilder::/);
1755 next if (/^our /);
1756 print SCRIPT $_;
1757 }
1758 close(PBD);
1759 }
1760 }
1761 }
1762 close(SCRIPT);
1763 chmod 0755,"$pbscript";
1764
1765 # That build script needs to be run as root and force stop of VM at end
1766 $pbaccount = "root";
1767
1768 # Force shutdown of VM exept if it was already launched
1769 my $force = 0;
1770 if ((! $vmexist) && ($vtype eq "vm")) {
1771 $force = 1;
1772 }
1773
1774 pb_script2v($pbscript,$vtype,$force,$v);
1775}
1776return;
1777}
1778
1779sub pb_install_deps {
1780
1781my $SCRIPT = shift;
1782
1783print {$SCRIPT} << 'EOF';
1784# We need to have that pb_distro_init function
1785# Get it from Project-Builder::Distribution
1786my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
1787print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n";
1788
1789# Get and install pb
1790my $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*";
1791my $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*";
1792my $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*";
1793my $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*";
1794my $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*";
1795my $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*";
1796my $cmtdm = "Installing Date-Manip perl module";
1797my $cmtmb = "Installing Module-Build perl module";
1798my $cmtfm = "Installing File-MimeInfo perl module";
1799my $cmtfb = "Installing File-Basedir perl module";
1800my $cmtms = "Installing Perl-Sendmail perl module";
1801my $cmtlg = "Installing Perl-Locale-gettext perl module";
1802my $cmtall = "Installing required modules";
1803
1804if ( $ddir eq "fedora" ) {
1805 pb_system("yum clean all","Cleaning yum env");
1806 #system "yum update -y";
1807 my $arch=`uname -m`;
1808 my $opt = "";
1809 chomp($arch);
1810 if ($arch eq "x86_64") {
1811 $opt="--exclude=*.i?86";
1812 }
1813
1814 if ($dver eq 4) {
1815 pb_system("yum -y $opt install rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker",$cmtall);
1816 pb_system("$insmb","$cmtmb");
1817 pb_system("$insfm","$cmtfm");
1818 pb_system("$insfb","$cmtfb");
1819 pb_system("$insms","$cmtms");
1820 pb_system("$inslg","$cmtlg");
1821 } else {
1822 pb_system("yum -y $opt install rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker perl-File-MimeInfo perl-Mail-Sendmail",$cmtall);
1823 pb_system("$inslg","$cmtlg");
1824 }
1825} elsif (( $dfam eq "rh" ) || ($ddir eq "sles") || (($ddir eq "suse") && (($dver eq "10.1") || ($dver eq "10.0"))) || ($ddir eq "slackware")) {
1826 # Suppose pkg are installed already as no online mirror available
1827 pb_system("rpm -e lsb 2>&1 > /dev/null","Removing lsb package");
1828 pb_system("$insdm","$cmtdm");
1829 pb_system("$insmb","$cmtmb");
1830 pb_system("$insfm","$cmtfm");
1831 pb_system("$insfb","$cmtfb");
1832 pb_system("$insms","$cmtms");
1833 pb_system("$inslg","$cmtlg");
1834} elsif ($ddir eq "suse") {
1835 # New OpenSuSE
1836 pb_system("$insmb","$cmtmb");
1837 pb_system("$insfm","$cmtfm");
1838 pb_system("$insfb","$cmtfb");
1839 pb_system("$insms","$cmtms");
1840 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");
1841} elsif ( $dfam eq "md" ) {
1842 pb_system("urpmi.update -a ; urpmi --auto rpm-build wget sudo patch ntp-client perl-File-MimeInfo perl-Mail-Sendmail perl-Locale-gettext","$cmtall");
1843 if (($ddir eq "mandrake") && ($dver eq "10.1")) {
1844 pb_system("$insdm","$cmtdm");
1845 pb_system("$inslg","$cmtlg");
1846 } else {
1847 pb_system("urpmi --auto perl-DateManip","$cmtdm");
1848 pb_system("urpmi --auto perl-Locale-gettext","$cmtdm");
1849 }
1850} elsif ( $dfam eq "du" ) {
1851 if (( $dver eq "3.1" ) && ($ddir eq "debian")) {
1852 #system "apt-get update";
1853 pb_system("$insfb","$cmtfb");
1854 pb_system("$insfm","$cmtfm");
1855 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");
1856 } else {
1857 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");
1858 }
1859} elsif ( $dfam eq "gen" ) {
1860 #system "emerge -u system";
1861 pb_system("emerge wget sudo ntp DateManip File-MimeInfo Mail-Sendmail Locale-gettext","$cmtall");
1862} else {
1863 pb_log(0,"No pkg to install\n");
1864}
1865EOF
1866}
1867
1868sub pb_announce {
1869
1870 # Get all required parameters
1871 my ($pbpackager,$pbrepo,$pbml,$pbsmtp) = pb_conf_get("pbpackager","pbrepo","pbml","pbsmtp");
1872 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
1873 my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
1874 my @pkgs = @$pkg;
1875 my %pkgs;
1876 my $first = 0;
1877
1878 # Command to find packages on repo
1879 my $findstr = "find . ";
1880 # Generated announce files
1881 my @files;
1882
1883 foreach my $pbpkg (@pkgs) {
1884 if ($first != 0) {
1885 $findstr .= "-o ";
1886 }
1887 $first++;
1888 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
1889 $pbver = $pkgv->{$pbpkg};
1890 } else {
1891 $pbver = $ENV{'PBPROJVER'};
1892 }
1893 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
1894 $pbtag = $pkgt->{$pbpkg};
1895 } else {
1896 $pbtag = $ENV{'PBPROJTAG'};
1897 }
1898
1899 $findstr .= "-name \'$pbpkg-$pbver-$pbtag\.*.rpm\' -o -name \'$pbpkg"."_$pbver*\.deb\' -o -name \'$pbpkg-$pbver\.ebuild\' ";
1900
1901 my $chglog;
1902
1903 # Get project info on log file and generate tmp files used later on
1904 pb_cms_init($pbinit);
1905 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
1906 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
1907 $chglog = undef if (! -f $chglog);
1908
1909 open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
1910 pb_changelog("announce",$pbpkg,$pbver,"N/A","N/A","N/A",\*OUT,"yes",$chglog);
1911 close(OUT);
1912 push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
1913 }
1914 $findstr .= " | grep -Ev \'src.rpm\'";
1915 if ((not defined $testver) || (not defined $testver->{$ENV{'PBPROJ'}}) || ($testver->{$ENV{'PBPROJ'}} !~ /true/i)) {
1916 $findstr .= " | grep -v ./test/";
1917 }
1918
1919 # Prepare the command to run and execute it
1920 open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
1921 print PBS "$findstr\n";
1922 close(PBS);
1923 chmod 0755,"$ENV{'PBTMP'}/pbscript";
1924 pb_send2target("Announce");
1925
1926 # Get subject line
1927 my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
1928 pb_log(0,"Please enter the title of your announce\n");
1929 pb_log(0,"(By default: $sl)\n");
1930 my $sl2 = <STDIN>;
1931 $sl = $sl2 if ($sl2 !~ /^$/);
1932
1933 # Prepare a template of announce
1934 open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
1935 print ANN << "EOF";
1936$sl</p>
1937
1938<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
1939<p>
1940Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
1941</p>
1942<p>
1943EOF
1944 open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to read $ENV{'PBTMP'}/system.log: $!";
1945 my $col = 2;
1946 my $i = 1;
1947 print ANN << 'EOF';
1948<TABLE WIDTH="700" CELLPADDING="0" CELLSPACING="0" BORDER="0">
1949<TR>
1950EOF
1951 while (<LOG>) {
1952 print ANN "<TD>$_</TD>";
1953 $i++;
1954 if ($i > $col) {
1955 print ANN "</TR>\n<TR>";
1956 $i = 1;
1957 }
1958 }
1959 close(LOG);
1960 print ANN << "EOF";
1961</TR>
1962</TABLE>
1963</p>
1964
1965<p>As usual source packages are also available in the same directory.</p>
1966
1967<p>
1968Changes are :
1969</p>
1970<p>
1971EOF
1972 # Get each package changelog content
1973 foreach my $f (sort(@files)) {
1974 open(IN,"$f") || die "Unable to read $f:$!";
1975 while (<IN>) {
1976 print ANN $_;
1977 }
1978 close(IN);
1979 print ANN "</p><p>\n";
1980 }
1981 print ANN "</p>\n";
1982 close(ANN);
1983
1984 # Allow for modification
1985 pb_system("vi $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
1986
1987 # Store it in DB for external usage (Web pages generation)
1988 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
1989
1990 my $precmd = "";
1991 if (! -f $db) {
1992 $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
1993 }
1994
1995 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
1996 { RaiseError => 1, AutoCommit => 1 })
1997 || die "Unable to connect to $db";
1998
1999 if ($precmd ne "") {
2000 my $sth = $dbh->prepare(qq{$precmd})
2001 || die "Unable to create table into $db";
2002 $sth->execute();
2003 }
2004
2005 # To read whole file
2006 local $/;
2007 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2008 my $announce = <ANN>;
2009 close(ANN);
2010
2011 pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
2012 my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
2013 || die "Unable to insert into $db";
2014 $sth->execute($pbdate, $announce);
2015 $dbh->disconnect;
2016
2017 # Then deliver it on the Web
2018 # $TOOLHOME/livwww www
2019
2020 # Mail it to project's ML
2021 open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
2022 print ML << 'EOF';
2023<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
2024
2025<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
2026 <head>
2027 </head>
2028 <body>
2029 <p>
2030EOF
2031 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2032 while(<ANN>) {
2033 print ML $_;
2034 }
2035 print ML << 'EOF';
2036</body>
2037</html>
2038EOF
2039 close(ML);
2040
2041 # To read whole file
2042 local $/;
2043 open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
2044 my $msg = <ANN>;
2045 close(ANN);
2046
2047 # Preparation of headers
2048
2049 my %mail = (
2050 To => $pbml->{$ENV{'PBPROJ'}},
2051 From => $pbpackager->{$ENV{'PBPROJ'}},
2052 Smtp => $pbsmtp->{$ENV{'PBPROJ'}},
2053 Body => $msg,
2054 Subject => "[ANNOUNCE] $sl",
2055 );
2056
2057 # Send mail
2058 sendmail(%mail) or die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
2059}
2060
2061# Return the SSH key file to use
2062# Potentially create it if needed
2063
2064sub pb_ssh_get {
2065
2066my $create = shift || 0; # Do not create keys by default
2067
2068# Check the SSH environment
2069my $keyfile = undef;
2070
2071# We have specific keys by default
2072$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa";
2073if (!(-e $keyfile) && ($create eq 1)) {
2074 pb_system("ssh-keygen -q -b 1024 -N '' -f $keyfile -t dsa","Generating SSH keys for pb");
2075}
2076
2077$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
2078$keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if (-s "$ENV{'HOME'}/.ssh/id_dsa");
2079$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
2080die "Unable to find your public ssh key under $keyfile" if (not defined $keyfile);
2081return($keyfile);
2082}
2083
2084
2085# Returns the pid of a running VM command using a specific VM file
2086sub pb_check_ps {
2087 my $vmcmd = shift;
2088 my $vmm = shift;
2089 my $vmexist = 0; # FALSE by default
2090
2091 open(PS, "ps auxhww|") || die "Unable to call ps";
2092 while (<PS>) {
2093 next if (! /$vmcmd/);
2094 next if (! /$vmm/);
2095 my ($void1, $void2);
2096 ($void1, $vmexist, $void2) = split(/ +/);
2097 last;
2098 }
2099 return($vmexist);
2100}
2101
2102
2103sub pb_extract_build_files {
2104
2105my $src=shift;
2106my $dir=shift;
2107my $ddir=shift;
2108my $mandatory=shift || "spec";
2109my @files;
2110
2111my $flag = "mayfail" if ($mandatory eq "patch");
2112my $res;
2113
2114if ($src =~ /tar\.gz$/) {
2115 $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
2116} elsif ($src =~ /tar\.bz2$/) {
2117 $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
2118} else {
2119 die "Unknown compression algorithm for $src";
2120}
2121# If not mandatory return now
2122return() if (($res != 0) and ($mandatory eq "patch"));
2123opendir(DIR,"$dir") || die "Unable to open directory $dir";
2124foreach my $f (readdir(DIR)) {
2125 next if ($f =~ /^\./);
2126 # Skip potential patch dir
2127 next if ($f =~ /^pbpatch/);
2128 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
2129 pb_log(2,"mv $dir/$f $ddir\n");
2130 push @files,"$ddir/$f";
2131}
2132closedir(DIR);
2133# Not enough but still a first cleanup
2134pb_rm_rf("$dir");
2135return(@files);
2136}
2137
2138sub pb_list_bfiles {
2139
2140my $dir = shift;
2141my $pbpkg = shift;
2142my $bfiles = shift;
2143my $pkgfiles = shift;
2144my $supfiles = shift;
2145
2146opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
2147foreach my $f (readdir(BDIR)) {
2148 next if ($f =~ /^\./);
2149 $bfiles->{$f} = "$dir/$f";
2150 $bfiles->{$f} =~ s~$ENV{'PBROOTDIR'}~~;
2151 if (defined $supfiles->{$pbpkg}) {
2152 $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
2153 }
2154}
2155closedir(BDIR);
2156}
2157
2158
2159#
2160# Return the list of packages we are working on in a non CMS action
2161#
2162sub pb_get_pkg {
2163
2164my @pkgs = ();
2165
2166my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
2167@pkgs = keys %$var;
2168
2169pb_log(0,"Packages: ".join(',',@pkgs)."\n");
2170return(\@pkgs);
2171}
2172
2173# Which is our local arch ? (standardize on i386 for those platforms)
2174sub pb_get_arch {
2175
2176my $arch = `uname -m`;
2177chomp($arch);
2178$arch =~ s/i.86/i386/;
2179return($arch);
2180}
2181
21821;
Note: See TracBrowser for help on using the repository browser.