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

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

r4041@localhost: bruno | 2010-11-17 11:40:41 +0100

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