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

Last change on this file since 980 was 980, checked in by Bruno Cornec, 14 years ago
  • Fix more naming issues
  • Property svn:executable set to *
File size: 95.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 File::Find;
21use Time::localtime qw(localtime);
22use POSIX qw(strftime);
23use lib qw (lib);
24use ProjectBuilder::Version;
25use ProjectBuilder::Base;
26use ProjectBuilder::Display;
27use ProjectBuilder::Conf;
28use ProjectBuilder::Distribution;
29use ProjectBuilder::CMS;
30use ProjectBuilder::Env;
31use ProjectBuilder::Filter;
32use ProjectBuilder::Changelog;
33use Mail::Sendmail;
34
35# Global variables
36my %opts; # CLI Options
37my $action; # action to realize
38my $test = "FALSE"; # Not used
39my $pbforce = 0; # Force VE/VM rebuild
40my $pbsnap = 0; # Do not use snapshot mode for VM/VE by default
41my $option = ""; # Not used
42my @pkgs; # list of packages
43my $pbtag; # Global Tag variable
44my $pbver; # Global Version variable
45my $pbscript; # Name of the script
46my %pbver; # per package
47my %pbtag; # per package
48my $pbrev; # Global REVISION variable
49my $pbaccount; # Login to use to connect to the VM
50my $pbport; # Port to use to connect to the VM
51my $newver; # New version to create
52my $iso = undef; # ISO image for the VM to create
53
54my @date = pb_get_date();
55my $pbdate = strftime("%Y-%m-%d", @date);
56
57=pod
58
59=head1 NAME
60
61pb, aka project-builder.org - builds packages for your projects
62
63=head1 DESCRIPTION
64
65pb helps you build various packages directly from your project sources.
66Those sources could be handled by a CMS (Configuration Management System)
67such as Subversion, CVS, Git, Mercurial... or being a simple reference to a compressed tar file.
68It's based on a set of configuration files, a set of provided macros to help
69you keeping build files as generic as possible. For example, a single .spec
70file should be required to generate for all rpm based distributions, even
71if you could also have multiple .spec files if required.
72
73=head1 SYNOPSIS
74
75pb [-vhSq][-r pbroot][-p project][[-s script -a account -P port][-m mach-1[,...]]][-i iso] <action> [<pkg1> ...]
76
77pb [--verbose][--help][--man][--quiet][--snapshot][--revision pbroot][--project project][[--script script --account account --port port][--machine mach-1[,...]]][--iso iso] <action> [<pkg1> ...]
78
79=head1 OPTIONS
80
81=over 4
82
83=item B<-v|--verbose>
84
85Print a brief help message and exits.
86
87=item B<-q|--quiet>
88
89Do not print any output.
90
91=item B<-h|--help>
92
93Print a brief help message and exits.
94
95=item B<-S|--snapshot>
96
97Use the snapshot mode of VMs or VEs
98
99=item B<--man>
100
101Prints the manual page and exits.
102
103=item B<-m|--machine machine1[,machine2,...]>
104
105Name of the Virtual Machines (VM) or Virtual Environments (VE) you want to build on (coma separated).
106All if none precised (or use the env variable PBV).
107
108=item B<-s|--script script>
109
110Name of the script you want to execute on the related VMs or VEs.
111
112=item B<-i|--iso iso_image>
113
114Name of the ISO image of the distribution you want to install on the related VMs.
115
116=item B<-a|--account account>
117
118Name of the account to use to connect on the related VMs.
119
120=item B<-P|--port port_number>
121
122Port number to use to connect on the related VMs.\n";
123
124=item B<-p|--project project_name>
125
126Name of the project you're working on (or use the env variable PBPROJ)
127
128=item B<-r|--revision revision>
129
130Path Name of the project revision under the CMS (or use the env variable PBROOT)
131
132=item B<-V|--version new_version>
133
134New version of the project to create based on the current one.
135
136=back
137
138=head1 ARGUMENTS
139
140<action> can be:
141
142=over 4
143
144=item B<cms2build>
145
146Create tar files for the project under your CMS.
147CMS supported are SVN, CVS and Mercurial
148parameters are packages to build
149if not using default list
150
151=item B<build2pkg>
152
153Create packages for your running distribution
154
155=item B<cms2pkg>
156
157cms2build + build2pkg
158
159=item B<build2ssh>
160
161Send the tar files to a SSH host
162
163=item B<cms2ssh>
164
165cms2build + build2ssh
166
167=item B<pkg2ssh>
168
169Send the packages built to a SSH host
170
171=item B<build2vm>
172
173Create packages in VMs, launching them if needed
174and send those packages to a SSH host once built
175VM type supported are QEMU
176
177=item B<build2ve>
178
179Create packages in VEs, creating it if needed
180and send those packages to a SSH host once built
181
182=item B<cms2vm>
183
184cms2build + build2vm
185
186=item B<cms2ve>
187
188cms2build + build2ve
189
190=item B<launchvm>
191
192Launch one virtual machine
193
194=item B<launchve>
195
196Launch one virtual environment
197
198=item B<script2vm>
199
200Launch one virtual machine if needed
201and executes a script on it
202
203=item B<script2ve>
204
205Execute a script in a virtual environment
206
207=item B<newvm>
208
209Create a new virtual machine
210
211=item B<newve>
212
213Create a new virtual environment
214
215=item B<setupvm>
216
217Setup a virtual machine for pb usage
218
219=item B<setupve>
220
221Setup a virtual environment for pb usage
222
223=item B<snapvm>
224
225Snapshot a virtual machine for pb usage
226
227=item B<snapve>
228
229Snapshot a virtual environment for pb usage
230
231=item B<test2pkg>
232
233Test a package locally
234
235=item B<test2vm>
236
237Test a package in a virtual machine
238
239=item B<test2ve>
240
241Test a package in a virtual environment
242
243=item B<newver>
244
245Create a new version of the project derived
246from the current one
247
248=item B<newproj>
249
250Create a new project and a template set of
251configuration files under pbconf
252
253=item B<announce>
254
255Announce the availability of the project through various means
256
257=item B<web2ssh>
258
259Deliver the Web site content to the target server using ssh.
260
261=item B<clean>
262
263Purge the build and delivery directories related to the current project
264
265=back
266
267<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).
268
269=head1 WEB SITES
270
271The 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/>.
272
273=head1 USER MAILING LIST
274
275None exists for the moment.
276
277=head1 CONFIGURATION FILES
278
279Each pb user may have a configuration in F<$HOME/.pbrc>. The values in this file may overwrite any other configuration file value.
280
281Here is an example of such a configuration file:
282
283 #
284 # Define for each project the URL of its pbconf repository
285 # No default option allowed here as they need to be all different
286 #
287 # URL of the pbconf content
288 # This is the format of a classical URL with the extension of additional schema such as
289 # svn+ssh, cvs+ssh, ...
290 #
291 pbconfurl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe/pbconf
292
293 # This is normaly defined in the project's configuration file
294 # Url of the project
295 #
296 pburl linuxcoe = cvs+ssh://:ext:bcornec@linuxcoe.cvs.sourceforge.net:/cvsroot/linuxcoe
297
298 # All these URLs needs to be defined here as the are the entry point
299 # for how to build packages for the project
300 #
301 pbconfurl pb = svn+ssh://svn.project-builder.org/mondo/svn/pb/pbconf
302 pbconfurl mondorescue = svn+ssh://svn.project-builder.org/mondo/svn/project-builder/mondorescue/pbconf
303 pbconfurl collectl = svn+ssh://bruno@svn.mondorescue.org/mondo/svn/project-builder/collectl/pbconf
304 pbconfurl netperf = svn+ssh://svn.mondorescue.org/mondo/svn/project-builder/netperf/pbconf
305
306 # Under that dir will take place everything related to pb
307 # If you want to use VMs/chroot/..., then use $ENV{'HOME'} to make it portable
308 # to your VMs/chroot/...
309 # if not defined then /var/cache
310 pbdefdir default = $ENV{'HOME'}/project-builder
311 pbdefdir pb = $ENV{'HOME'}
312 pbdefdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
313 pbdefdir mondorescue = $ENV{'HOME'}/mondo/svn
314
315 # pbconfdir points to the directory where the CMS content of the pbconfurl is checked out
316 # If not defined, pbconfdir is under pbdefdir/pbproj/pbconf
317 pbconfdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs/pbconf
318 pbconfdir mondorescue = $ENV{'HOME'}/mondo/svn/pbconf
319
320 # pbdir points to the directory where the CMS content of the pburl is checked out
321 # If not defined, pbdir is under pbdefdir/pbproj
322 # Only defined if we have access to the dev of the project
323 pbdir linuxcoe = $ENV{'HOME'}/LinuxCOE/cvs
324 pbdir mondorescue = $ENV{'HOME'}/mondo/svn
325
326 # -daemonize doesn't work with qemu 0.8.2
327 vmopt default = -m 384
328
329=head1 AUTHORS
330
331The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
332
333=head1 COPYRIGHT
334
335Project-Builder.org is distributed under the GPL v2.0 license
336described in the file C<COPYING> included with the distribution.
337
338=cut
339
340# ---------------------------------------------------------------------------
341
342my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
343my $appname = "pb";
344
345# Initialize the syntax string
346
347pb_syntax_init("$appname (aka project-builder.org) Version $projectbuilderver-$projectbuilderrev\n");
348
349GetOptions("help|?|h" => \$opts{'h'},
350 "man" => \$opts{'man'},
351 "verbose|v+" => \$opts{'v'},
352 "snapshot|S" => \$opts{'S'},
353 "quiet|q" => \$opts{'q'},
354 "log-files|l=s" => \$opts{'l'},
355 "force|f" => \$opts{'f'},
356 "account|a=s" => \$opts{'a'},
357 "revision|r=s" => \$opts{'r'},
358 "script|s=s" => \$opts{'s'},
359 "machines|mock|m=s" => \$opts{'m'},
360 "port|P=i" => \$opts{'P'},
361 "project|p=s" => \$opts{'p'},
362 "iso|i=s" => \$opts{'i'},
363 "version|V=s" => \$opts{'V'},
364) || pb_syntax(-1,0);
365
366if (defined $opts{'h'}) {
367 pb_syntax(0,1);
368}
369if (defined $opts{'man'}) {
370 pb_syntax(0,2);
371}
372if (defined $opts{'v'}) {
373 $pbdebug = $opts{'v'};
374}
375if (defined $opts{'f'}) {
376 $pbforce=1;
377}
378if (defined $opts{'q'}) {
379 $pbdebug=-1;
380}
381if (defined $opts{'S'}) {
382 $pbsnap=1;
383}
384if (defined $opts{'l'}) {
385 open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
386 $pbLOG = \*pbLOG;
387 $pbdebug = 0 if ($pbdebug == -1);
388 }
389pb_log_init($pbdebug, $pbLOG);
390pb_display_init("text","");
391
392# Handle root of the project if defined
393if (defined $opts{'r'}) {
394 $ENV{'PBROOTDIR'} = $opts{'r'};
395}
396# Handle virtual machines if any
397if (defined $opts{'m'}) {
398 $ENV{'PBV'} = $opts{'m'};
399}
400if (defined $opts{'s'}) {
401 $pbscript = $opts{'s'};
402}
403if (defined $opts{'a'}) {
404 $pbaccount = $opts{'a'};
405 die "option -a requires a -s script option" if (not defined $pbscript);
406}
407if (defined $opts{'P'}) {
408 $pbport = $opts{'P'};
409}
410if (defined $opts{'V'}) {
411 $newver = $opts{'V'};
412}
413if (defined $opts{'i'}) {
414 $iso = $opts{'i'};
415}
416
417# Get Action
418$action = shift @ARGV;
419die pb_syntax(-1,1) if (not defined $action);
420
421#
422# Check for command requirements
423#
424my ($req,$opt) = pb_conf_get_if("oscmd","oscmdopt");
425pb_check_requirements($req->{$appname},$opt->{$appname});
426
427my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
428my $pbinit = undef;
429$pbinit = 1 if ($action =~ /^newproj$/);
430
431# Handles project name if any
432# And get global params
433($filteredfiles, $supfiles, $defpkgdir, $extpkgdir) = pb_env_init($opts{'p'},$pbinit,$action);
434
435pb_log(0,"Project: $ENV{'PBPROJ'}\n");
436pb_log(0,"Action: $action\n");
437
438# Act depending on action
439if ($action =~ /^cms2build$/) {
440 pb_cms2build();
441} elsif ($action =~ /^build2pkg$/) {
442 pb_build2pkg();
443} elsif ($action =~ /^cms2pkg$/) {
444 pb_cms2build();
445 pb_build2pkg();
446} elsif ($action =~ /^build2ssh$/) {
447 pb_build2ssh();
448} elsif ($action =~ /^cms2ssh$/) {
449 pb_cms2build();
450 pb_build2ssh();
451} elsif ($action =~ /^pkg2ssh$/) {
452 pb_pkg2ssh();
453} elsif ($action =~ /^build2ve$/) {
454 pb_build2v("ve","build");
455} elsif ($action =~ /^build2vm$/) {
456 pb_build2v("vm","build");
457} elsif ($action =~ /^cms2ve$/) {
458 pb_cms2build();
459 pb_build2v("ve","build");
460} elsif ($action =~ /^cms2vm$/) {
461 pb_cms2build();
462 pb_build2v("vm","build");
463} elsif ($action =~ /^launchvm$/) {
464 pb_launchv("vm",$ENV{'PBV'},0);
465} elsif ($action =~ /^launchve$/) {
466 pb_launchv("ve",$ENV{'PBV'},0);
467} elsif ($action =~ /^script2vm$/) {
468 pb_script2v($pbscript,"vm");
469} elsif ($action =~ /^script2ve$/) {
470 pb_script2v($pbscript,"ve");
471} elsif ($action =~ /^newver$/) {
472 pb_newver();
473} elsif ($action =~ /^newve$/) {
474 pb_launchv("ve",$ENV{'PBV'},1);
475} elsif ($action =~ /^newvm$/) {
476 pb_launchv("vm",$ENV{'PBV'},1);
477 pb_log(0, "Please ensure that sshd is running in your VM by default\n");
478 pb_log(0, "and that it allows remote root login (PermitRootLogin yes in /etc/ssh/sshd_config)\n");
479} elsif ($action =~ /^setupve$/) {
480 pb_setup2v("ve");
481} elsif ($action =~ /^setupvm$/) {
482 pb_setup2v("vm");
483} elsif ($action =~ /^snapve$/) {
484 pb_snap2v("ve");
485} elsif ($action =~ /^snapvm$/) {
486 pb_snap2v("vm");
487} elsif ($action =~ /^test2pkg$/) {
488 pb_test2pkg();
489} elsif ($action =~ /^test2ve$/) {
490 pb_build2v("ve","test");
491} elsif ($action =~ /^test2vm$/) {
492 pb_build2v("vm","test");
493} elsif ($action =~ /^newproj$/) {
494 # Nothing to do - already done in pb_env_init
495} elsif ($action =~ /^clean$/) {
496 pb_clean();
497} elsif ($action =~ /^announce$/) {
498 # For announce only. Require avoids the systematic load of these modules
499 require DBI;
500 require DBD::SQLite;
501
502 pb_announce();
503} elsif ($action =~ /^web2ssh$/) {
504 require DBI;
505 require DBD::SQLite;
506
507 pb_cms2build("Web");
508 pb_send2target("Web");
509} else {
510 pb_log(0,"\'$action\' is not available\n");
511 pb_syntax(-2,1);
512}
513
514sub pb_cms2build {
515
516 my $param = shift || undef;
517
518 my $pkg;
519 my @pkgs;
520 my $webdir;
521
522 my %pkgs;
523 my %pb; # Structure to store conf info
524
525 # If Website, then pkg is only the website
526 if ((defined $param) && ($param eq "Web")) {
527 ($webdir) = pb_conf_get("webdir");
528 pb_log(2,"webdir: ".Dumper($webdir)."\n");
529 $pkgs[0] = $webdir->{$ENV{'PBPROJ'}};
530 $extpkgdir = $webdir;
531 pb_log(0,"Package: $pkgs[0]\n");
532 } else {
533 $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
534 @pkgs = @$pkg;
535 }
536
537 my ($scheme, $uri) = pb_cms_init($pbinit);
538
539 # We need 2 lines here
540 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
541 my @pt = pb_conf_get_if("vmlist","velist");
542
543 # declare packager and repo for filtering
544 # TODO: Is pbrepo needed so early in the process ?
545 my ($tmp1, $tmp2) = pb_conf_get("pbpackager","pbrepo");
546 $ENV{'PBPACKAGER'} = $tmp1->{$ENV{'PBPROJ'}};
547 $ENV{'PBREPO'} = $tmp2->{$ENV{'PBPROJ'}};
548
549 foreach my $pbpkg (@pkgs) {
550 $ENV{'PBPKG'} = $pbpkg;
551
552 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
553 $pbver = $pkgv->{$pbpkg};
554 } else {
555 $pbver = $ENV{'PBPROJVER'};
556 }
557 # If it's a test version, then tag == 0.date
558 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
559 $pbtag = "0.".strftime("%Y%m%d%H%M%S", @date);
560 $ENV{'PBPROJTAG'} = $pbtag;
561 } elsif ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
562 $pbtag = $pkgt->{$pbpkg};
563 } else {
564 $pbtag = $ENV{'PBPROJTAG'};
565 }
566
567 $pbrev = $ENV{'PBREVISION'};
568 pb_log(0,"\n");
569 pb_log(0,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
570 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
571
572 # Clean up dest if necessary. The export will recreate it
573 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
574 pb_rm_rf($dest) if (-d $dest);
575
576 # Export CMS tree for the concerned package to dest
577 # And generate some additional files
578 $OUTPUT_AUTOFLUSH=1;
579
580 # computes in which dir we have to work
581 my $dir = $defpkgdir->{$pbpkg};
582 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
583 $dir = $webdir->{$ENV{'PBPROJ'}} if ((defined $param) && ($param eq "Web"));
584 pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");
585
586 # Exporting content from CMS
587 my $preserve = pb_cms_export($uri,"$ENV{'PBDIR'}/$dir",$dest);
588
589 # Generated fake content for test versions to speed up stuff
590 my $chglog;
591
592 # Get project info on authors and log file
593 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
594 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
595 $chglog = undef if (! -f $chglog);
596
597 my $authors = "$ENV{'PBROOTDIR'}/$pbpkg/pbauthors";
598 $authors = "$ENV{'PBROOTDIR'}/pbauthors" if (! -f $authors);
599 $authors = "/dev/null" if (! -f $authors);
600
601 # Extract cms log history and store it
602 if ((defined $chglog) && (! -f "$dest/NEWS")) {
603 pb_log(2,"Generating NEWS file from $chglog\n");
604 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
605 }
606 pb_cms_log($scheme,"$ENV{'PBDIR'}/$dir",$dest,$chglog,$authors,$testver);
607
608 my %build;
609 # We want to at least build for the underlying distro
610 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
611 my $tmpl = "$ddir-$dver-$arch,";
612 my %patches;
613
614 # Get list of distributions for which I need to generate build files
615 if (defined $pt[0]->{$ENV{'PBPROJ'}}) {
616 $tmpl .= $pt[0]->{$ENV{'PBPROJ'}};
617 }
618 if (defined $pt[1]->{$ENV{'PBPROJ'}}) {
619 # the 2 lists needs to be grouped with a ',' separating them
620 if ($tmpl ne "") {
621 $tmpl .= ",";
622 }
623 $tmpl .= $pt[1]->{$ENV{'PBPROJ'}}
624 }
625
626 # Setup %pb structure to allow filtering later on, on files using that structure
627 $pb{'tag'} = $pbtag;
628 $pb{'rev'} = $pbrev;
629 $pb{'ver'} = $pbver;
630 $pb{'pkg'} = $pbpkg;
631 $pb{'date'} = $pbdate;
632 $pb{'defpkgdir'} = $defpkgdir;
633 $pb{'extpkgdir'} = $extpkgdir;
634 $pb{'chglog'} = $chglog;
635 $pb{'packager'} = $ENV{'PBPACKAGER'};
636 $pb{'proj'} = $ENV{'PBPROJ'};
637 $pb{'repo'} = $ENV{'PBREPO'};
638 $pb{'patches'} = \%patches;
639 pb_log(2,"DEBUG: pb: ".Dumper(%pb)."\n");
640
641 # Do not do that for website
642 if ((not defined $param) || ($param ne "Web")) {
643 pb_log(0,"Build files are being generated for ...\n");
644 my %virt;
645 # De-duplicate similar VM and VE
646 foreach my $d (split(/,/,$tmpl)) {
647 # skip ill-formatted vms (name-ver-arch)
648 next if ($d !~ /-/);
649 $virt{$d} = $d;
650 }
651
652 foreach my $d (keys %virt) {
653 my ($name,$ver,$arch) = split(/-/,$d);
654 pb_log(0,"Bad format for $d") if ((not defined $name) || (not defined $ver) || (not defined $arch)) ;
655 chomp($arch);
656 my ($ddir, $dver, $dfam);
657 ($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'}, $pb{'upd'}, $pb{'arch'}) = pb_distro_init($name,$ver,$arch);
658 pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $pb{'dtype'}, $pb{'suf'})."\n");
659 pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
660
661 # We need to compute the real name of the package
662 my $pbrealpkg = pb_cms_get_real_pkg($pbpkg,$pb{'dtype'});
663 $pb{'realpkg'} = $pbrealpkg;
664 pb_log(1,"Virtual package $pbpkg has a real package name of $pbrealpkg on $ddir-$dver\n") if ($pbrealpkg ne $pbpkg);
665
666 # Filter build files from the less precise up to the most with overloading
667 # Filter all files found, keeping the name, and generating in dest
668
669 # Find all build files first relatively to PBROOTDIR
670 # Find also all specific files referenced in the .pb conf file
671 my %bfiles = ();
672 my %pkgfiles = ();
673 $build{"$ddir-$dver-$arch"} = "yes";
674
675 if (-d "$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}") {
676 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$pb{'dtype'}",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
677 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$dfam") {
678 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
679 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir") {
680 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
681 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver") {
682 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
683 } elsif (-d "$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch") {
684 pb_list_bfiles("$ENV{'PBROOTDIR'}/$pbpkg/$ddir-$dver-$arch",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
685 } else {
686 $build{"$ddir-$dver-$arch"} = "no";
687 next;
688 }
689 pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");
690
691 # Get all filters to apply
692 my $ptr = pb_get_filters($pbpkg, $pb{'dtype'}, $dfam, $ddir, $dver);
693
694 # Prepare local patches for this distro - They are always applied first - May be a problem one day
695 foreach my $p (sort(<$ENV{'PBROOTDIR'}/$pbpkg/pbpatch/*>)) {
696 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.all$/));
697 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.all$/);
698 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$pb{'dtype'}$/));
699 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$pb{'dtype'}$/);
700 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$dfam$/));
701 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$dfam$/);
702 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir$/));
703 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir$/);
704 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver$/));
705 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver$/);
706 $patches{"$ddir-$dver-$arch"} .= "," if ((defined $patches{"$ddir-$dver-$arch"}) and ($p =~ /\.$ddir-$dver-$arch$/));
707 $patches{"$ddir-$dver-$arch"} .= "file://$p" if ($p =~ /\.$ddir-$dver-$arch$/);
708 }
709
710 # Prepare also remote patches to be included - Applied after the local ones
711 foreach my $p ("all","$pb{'dtype'}","$dfam","$ddir","$ddir-$dver","$ddir-$dver-$arch") {
712 my $f = "$ENV{'PBROOTDIR'}/$pbpkg/pbextpatch.$p";
713 next if (not -f $f);
714 if (not open(PATCH,$f)) {
715 pb_display("Unable to open existing external patch file content $f\n");
716 next;
717 }
718 while (<PATCH>) {
719 chomp();
720 $patches{"$ddir-$dver-$arch"} .= "," if (defined $patches{"$ddir-$dver-$arch"});
721 $patches{"$ddir-$dver-$arch"} .= "$_";
722 }
723 close(PATCH);
724 }
725 pb_log(2,"DEBUG: pb->patches: ".Dumper($pb{'patches'})."\n");
726
727 # Apply now all the filters on all the files concerned
728 # destination dir depends on the type of file
729 if (defined $ptr) {
730 # For patch support
731 $pb{'tuple'} = "$ddir-$dver-$arch";
732 foreach my $f (values %bfiles,values %pkgfiles) {
733 pb_filter_file("$ENV{'PBROOTDIR'}/$f",$ptr,"$dest/pbconf/$ddir-$dver-$arch/".basename($f),\%pb);
734 }
735 }
736 }
737 my @found;
738 my @notfound;
739 foreach my $b (keys %build) {
740 push @found,$b if ($build{$b} =~ /yes/);
741 push @notfound,$b if ($build{$b} =~ /no/);
742 }
743 pb_log(0," ... ".join(',',sort(@found))."\n");
744 pb_log(0,"No Build files found for ".join(',',sort(@notfound))."\n") if (@notfound);
745 pb_log(2,"DEBUG: patches: ".Dumper(%patches)."\n");
746 }
747
748 # Get the generic filter (all.pbf) and
749 # apply those to the non-build files including those
750 # generated by pbinit if applicable
751
752 # Get only all.pbf filter
753 my $ptr = pb_get_filters($pbpkg);
754
755 my $liste ="";
756 if (defined $filteredfiles->{$pbpkg}) {
757 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
758 pb_filter_file_inplace($ptr,"$dest/$f",\%pb);
759 $liste = "$f $liste";
760 }
761 }
762 pb_log(2,"Files ".$liste."have been filtered\n");
763
764 # Do not do that for website
765 if ((not defined $param) || ($param ne "Web")) {
766 my %tmp;
767 # Filter potential patches (local + remote)
768 pb_log(0,"Delivering and compressing patches ");
769 foreach my $v (keys %patches) {
770 pb_mkdir_p("$dest/pbconf/$v/pbpatch");
771 foreach my $pf (split(/,/,$patches{$v})) {
772 my $pp = basename($pf);
773 pb_cms_export($pf,undef,"$dest/pbconf/$v/pbpatch");
774 pb_filter_file_inplace($ptr,"$dest/pbconf/$v/pbpatch/$pp",\%pb);
775 pb_system("gzip -9f $dest/pbconf/$v/pbpatch/$pp","","quiet");
776 $tmp{$pf} = "";
777 }
778 }
779 foreach my $v (keys %tmp) {
780 pb_log(0,"$v ");
781 }
782 pb_log(0,"\n");
783 } else {
784 # Instead call News generation
785 pb_web_news2html($dest);
786 # And create an empty pbconf
787 pb_mkdir_p("$dest/pbconf");
788 # And prepare the pbscript to execute remotely
789 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
790 print SCRIPT "#!/bin/bash\n";
791 print SCRIPT "#set -x\n";
792 print SCRIPT "echo ... Extracting Website content\n";
793 print SCRIPT "find . -type f | grep -Ev '^./$pbpkg-$pbver.tar.gz|^./pbscript' | xargs rm -f non-existent\n";
794 print SCRIPT "find * -type d -depth | xargs rmdir 2> /dev/null \n";
795 print SCRIPT "tar xfz $pbpkg-$pbver.tar.gz\n";
796 print SCRIPT "mv $pbpkg-$pbver/* .\n";
797 print SCRIPT "rm -f $pbpkg-$pbver.tar.gz\n";
798 print SCRIPT "rmdir $pbpkg-$pbver\n";
799 close(SCRIPT);
800 }
801
802 # Prepare the dest directory for archive
803 if (-x "$ENV{'PBROOTDIR'}/$pbpkg/pbinit") {
804 pb_filter_file("$ENV{'PBROOTDIR'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",\%pb);
805 chmod 0755,"$ENV{'PBTMP'}/pbinit";
806 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOTDIR'}/$pbpkg/pbinit","verbose");
807 }
808
809 # Do we have additional script to run to prepare the environement for the project ?
810 # Then include it in the pbconf delivery
811 foreach my $pbvf (<$ENV{'PBROOTDIR'}/pbv*.pre>,<$ENV{'PBROOTDIR'}/pbv*.post>, <$ENV{'PBROOTDIR'}/pbtest*>) {
812 if (-x "$pbvf") {
813 my $target = "$ENV{'PBDESTDIR'}/".basename($pbvf);
814 pb_filter_file("$pbvf",$ptr,$target,\%pb);
815 chmod 0755,"$target";
816 }
817 }
818
819 # Archive dest dir
820 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
821 if (defined $preserve) {
822 # In that case we want to preserve the original tar file for checksum purposes
823 # The one created is btw equivalent in that case to this one
824 # Maybe check basename of both to be sure they are the same ?
825 pb_log(0,"Preserving original tar file ");
826 move("$preserve","$pbpkg-$pbver.tar.gz");
827 } else {
828 # Possibility to look at PBSRC to guess more the filename
829 pb_system("tar cfz $pbpkg-$pbver.tar.gz --exclude=$pbpkg-$pbver/pbconf $pbpkg-$pbver","Creating $pbpkg tar files compressed");
830 }
831 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");
832 pb_system("tar cfz $pbpkg-$pbver.pbconf.tar.gz $pbpkg-$pbver/pbconf","Creating pbconf tar files compressed");
833 pb_log(0,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz\n");
834
835 # Keep track of version-tag per pkg
836 $pkgs{$pbpkg} = "$pbver-$pbtag";
837
838 # Final cleanup
839 pb_rm_rf($dest) if (-d $dest);
840 }
841
842 # Keep track of per package version
843 pb_log(2,"DEBUG pkgs: ".Dumper(%pkgs)."\n");
844 open(PKG,"> $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb";
845 foreach my $pbpkg (keys %pkgs) {
846 print PKG "pbpkg $pbpkg = $pkgs{$pbpkg}\n";
847 }
848 close(PKG);
849
850 # Keep track of what is generated by default
851 # We need to store the dir and info on version-tag
852 # Base our content on the existing .pb file
853 copy("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb","$ENV{'PBDESTDIR'}/pbrc");
854 open(LAST,">> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
855 print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOTDIR'}\n";
856 print LAST "projver $ENV{'PBPROJ'} = $ENV{'PBPROJVER'}\n";
857 print LAST "projtag $ENV{'PBPROJ'} = $ENV{'PBPROJTAG'}\n";
858 print LAST "pbpackager $ENV{'PBPROJ'} = $ENV{'PBPACKAGER'}\n";
859 close(LAST);
860}
861
862sub pb_test2pkg {
863 # Get the running distro to test on
864 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
865 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch))."\n");
866
867 # Get list of packages to test
868 # Get content saved in cms2build
869 my $ptr = pb_get_pkg();
870 @pkgs = @$ptr;
871
872 # Additional potential repo
873 pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
874 foreach my $pbpkg (@pkgs) {
875 # We need to install the package to test, and deps brought with it
876 pb_distro_installdeps(undef,$dtype,$pbupd,$pbpkg);
877 pb_system("$ENV{'PBDESTDIR'}/pbtest","Launching test for $pbpkg","verbose");
878 }
879}
880
881sub pb_build2pkg {
882
883 # Get the running distro to build on
884 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
885 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch))."\n");
886
887 # Get list of packages to build
888 my $ptr = pb_get_pkg();
889 @pkgs = @$ptr;
890
891 # Get content saved in cms2build
892 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
893 $pkg = { } if (not defined $pkg);
894
895 chdir "$ENV{'PBBUILDDIR'}";
896 my $made = ""; # pkgs made during build
897 foreach my $pbpkg (@pkgs) {
898 my $vertag = $pkg->{$pbpkg};
899 # get the version of the current package - maybe different
900 ($pbver,$pbtag) = split(/-/,$vertag);
901
902 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
903 my $src2="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
904 pb_log(2,"Source file: $src\n");
905 pb_log(2,"Pbconf file: $src2\n");
906
907 pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
908 if ($dtype eq "rpm") {
909 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
910 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
911 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";
912 }
913 }
914
915 # Remove in case a previous link/file was there
916 unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
917 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
918 # We need to first extract the spec file
919 my @specfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$ENV{'PBBUILDDIR'}/SPECS","spec");
920
921 # We need to handle potential patches to upstream sources
922 pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/pbpatch/","$ENV{'PBBUILDDIR'}/SOURCES","patch");
923
924 pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
925 # set LANGUAGE to check for correct log messages
926 $ENV{'LANGUAGE'}="C";
927 # Older Redhat use _target_platform in %configure incorrectly
928 my $specialdef = "";
929 if (($ddir eq "redhat") || (($ddir eq "rhel") && ($dver eq "2.1"))) {
930 $specialdef = "--define \'_target_platform \"\"\'";
931 }
932
933 # If needed we may add repository to the build env
934 pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
935 foreach my $f (@specfile) {
936 if ($f =~ /\.spec$/) {
937 pb_distro_installdeps($f,$dtype,$pbupd);
938 pb_system("rpmbuild $specialdef --define \'packager $ENV{'PBPACKAGER'}\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}","verbose");
939 last;
940 }
941 }
942 # Get the name of the generated packages
943 open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
944 while (<LOG>) {
945 chomp($_);
946 next if ($_ !~ /^Wrote:/);
947 s|.*/([S]*RPMS.*)|$1|;
948 $made .=" $_";
949 }
950 close(LOG);
951
952 } elsif ($dtype eq "deb") {
953 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
954 pb_system("tar xfz $src","Extracting sources");
955 pb_system("tar xfz $src2","Extracting pbconf");
956
957 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
958 pb_rm_rf("debian");
959 symlink "pbconf/$ddir-$dver-$arch","debian" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
960 chmod 0755,"debian/rules";
961
962 pb_distro_setuprepo($ddir,$dver,$arch,$dtype);
963 pb_distro_installdeps("debian/control",$dtype,$pbupd);
964 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package","verbose");
965 # Get the name of the generated packages
966 open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to open $ENV{'PBTMP'}/system.log";
967 while (<LOG>) {
968 chomp();
969 my $tmp = $_;
970 next if ($tmp !~ /^dpkg-deb.*:/);
971 $tmp =~ s|.*../(.*)_(.*).deb.*|$1|;
972 $made="$made $tmp.dsc $tmp.tar.gz $tmp"."_*.deb $tmp"."_*.changes";
973 }
974 close(LOG);
975 } elsif ($dtype eq "ebuild") {
976 my @ebuildfile;
977 # For gentoo we need to take pb as subsystem name
978 # We put every apps here under sys-apps. hope it's correct
979 # We use pb's home dir in order to have a single OVERLAY line
980 my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
981 pb_mkdir_p($tmpd) if (! -d "$tmpd");
982 pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
983
984 # We need to first extract the ebuild file
985 @ebuildfile = pb_extract_build_files($src2,"$pbpkg-$pbver/pbconf/$ddir-$dver-$arch/","$tmpd","ebuild");
986
987 # Prepare the build env for gentoo
988 my $found = 0;
989 my $pbbd = $ENV{'HOME'};
990 $pbbd =~ s|/|\\/|g;
991 if (-r "/etc/make.conf") {
992 open(MAKE,"/etc/make.conf");
993 while (<MAKE>) {
994 $found = 1 if (/$pbbd\/portage/);
995 }
996 close(MAKE);
997 }
998 if ($found == 0) {
999 pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
1000 }
1001 #$found = 0;
1002 #if (-r "/etc/portage/package.keywords") {
1003 #open(KEYW,"/etc/portage/package.keywords");
1004 #while (<KEYW>) {
1005 #$found = 1 if (/portage\/pb/);
1006 #}
1007 #close(KEYW);
1008 #}
1009 #if ($found == 0) {
1010 #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
1011 #}
1012
1013 # Build
1014 foreach my $f (@ebuildfile) {
1015 if ($f =~ /\.ebuild$/) {
1016 pb_distro_installdeps($f,$dtype,$pbupd);
1017 move($f,"$tmpd/$pbpkg-$pbver.ebuild");
1018 pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package","verbose");
1019 # Now move it where pb expects it
1020 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
1021 move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver-$pbtag.ebuild");
1022 }
1023 }
1024
1025 $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver-$pbtag.ebuild";
1026 } elsif ($dtype eq "tgz") {
1027 # Slackware family
1028 $made="$made $pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
1029
1030 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1031 pb_system("tar xfz $src","Extracting sources");
1032 pb_system("tar xfz $src2","Extracting pbconf");
1033 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
1034 symlink "pbconf/$ddir-$dver-$arch","install" || die "Unable to symlink to pbconf/$ddir-$dver-$arch";
1035 if (-x "install/pbslack") {
1036 pb_distro_installdeps("./install/pbslack",$dtype,$pbupd);
1037 pb_system("./install/pbslack","Building software");
1038 pb_system("sudo /sbin/makepkg -p -l y -c y $pbpkg","Packaging $pbpkg","verbose");
1039 }
1040 } elsif ($dtype eq "pkg") {
1041 # Solaris
1042 $made="$made $pbpkg-$pbver-$pbtag.pkg.gz";
1043 my $pkgdestdir="$ENV{'PBBUILDDIR'}/install";
1044
1045 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
1046 # Will host resulting packages
1047 pb_mkdir_p("$dtype");
1048 pb_mkdir_p("$pkgdestdir/delivery");
1049 pb_system("tar xfz $src","Extracting sources under $ENV{'PBBUILDDIR'}");
1050 pb_system("tar xfz $src2","Extracting pbconf under $ENV{'PBBUILDDIR'}");
1051 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
1052 if (-f "pbconf/$ddir-$dver-$arch/pbbuild") {
1053 chmod 0755,"pbconf/$ddir-$dver-$arch/pbbuild";
1054 # pkginfo file is mandatory
1055 die "Unable to find pkginfo file in pbconf/$ddir-$dver-$arch" if (! -f "pbconf/$ddir-$dver-$arch/pkginfo");
1056 # Build
1057 pb_system("pbconf/$ddir-$dver-$arch/pbbuild $pkgdestdir/delivery","Building software and installing under $pkgdestdir/delivery");
1058 # Copy complementary files
1059 if (-f "pbconf/$ddir-$dver-$arch/prototype") {
1060 copy("pbconf/$ddir-$dver-$arch/prototype", $pkgdestdir)
1061 } else {
1062 # No prototype provided, calculating it
1063 open(PROTO,"> $pkgdestdir/prototype") || die "Unable to create prototype file";
1064 print PROTO "i pkginfo\n";
1065 print PROTO "i depend\n" if (-f "pbconf/$ddir-$dver-$arch/depend");
1066 $ENV{'PBSOLDESTDIR'} = "$pkgdestdir/delivery";
1067 find(\&create_solaris_prototype, "$pkgdestdir/delivery");
1068 }
1069 copy("pbconf/$ddir-$dver-$arch/depend", $pkgdestdir) if (-f "pbconf/$ddir-$dver-$arch/depend");
1070 copy("pbconf/$ddir-$dver-$arch/pkginfo", $pkgdestdir);
1071 pb_system("cd $pkgdestdir/delivery ; pkgmk -o -f ../prototype -r $pkgdestdir/delivery -d $ENV{'PBBUILDDIR'}/$dtype","Packaging $pbpkg","verbose");
1072 pb_system("cd $ENV{'PBBUILDDIR'}/$dtype ; echo \"\" | pkgtrans -o -n -s $ENV{'PBBUILDDIR'}/$dtype $ENV{'PBBUILDDIR'}/$pbpkg-$pbver-$pbtag.pkg all","Transforming $pbpkg","verbose");
1073 pb_system("cd $ENV{'PBBUILDDIR'} ; gzip -9f $pbpkg-$pbver-$pbtag.pkg","Compressing $pbpkg-$pbver-$pbtag.pkg","verbose");
1074 } else {
1075 pb_log(0,"No pbconf/$ddir-$dver-$arch/pbbuild file found for $pbpkg-$pbver in \n");
1076 }
1077 chdir ".." || die "Unable to chdir to parent dir";
1078 pb_system("rm -rf $pbpkg-$pbver $ENV{'PBBUILDDIR'}/$dtype $pkgdestdir", "Cleanup");
1079 } else {
1080 die "Unknown dtype format $dtype";
1081 }
1082 }
1083 # Packages check if needed
1084 if ($dtype eq "rpm") {
1085 if (-f "/usr/bin/rpmlint") {
1086 pb_system("rpmlint $made","Checking validity of rpms with rpmlint","verbose");
1087 } else {
1088 pb_log(0,"rpm packages generated: $made\n");
1089 }
1090 } elsif ($dtype eq "deb") {
1091 my $made2 = "";
1092 foreach my $f (split(/ /,$made)) {
1093 $made2 .= "../$f " if ($f =~ /\.changes$/);
1094 }
1095 if (-f "/usr/bin/lintian") {
1096 pb_system("lintian $made2","Checking validity of debs with lintian","verbose");
1097 } else {
1098 pb_log(0,"deb packages generated: $made2\n");
1099 }
1100 } else {
1101 pb_log(0,"No check done for $dtype yet\n");
1102 pb_log(0,"Packages generated: $made\n");
1103 }
1104
1105 # Keep track of what is generated so that we can get them back from VMs
1106 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1107 print KEEP "$made\n";
1108 close(KEEP);
1109}
1110
1111sub create_solaris_prototype {
1112
1113 my $uidgid = "bin bin";
1114 my $pkgdestdir = $ENV{'PBSOLDESTDIR'};
1115
1116 return if ($_ =~ /^$pkgdestdir$/);
1117 if (-d $_) {
1118 my $n = $File::Find::name;
1119 $n =~ s~$pkgdestdir/~~;
1120 print PROTO "d none $n 0755 $uidgid\n";
1121 } elsif (-x $_) {
1122 my $n = $File::Find::name;
1123 $n =~ s~$pkgdestdir/~~;
1124 print PROTO "f none $n 0755 $uidgid\n";
1125 } elsif (-f $_) {
1126 my $n = $File::Find::name;
1127 $n =~ s~$pkgdestdir/~~;
1128 print PROTO "f none $n 0644 $uidgid\n";
1129 }
1130}
1131
1132sub pb_build2ssh {
1133 pb_send2target("Sources");
1134}
1135
1136sub pb_pkg2ssh {
1137 pb_send2target("Packages");
1138}
1139
1140# By default deliver to the the public site hosting the
1141# ftp structure (or whatever) or a VM/VE
1142sub pb_send2target {
1143
1144 my $cmt = shift;
1145 my $v = shift || undef;
1146 my $vmexist = shift || 0; # 0 is FALSE
1147 my $vmpid = shift || 0; # 0 is FALSE
1148 my $snapme = shift || 0; # 0 is FALSE
1149
1150 pb_log(2,"DEBUG: pb_send2target($cmt,".Dumper($v).",$vmexist,$vmpid)\n");
1151 my $host = "sshhost";
1152 my $login = "sshlogin";
1153 my $dir = "sshdir";
1154 my $port = "sshport";
1155 my $conf = "sshconf";
1156 my $rebuild = "sshrebuild";
1157 my $tmout = undef;
1158 my $path = undef;
1159 if ($cmt =~ /^VM/) {
1160 $login = "vmlogin";
1161 $dir = "pbdefdir";
1162 $rebuild = "vmrebuild";
1163 # Specific VM
1164 $tmout = "vmtmout";
1165 $path = "vmpath";
1166 $host = "vmhost";
1167 $port = "vmport";
1168 } elsif ($cmt =~ /^VE/) {
1169 $login = "velogin";
1170 $dir = "pbdefdir";
1171 # Specific VE
1172 $path = "vepath";
1173 $conf = "veconf";
1174 $rebuild = "verebuild";
1175 } elsif ($cmt eq "Web") {
1176 $host = "websshhost";
1177 $login = "websshlogin";
1178 $dir = "websshdir";
1179 $port = "websshport";
1180 }
1181 my $cmd = "";
1182 my $src = "";
1183 my ($odir,$over,$oarch) = (undef, undef, undef);
1184 my ($ddir, $dver, $dfam, $dtype, $pbsuf);
1185
1186 if ($cmt ne "Announce") {
1187 # Get list of packages to build
1188 my $ptr = pb_get_pkg();
1189 @pkgs = @$ptr;
1190
1191 # Get the running distro to consider
1192 if (defined $v) {
1193 ($odir,$over,$oarch) = split(/-/,$v);
1194 }
1195 ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over,$oarch);
1196 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
1197
1198 # Get list of packages to build
1199 # Get content saved in cms2build
1200 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
1201 $pkg = { } if (not defined $pkg);
1202
1203 chdir "$ENV{'PBBUILDDIR'}";
1204 foreach my $pbpkg (@pkgs) {
1205 my $vertag = $pkg->{$pbpkg};
1206 # get the version of the current package - maybe different
1207 ($pbver,$pbtag) = split(/-/,$vertag);
1208
1209 if (($cmt eq "Sources") || ($cmt =~ /V[EM]build/)) {
1210 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz $ENV{'PBDESTDIR'}/$pbpkg-$pbver.pbconf.tar.gz";
1211 if ($cmd eq "") {
1212 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
1213 } else {
1214 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
1215 }
1216 } elsif ($cmt eq "Web") {
1217 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz"
1218 }
1219 }
1220 # Adds conf file for availability of conf elements
1221 pb_conf_add("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb");
1222 }
1223
1224 if ($cmt =~ /V[EM]build/) {
1225 $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript";
1226 } elsif (($cmt =~ /V[EM]Script/) || ($cmt eq "Web")) {
1227 $src="$src $ENV{'PBDESTDIR'}/pbscript";
1228 } elsif ($cmt =~ /V[EM]test/) {
1229 $src="$src $ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb $ENV{'PBETC'} $ENV{'PBDESTDIR'}/pbrc $ENV{'PBDESTDIR'}/pbscript $ENV{'PBDESTDIR'}/pbtest";
1230 } elsif ($cmt eq "Announce") {
1231 $src="$src $ENV{'PBTMP'}/pbscript";
1232 } elsif ($cmt eq "Packages") {
1233 # Get package list from file made during build2pkg
1234 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1235 $src = <KEEP>;
1236 chomp($src);
1237 close(KEEP);
1238 $src="$src $ENV{'PBBUILDDIR'}/pbscript";
1239 }
1240 # Remove potential leading spaces (cause problem with basename)
1241 $src =~ s/^ *//;
1242 my $basesrc = "";
1243 foreach my $i (split(/ +/,$src)) {
1244 $basesrc .= " ".basename($i);
1245 }
1246
1247 pb_log(0,"Sources handled ($cmt): $src\n");
1248 pb_log(2,"values: ".Dumper(($host,$login,$dir,$port,$tmout,$rebuild,$path,$conf))."\n");
1249 my ($sshhost,$sshlogin,$sshdir,$sshport) = pb_conf_get($host,$login,$dir,$port);
1250 # Not mandatory...
1251 my ($vrebuild,$veconf,$testver) = pb_conf_get_if($rebuild,$conf,"testver");
1252 my ($vtmout,$vepath);
1253 # ...Except those in virtual context
1254 if ($cmt =~ /^VE/) {
1255 ($vepath) = pb_conf_get($path);
1256 }
1257 if ($cmt =~ /^VM/) {
1258 ($vtmout) = pb_conf_get($tmout);
1259 }
1260 pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf))."\n");
1261
1262 my $mac;
1263 if ($cmt !~ /^VE/) {
1264 $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
1265 # Overwrite account value if passed as parameter
1266 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
1267 pb_log(2, "DEBUG: pbaccount: $pbaccount => mac: $mac\n") if (defined $pbaccount);
1268 } else {
1269 # VE
1270 # Overwrite account value if passed as parameter (typically for setup2ve)
1271 $mac = $sshlogin->{$ENV{'PBPROJ'}};
1272 $mac = $pbaccount if (defined $pbaccount);
1273 }
1274
1275 my $tdir;
1276 my $bdir;
1277 if (($cmt eq "Sources") || ($cmt =~ /V[EM]Script/)) {
1278 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/src";
1279 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1280 # This is a test pkg => target dir is under test
1281 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/src";
1282 }
1283 } elsif (($cmt =~ /V[EM]build/) || ($cmt =~ /V[EM]test/)) {
1284 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery";
1285 $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build";
1286 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
1287 $bdir =~ s|\$ENV.+\}/||;
1288 } elsif ($cmt eq "Announce") {
1289 $tdir = "$sshdir->{$ENV{'PBPROJ'}}";
1290 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1291 # This is a test pkg => target dir is under test
1292 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test";
1293 }
1294 } elsif ($cmt eq "Web") {
1295 $tdir = "$sshdir->{$ENV{'PBPROJ'}}";
1296 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1297 # This is a test website => target dir is under test
1298 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/../test";
1299 }
1300 } elsif ($cmt eq "Packages") {
1301 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ddir/$dver";
1302
1303 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} =~ /true/i)) {
1304 # This is a test pkg => target dir is under test
1305 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/test/$ddir/$dver";
1306 }
1307
1308 my $repodir = $tdir;
1309 $repodir =~ s|^$sshdir->{$ENV{'PBPROJ'}}/||;
1310
1311 my ($pbrepo) = pb_conf_get("pbrepo");
1312
1313 # Repository management
1314 open(PBS,"> $ENV{'PBBUILDDIR'}/pbscript") || die "Unable to create $ENV{'PBBUILDDIR'}/pbscript";
1315 if ($dtype eq "rpm") {
1316 # Also make a pbscript to generate yum/urpmi bases
1317 print PBS << "EOF";
1318#!/bin/bash
1319# Prepare a script to ease yum setup
1320cat > $ENV{'PBPROJ'}.repo << EOT
1321[$ENV{'PBPROJ'}]
1322name=$ddir $dver - $ENV{'PBPROJ'} Vanilla Packages
1323baseurl=$pbrepo->{$ENV{'PBPROJ'}}/$repodir
1324enabled=1
1325gpgcheck=0
1326EOT
1327chmod 644 $ENV{'PBPROJ'}.repo
1328
1329# Clean up old repo content
1330rm -rf headers/ repodata/
1331# Create yum repo
1332yum-arch .
1333# Create repodata
1334createrepo .
1335EOF
1336 if ($dfam eq "md") {
1337 # For Mandriva add urpmi management
1338 print PBS << "EOF";
1339# Prepare a script to ease urpmi setup
1340cat > $ENV{'PBPROJ'}.addmedia << EOT
1341urpmi.addmedia $ENV{'PBPROJ'} $pbrepo->{$ENV{'PBPROJ'}}/$repodir with hdlist.cz
1342EOT
1343chmod 755 $ENV{'PBPROJ'}.addmedia
1344
1345# Clean up old repo content
1346rm -f hdlist.cz synthesis.hdlist.cz
1347# Create urpmi repo
1348genhdlist .
1349EOF
1350 }
1351 if ($ddir eq "fedora") {
1352 # Extract the spec file to please Fedora maintainers :-(
1353 print PBS << "EOF";
1354for p in $basesrc; do
1355 echo \$p | grep -q 'src.rpm'
1356 if [ \$\? -eq 0 ]; then
1357 rpm2cpio \$p | cpio -ivdum --quiet '*.spec'
1358 fi
1359done
1360EOF
1361 }
1362 } elsif ($dtype eq "deb") {
1363 # Also make a pbscript to generate apt bases
1364 # Cf: http://www.debian.org/doc/manuals/repository-howto/repository-howto.fr.html
1365 my $rpd = dirname("$pbrepo->{$ENV{'PBPROJ'}}/$repodir");
1366 print PBS << "EOF";
1367#!/bin/bash
1368# Prepare a script to ease apt setup
1369cat > $ENV{'PBPROJ'}.sources.list << EOT
1370deb $rpd $dver contrib
1371deb-src $rpd $dver contrib
1372EOT
1373chmod 644 $ENV{'PBPROJ'}.sources.list
1374
1375# Prepare a script to create apt info file
1376(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)
1377#(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)
1378EOF
1379 } elsif ($dtype eq "ebuild") {
1380 # make a pbscript to generate links to latest version
1381 print PBS << "EOF";
1382#!/bin/bash
1383# Prepare a script to create correct links
1384for p in $src; do
1385 echo \$p | grep -q '.ebuild'
1386 if [ \$\? -eq 0 ]; then
1387 j=`basename \$p`
1388 pp=`echo \$j | cut -d'-' -f1`
1389 ln -sf \$j \$pp.ebuild
1390 fi
1391done
1392EOF
1393 }
1394 close(PBS);
1395 chmod 0755,"$ENV{'PBBUILDDIR'}/pbscript";
1396 } else {
1397 return;
1398 }
1399
1400 # Useless for VE
1401 my $nport;
1402 if ($cmt !~ /^VE/) {
1403 $nport = $sshport->{$ENV{'PBPROJ'}};
1404 $nport = "$pbport" if (defined $pbport);
1405 }
1406
1407 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
1408 $tdir =~ s|\$ENV.+\}/||;
1409
1410 my $tm = undef;
1411 if ($cmt =~ /^VM/) {
1412 $tm = $vtmout->{$ENV{'PBPROJ'}};
1413 }
1414
1415 # ssh communication if not VE
1416 # should use a hash instead...
1417 my ($shcmd,$cpcmd,$cptarget,$cp2target);
1418 if ($cmt !~ /^VE/) {
1419 my $keyfile = pb_ssh_get(0);
1420 $shcmd = "ssh -i $keyfile -q -o UserKnownHostsFile=/dev/null -p $nport $mac";
1421 $cpcmd = "scp -i $keyfile -p -o UserKnownHostsFile=/dev/null -P $nport";
1422 $cptarget = "$mac:$tdir";
1423 if ($cmt =~ /^VMbuild/) {
1424 $cp2target = "$mac:$bdir";
1425 }
1426 } else {
1427 my $tp = $vepath->{$ENV{'PBPROJ'}};
1428 ($odir,$over,$oarch) = split(/-/,$v);
1429 my $tpdir = "$tp/$odir/$over/$oarch";
1430 my ($ptr) = pb_conf_get("vetype");
1431 my $vetype = $ptr->{$ENV{'PBPROJ'}};
1432 if ($vetype eq "chroot") {
1433 $shcmd = "sudo chroot $tpdir /bin/su - $mac -c ";
1434 } elsif ($vetype eq "schroot") {
1435 $shcmd = "schroot $tp -u $mac -- ";
1436 }
1437 $cpcmd = "sudo cp -r ";
1438 # We need to get the home dir of the target account to deliver in the right place
1439 open(PASS,"$tpdir/etc/passwd") || die "Unable to open $tpdir/etc/passwd";
1440 my $homedir = "";
1441 while (<PASS>) {
1442 my ($c1,$c2,$c3,$c4,$c5,$c6,$c7) = split(/:/);
1443 $homedir = $c6 if ($c1 =~ /^$mac$/);
1444 pb_log(3,"Homedir: $homedir - account: $c6\n");
1445 }
1446 close(PASS);
1447 $cptarget = "$tpdir/$homedir/$tdir";
1448 if ($cmt eq "VEbuild") {
1449 $cp2target = "$tpdir/$homedir/$bdir";
1450 }
1451 pb_log(2,"On VE using $cptarget as target dir to copy to\n");
1452 }
1453
1454 my $logres = "";
1455 # Do not touch when just announcing
1456 if ($cmt ne "Announce") {
1457 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");
1458 } else {
1459 $logres = "> ";
1460 }
1461 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
1462
1463 # For VE we need to change the owner manually
1464 if ($cmt =~ /^VE/) {
1465 pb_system("$shcmd \"sudo chown -R $mac $tdir\"","Adapt owner in $tdir to $mac");
1466 }
1467
1468 pb_system("$shcmd \"echo \'cd $tdir ; if [ -x pbscript ]; then ./pbscript; fi ; rm -f ./pbscript\' | bash\"","Executing pbscript on $cptarget if needed","verbose");
1469 if ($cmt =~ /^V[EM]build/) {
1470 # Get back info on pkg produced, compute their name and get them from the VM
1471 pb_system("$cpcmd $cp2target/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'} $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target");
1472 # For VE we need to change the owner manually
1473 if ($cmt eq "VEbuild") {
1474 pb_system("sudo chown $UID $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}","Adapt owner in $tdir to $UID");
1475 }
1476 # return here to avoid breaking the load on VMs/VEs in case of a bad one
1477 if (not -f "$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") {
1478 pb_log(0,"Problem with VM $v on $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}");
1479 return;
1480 }
1481 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1482 my $src = <KEEP>;
1483 chomp($src);
1484 close(KEEP);
1485 $src =~ s/^ *//;
1486 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
1487 # Change pgben to make the next send2target happy
1488 my $made = "";
1489 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}";
1490 foreach my $p (split(/ +/,$src)) {
1491 my $j = basename($p);
1492 # For VM we don't want shell expansion to hapen locally but remotely
1493 my $delim = '\'';
1494 if ($cmt =~ /^VEbuild/) {
1495 # For VE we need to support shell expansion locally
1496 $delim = "";
1497 }
1498 pb_system("$cpcmd $cp2target/$delim$p$delim $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Recovery of package $j in $ENV{'PBBUILDDIR'}/$odir/$over");
1499 $made="$made $odir/$over/$j"; # if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
1500 }
1501 print KEEP "$made\n";
1502 close(KEEP);
1503 pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
1504
1505 # Sign packages locally
1506 if ($dtype eq "rpm") {
1507 #pb_system("rpm --addsign --define \'_signature gpg\' --define \'__gpg_sign_cmd /usr/bin/gpg --batch --no-verbose --no-armor --no-secmem-warning -u \"$ENV{'PBPACKAGER'}\" -sbo %{__signature_filename} %{__plaintext_filename} --use-agent\' $made","Signing RPM packages packages");
1508 } elsif ($dtype eq "deb") {
1509 #pb_system("debsign $made","Signing DEB packages");
1510 } else {
1511 pb_log(0,"I don't know yet how to sign packages for type $dtype.\nPlease give feedback to dev team\n");
1512 }
1513
1514 # We want to send them to the ssh account so overwrite what has been done before
1515 undef $pbaccount;
1516 pb_log(2,"Before sending pkgs, vmexist: $vmexist, vmpid: $vmpid\n");
1517 pb_send2target("Packages",$odir."-".$over."-".$oarch,$vmexist,$vmpid);
1518 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
1519 }
1520 pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
1521 if ((! $vmexist) && ($cmt =~ /^VM/)) {
1522 # If in setupvm then takes a snapshot just before halting
1523 if ($snapme != 0) {
1524 my ($vmmonport,$vmtype) = pb_conf_get("vmmonport","vmtype");
1525 # For monitoring control
1526 if ((($vmtype->{$ENV{'PBPROJ'}}) eq "kvm") || (($vmtype->{$ENV{'PBPROJ'}}) eq "qemu")) {
1527 require Net::Telnet;
1528 my $t = new Net::Telnet (Timeout => 120, Host => "localhost", Port => $vmmonport->{$ENV{'PBPROJ'}}) || die "Unable to dialog on the monitor";
1529 # move to monitor mode
1530 my @lines = $t->cmd("c");
1531 # Create a snapshot named pb
1532 @lines = $t->cmd("savevm pb");
1533 # Write the new status in the VM
1534 @lines = $t->cmd("commit all");
1535 # End
1536 @lines = $t->cmd("quit");
1537 }
1538 }
1539 my $hoption = "-p";
1540 # Solaris doesn't support -h
1541 $hoption = "" if ($dtype eq "pkg");
1542 pb_system("$shcmd \"sudo /sbin/halt $hoption \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $v halt (pid $vmpid)");
1543 }
1544 if (($cmt =~ /^VE/) && ($snapme != 0)) {
1545 ($odir,$over,$oarch) = split(/-/,$v);
1546 my $tpdir = "$vepath->{$ENV{'PBPROJ'}}/$odir/$over/$oarch";
1547 pb_system("sudo tar cz -f $vepath->{$ENV{'PBPROJ'}}/$odir-$over-$oarch.tar.gz -C $tpdir .","Creating a snapshot of $tpdir");
1548 }
1549}
1550
1551sub pb_script2v {
1552 my $pbscript=shift;
1553 my $vtype=shift;
1554 my $pbforce=shift || 0; # Force stop of VM. Default not
1555 my $vm1=shift || undef; # Only that VM to treat
1556 my $snapme=shift || 0; # Do we have to create a snapshot
1557 my $vm;
1558 my $all;
1559
1560 pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$pbforce,".Dumper($vm1).",$snapme)\n");
1561 # Prepare the script to be executed on the VM
1562 # in $ENV{'PBDESTDIR'}/pbscript
1563 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
1564 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1565 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
1566 }
1567
1568 if (not defined $vm1) {
1569 ($vm,$all) = pb_get2v($vtype);
1570 } else {
1571 @$vm = ($vm1);
1572 }
1573 my ($vmexist,$vmpid) = (undef,undef);
1574
1575 foreach my $v (@$vm) {
1576 # Launch VM/VE
1577 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,$snapme,$pbsnap);
1578
1579 if ($vtype eq "vm") {
1580 pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
1581
1582 # Skip that VM if something went wrong
1583 next if (($vmpid == 0) && ($vmexist == 0));
1584
1585 # If force stopping the VM then reset vmexist
1586 if ($pbforce == 1) {
1587 $vmpid = $vmexist;
1588 $vmexist = 0;
1589 }
1590 } else {
1591 #VE
1592 $vmexist = 0;
1593 $vmpid = 0;
1594 }
1595
1596 # Gather all required files to send them to the VM
1597 # and launch the build through pbscript
1598 pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
1599 pb_send2target(uc($vtype)."Script","$v",$vmexist,$vmpid,$snapme);
1600
1601 }
1602}
1603
1604sub pb_launchv {
1605 my $vtype = shift;
1606 my $v = shift;
1607 my $create = shift || 0; # By default do not create a VM/VE
1608 my $snapme = shift || 0; # By default do not snap a VM/VE
1609 my $usesnap = shift || 1; # By default study the usage of the snapshot feature of VM/VE
1610
1611 # If creation or snapshot creation mode, no snapshot usable
1612 if (($create == 1) || ($snapme == 1)) {
1613 $usesnap = 0;
1614 }
1615
1616 pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create,$snapme,$usesnap)\n");
1617 die "No VM/VE defined, unable to launch" if (not defined $v);
1618 # Keep only the first VM in case many were given
1619 $v =~ s/,.*//;
1620
1621 my $arch = pb_get_arch();
1622
1623 # Launch the VMs/VEs
1624 if ($vtype eq "vm") {
1625 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
1626
1627 # TODO: vmmonport should be optional
1628 my ($ptr,$vmpath,$vmport,$vmsize,$vmmonport) = pb_conf_get("vmtype","vmpath","vmport","vmsize","vmmonport");
1629 my ($vmopt,$vmtmout,$vmsnap) = pb_conf_get_if("vmopt","vmtmout","vmsnap");
1630
1631 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
1632 if (not defined $ENV{'PBVMOPT'}) {
1633 $ENV{'PBVMOPT'} = "";
1634 }
1635 # Save the current status for later restoration
1636 $ENV{'PBOLDVMOPT'} = $ENV{'PBVMOPT'};
1637 # Set a default timeout of 2 minutes
1638 if (not defined $ENV{'PBVMTMOUT'}) {
1639 $ENV{'PBVMTMOUT'} = "120";
1640 }
1641 if (defined $vmopt->{$v}) {
1642 $ENV{'PBVMOPT'} .= " $vmopt->{$v}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$v}/);
1643 } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
1644 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
1645 }
1646
1647 # Are we allowed to use snapshot feature
1648 if ($usesnap == 1) {
1649 if ((defined $vmsnap->{$v}) && ($vmsnap->{$v} =~ /true/i)) {
1650 $ENV{'PBVMOPT'} .= "-snapshot ";
1651 } elsif ((defined $vmsnap->{$ENV{'PBPROJ'}}) && ($vmsnap->{$ENV{'PBPROJ'}} =~ /true/i)) {
1652 $ENV{'PBVMOPT'} .= "-snapshot ";
1653 } elsif ($pbsnap eq 1) {
1654 $ENV{'PBVMOPT'} .= "-snapshot ";
1655 }
1656 }
1657 if ($snapme != 0) {
1658 if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
1659 # Configure the monitoring to automize the creation of the 'pb' snapshot
1660 $ENV{'PBVMOPT'} .= "-serial mon:telnet::$vmmonport->{$ENV{'PBPROJ'}},server,nowait ";
1661 # In that case no snapshot call needed
1662 $ENV{'PBVMOPT'} =~ s/-snapshot //;
1663 }
1664 }
1665 if (defined $vmtmout->{$v}) {
1666 $ENV{'PBVMTMOUT'} = $vmtmout->{$v};
1667 } elsif (defined $vmtmout->{$ENV{'PBPROJ'}}) {
1668 $ENV{'PBVMTMOUT'} = $vmtmout->{$ENV{'PBPROJ'}};
1669 }
1670 my $nport = $vmport->{$ENV{'PBPROJ'}};
1671 $nport = "$pbport" if (defined $pbport);
1672
1673 my $cmd;
1674 my $vmcmd; # has to be used for pb_check_ps
1675 my $vmm; # has to be used for pb_check_ps
1676 if (($vmtype eq "qemu") || ($vmtype eq "kvm")) {
1677 my $qemucmd32;
1678 my $qemucmd64;
1679 if ($arch eq "x86_64") {
1680 $qemucmd32 = "/usr/bin/qemu-system-i386";
1681 $qemucmd64 = "/usr/bin/qemu";
1682 } else {
1683 $qemucmd32 = "/usr/bin/qemu";
1684 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
1685 }
1686 if ($v =~ /x86_64/) {
1687 $vmcmd = "$qemucmd64";
1688 } else {
1689 $vmcmd = "$qemucmd32";
1690 }
1691 if ($vmtype eq "kvm") {
1692 $vmcmd = "/usr/bin/kvm";
1693 }
1694 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
1695 if (($create != 0) || (defined $iso)) {
1696 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
1697 }
1698 # Always redirect the network and always try to use a 'pb' snapshot
1699 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 -loadvm pb $vmm"
1700 } elsif ($vmtype eq "xen") {
1701 } elsif ($vmtype eq "vmware") {
1702 } else {
1703 die "VM of type $vmtype not supported. Report to the dev team";
1704 }
1705 # Restore the ENV VAR Value
1706 $ENV{'PBVMOPT'} = $ENV{'PBOLDVMOPT'};
1707
1708 my ($tmpcmd,$void) = split(/ +/,$cmd);
1709 my $vmexist = pb_check_ps($tmpcmd,$vmm);
1710 my $vmpid = 0;
1711 if (! $vmexist) {
1712 if ($create != 0) {
1713 die("Found an existing Virtual machine $vmm. Won't overwrite") if (-r $vmm);
1714 if (($vmtype eq "qemu") || ($vmtype eq "xen") || ($vmtype eq "kvm")) {
1715 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
1716 } elsif ($vmtype eq "vmware") {
1717 } else {
1718 }
1719 }
1720 if (! -f "$vmm") {
1721 pb_log(0,"Unable to find VM $vmm\n");
1722 } else {
1723 pb_system("$cmd &","Launching the VM $vmm");
1724 pb_system("sleep $ENV{'PBVMTMOUT'}","Waiting $ENV{'PBVMTMOUT'} s for VM $v to come up");
1725 $vmpid = pb_check_ps($tmpcmd,$vmm);
1726 pb_log(0,"VM $vmm launched (pid $vmpid)\n");
1727 }
1728 } else {
1729 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
1730 }
1731 pb_log(2,"DEBUG: pb_launchv returns ($vmexist,$vmpid)\n");
1732 return($vmexist,$vmpid);
1733 # VE here
1734 } else {
1735 # Get distro context
1736 my ($name,$ver,$darch) = split(/-/,$v);
1737 chomp($darch);
1738 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver,$darch);
1739
1740 # Get VE context
1741 my ($ptr,$vetmout,$vepath,$verebuild,$veconf) = pb_conf_get("vetype","vetmout","vepath","verebuild","veconf");
1742 my $vetype = $ptr->{$ENV{'PBPROJ'}};
1743
1744 # We can probably only get those params now we have the distro context
1745 my ($veb4pi,$vepi,$vesnap,$oscodename,$vepkglist,$osmindep) = pb_conf_get_if("veb4pi","vepi","vesnap","oscodename","vepkglist","osmindep");
1746
1747 if (($vetype eq "chroot") || ($vetype eq "schroot")) {
1748 # Architecture consistency
1749 if ($arch ne $darch) {
1750 die "Unable to launch a VE of architecture $darch on a $arch platform" if (($darch eq "x86_64") && ($arch =~ /i?86/));
1751 }
1752
1753 my ($verpmtype,$vedebtype) = pb_conf_get("verpmtype","vedebtype");
1754 if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i) || ($pbforce == 1)) {
1755 # We have to rebuild the chroot
1756 if ($dtype eq "rpm") {
1757
1758 my $verpmstyle = $verpmtype->{$ENV{'PBPROJ'}};
1759 if ($verpmstyle eq "rinse") {
1760 # Need to reshape the mirrors generated with local before-post-install script
1761 my $b4post = "--before-post-install ";
1762 my $postparam = pb_distro_get_param($ddir,$dver,$darch,$veb4pi);
1763 if ($postparam eq "") {
1764 $b4post = "";
1765 } else {
1766 $b4post .= $postparam;
1767 }
1768 # Do we have a local post-install script
1769 my $post = "--post-install ";
1770 $postparam = pb_distro_get_param($ddir,$dver,$darch,$vepi);
1771 if ($postparam eq "") {
1772 $post = "";
1773 } else {
1774 $post .= $postparam;
1775 }
1776
1777 # Need to reshape the package list for pb
1778 my $addpkgs;
1779 $postparam = "";
1780 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
1781 $postparam .= ",";
1782 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$vepkglist);
1783 if ($postparam eq ",") {
1784 $addpkgs = "";
1785 } else {
1786 my $pkgfile = "$ENV{'PBTMP'}/addpkgs.lis";
1787 open(PKG,"> $pkgfile") || die "Unable to create $pkgfile";
1788 foreach my $p (split(/,/,$postparam)) {
1789 print PKG "$p\n";
1790 }
1791 close(PKG);
1792 $addpkgs = "--add-pkg-list $pkgfile";
1793 }
1794
1795 my $rinseverb = "";
1796 $rinseverb = "--verbose" if ($pbdebug gt 0);
1797
1798 pb_system("sudo /usr/sbin/rinse --directory \"$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch\" --arch \"$darch\" --distribution \"$ddir-$dver\" --config \"$veconf->{$ENV{'PBPROJ'}}\" $b4post $post $addpkgs $rinseverb","Creating the rinse VE for $ddir-$dver ($darch)", "verbose");
1799 } elsif ($verpmstyle eq "rpmbootstrap") {
1800 my $rbsverb = "";
1801 $rbsverb = "--verbose" if ($pbdebug gt 0);
1802 pb_system("sudo /usr/bin/rpmbootstrap $ddir-$dver-$darch $rbsverb","Creating the rpmbootstrap VE for $ddir-$dver ($darch)", "verbose");
1803 } elsif ($verpmstyle eq "mock") {
1804 pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE for $ddir-$dver ($darch)");
1805 # Once setup we need to install some packages, the pb account, ...
1806 pb_system("sudo /usr/sbin/mock --install --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
1807 } else {
1808 die "Unknown verpmtype type $verpmstyle. Report to dev team";
1809 }
1810 } elsif ($dtype eq "deb") {
1811 my $vedebstyle = $vedebtype->{$ENV{'PBPROJ'}};
1812 my $codename = pb_distro_get_param($ddir,$dver,$darch,$oscodename);
1813 my $postparam = "";
1814 my $addpkgs;
1815 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
1816 $postparam .= ",";
1817 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$vepkglist);
1818 if ($postparam eq ",") {
1819 $addpkgs = "";
1820 } else {
1821 $addpkgs = "--include $postparam";
1822 }
1823
1824 # debootstrap works with amd64 not x86_64
1825 my $debarch = $darch;
1826 $debarch = "amd64" if ($darch eq "x86_64");
1827 if ($vedebstyle eq "debootstrap") {
1828 my $dbsverb = "";
1829 $dbsverb = "--verbose" if ($pbdebug gt 0);
1830
1831 pb_system("sudo /usr/sbin/debootstrap $dbsverb --arch=$debarch $addpkgs $codename \"$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch\"","Creating the debootstrap VE for $ddir-$dver ($darch)", "verbose");
1832 # debootstrap doesn't create an /etc/hosts file
1833 if (! -f "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/etc/hosts" ) {
1834 pb_system("sudo cp /etc/hosts $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch/etc/hosts");
1835 }
1836 } else {
1837 die "Unknown vedebtype type $vedebstyle. Report to dev team";
1838 }
1839 } elsif ($dtype eq "ebuild") {
1840 die "Please teach the dev team how to build gentoo chroot";
1841 } else {
1842 die "Unknown distribution type $dtype. Report to dev team";
1843 }
1844 }
1845 # Test if an existing snapshot exists and use it if appropriate
1846 # And also use it of no local extracted VE is present
1847 if ((-f "$vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz") &&
1848 (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
1849 ((defined $vesnap->{$ENV{'PBPROJ'}}) && ($vesnap->{$ENV{'PBPROJ'}} =~ /true/i)) ||
1850 ($pbsnap eq 1) ||
1851 (! -d "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch"))) {
1852 pb_system("sudo rm -rf $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch ; sudo mkdir -p $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch ; sudo tar xz -C $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch -f $vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz","Extracting snapshot of $ddir-$dver-$darch.tar.gz under $vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch");
1853 }
1854 # Nothing more to do for VE. No real launch
1855 } else {
1856 die "VE of type $vetype not supported. Report to the dev team";
1857 }
1858 }
1859}
1860
1861# Return string for date synchro
1862sub pb_date2v {
1863
1864my $vtype = shift;
1865my $v = shift;
1866
1867my ($ntp) = pb_conf_get($vtype."ntp");
1868my $vntp = $ntp->{$ENV{'PBPROJ'}};
1869my $ntpline;
1870
1871if (defined $vntp) {
1872 my ($ntpcmd) = pb_conf_get($vtype."ntpcmd");
1873 my $vntpcmd;
1874 if (defined $ntpcmd->{$v}) {
1875 $vntpcmd = $ntpcmd->{$v};
1876 } elsif (defined $ntpcmd->{$ENV{'PBPROJ'}}) {
1877 $vntpcmd = $ntpcmd->{$ENV{'PBPROJ'}};
1878 } else {
1879 $vntpcmd = "/bin/true";
1880 }
1881 $ntpline = "sudo $vntpcmd $vntp";
1882} else {
1883 $ntpline = undef;
1884}
1885# Force new date to be in the future compared to the date
1886# of the host by adding 1 minute
1887my @date=pb_get_date();
1888$date[1]++;
1889my $upddate = strftime("%m%d%H%M%Y", @date);
1890my $dateline = "sudo date $upddate";
1891return($ntpline,$dateline);
1892}
1893
1894sub pb_build2v {
1895
1896my $vtype = shift;
1897my $action = shift || "build";
1898
1899my ($v,$all) = pb_get2v($vtype);
1900
1901# Send tar files when we do a global generation
1902pb_build2ssh() if (($all == 1) && ($action eq "build"));
1903
1904my ($vmexist,$vmpid) = (undef,undef);
1905
1906foreach my $v (@$v) {
1907 # Prepare the script to be executed on the VM/VE
1908 # in $ENV{'PBDESTDIR'}/pbscript
1909 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
1910 print SCRIPT "#!/bin/bash\n";
1911
1912 # Transmit the verbosity level to the virtual env/mach.
1913 my $verbose = "";
1914 my $i = 0; # minimal debug level
1915 while ($i lt $pbdebug) {
1916 $verbose .= "-v ";
1917 $i++;
1918 }
1919 # Activate script verbosity if at least 2 for pbdebug
1920 print SCRIPT "set -x\n" if ($i gt 1);
1921 # Quiet if asked to be so on the original system
1922 $verbose = "-q" if ($pbdebug eq -1);
1923
1924 print SCRIPT "echo ... Execution needed\n";
1925 print SCRIPT "# This is in directory delivery\n";
1926 print SCRIPT "# Setup the variables required for building\n";
1927 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
1928
1929 if ($action eq "build") {
1930 print SCRIPT "# Preparation for pb\n";
1931 print SCRIPT "mv .pbrc \$HOME\n";
1932 print SCRIPT "cd ..\n";
1933 }
1934
1935 # VE needs a good /proc
1936 if ($vtype eq "ve") {
1937 print SCRIPT "sudo mount -t proc /proc /proc\n";
1938 }
1939
1940 my ($ntpline,$dateline) = pb_date2v($vtype,$v);
1941 print SCRIPT "# Time sync\n";
1942 print SCRIPT "echo 'setting up date with '";
1943 if (defined $ntpline) {
1944 print SCRIPT "echo $ntpline\n";
1945 print SCRIPT "$ntpline\n";
1946 } else {
1947 print SCRIPT "echo $dateline\n";
1948 print SCRIPT "$dateline\n";
1949 }
1950 # Use potential local proxy declaration in case we need it to download repo, pkgs, ...
1951 if (defined $ENV{'http_proxy'}) {
1952 print SCRIPT "export http_proxy=\"$ENV{'http_proxy'}\"\n";
1953 }
1954
1955 if (defined $ENV{'ftp_proxy'}) {
1956 print SCRIPT "export ftp_proxy=\"$ENV{'ftp_proxy'}\"\n";
1957 }
1958
1959 # Get list of packages to build/test and get some ENV vars as well
1960 my $ptr = pb_get_pkg();
1961 @pkgs = @$ptr;
1962 my $p = join(' ',@pkgs) if (@pkgs);
1963 print SCRIPT "export PBPROJVER=$ENV{'PBPROJVER'}\n";
1964 print SCRIPT "export PBPROJTAG=$ENV{'PBPROJTAG'}\n";
1965 print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
1966
1967 # We may need to do some other tasks before building. Read a script here to finish setup
1968 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".pre") {
1969 print SCRIPT "# Special pre-instructions to be launched\n";
1970 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".pre");
1971 }
1972
1973 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre") {
1974 print SCRIPT "# Special pre-$action instructions to be launched\n";
1975 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.pre");
1976 }
1977
1978 print SCRIPT "# $action\n";
1979 print SCRIPT "echo $action"."ing packages on $vtype...\n";
1980
1981 if (($action eq "test") && (! -x "$ENV{'PBDESTDIR'}/pbtest")) {
1982 die "No test script ($ENV{'PBDESTDIR'}/pbtest) found when in test mode. Aborting ...";
1983 }
1984 print SCRIPT "pb $verbose -p $ENV{'PBPROJ'} $action"."2pkg $p\n";
1985
1986 if ($vtype eq "ve") {
1987 print SCRIPT "sudo umount /proc\n";
1988 }
1989
1990 # We may need to do some other tasks after building. Read a script here to exit properly
1991 if (-x "$ENV{'PBDESTDIR'}/pb$vtype"."$action.post") {
1992 print SCRIPT "# Special post-$action instructions to be launched\n";
1993 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype"."$action.post");
1994 }
1995
1996 if (-x "$ENV{'PBDESTDIR'}/pb$vtype".".post") {
1997 print SCRIPT "# Special post-instructions to be launched\n";
1998 print SCRIPT pb_get_content("$ENV{'PBDESTDIR'}/pb$vtype".".post");
1999 }
2000
2001 close(SCRIPT);
2002 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
2003
2004 # Launch the VM/VE
2005 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
2006
2007 if ($vtype eq "vm") {
2008 # Skip that VM if it something went wrong
2009 next if (($vmpid == 0) && ($vmexist == 0));
2010 } else {
2011 # VE
2012 $vmexist = 0;
2013 $vmpid = 0;
2014 }
2015 # Gather all required files to send them to the VM/VE
2016 # and launch the build through pbscript
2017 pb_log(2,"Calling send2target $vtype,$v,$vmexist,$vmpid\n");
2018 pb_send2target(uc($vtype).$action,"$v",$vmexist,$vmpid);
2019}
2020}
2021
2022
2023sub pb_clean {
2024
2025 my $sleep=10;
2026 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
2027 die "Unable to get env var PBBUILDDIR" if (not defined $ENV{'PBBUILDDIR'});
2028 pb_log(0,"We will now wait $sleep s before removing both directories\n$ENV{'PBDESTDIR'} and $ENV{'PBBUILDDIR'}.\nPlease break me if this is wrong\n");
2029 sleep $sleep;
2030 pb_rm_rf($ENV{'PBDESTDIR'});
2031 pb_rm_rf($ENV{'PBBUILDDIR'});
2032}
2033
2034sub pb_newver {
2035
2036 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
2037
2038 # Need this call for PBDIR
2039 my ($scheme2,$uri) = pb_cms_init($pbinit);
2040
2041 my ($pbconf,$pburl) = pb_conf_get("pbconfurl","pburl");
2042 $uri = $pbconf->{$ENV{'PBPROJ'}};
2043 my ($scheme, $account, $host, $port, $path) = pb_get_uri($uri);
2044
2045 # Checking CMS repositories status
2046 ($scheme2, $account, $host, $port, $path) = pb_get_uri($pburl->{$ENV{'PBPROJ'}});
2047
2048 if ($scheme !~ /^svn/) {
2049 die "Only SVN is supported at the moment";
2050 }
2051
2052 my $res = pb_cms_isdiff($scheme,$ENV{'PBROOTDIR'});
2053 die "ERROR: No differences accepted in CMS for $ENV{'PBROOTDIR'} before creating a new version" if ($res != 0);
2054
2055 $res = pb_cms_isdiff($scheme2,$ENV{'PBDIR'});
2056 die "ERROR: No differences accepted in CMS for $ENV{'PBDIR'} before creating a new version" if ($res != 0);
2057
2058 # Tree identical between PBCONFDIR and PBROOTDIR. The delta is what
2059 # we want to get for the root of the new URL
2060
2061 my $tmp = $ENV{'PBROOTDIR'};
2062 $tmp =~ s|^$ENV{'PBCONFDIR'}||;
2063
2064 my $newurl = "$uri/".dirname($tmp)."/$newver";
2065 # Should probably use projver in the old file
2066 my $oldver= basename($tmp);
2067
2068 # Duplicate and extract project-builder part
2069 pb_log(2,"Copying $uri/$tmp to $newurl\n");
2070 pb_cms_copy($scheme,"$uri/$tmp",$newurl);
2071 pb_log(2,"Checkout $newurl to $ENV{'PBROOTDIR'}/../$newver\n");
2072 pb_cms_up($scheme,"$ENV{'PBCONFDIR'}/..");
2073
2074 # Duplicate and extract project
2075 my $newurl2 = "$pburl->{$ENV{'PBPROJ'}}/".dirname($tmp)."/$newver";
2076
2077 pb_log(2,"Copying $pburl->{$ENV{'PBPROJ'}}/$tmp to $newurl2\n");
2078 pb_cms_copy($scheme2,"$pburl->{$ENV{'PBPROJ'}}/$tmp",$newurl2);
2079 pb_log(2,"Checkout $newurl2 to $ENV{'PBDIR'}/../$newver\n");
2080 pb_cms_up($scheme2,"$ENV{'PBDIR'}/..");
2081
2082 # Update the .pb file
2083 open(FILE,"$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb";
2084 open(OUT,"> $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new";
2085 while(<FILE>) {
2086 s/^projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
2087 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/);
2088 pb_log(0,"Commenting testver in $ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb\n") if (/^testver/);
2089 s/^testver/#testver/;
2090 print OUT $_;
2091 }
2092 close(FILE);
2093 close(OUT);
2094 rename("$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOTDIR'}/../$newver/$ENV{'PBPROJ'}.pb");
2095
2096 # Checking pbcl files
2097 foreach my $f (<$ENV{'PBROOTDIR'}/*/pbcl>) {
2098 # Compute new pbcl file
2099 my $f2 = $f;
2100 $f2 =~ s|$ENV{'PBROOTDIR'}|$ENV{'PBROOTDIR'}/../$newver/|;
2101 open(PBCL,$f) || die "Unable to open $f";
2102 my $foundnew = 0;
2103 while (<PBCL>) {
2104 $foundnew = 1 if (/^$newver \(/);
2105 }
2106 close(PBCL);
2107 open(OUT,"> $f2") || die "Unable to write to $f2: $!";
2108 open(PBCL,$f) || die "Unable to open $f";
2109 while (<PBCL>) {
2110 print OUT "$_" if (not /^$oldver \(/);
2111 if ((/^$oldver \(/) && ($foundnew == 0)) {
2112 print OUT "$newver ($pbdate)\n";
2113 print OUT "- TBD\n";
2114 print OUT "\n";
2115 pb_log(0,"WARNING: version $newver not found in $f so added to $f2...\n") if ($foundnew == 0);
2116 }
2117 }
2118 close(OUT);
2119 close(PBCL);
2120 }
2121
2122 pb_log(2,"Checkin $ENV{'PBROOTDIR'}/../$newver\n");
2123 pb_cms_checkin($scheme,"$ENV{'PBROOTDIR'}/../$newver",undef);
2124}
2125
2126#
2127# Return the list of VMs/VEs we are working on
2128# $all is a flag to know if we return all of them
2129# or only some (if all we publish also tar files in addition to pkgs
2130#
2131sub pb_get2v {
2132
2133my $vtype = shift;
2134my @v;
2135my $all = 0;
2136my $vlist;
2137my $pbv = 'PBV';
2138
2139if ($vtype eq "vm") {
2140 $vlist = "vmlist";
2141} elsif ($vtype eq "ve") {
2142 $vlist = "velist";
2143}
2144# Get VM/VE list
2145if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
2146 my ($ptr) = pb_conf_get($vlist);
2147 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
2148 $all = 1;
2149}
2150pb_log(2,"$vtype: $ENV{$pbv}\n");
2151@v = split(/,/,$ENV{$pbv});
2152return(\@v,$all);
2153}
2154
2155# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
2156# Needs to use root account to connect to the VM/VE
2157# pb will take your local public SSH key to access
2158# the pb account in the VM later on if needed
2159sub pb_setup2v {
2160
2161my $vtype = shift;
2162
2163my ($vm,$all) = pb_get2v($vtype);
2164
2165# Script generated
2166my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
2167
2168foreach my $v (@$vm) {
2169 # Deal with date sync.
2170 my ($ntpline,$dateline) = pb_date2v($vtype,$v);
2171
2172 # Get distro context
2173 my ($name,$ver,$darch) = split(/-/,$v);
2174 chomp($darch);
2175 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd) = pb_distro_init($name,$ver,$darch);
2176
2177 # Name of the account to deal with for VM/VE
2178 # Do not use the one passed potentially with -a
2179 my ($pbac) = pb_conf_get($vtype."login");
2180 my ($key,$zero0,$zero1,$zero2);
2181 my ($vmexist,$vmpid);
2182
2183 # Prepare the script to be executed on the VM/VE
2184 # in $ENV{'PBDESTDIR'}/setupv
2185 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2186
2187 print SCRIPT << 'EOF';
2188#!/usr/bin/perl -w
2189
2190use strict;
2191use File::Copy;
2192
2193# We should not need in this script more functions than what is provided
2194# by Base and Distribution to avoid problems at exec time.
2195# They are appended at the end.
2196
2197our $pbdebug;
2198our $pbLOG;
2199our $pbsynmsg = "pbscript";
2200our $pbdisplaytype = "text";
2201our $pblocale = "";
2202pb_log_init($pbdebug, $pbLOG);
2203pb_temp_init();
2204
2205EOF
2206
2207 # Launch the VM/VE - Usage of snapshot disabled
2208 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,0,0);
2209
2210 if ($vtype eq "vm") {
2211 # Prepare the key to be used and transfered remotely
2212 my $keyfile = pb_ssh_get(1);
2213
2214 my ($vmhost,$vmport,$vmntp) = pb_conf_get("vmhost","vmport","vmntp");
2215 my $nport = $vmport->{$ENV{'PBPROJ'}};
2216 $nport = "$pbport" if (defined $pbport);
2217
2218 # Skip that VM if something went wrong
2219 next if (($vmpid == 0) && ($vmexist == 0));
2220
2221 # Store the pub key part in a variable
2222 open(FILE,"$keyfile.pub") || die "Unable to open $keyfile.pub";
2223 ($zero0,$zero1,$zero2) = split(/ /,<FILE>);
2224 close(FILE);
2225
2226 $key = "\Q$zero1";
2227
2228 pb_system("cat $keyfile.pub | ssh -q -o UserKnownHostsFile=/dev/null -p $nport -i $keyfile root\@$vmhost->{$ENV{'PBPROJ'}} \"mkdir -p .ssh ; chmod 700 .ssh ; cat >> .ssh/authorized_keys ; chmod 600 .ssh/authorized_keys ; if [ -x /usr/bin/chcon ]; then /usr/bin/chcon -Rt home_ssh_t .ssh 2> /dev/null; fi\"","Copying local keys to $vtype. This may require the root password");
2229 # once this is done, we can do what we want on the VM remotely
2230 } elsif ($vtype eq "ve") {
2231 # We need to finish the setup of packages needed in the VE if needed
2232 # rinse/debootstrap at least needs it
2233 my ($vepkglist,$osmindep) = pb_conf_get_if("vepkglist","osmindep");
2234
2235 my $postparam = "";
2236 if (defined $osmindep) {
2237 $postparam .= pb_distro_get_param($ddir,$dver,$darch,$osmindep);
2238 }
2239 if (defined $vepkglist) {
2240 $postparam .= " ".pb_distro_get_param($ddir,$dver,$darch,$vepkglist);
2241 }
2242 # Change the list of pkg in to a space separated list
2243 $postparam =~ s/,/ /g;
2244 # remove potential sudo from the update command for this time,
2245 # as this will be run as root anyway, and if not we will have
2246 # a problem with tty
2247 $pbupd =~ s/sudo //g;
2248
2249 print SCRIPT << "EOF";
2250# For VE we need a good null dev
2251pb_system("rm -f /dev/null; mknod /dev/null c 1 3; chmod 777 /dev/null");
2252EOF
2253 if ($postparam ne "") {
2254 print SCRIPT << "EOF";
2255# For VE we first need to mount some FS
2256pb_system("mount -t proc /proc /proc");
2257
2258# For VE we need some additional packages that are not there yet
2259pb_system("$pbupd $postparam");
2260
2261EOF
2262 }
2263 }
2264 if ($vtype eq "vm") {
2265 print SCRIPT << 'EOF';
2266# Removes duplicate in .ssh/authorized_keys of our key if needed
2267#
2268my $file1="$ENV{'HOME'}/.ssh/authorized_keys";
2269open(PBFILE,$file1) || die "Unable to open $file1";
2270open(PBOUT,"> $file1.new") || die "Unable to open $file1.new";
2271my $count = 0;
2272while (<PBFILE>) {
2273
2274EOF
2275 print SCRIPT << "EOF";
2276 if (/ $key /) {
2277 \$count++;
2278 }
2279print PBOUT \$_ if ((\$count <= 1) || (\$_ !~ / $key /));
2280}
2281close(PBFILE);
2282close(PBOUT);
2283rename("\$file1.new",\$file1);
2284chmod 0600,\$file1;
2285
2286# Sync date
2287EOF
2288 if (defined $ntpline) {
2289 print SCRIPT "pb_system(\"$ntpline\");\n";
2290 } else {
2291 print SCRIPT "pb_system(\"$dateline\");\n";
2292 }
2293 }
2294 print SCRIPT << 'EOF';
2295
2296# Adds $pbac->{$ENV{'PBPROJ'}} as an account if needed
2297#
2298my $file="/etc/passwd";
2299open(PBFILE,$file) || die "Unable to open $file";
2300my $found = 0;
2301while (<PBFILE>) {
2302EOF
2303 print SCRIPT << "EOF";
2304 \$found = 1 if (/^$pbac->{$ENV{'PBPROJ'}}:/);
2305EOF
2306
2307my $home = "/home";
2308# Solaris doesn't like that we use /home
2309$home = "/export/home" if ($dtype eq "pkg");
2310
2311 print SCRIPT << "EOF";
2312}
2313close(PBFILE);
2314
2315if ( \$found == 0 ) {
2316 if ( ! -d "$home" ) {
2317 pb_mkdir_p("$home");
2318 }
2319EOF
2320 print SCRIPT << "EOF";
2321pb_system("groupadd $pbac->{$ENV{'PBPROJ'}}","Adding group $pbac->{$ENV{'PBPROJ'}}");
2322pb_system("useradd -g $pbac->{$ENV{'PBPROJ'}} -m -d $home/$pbac->{$ENV{'PBPROJ'}} $pbac->{$ENV{'PBPROJ'}}","Adding user $pbac->{$ENV{'PBPROJ'}} (group $pbac->{$ENV{'PBPROJ'}} - home $home/$pbac->{$ENV{'PBPROJ'}}");
2323}
2324EOF
2325
2326 # Copy the content of our local conf file to the VM/VE
2327 my $content = pb_get_content(pb_distro_conffile());
2328 print SCRIPT << "EOF";
2329 #
2330 # Create a temporary local conf file for distribution support
2331 # This is created here before its use later. Its place is hardcoded, so no choice for the path
2332 #
2333 my \$tempconf = pb_distro_conffile();
2334 pb_mkdir_p(dirname(\$tempconf));
2335 open(CONF,"> \$tempconf") || die "Unable to create \$tempconf";
2336 print CONF q{$content};
2337 close(CONF);
2338EOF
2339
2340 if ($vtype eq "vm") {
2341 print SCRIPT << "EOF";
2342# allow ssh entry to build
2343#
2344mkdir "$home/$pbac->{$ENV{'PBPROJ'}}/.ssh",0700;
2345# Allow those accessing root to access the build account
2346copy("\$ENV{'HOME'}/.ssh/authorized_keys","$home/$pbac->{$ENV{'PBPROJ'}}/.ssh/authorized_keys");
2347chmod 0600,".ssh/authorized_keys";
2348pb_system("chown -R $pbac->{$ENV{'PBPROJ'}}:$pbac->{$ENV{'PBPROJ'}} $home/$pbac->{$ENV{'PBPROJ'}}","Finish setting up the account env for $pbac->{$ENV{'PBPROJ'}}");
2349
2350EOF
2351}
2352 print SCRIPT << 'EOF';
2353# No passwd for build account only keys
2354$file="/etc/shadow";
2355if (-f $file) {
2356 open(PBFILE,$file) || die "Unable to open $file";
2357 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2358 while (<PBFILE>) {
2359EOF
2360 print SCRIPT << "EOF";
2361 s/^$pbac->{$ENV{'PBPROJ'}}:\!\!:/$pbac->{$ENV{'PBPROJ'}}:*:/;
2362 s/^$pbac->{$ENV{'PBPROJ'}}:\!:/$pbac->{$ENV{'PBPROJ'}}:*:/; #SLES 9 e.g.
2363 s/^$pbac->{$ENV{'PBPROJ'}}:\\*LK\\*:/$pbac->{$ENV{'PBPROJ'}}:NP:/; #Solaris e.g.
2364EOF
2365 print SCRIPT << 'EOF';
2366 print PBOUT $_;
2367 }
2368 close(PBFILE);
2369 close(PBOUT);
2370 rename("$file.new",$file);
2371 chmod 0640,$file;
2372 }
2373
2374# Keep the VM in text mode
2375$file="/etc/inittab";
2376if (-f $file) {
2377 open(PBFILE,$file) || die "Unable to open $file";
2378 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2379 while (<PBFILE>) {
2380 s/^(..):5:initdefault:$/$1:3:initdefault:/;
2381 print PBOUT $_;
2382 }
2383 close(PBFILE);
2384 close(PBOUT);
2385 rename("$file.new",$file);
2386 chmod 0640,$file;
2387}
2388
2389# pb has to be added to portage group on gentoo
2390
2391# We need to have that pb_distro_init function
2392# Get it from Project-Builder::Distribution
2393# And we now need the conf file required for this to work created above
2394
2395my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $darch) = pb_distro_init();
2396print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf, $darch))."\n";
2397
2398# Adapt sudoers
2399# sudo is not default on Solaris and needs to be installed first
2400# from http://www.sunfreeware.com/programlistsparc10.html#sudo
2401if ($dtype eq "pkg") {
2402 $file="/usr/local/etc/sudoers";
2403} else {
2404 $file="/etc/sudoers";
2405}
2406open(PBFILE,$file) || die "Unable to open $file";
2407open(PBOUT,"> $file.new") || die "Unable to open $file.new";
2408while (<PBFILE>) {
2409EOF
2410 print SCRIPT << "EOF";
2411 next if (/^$pbac->{$ENV{'PBPROJ'}} /);
2412EOF
2413 print SCRIPT << 'EOF';
2414 s/Defaults[ \t]+requiretty//;
2415 print PBOUT $_;
2416}
2417close(PBFILE);
2418EOF
2419 print SCRIPT << "EOF";
2420# This is needed in order to be able to halt the machine from the $pbac->{$ENV{'PBPROJ'}} account at least
2421print PBOUT "Defaults:pb env_keep += \\\"http_proxy ftp_proxy\\\"\n";
2422print PBOUT "$pbac->{$ENV{'PBPROJ'}} ALL=(ALL) NOPASSWD:ALL\n";
2423EOF
2424 print SCRIPT << 'EOF';
2425close(PBOUT);
2426rename("$file.new",$file);
2427chmod 0440,$file;
2428
2429EOF
2430
2431 # We may need a proxy configuration. Get it from the local env
2432
2433 if (defined $ENV{'http_proxy'}) {
2434 print SCRIPT "\$ENV\{'http_proxy'\}=\"$ENV{'http_proxy'}\";\n";
2435 }
2436
2437 if (defined $ENV{'ftp_proxy'}) {
2438 print SCRIPT "\$ENV\{'ftp_proxy'\}=\"$ENV{'ftp_proxy'}\";\n";
2439 }
2440
2441 print SCRIPT << 'EOF';
2442
2443 my ($ospkgdep,$osperldep,$osperlver) = pb_conf_get_if("ospkgdep","osperldep","osperlver");
2444
2445 # First install all required packages
2446 pb_system("yum clean all","Cleaning yum env") if (($ddir eq "fedora") || ($ddir eq "asianux") || ($ddir eq "rhel"));
2447 my $pkgdep = pb_distro_get_param($ddir,$dver,$darch,$ospkgdep,$dfam,$dtype);
2448 pb_distro_installdeps(undef,$dtype,$pbupd,pb_distro_only_deps_needed($dtype,join(' ',split(/,/,$pkgdep))));
2449
2450 # Then install manually the missing perl modules
2451 my $perldep = pb_distro_get_param($ddir,$dver,$darch,$osperldep,$dfam,$dtype);
2452 foreach my $m (split(/,/,$perldep)) {
2453 # Skip empty deps
2454 next if ($m =~ /^\s*$/);
2455 my $dir = $m;
2456 $dir =~ s/-.*//;
2457 pb_system("echo \"rm -rf $m* ; wget http://search.cpan.org/CPAN/modules/by-module/$dir/$m-$osperlver->{$m}.tar.gz ; gzip -cd $m-$osperlver->{$m}.tar.gz | tar xf - ; cd $m* ; if [ -f Build.PL ]; then perl Build.PL; ./Build ; ./Build install ; else perl Makefile.PL; make ; make install ; fi; cd .. ; rm -rf $m*\" | bash" ,"Installing perl module $m-$osperlver->{$m}");
2458 }
2459
2460# Suse wants sudoers as 640
2461if (($ddir eq "sles") || (($ddir eq "opensuse") && ($dver =~ /10.[012]/))) {
2462 chmod 0640,$file;
2463}
2464
2465pb_system("rm -rf ProjectBuilder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/ProjectBuilder-latest.tar.gz ; gzip -cd ProjectBuilder-latest.tar.gz | tar xf - ; cd ProjectBuilder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf ProjectBuilder-* ; rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; gzip -cd project-builder-latest.tar.gz | tar xf - ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd .. ; rm -rf project-builder-* ;","Building Project-Builder");
2466pb_system("pb 2>&1 | head -5",undef,"verbose");
2467EOF
2468 if ($vtype eq "ve") {
2469 print SCRIPT << 'EOF';
2470# For VE we need to umount some FS at the end
2471
2472pb_system("umount /proc");
2473
2474# Create a basic network file if not already there
2475
2476my $nf="/etc/sysconfig/network";
2477if ((! -f $nf) && ($dtype eq "rpm")) {
2478 open(NF,"> $nf") || die "Unable to create $nf";
2479 print NF "NETWORKING=yes\n";
2480 print NF "HOSTNAME=localhost\n";
2481 close(NF);
2482}
2483chmod 0755,$nf;
2484EOF
2485 }
2486
2487 # Adds pb_distro_init and all functions needed from ProjectBuilder::Distribution and Base
2488 foreach my $d (@INC) {
2489 my @f = ("$d/ProjectBuilder/Base.pm","$d/ProjectBuilder/Distribution.pm","$d/ProjectBuilder/Conf.pm");
2490 foreach my $f (@f) {
2491 if (-f "$f") {
2492 open(PBD,"$f") || die "Unable to open $f";
2493 while (<PBD>) {
2494 next if (/^package/);
2495 next if (/^use Exporter/);
2496 next if (/^use ProjectBuilder::/);
2497 next if (/^our /);
2498 print SCRIPT $_;
2499 }
2500 close(PBD);
2501 }
2502 }
2503 }
2504 close(SCRIPT);
2505 chmod 0755,"$pbscript";
2506
2507 # That build script needs to be run as root and force stop of VM at end
2508 $pbaccount = "root";
2509
2510 # Force shutdown of VM exept if it was already launched
2511 my $pbforce = 0;
2512 if ((! $vmexist) && ($vtype eq "vm")) {
2513 $pbforce = 1;
2514 }
2515
2516 pb_script2v($pbscript,$vtype,$pbforce,$v);
2517}
2518return;
2519}
2520
2521# Function to create a snapshot named 'pb' for VMs and a compressed tar for VEs
2522sub pb_snap2v {
2523
2524my $vtype = shift;
2525
2526my ($vm,$all) = pb_get2v($vtype);
2527
2528# Script generated
2529my $pbscript = "$ENV{'PBDESTDIR'}/snapv";
2530
2531my ($pbac) = pb_conf_get($vtype."login");
2532
2533foreach my $v (@$vm) {
2534 # Get distro context
2535 my ($name,$ver,$darch) = split(/-/,$v);
2536 chomp($darch);
2537 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver,$darch);
2538 my ($vepath) = pb_conf_get("vepath");
2539
2540 # Test if an existing snapshot exists and remove it if there is a VE
2541 if ((-f "$vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz") &&
2542 (! -d "$vepath->{$ENV{'PBPROJ'}}/$ddir/$dver/$darch")) {
2543 pb_system("sudo rm -f $vepath->{$ENV{'PBPROJ'}}/$ddir-$dver-$darch.tar.gz","Removing previous snapshot $ddir-$dver-$darch.tar.gz");
2544 }
2545
2546 # Name of the account to deal with for VM/VE
2547 # Do not use the one passed potentially with -a
2548 my ($vmexist,$vmpid);
2549
2550 # Prepare the script to be executed on the VM/VE
2551 # in $ENV{'PBDESTDIR'}/setupv
2552 open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
2553
2554 print SCRIPT << 'EOF';
2555 #!/bin/bash
2556 sleep 2
2557EOF
2558 close(SCRIPT);
2559 chmod 0755,"$pbscript";
2560
2561 # Force shutdown of VM/VE
2562 # Force snapshot of VM/VE
2563 pb_script2v($pbscript,$vtype,1,$v,1);
2564}
2565return;
2566}
2567
2568sub pb_announce {
2569
2570 # Get all required parameters
2571 my ($pbpackager,$pbrepo,$pbml,$pbsmtp) = pb_conf_get("pbpackager","pbrepo","pbml","pbsmtp");
2572 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
2573 my $pkg = pb_cms_get_pkg($defpkgdir,$extpkgdir);
2574 my @pkgs = @$pkg;
2575 my %pkgs;
2576 my $first = 0;
2577
2578 # Command to find packages on repo
2579 my $findstr = "find . ";
2580 # Generated announce files
2581 my @files;
2582
2583 foreach my $pbpkg (@pkgs) {
2584 if ($first != 0) {
2585 $findstr .= "-o ";
2586 }
2587 $first++;
2588 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
2589 $pbver = $pkgv->{$pbpkg};
2590 } else {
2591 $pbver = $ENV{'PBPROJVER'};
2592 }
2593 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
2594 $pbtag = $pkgt->{$pbpkg};
2595 } else {
2596 $pbtag = $ENV{'PBPROJTAG'};
2597 }
2598
2599 # TODO: use virtual/real names here now
2600 $findstr .= "-name \'$pbpkg-$pbver-$pbtag\.*.rpm\' -o -name \'$pbpkg"."_$pbver*\.deb\' -o -name \'$pbpkg-$pbver\.ebuild\' ";
2601
2602 my $chglog;
2603
2604 # Get project info on log file and generate tmp files used later on
2605 pb_cms_init($pbinit);
2606 $chglog = "$ENV{'PBROOTDIR'}/$pbpkg/pbcl";
2607 $chglog = "$ENV{'PBROOTDIR'}/pbcl" if (! -f $chglog);
2608 $chglog = undef if (! -f $chglog);
2609
2610 open(OUT,"> $ENV{'PBTMP'}/$pbpkg.ann") || die "Unable to create $ENV{'PBTMP'}/$pbpkg.ann: $!";
2611 my %pb;
2612 $pb{'dtype'} = "announce";
2613 $pb{'realpkg'} = $pbpkg;
2614 $pb{'ver'} = $pbver;
2615 $pb{'tag'} = $pbtag;
2616 $pb{'suf'} = "N/A"; # Should not be empty even if unused
2617 $pb{'date'} = $pbdate;
2618 $pb{'chglog'} = $chglog;
2619 $pb{'packager'} = $pbpackager;
2620 $pb{'proj'} = $ENV{'PBPROJ'};
2621 $pb{'repo'} = $pbrepo;
2622 pb_changelog(\%pb,\*OUT,"yes");
2623 close(OUT);
2624 push(@files,"$ENV{'PBTMP'}/$pbpkg.ann");
2625 }
2626 $findstr .= " | grep -Ev \'src.rpm\'";
2627 if ((not defined $testver) || (not defined $testver->{$ENV{'PBPROJ'}}) || ($testver->{$ENV{'PBPROJ'}} !~ /true/i)) {
2628 $findstr .= " | grep -v ./test/";
2629 }
2630
2631 # Prepare the command to run and execute it
2632 open(PBS,"> $ENV{'PBTMP'}/pbscript") || die "Unable to create $ENV{'PBTMP'}/pbscript";
2633 print PBS "$findstr\n";
2634 close(PBS);
2635 chmod 0755,"$ENV{'PBTMP'}/pbscript";
2636 pb_send2target("Announce");
2637
2638 # Get subject line
2639 my $sl = "Project $ENV{'PBPROJ'} version $ENV{'PBPROJVER'} is now available";
2640 pb_log(0,"Please enter the title of your announce\n");
2641 pb_log(0,"(By default: $sl)\n");
2642 my $sl2 = <STDIN>;
2643 $sl = $sl2 if ($sl2 !~ /^$/);
2644
2645 # Prepare a template of announce
2646 open(ANN,"> $ENV{'PBTMP'}/announce.html") || die "Unable to create $ENV{'PBTMP'}/announce.html: $!";
2647 print ANN << "EOF";
2648$sl</p>
2649
2650<p>The project team is happy to announce the availability of a newest version of $ENV{'PBPROJ'} $ENV{'PBPROJVER'}. Enjoy it as usual!</p>
2651<p>
2652Now available at <a href="$pbrepo->{$ENV{'PBPROJ'}}">$pbrepo->{$ENV{'PBPROJ'}}</a>
2653</p>
2654<p>
2655EOF
2656 open(LOG,"$ENV{'PBTMP'}/system.log") || die "Unable to read $ENV{'PBTMP'}/system.log: $!";
2657 my $col = 2;
2658 my $i = 1;
2659 print ANN << 'EOF';
2660<TABLE WIDTH="700" CELLPADDING="0" CELLSPACING="0" BORDER="0">
2661<TR>
2662EOF
2663 while (<LOG>) {
2664 print ANN "<TD><A HREF=\"$pbrepo->{$ENV{'PBPROJ'}}/$_\">$_</A></TD>";
2665 $i++;
2666 if ($i > $col) {
2667 print ANN "</TR>\n<TR>";
2668 $i = 1;
2669 }
2670 }
2671 close(LOG);
2672 print ANN << "EOF";
2673</TR>
2674</TABLE>
2675</p>
2676
2677<p>As usual source packages are also available in the same directory.</p>
2678
2679<p>
2680Changes are :
2681</p>
2682<p>
2683EOF
2684 # Get each package changelog content
2685 foreach my $f (sort(@files)) {
2686 open(IN,"$f") || die "Unable to read $f:$!";
2687 while (<IN>) {
2688 print ANN $_;
2689 }
2690 close(IN);
2691 print ANN "</p><p>\n";
2692 }
2693 print ANN "</p>\n";
2694 close(ANN);
2695
2696 # Allow for modification
2697 my $editor = "vi";
2698 $editor = $ENV{'EDITOR'} if (defined $ENV{'EDITOR'});
2699 pb_system("$editor $ENV{'PBTMP'}/announce.html","Allowing modification of the announce","noredir");
2700
2701 # Store it in DB for external usage (Web pages generation)
2702 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
2703
2704 my $precmd = "";
2705 if (! -f $db) {
2706 $precmd = "CREATE TABLE announces (id INTEGER PRIMARY KEY AUTOINCREMENT, date DATE, announce VARCHAR[65535])";
2707 }
2708
2709 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
2710 { RaiseError => 1, AutoCommit => 1 })
2711 || die "Unable to connect to $db";
2712
2713 if ($precmd ne "") {
2714 my $sth = $dbh->prepare(qq{$precmd})
2715 || die "Unable to create table into $db";
2716 $sth->execute();
2717 }
2718
2719 # To read whole file
2720 local $/;
2721 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2722 my $announce = <ANN>;
2723 close(ANN);
2724
2725 pb_log(2,"INSERT INTO announces VALUES (NULL, $pbdate, $announce)");
2726 my $sth = $dbh->prepare(qq{INSERT INTO announces VALUES (NULL,?,?)})
2727 || die "Unable to insert into $db";
2728 $sth->execute($pbdate, $announce);
2729 $sth->finish();
2730 $dbh->disconnect;
2731
2732 # Then deliver it on the Web
2733 # $TOOLHOME/livwww www
2734
2735 # Mail it to project's ML
2736 open(ML,"| w3m -dump -T text/html > $ENV{'PBTMP'}/announce.txt") || die "Unable to create $ENV{'PBTMP'}/announce.txt: $!";
2737 print ML << 'EOF';
2738<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x html1/DTD/xhtml1-strict.dtd">
2739
2740<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="en" lang="en">
2741 <head>
2742 </head>
2743 <body>
2744 <p>
2745EOF
2746 open(ANN,"$ENV{'PBTMP'}/announce.html") || die "Unable to read $ENV{'PBTMP'}/announce.html: $!";
2747 while(<ANN>) {
2748 print ML $_;
2749 }
2750 print ML << 'EOF';
2751</body>
2752</html>
2753EOF
2754 close(ML);
2755
2756 # To read whole file
2757 local $/;
2758 open(ANN,"$ENV{'PBTMP'}/announce.txt") || die "Unable to read $ENV{'PBTMP'}/announce.txt: $!";
2759 my $msg = <ANN>;
2760 close(ANN);
2761
2762 # Preparation of headers
2763
2764 my %mail = (
2765 To => $pbml->{$ENV{'PBPROJ'}},
2766 From => $pbpackager->{$ENV{'PBPROJ'}},
2767 Smtp => $pbsmtp->{$ENV{'PBPROJ'}},
2768 Body => $msg,
2769 Subject => "[ANNOUNCE] $sl",
2770 );
2771
2772 # Send mail
2773 sendmail(%mail) or die "Unable to send mail ($Mail::Sendmail::error): $Mail::Sendmail::log";
2774}
2775
2776#
2777# Creates a set of HTML file containing the news for the project
2778# based on what has been generated by the pb_announce function
2779#
2780sub pb_web_news2html {
2781
2782 my $dest = shift || $ENV{'PBTMP'};
2783
2784 # Get all required parameters
2785 my ($pkgv, $pkgt, $testver) = pb_conf_get_if("pkgver","pkgtag","testver");
2786
2787 # DB of announces for external usage (Web pages generation)
2788 my $db = "$ENV{'PBCONFDIR'}/announces3.sql";
2789
2790 my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","",
2791 { RaiseError => 1, AutoCommit => 1 })
2792 || die "Unable to connect to $db";
2793 # For date handling
2794 $ENV{LANGUAGE}="C";
2795 my $firstjan = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year(), 0, 0, -1);
2796 my $oldfirst = strftime("%Y-%m-%d", 0, 0, 0, 1, 0, localtime->year()-1, 0, 0, -1);
2797 pb_log(2,"firstjan: $firstjan, oldfirst: $oldfirst, pbdate:$pbdate\n");
2798 my $all = $dbh->selectall_arrayref("SELECT id,date,announce FROM announces ORDER BY date DESC");
2799 my %news;
2800 $news{"cy"} = ""; # current year's news
2801 $news{"ly"} = ""; # last year news
2802 $news{"py"} = ""; # previous years news
2803 $news{"fp"} = ""; # first page news
2804 my $cpt = 4; # how many news for first page
2805 # Extract info from DB
2806 foreach my $row (@$all) {
2807 my ($id, $date, $announce) = @$row;
2808 $news{"cy"} = $news{"cy"}."<p><B>$date</B> $announce\n" if ((($date cmp $pbdate) le 0) && (($firstjan cmp $date) le 0));
2809 $news{"ly"} = $news{"ly"}."<p><B>$date</B> $announce\n" if ((($date cmp $firstjan) le 0) && (($oldfirst cmp $date) le 0));
2810 $news{"py"} = $news{"py"}."<p><B>$date</B> $announce\n" if (($date cmp $oldfirst) le 0);
2811 $news{"fp"} = $news{"fp"}."<p><B>$date</B> $announce\n" if ($cpt > 0);
2812 $cpt--;
2813 }
2814 pb_log(1,"news{fp}: ".$news{"fp"}."\n");
2815 $dbh->disconnect;
2816
2817 # Generate the HTML content
2818 foreach my $pref (keys %news) {
2819 open(NEWS,"> $dest/pb_web_$pref"."news.html") || die "Unable to create $dest/pb_web_$pref"."news.html: $!";
2820 print NEWS "$news{$pref}";
2821 close(NEWS);
2822 }
2823}
2824
2825
2826# Return the SSH key file to use
2827# Potentially create it if needed
2828
2829sub pb_ssh_get {
2830
2831my $create = shift || 0; # Do not create keys by default
2832
2833# Check the SSH environment
2834my $keyfile = undef;
2835
2836# We have specific keys by default
2837$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa";
2838if (!(-e $keyfile) && ($create eq 1)) {
2839 pb_system("ssh-keygen -q -b 1024 -N '' -f $keyfile -t dsa","Generating SSH keys for pb");
2840}
2841
2842$keyfile = "$ENV{'HOME'}/.ssh/id_rsa" if (-s "$ENV{'HOME'}/.ssh/id_rsa");
2843$keyfile = "$ENV{'HOME'}/.ssh/id_dsa" if (-s "$ENV{'HOME'}/.ssh/id_dsa");
2844$keyfile = "$ENV{'HOME'}/.ssh/pb_dsa" if (-s "$ENV{'HOME'}/.ssh/pb_dsa");
2845die "Unable to find your public ssh key under $keyfile" if (not defined $keyfile);
2846return($keyfile);
2847}
2848
2849
2850# Returns the pid of a running VM command using a specific VM file
2851sub pb_check_ps {
2852 my $vmcmd = shift;
2853 my $vmm = shift;
2854 my $vmexist = 0; # FALSE by default
2855
2856 open(PS, "ps auxhww|") || die "Unable to call ps";
2857 while (<PS>) {
2858 next if (! /$vmcmd/);
2859 next if (! /$vmm/);
2860 my ($void1, $void2);
2861 ($void1, $vmexist, $void2) = split(/ +/);
2862 last;
2863 }
2864 return($vmexist);
2865}
2866
2867
2868sub pb_extract_build_files {
2869
2870my $src=shift;
2871my $dir=shift;
2872my $ddir=shift;
2873my $mandatory=shift || "spec";
2874my @files;
2875
2876my $flag = "mayfail" if ($mandatory eq "patch");
2877my $res;
2878
2879if ($src =~ /tar\.gz$/) {
2880 $res = pb_system("tar xfpz $src $dir","Extracting $mandatory files from $src",$flag);
2881} elsif ($src =~ /tar\.bz2$/) {
2882 $res = pb_system("tar xfpj $src $dir","Extracting $mandatory files from $src",$flag);
2883} else {
2884 die "Unknown compression algorithm for $src";
2885}
2886# If not mandatory return now
2887return() if (($res != 0) and ($mandatory eq "patch"));
2888opendir(DIR,"$dir") || die "Unable to open directory $dir";
2889foreach my $f (readdir(DIR)) {
2890 next if ($f =~ /^\./);
2891 # Skip potential patch dir
2892 next if ($f =~ /^pbpatch/);
2893 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
2894 pb_log(2,"mv $dir/$f $ddir\n");
2895 push @files,"$ddir/$f";
2896}
2897closedir(DIR);
2898# Not enough but still a first cleanup
2899pb_rm_rf("$dir");
2900return(@files);
2901}
2902
2903sub pb_list_bfiles {
2904
2905my $dir = shift;
2906my $pbpkg = shift;
2907my $bfiles = shift;
2908my $pkgfiles = shift;
2909my $supfiles = shift;
2910
2911opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
2912foreach my $f (readdir(BDIR)) {
2913 next if ($f =~ /^\./);
2914 $bfiles->{$f} = "$dir/$f";
2915 $bfiles->{$f} =~ s~$ENV{'PBROOTDIR'}~~;
2916 if (defined $supfiles->{$pbpkg}) {
2917 $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
2918 }
2919}
2920closedir(BDIR);
2921}
2922
2923
2924#
2925# Return the list of packages we are working on in a non CMS action
2926#
2927sub pb_get_pkg {
2928
2929my @pkgs = ();
2930
2931my ($var) = pb_conf_read("$ENV{'PBDESTDIR'}/$ENV{'PBPROJVER'}-$ENV{'PBPROJTAG'}.pb","pbpkg");
2932@pkgs = keys %$var;
2933
2934pb_log(0,"Packages: ".join(',',@pkgs)."\n");
2935return(\@pkgs);
2936}
2937
29381;
Note: See TracBrowser for help on using the repository browser.