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

Last change on this file since 329 was 329, checked in by Bruno Cornec, 16 years ago
  • All modules are packages now (simplifies use and spec)
  • Property svn:executable set to *
File size: 39.8 KB
Line 
1#!/usr/bin/perl -w
2#
3# Project Builder main application
4#
5# $Id$
6#
7# Copyright B. Cornec 2007
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13use Getopt::Std;
14use Data::Dumper;
15use English;
16use File::Basename;
17use File::Copy;
18use Time::localtime qw(localtime);
19use POSIX qw(strftime);
20
21# Global variables
22use lib qw (lib);
23use ProjectBuilder::Distribution;
24use ProjectBuilder::Version;
25use ProjectBuilder::Base;
26
27my %opts; # CLI Options
28my $action; # action to realize
29my $test = "FALSE"; # Not used
30my $force = 0; # Force VE/VM rebuild
31my $option = ""; # Not used
32my @pkgs; # list of packages
33my $pbtag; # Global Tag variable
34my $pbver; # Global Version variable
35my $pbscript; # Name of the script
36my %pbver; # per package
37my %pbtag; # per package
38my $pbrev; # Global REVISION variable
39my $pbaccount; # Login to use to connect to the VM
40my $pbport; # Port to use to connect to the VM
41my $newver; # New version to create
42my $iso; # ISO iage for the VM to create
43
44my @date = pb_get_date();
45my $pbdate = strftime("%Y-%m-%d", @date);
46
47getopts('a:fhi:l:m:P:p:qr:s:tvV:',\%opts);
48
49my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
50if (defined $opts{'h'}) {
51 pb_syntax();
52 exit(0);
53}
54if (defined $opts{'v'}) {
55 $debug = 2;
56 #$debug = $opts{'v'};
57 pb_log(0,"Debug value: $debug\n");
58}
59if (defined $opts{'f'}) {
60 $force=1;
61}
62if (defined $opts{'q'}) {
63 $debug=-1;
64}
65if (defined $opts{'l'}) {
66 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
67 $LOG = *LOG;
68 $debug = 0 if ($debug == -1);
69 }
70pb_log_init($debug, $LOG);
71
72# Handles test option
73if (defined $opts{'t'}) {
74 $test = "TRUE";
75 # Works only for SVN
76 $option = "-r BASE";
77}
78
79# Handle root of the project if defined
80if (defined $opts{'r'}) {
81 $ENV{'PBROOT'} = $opts{'r'};
82}
83# Handle virtual machines if any
84if (defined $opts{'m'}) {
85 $ENV{'PBV'} = $opts{'m'};
86}
87if (defined $opts{'s'}) {
88 $pbscript = $opts{'s'};
89}
90if (defined $opts{'a'}) {
91 $pbaccount = $opts{'a'};
92}
93if (defined $opts{'P'}) {
94 $pbport = $opts{'P'};
95}
96if (defined $opts{'V'}) {
97 $newver = $opts{'V'};
98}
99if (defined $opts{'i'}) {
100 $iso = $opts{'i'};
101}
102
103# Get Action
104$action = shift @ARGV;
105die pb_syntax() if (not defined $action);
106
107my ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir);
108my $pbinit = undef;
109$pbinit = 1 if ($action =~ /^newproj$/);
110
111# Handles project name if any
112# And get global params
113if (defined $opts{'p'}) {
114 ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir)
115 = pb_env_init($opts{'p'},$pbinit);
116} else {
117 ($filteredfiles, $supfiles, $defpkgdir, $extpkgdir)
118 = pb_env_init(undef,$pbinit);
119}
120
121pb_log(0,"Project: $ENV{'PBPROJ'}\n");
122pb_log(0,"Action: $action\n");
123
124# Keep those project values to store them at the end each time
125my $pbprojtag = $ENV{'PBTAG'};
126my $pbprojver = $ENV{'PBVER'};
127
128# Act depending on action
129if ($action =~ /^cms2build$/) {
130 pb_cms2build();
131} elsif ($action =~ /^build2pkg$/) {
132 pb_build2pkg();
133} elsif ($action =~ /^cms2pkg$/) {
134 pb_cms2build();
135 pb_build2pkg();
136} elsif ($action =~ /^build2ssh$/) {
137 pb_build2ssh();
138} elsif ($action =~ /^cms2ssh$/) {
139 pb_cms2build();
140 pb_build2ssh();
141} elsif ($action =~ /^pkg2ssh$/) {
142 pb_pkg2ssh();
143} elsif ($action =~ /^build2ve$/) {
144 pb_build2v("ve");
145} elsif ($action =~ /^build2vm$/) {
146 pb_build2v("vm");
147} elsif ($action =~ /^cms2ve$/) {
148 pb_cms2build();
149 pb_build2v("ve");
150} elsif ($action =~ /^cms2vm$/) {
151 pb_cms2build();
152 pb_build2v("vm");
153} elsif ($action =~ /^launchvm$/) {
154 pb_launchv("vm",$ENV{'PBV'},0);
155} elsif ($action =~ /^launchve$/) {
156 pb_launchv("ve",$ENV{'PBV'},0);
157} elsif ($action =~ /^script2vm$/) {
158 pb_script2v($pbscript,"vm");
159} elsif ($action =~ /^script2ve$/) {
160 pb_script2v($pbscript,"ve");
161} elsif ($action =~ /^newver$/) {
162 pb_newver();
163} elsif ($action =~ /^newve$/) {
164 pb_launchv("ve",$ENV{'PBV'},1);
165} elsif ($action =~ /^newvm$/) {
166 pb_launchv("vm",$ENV{'PBV'},1);
167} elsif ($action =~ /^newproj$/) {
168 # Nothing to do - already done in pb_env_init
169} elsif ($action =~ /^clean$/) {
170} else {
171 pb_log(0,"\'$action\' is not available\n");
172 pb_syntax();
173}
174
175sub pb_cms2build {
176
177 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
178 @pkgs = @$ptr;
179 my ($scheme,$uri)=pb_cms_init($ENV{'PBPROJ'});
180
181 #
182 # Check project cms compliance
183 #
184 pb_cms_compliant("pbdir",'PBDIR',"$ENV{'PBDEFDIR'}/$ENV{'PBPROJ'}",$uri,$pbinit);
185
186 my ($pkgv, $pkgt) = pb_conf_get_if("pkgver","pkgtag");
187
188 # declare packager for filtering
189 my ($tmp) = pb_conf_get("pbpackager");
190 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
191
192 foreach my $pbpkg (@pkgs) {
193 $ENV{'PBPKG'} = $pbpkg;
194 $ENV{'PBVER'} = $pbprojver;
195 $ENV{'PBTAG'} = $pbprojtag;
196 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
197 $pbver = $pkgv->{$pbpkg};
198 $ENV{'PBVER'} = $pbver;
199 } else {
200 $pbver = $ENV{'PBVER'};
201 }
202 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
203 $pbtag = $pkgt->{$pbpkg};
204 $ENV{'PBTAG'} = $pbtag;
205 } else {
206 $pbtag = $ENV{'PBTAG'};
207 }
208
209 $pbrev = $ENV{'PBREVISION'};
210 pb_log(0,"\n");
211 pb_log(0,"Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n");
212 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
213 # Clean up dest if necessary. The export will recreate it
214 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
215 pb_rm_rf($dest) if (-d $dest);
216
217 # Export CMS tree for the concerned package to dest
218 # And generate some additional files
219 $OUTPUT_AUTOFLUSH=1;
220
221 # computes in which dir we have to work
222 my $dir = $defpkgdir->{$pbpkg};
223 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
224 pb_log(2,"def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n");
225
226 # If it isn't a flat CMS, then we have the choice to export subdir
227 #$dir = "$uri/$dir" if ($ENV{'PBREVISION'} ne "flat");
228 pb_cms_export($uri,"$ENV{'PBDIR'}/$pbprojver/$dir",$dest);
229
230 # Get project info on authors and log file
231 my $chglog = "$ENV{'PBROOT'}/$pbpkg/pbcl";
232 $chglog = "$ENV{'PBROOT'}/pbcl" if (! -f $chglog);
233 $chglog = undef if (! -f $chglog);
234
235 my $authors = "$ENV{'PBROOT'}/$pbpkg/pbauthors";
236 $authors = "$ENV{'PBROOT'}/pbauthors" if (! -f $authors);
237 $authors = "/dev/null" if (! -f $authors);
238
239 # Extract cms log history and store it
240 if ((defined $chglog) && (! -f "$dest/NEWS")) {
241 pb_log(2,"Generating NEWS file from $chglog\n");
242 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
243 }
244 pb_cms_log($scheme,"$ENV{'PBDIR'}/$pbprojver/$dir",$dest,$chglog,$authors);
245
246 my %build;
247
248 my @pt;
249 @pt = pb_conf_get_if("vmlist","velist");
250 foreach my $d (split(/,/,$pt[0]->{$ENV{'PBPROJ'}}),split(/,/,$pt[1]->{$ENV{'PBPROJ'}})) {
251 my ($name,$ver,$arch) = split(/-/,$d);
252 chomp($arch);
253 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
254 pb_log(2,"DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n");
255 pb_log(2,"DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n");
256
257 # Filter build files from the less precise up to the most with overloading
258 # Filter all files found, keeping the name, and generating in dest
259
260 # Find all build files first relatively to PBROOT
261 # Find also all specific files referenced in the .pb conf file
262 my %bfiles = ();
263 my %pkgfiles = ();
264 $build{"$ddir-$dver"} = "yes";
265
266 if (-d "$ENV{'PBROOT'}/$pbpkg/$dtype") {
267 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$dtype",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
268 } elsif (-d "$ENV{'PBROOT'}/$pbpkg/$dfam") {
269 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$dfam",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
270 } elsif (-d "$ENV{'PBROOT'}/$pbpkg/$ddir") {
271 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$ddir",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
272 } elsif (-d "$ENV{'PBROOT'}/$pbpkg/$ddir-$dver") {
273 pb_list_bfiles("$ENV{'PBROOT'}/$pbpkg/$ddir-$dver",$pbpkg,\%bfiles,\%pkgfiles,$supfiles);
274 } else {
275 $build{"$ddir-$dver"} = "no";
276 next;
277 }
278 pb_log(2,"DEBUG bfiles: ".Dumper(\%bfiles)."\n");
279
280 # Get all filters to apply
281 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
282
283 # Apply now all the filters on all the files concerned
284 # destination dir depends on the type of file
285 if (defined $ptr) {
286 foreach my $f (values %bfiles,values %pkgfiles) {
287 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$pbsuf,$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$defpkgdir,$extpkgdir,$pbpackager,$chglog);
288 }
289 }
290 }
291 my @found;
292 my @notfound;
293 foreach my $b (keys %build) {
294 push @found,$b if ($build{$b} =~ /yes/);
295 push @notfound,$b if ($build{$b} =~ /no/);
296 }
297 pb_log(0,"Build files generated for ".join(',',@found)."\n");
298 pb_log(0,"No Build files found for ".join(',',@notfound)."\n") if (@notfound);
299 # Get the generic filter (all.pbf) and
300 # apply those to the non-build files including those
301 # generated by pbinit if applicable
302
303 # Get only all.pbf filter
304 $ptr = pb_get_filters($pbpkg);
305
306 my $liste ="";
307 if (defined $filteredfiles->{$pbpkg}) {
308 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
309 pb_filter_file_inplace($ptr,"$dest/$f",$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
310 $liste = "$f $liste";
311 }
312 }
313 pb_log(2,"Files ".$liste."have been filtered\n");
314
315 # Prepare the dest directory for archive
316 if (-x "$ENV{'PBROOT'}/$pbpkg/pbinit") {
317 pb_filter_file("$ENV{'PBROOT'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",$ENV{'PBPROJ'},$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
318 chmod 0755,"$ENV{'PBTMP'}/pbinit";
319 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBROOT'}/$pbpkg/pbinit");
320 }
321
322 # Archive dest dir
323 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
324 # Possibility to look at PBSRC to guess more the filename
325 pb_system("tar cfz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
326 pb_log(2,"Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n");
327
328 # Keep track of what is generated by default
329 open(LAST,"> $ENV{'PBDESTDIR'}/pbrc") || die "Unable to create $ENV{'PBDESTDIR'}/pbrc";
330 #print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
331 # Why not use pbproj ?
332 print LAST "pbroot $ENV{'PBPROJ'} = $ENV{'PBROOT'}\n";
333 close(LAST);
334
335 # Keep track of per package version
336 my ($pkg) = pb_conf_read_if("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
337 $pkg = { } if (not defined $pkg);
338 if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
339 $pkg->{$pbpkg} = "$pbver-$pbtag";
340 }
341
342 pb_log(2,"DEBUG pkg: ".Dumper($pkg)."\n");
343 open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
344 foreach my $p (keys %$pkg) {
345 print PKG "pbpkg $p = $pkg->{$p}\n";
346 }
347 close(PKG);
348
349 # Final cleanup
350 pb_rm_rf($dest) if (-d $dest);
351 }
352}
353
354sub pb_build2pkg {
355
356 # Get list of packages to build
357 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
358 @pkgs = @$ptr;
359
360 # Get the running distro to build on
361 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
362 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
363
364 # Get content saved in cms2build
365 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
366 $pkg = { } if (not defined $pkg);
367
368 # declare packager (via env var in VM/VE
369 my $pbpackager;
370 if (not defined $ENV{'PBPACKAGER'}) {
371 my ($tmp) = pb_conf_get("pbpackager");
372 $pbpackager = $tmp->{$ENV{'PBPROJ'}};
373 } else {
374 $pbpackager = $ENV{'PBPACKAGER'};
375 }
376
377 chdir "$ENV{'PBBUILDDIR'}";
378 my $made = ""; # pkgs made during build
379 foreach my $pbpkg (@pkgs) {
380 my $vertag = $pkg->{$pbpkg};
381 # get the version of the current package - maybe different
382 ($pbver,$pbtag) = split(/-/,$vertag);
383
384 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
385 pb_log(2,"Source file: $src\n");
386
387 pb_log(2,"Working directory: $ENV{'PBBUILDDIR'}\n");
388 if ($dtype eq "rpm") {
389 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
390 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
391 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";
392 }
393 }
394
395 # Remove in case a previous link/file was there
396 unlink "$ENV{'PBBUILDDIR'}/SOURCES/".basename($src);
397 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
398 # We need to first extract the spec file
399 my @specfile;
400 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
401
402 pb_log(2,"specfile: ".Dumper(\@specfile)."\n");
403 # set LANGUAGE to check for correct log messages
404 $ENV{'LANGUAGE'}="C";
405 foreach my $f (@specfile) {
406 if ($f =~ /\.spec$/) {
407 pb_system("rpmbuild --define \'packager $pbpackager\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}");
408 last;
409 }
410 }
411 $made="$made RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm";
412 if (-f "/usr/bin/rpmlint") {
413 pb_system("rpmlint $made","Checking validity of rpms with rpmlint");
414 }
415 } elsif ($dtype eq "deb") {
416 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
417 pb_system("tar xfz $src","Extracting sources");
418
419 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
420 pb_rm_rf("debian");
421 symlink "pbconf/$ddir-$dver","debian" || die "Unable to symlink to pbconf/$ddir-$dver";
422 chmod 0755,"debian/rules";
423 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package");
424 $made="$made $pbpkg"."_*.deb $pbpkg"."_*.dsc $pbpkg"."_*.tar.gz";
425 if (-f "/usr/bin/lintian") {
426 pb_system("lintian $made","Checking validity of debs with lintian");
427 }
428 } elsif ($dtype eq "ebuild") {
429 my @ebuildfile;
430 # For gentoo we need to take pb as subsystem name
431 # We put every apps here under sys-apps. hope it's correct
432 # We use pb's home dir in order o have a single OVERLAY line
433 my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
434 pb_mkdir_p($tmpd) if (! -d "$tmpd");
435 pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
436
437 # We need to first extract the ebuild file
438 @ebuildfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$tmpd");
439
440 # Prepare the build env for gentoo
441 my $found = 0;
442 my $pbbd = $ENV{'HOME'};
443 $pbbd =~ s|/|\\/|g;
444 if (-r "/etc/make.conf") {
445 open(MAKE,"/etc/make.conf");
446 while (<MAKE>) {
447 $found = 1 if (/$pbbd\/portage/);
448 }
449 close(MAKE);
450 }
451 if ($found == 0) {
452 pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
453 }
454 #$found = 0;
455 #if (-r "/etc/portage/package.keywords") {
456 #open(KEYW,"/etc/portage/package.keywords");
457 #while (<KEYW>) {
458 #$found = 1 if (/portage\/pb/);
459 #}
460 #close(KEYW);
461 #}
462 #if ($found == 0) {
463 #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
464 #}
465
466 # Build
467 foreach my $f (@ebuildfile) {
468 if ($f =~ /\.ebuild$/) {
469 move($f,"$tmpd/$pbpkg-$pbver.ebuild");
470 pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package");
471 # Now move it where pb expects it
472 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
473 move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
474 }
475 }
476
477 $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver.ebuild";
478 } elsif ($dtype eq "slackware") {
479 $made="$made build-$pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
480 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
481 } else {
482 die "Unknown dtype format $dtype";
483 }
484 }
485 # Keep track of what is generated so that we can get them back from VMs
486 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
487 print KEEP "$made\n";
488 close(KEEP);
489}
490
491sub pb_build2ssh {
492 pb_send2target("Sources");
493}
494
495sub pb_pkg2ssh {
496 pb_send2target("Packages");
497}
498
499# By default deliver to the the public site hosting the
500# ftp structure (or whatever) or a VM/VE
501sub pb_send2target {
502
503 my $cmt = shift;
504 my $v = shift || undef;
505 my $vmexist = shift || 0; # 0 is FALSE
506 my $vmpid = shift || 0; # 0 is FALSE
507
508 my $host = "sshhost";
509 my $login = "sshlogin";
510 my $dir = "sshdir";
511 my $port = "sshport";
512 my $tmout = "sshtmout";
513 my $path = "sshpath";
514 my $conf = "sshconf";
515 my $rebuild = "sshrebuild";
516 if ($cmt eq "vm") {
517 $login = "vmlogin";
518 $dir = "pbdefdir";
519 $tmout = "vmtmout";
520 $rebuild = "vmrebuild";
521 # Specific VM
522 $host = "vmhost";
523 $port = "vmport";
524 } elsif ($cmt eq "ve") {
525 $login = "velogin";
526 $dir = "pbdefdir";
527 $tmout = "vetmout";
528 # Specific VE
529 $path = "vepath";
530 $conf = "veconf";
531 $rebuild = "verebuild";
532 }
533 my $cmd = "";
534
535 # Get list of packages to build
536 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
537 @pkgs = @$ptr;
538
539 # Get the running distro to consider
540 my ($odir,$over,$oarch) = (undef, undef);
541 if (defined $v) {
542 ($odir,$over,$oarch) = split(/-/,$v);
543 }
544 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
545 pb_log(2,"DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n");
546
547 # Get content saved in cms2build
548 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
549 $pkg = { } if (not defined $pkg);
550
551 my $src = "";
552 chdir "$ENV{'PBBUILDDIR'}";
553 foreach my $pbpkg (@pkgs) {
554 my $vertag = $pkg->{$pbpkg};
555 # get the version of the current package - maybe different
556 ($pbver,$pbtag) = split(/-/,$vertag);
557
558 if (($cmt eq "Sources") || ($cmt eq "vm") || ($cmt eq "ve")) {
559 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
560 if ($cmd eq "") {
561 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
562 } else {
563 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
564 }
565 }
566 }
567 if (($cmt eq "vm") || ($cmt eq "ve")) {
568 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBROOT'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
569 } elsif ($cmt eq "Script") {
570 $src="$src $ENV{'PBDESTDIR'}/pbscript";
571 } elsif ($cmt eq "Packages") {
572 # Get package list from file made during build2pkg
573 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
574 $src = <KEEP>;
575 chomp($src);
576 close(KEEP);
577 if ($dtype eq "rpm") {
578 # Also make a pbscript to generate yum/urpmi bases
579 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
580 } elsif ($dtype eq "deb") {
581 # Also make a pbscript to generate apt bases
582 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
583 }
584 }
585 # Remove potential leading spaces (cause problem with basename)
586 $src =~ s/^ *//;
587 my $basesrc = "";
588 foreach my $i (split(/ +/,$src)) {
589 $basesrc .= " ".basename($i);
590 }
591
592 pb_log(0,"Sources handled ($cmt): $src\n");
593 my ($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf) = pb_conf_get($host,$login,$dir,$port,$tmout,$rebuild,$path,$conf);
594 pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf))."\n");
595 # Not mandatory
596 my ($testver) = pb_conf_get_if("testver");
597
598 my $mac;
599 # Useless for VE
600 if ($cmt ne "ve") {
601 $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
602 # Overwrite account value if passed as parameter
603 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
604 }
605
606 my $tdir;
607 my $bdir;
608 if (($cmt eq "Sources") || ($cmt eq "Script")) {
609 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
610 } elsif (($cmt eq "vm") || ($cmt eq "ve")) {
611 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery";
612 $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build";
613 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
614 $bdir =~ s|\$ENV.+\}/||;
615 } elsif ($cmt eq "Packages") {
616 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
617 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} eq "true")) {
618 # This is a test pkg => target dir is under test
619 $tdir .= "/test";
620 }
621 } else {
622 return;
623 }
624
625 # Useless for VE
626 my $nport;
627 if ($cmt ne "ve") {
628 $nport = $sshport->{$ENV{'PBPROJ'}};
629 $nport = "$pbport" if (defined $pbport);
630 }
631
632 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
633 $tdir =~ s|\$ENV.+\}/||;
634
635 my $tm = $vtmout->{$ENV{'PBPROJ'}};
636
637 # ssh communication if not VE
638 my ($shcmd,$cpcmd,$cptarget,$cp2target);
639 if ($cmt ne "ve") {
640 $shcmd = "ssh -q -p $nport $mac";
641 $cpcmd = "scp -p -P $nport";
642 $cptarget = "$mac:$tdir";
643 $cp2target = "$mac:$bdir";
644 } else {
645 my $tp = $vepath->{$ENV{'PBPROJ'}};
646 $shcmd = "sudo chroot $tp/$v /bin/su - $sshlogin->{$ENV{'PBPROJ'}} -c ";
647 $cpcmd = "cp -a ";
648 $cptarget = "$tp/$tdir";
649 $cp2target = "$tp/$bdir";
650 }
651
652 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");
653 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget");
654 # For VE we need to change the owner manually - To be tested if needed
655 #if ($cmt eq "ve") {
656 #pb_system("cd $cptarget ; sudo chown -R $sshlogin->{$ENV{'PBPROJ'}} .","$cmt chown in $cptarget to $sshlogin->{$ENV{'PBPROJ'}}");
657 #}
658 pb_system("$shcmd \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $cptarget if needed");
659 if (($cmt eq "vm") || ($cmt eq "ve")) {
660 # Get back info on pkg produced, compute their name and get them from the VM
661 pb_system("$cpcmd $cp2target/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target");
662 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
663 my $src = <KEEP>;
664 chomp($src);
665 close(KEEP);
666 $src =~ s/^ *//;
667 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
668 # Change pgben to make the next send2target happy
669 my $made = "";
670 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
671 foreach my $p (split(/ +/,$src)) {
672 my $j = basename($p);
673 pb_system("$cpcmd $cp2target/\'$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $cp2target");
674 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
675 }
676 print KEEP "$made\n";
677 close(KEEP);
678 pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup");
679 pb_send2target("Packages","$odir"."_"."$over");
680 if ((! $vmexist) && ($cmt eq "vm")) {
681 pb_system("$shcmd \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $v halt (pid $vmpid)");
682 }
683 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
684 }
685}
686
687sub pb_script2v {
688 my $pbscript=shift;
689 my $vtype=shift;
690
691 # Prepare the script to be executed on the VM
692 # in $ENV{'PBDESTDIR'}/pbscript
693 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
694 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
695 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
696 }
697
698 my ($vm,$all) = pb_get_v($vtype);
699 my ($vmexist,$vmpid) = (undef,undef);
700
701 foreach my $v (@$vm) {
702 # Launch the VM/VE
703 if ($vtype eq "vm") {
704 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
705
706 # Skip that VM if something went wrong
707 next if (($vmpid == 0) && ($vmexist ==0));
708 }
709
710 # Gather all required files to send them to the VM
711 # and launch the build through pbscript
712 pb_send2target("Script","$v",$vmexist,$vmpid);
713
714 }
715}
716
717sub pb_launchv {
718 my $vtype = shift;
719 my $v = shift;
720 my $create = shift || 0; # By default do not create a VM
721
722 die "No VM/VE defined, unable to launch" if (not defined $v);
723 # Keep only the first VM in case many were given
724 $v =~ s/,.*//;
725
726 # Which is our local arch ? (standardize on i386 for those platforms)
727 my $arch = `uname -m`;
728 chomp($arch);
729 $arch =~ s/i.86/i386/;
730
731 # Launch the VMs/VEs
732 if ($vtype eq "vm") {
733 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
734
735 my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout","vmsize");
736
737 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
738 if (not defined $ENV{'PBVMOPT'}) {
739 $ENV{'PBVMOPT'} = "";
740 }
741 if (defined $vmopt->{$ENV{'PBPROJ'}}) {
742 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
743 }
744 my $nport = $vmport->{$ENV{'PBPROJ'}};
745 $nport = "$pbport" if (defined $pbport);
746
747 my $cmd;
748 my $vmcmd; # has to be used for pb_check_ps
749 my $vmm; # has to be used for pb_check_ps
750 if ($vmtype eq "qemu") {
751 my $qemucmd32;
752 my $qemucmd64;
753 if ($arch eq "x86_64") {
754 $qemucmd32 = "/usr/bin/qemu-system-i386";
755 $qemucmd64 = "/usr/bin/qemu";
756 } else {
757 $qemucmd32 = "/usr/bin/qemu";
758 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
759 }
760 if ($v =~ /x86_64/) {
761 $vmcmd = "$qemucmd64 -no-kqemu";
762 } else {
763 $vmcmd = "$qemucmd32";
764 }
765 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
766 if ($create != 0) {
767 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
768 }
769 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
770 } elsif ($vmtype eq "xen") {
771 } elsif ($vmtype eq "vmware") {
772 } else {
773 die "VM of type $vmtype not supported. Report to the dev team";
774 }
775 my ($tmpcmd,$void) = split(/ +/,$cmd);
776 my $vmexist = pb_check_ps($tmpcmd,$vmm);
777 my $vmpid = 0;
778 if (! $vmexist) {
779 if ($create != 0) {
780 if (($vmtype eq "qemu") || ($vmtype eq "xen")) {
781 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
782 } elsif ($vmtype eq "vmware") {
783 } else {
784 }
785 }
786 if (! -f "$vmm") {
787 pb_log(0,"Unable to find VM $vmm\n");
788 } else {
789 pb_system("$cmd &","Launching the VM $vmm");
790 pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $v to come up");
791 $vmpid = pb_check_ps($tmpcmd,$vmm);
792 }
793 } else {
794 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n");
795 }
796 return($vmexist,$vmpid);
797 # VE here
798 } else {
799 # Get VE context
800 my ($ptr,$vepath,$vetmout,$verebuild,$veconf) = pb_conf_get("vetype","vepath","vetmout","verebuild","veconf");
801 my $vetype = $ptr->{$ENV{'PBPROJ'}};
802
803 # Get distro context
804 my ($name,$ver,$darch) = split(/-/,$v);
805 chomp($darch);
806 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
807
808 if ($vetype eq "chroot") {
809 # Architecture consistency
810 if ($arch ne $darch) {
811 die "Unable to launch a VE of architecture $darch on a $arch platform" if (not (($darch eq "x86_64") && ($arch =~ /i?86/)));
812 }
813
814 if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($force == 1)) {
815 # We have to rebuild the chroot
816 if ($dtype eq "rpm") {
817 pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
818 # Once setup we need to install some packages, the pb account, ...
819 pb_system("sudo /usr/sbin/mock --install --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
820 #pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" --basedir=\"$vepath->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE");
821 } elsif ($dtype eq "deb") {
822 pb_system("","Creating the pbuilder VE");
823 } elsif ($dtype eq "ebuild") {
824 die "Please teach the dev team how to build gentoo chroot";
825 } else {
826 die "Unknown distribution type $dtype. Report to dev team";
827 }
828 }
829 # Nothing more to do for VE. No real launch
830 } else {
831 die "VE of type $vetype not supported. Report to the dev team";
832 }
833 }
834}
835
836sub pb_build2v {
837
838my $vtype = shift;
839
840# Prepare the script to be executed on the VM/VE
841# in $ENV{'PBDESTDIR'}/pbscript
842my ($ntp) = pb_conf_get($vtype."ntp");
843my $vntp = $ntp->{$ENV{'PBPROJ'}};
844
845open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
846print SCRIPT "#!/bin/bash\n";
847print SCRIPT "echo ... Execution needed\n";
848print SCRIPT "# This is in directory delivery\n";
849print SCRIPT "# Setup the variables required for building\n";
850print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
851print SCRIPT "# Preparation for pb\n";
852print SCRIPT "mv .pbrc \$HOME\n";
853print SCRIPT "cd ..\n";
854# Force new date to be in the future compared to the date of the tar file by adding 1 minute
855my @date=pb_get_date();
856$date[1]++;
857my $upddate = strftime("%m%d%H%M%Y", @date);
858print SCRIPT "echo Setting up date on $vntp...\n";
859# Or use ntpdate if available TBC
860print SCRIPT "sudo date $upddate\n";
861# This exports avoids pb_env_init to deal with PBCONF stuff
862print SCRIPT "export PBCONF=/tmp\n";
863print SCRIPT "export PBVER=$ENV{'PBVER'}\n";
864print SCRIPT "export PBTAG=$ENV{'PBTAG'}\n";
865print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n";
866print SCRIPT "# Build\n";
867# Get list of packages to build
868my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
869@pkgs = @$ptr;
870my $p = join(' ',@pkgs) if (@pkgs);
871print SCRIPT "echo Building packages on $vtype...\n";
872print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
873close(SCRIPT);
874chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
875
876my ($v,$all) = pb_get_v($vtype);
877
878# Send tar files when we do a global generation
879pb_build2ssh() if ($all == 1);
880
881my ($vmexist,$vmpid) = (undef,undef);
882
883foreach my $v (@$v) {
884 if ($vtype eq "vm") {
885 # Launch the VM
886 my ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
887
888 # Skip that VM if it something went wrong
889 next if (($vmpid == 0) && ($vmexist == 0));
890 }
891 # Gather all required files to send them to the VM/VE
892 # and launch the build thourgh pbscript
893 pb_send2target($vtype,"$v",$vmexist,$vmpid);
894}
895}
896
897
898sub pb_newver {
899
900 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
901 my $scheme=pb_cms_init($ENV{'PBPROJ'});
902 if ($scheme ne "svn") {
903 die "Only SVN is supported at the moment";
904 }
905 my $res = pb_cms_isdiff($scheme);
906 die "You need to have no differences before creating a new version" if ($res != 0);
907 my $cmsurl = pb_cms_getinfo($scheme,$ENV{'PBROOT'},"URL:");
908 my $newurl = dirname($cmsurl)."/$newver";
909 pb_cms_copy($scheme,$cmsurl,$newurl);
910 pb_cms_checkout($scheme,$newurl,"$ENV{'PBROOT'}/../$newver");
911 my $oldver=basename($cmsurl);
912 open(FILE,"$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb";
913 open(OUT,"> $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new";
914 while(<FILE>) {
915 s/projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
916 print OUT $_;
917 }
918 close(FILE);
919 close(OUT);
920 rename("$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb");
921 pb_cms_checkin($scheme,"$ENV{'PBROOT'}/../$newver");
922}
923
924#
925# Return the list of VMs/VEs we are working on
926# $all is a flag to know if we return all of them
927# or only some (if all we publish also tar files in addition to pkgs
928#
929sub pb_get_v {
930
931my $vtype = shift;
932my @v;
933my $all = 0;
934my $vlist;
935my $pbv = 'PBV';
936
937if ($vtype eq "vm") {
938 $vlist = "vmlist";
939} elsif ($vtype eq "ve") {
940 $vlist = "velist";
941}
942# Get VM/VE list
943if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) {
944 my ($ptr) = pb_conf_get($vlist);
945 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}};
946 $all = 1;
947}
948pb_log(2,"$vtype: $ENV{$pbv}\n");
949@v = split(/,/,$ENV{$pbv});
950return(\@v,$all);
951}
952
953sub pb_setup_v {
954# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
955# Needs to use root account to connect to the VM/VE
956
957# pb will take your local public SSH key to access
958# the pb account in the VM later on
959
960my $vtype = shift;
961
962my $file = "$ENV{'HOME'}/.ssh/id_dsa.pub";
963die "Unable to find your public ssh key as $file";
964
965open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
966print SCRIPT << 'EOF';
967#!/usr/bin/perl -w
968
969$file="/etc/passwd";
970open(PBFILE,$file) || die "Unable to open $file";
971my $found = 0;
972while (<PBFILE>) {
973 $found = 1 if (/^pb:/);
974}
975close(PBFILE);
976
977if ( $found == 0 ) {
978 if ( ! -d "/home" ) {
979 mkdir "/home";
980 }
981 system "groupadd pb";
982 system "useradd pb -g pb -m -d /home/pb";
983}
984
985# For root
986mkdir ".ssh",0700;
987system 'cp /tmp/pbkey .ssh/authorized_keys';
988chmod 0600,".ssh/authorized_keys";
989
990# For pb
991chdir "/home/pb";
992mkdir ".ssh",0700;
993system 'cp /tmp/pbkey .ssh/authorized_keys';
994chmod 0600,".ssh/authorized_keys";
995system 'chown -R pb:pb .ssh';
996
997# No passwd for pb only keys
998$file="/etc/shadow";
999open(PBFILE,$file) || die "Unable to open $file";
1000open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1001while (<PBFILE>) {
1002 s/^pb:\!\!:/pb:*:/;
1003 s/^pb:\!:/pb:*:/; #SLES 9 e.g.
1004 print PBOUT $_;
1005}
1006close(PBFILE);
1007close(PBOUT);
1008rename("$file.new",$file);
1009chmod 0640,$file;
1010
1011# pb has to be added to portage group on gentoo
1012unlink "/tmp/pbkey";
1013
1014my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
1015print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n";
1016
1017# Get and install pb
1018if ( $ddir eq "fedora" ) {
1019 system "yum clean all";
1020 system "yum update -y";
1021 my $arch=`uname -m`;
1022 my $opt = "";
1023 chomp($arch);
1024 if ($arch eq "x86_64") {
1025 $opt="--exclude=*.i?86";
1026 }
1027
1028 system "yum -y $opt install rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker";
1029} elsif (( $dfam eq "rh" ) || ($ddir eq "sles") || (($ddir eq "suse") && (($dver eq "10.1") || ($dver eq "10.0"))) || (($ddir eq "mandrake") && ($dver eq "10.1"))) {
1030 # Suppose pkg are installed already
1031 system "rpm -e lsb 2>&1 > /dev/null";
1032 system "rm -rf DateManip* ; wget http://search.cpan.org/CPAN/authors/id/S/SB/SBECK/Date-Manip-5.46.tar.gz ; tar xvfz Date-Manip-5.46.tar.gz ; cd Date-Manip* ; perl Makefile.PL ; make ; make install ; cd .. ";
1033} elsif ($ddir eq "suse") {
1034 # New OpenSuSE
1035 system "export TERM=linux ; liste=\"\" ; for i in make wget patch sudo perl-DateManip perl-File-HomeDir xntp; do rpm -q \$i 1> /dev/null 2> /dev/null ; if [ \$\? != 0 ]; then liste=\"\$liste \$i\"; fi; done; echo \"Liste: \$liste\" ; if [ \"\$liste\" != \"\" ]; then yast2 -i \$liste ; fi";
1036} elsif ( $dfam eq "md" ) {
1037 system "urpmi.update -a ; urpmi --auto rpm-build wget sudo patch ntp-client perl-DateManip";
1038} elsif ( $dfam eq "du" ) {
1039 if (( $dver eq "3.1" ) && ($ddir eq "debian")) {
1040 system "apt-get update; apt-get -y install wget patch ssh sudo debian-builder dh-make fakeroot ntpdate libdate-manip-perl";
1041 } else {
1042 system "apt-get update; apt-get -y install wget patch openssh-server dpkg-dev sudo debian-builder dh-make fakeroot ntpdate rses5-dev libdate-manip-perl";
1043 }
1044} elsif ( $dfam eq "gen" ) {
1045 system "emerge -u system ; emerge wget sudo ntp DateManip";
1046} else {
1047 print "No pkg to install\n";
1048}
1049
1050# Adapt sudoers
1051$file="/etc/sudoers";
1052open(PBFILE,$file) || die "Unable to open $file";
1053open(PBOUT,"> $file.new") || die "Unable to open $file.new";
1054while (<PBFILE>) {
1055 next if (/^pb /);
1056 s/Defaults[ \t]+requiretty//;
1057 print PBOUT $_;
1058}
1059close(PBFILE);
1060print PBOUT "pb ALL=(ALL) NOPASSWD:ALL\n";
1061close(PBOUT);
1062rename("$file.new",$file);
1063chmod 0440,$file;
1064
1065# Suse wants sudoers as 640
1066if (($ddir eq "sles") || (($ddir eq "suse")) && ($dver ne "10.3")) {
1067 chmod 0640,$file;
1068}
1069
1070# Sync date
1071system "/usr/sbin/ntpdate ntp.pool.org";
1072
1073system "rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd ..";
1074EOF
1075close(SCRIPT);
1076}
1077
1078# Returns the pid of a running VM command using a specific VM file
1079sub pb_check_ps {
1080 my $vmcmd = shift;
1081 my $vmm = shift;
1082 my $vmexist = 0; # FALSE by default
1083
1084 open(PS, "ps auxhww|") || die "Unable to call ps";
1085 while (<PS>) {
1086 next if (! /$vmcmd/);
1087 next if (! /$vmm/);
1088 my ($void1, $void2);
1089 ($void1, $vmexist, $void2) = split(/ +/);
1090 last;
1091 }
1092 return($vmexist);
1093}
1094
1095
1096sub pb_extract_build_files {
1097
1098my $src=shift;
1099my $dir=shift;
1100my $ddir=shift;
1101my @files;
1102
1103if ($src =~ /tar\.gz$/) {
1104 pb_system("tar xfpz $src $dir","Extracting build files");
1105} elsif ($src =~ /tar\.bz2$/) {
1106 pb_system("tar xfpj $src $dir","Extracting build files");
1107} else {
1108 die "Unknown compression algorithm for $src";
1109}
1110opendir(DIR,"$dir") || die "Unable to open directory $dir";
1111foreach my $f (readdir(DIR)) {
1112 next if ($f =~ /^\./);
1113 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
1114 pb_log(2,"mv $dir/$f $ddir\n");
1115 push @files,"$ddir/$f";
1116}
1117closedir(DIR);
1118# Not enough but still a first cleanup
1119pb_rm_rf("$dir");
1120return(@files);
1121}
1122
1123sub pb_list_bfiles {
1124
1125my $dir = shift;
1126my $pbpkg = shift;
1127my $bfiles = shift;
1128my $pkgfiles = shift;
1129my $supfiles = shift;
1130
1131opendir(BDIR,"$dir") || die "Unable to open dir $dir: $!";
1132foreach my $f (readdir(BDIR)) {
1133 next if ($f =~ /^\./);
1134 $bfiles->{$f} = "$dir/$f";
1135 $bfiles->{$f} =~ s~$ENV{'PBROOT'}~~;
1136 if (defined $supfiles->{$pbpkg}) {
1137 $pkgfiles->{$f} = "$dir/$f" if ($f =~ /$supfiles->{$pbpkg}/);
1138 }
1139}
1140closedir(BDIR);
1141}
1142
1143sub pb_syntax {
1144
1145 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
1146 print "\n";
1147 print "Syntax: pb [-vhqt][-r pbroot][-p project][[-s script -a account -P port] -m \"mach-1[,...]\"][-i iso] <action> [<pkg1> ...]\n";
1148 print "\n";
1149 print "-h: This help file\n";
1150 print "-q: Quiet mode\n";
1151 print "-t: Test mode (not done yet)\n";
1152 print "-v: Verbose mode\n";
1153 print "\n";
1154 print "-m machine: Name of the Virtual Machines (VM) you want\n";
1155 print " to build on (coma separated). All if none precised\n";
1156 print " (or use the env variable PBV) \n";
1157 print "\n";
1158 print "-s script: Name of the script you want\n";
1159 print " to execute on the related VMs.\n";
1160 print "\n";
1161 print "-i iso: Name of the ISO image of the distribution you want\n";
1162 print " to install on the related VMs.\n";
1163 print "\n";
1164 print "-a account: Name of the account to use\n";
1165 print " to connect on the related VMs.\n";
1166 print "\n";
1167 print "-P port: Port number to use\n";
1168 print " to connect on the related VMs.\n";
1169 print "\n";
1170 print "-p project: Name of the project you're working on\n";
1171 print " (or use the env variable PBPROJ) \n";
1172 print "\n";
1173 print "-r pbroot: Path Name of project under the CMS \n";
1174 print " (or use the env variable PBROOT) \n";
1175 print "\n";
1176 print "-V newver: New version of the project to create\n";
1177 print " from the current one. \n";
1178 print "\n";
1179 print "<action> can be:\n";
1180 print "\n";
1181 print "\tcms2build: Create tar files for the project under your CMS\n";
1182 print "\t CMS supported are SVN and CVS\n";
1183 print "\t parameters are packages to build\n";
1184 print "\t if not using default list\n";
1185 print "\n";
1186 print "\tbuild2pkg: Create packages for your running distribution \n";
1187 print "\n";
1188 print "\tcms2pkg: cms2build + build2pkg\n";
1189 print "\n";
1190 print "\tbuild2ssh: Send the tar files to a SSH host \n";
1191 print "\n";
1192 print "\tcms2ssh: cms2build + build2ssh\n";
1193 print "\n";
1194 print "\tpkg2ssh: Send the packages built to a SSH host \n";
1195 print "\n";
1196 print "\tbuild2vm: Create packages in VMs, launching them if needed\n";
1197 print "\t and send those packages to a SSH host once built\n";
1198 print "\t VM type supported are QEMU \n";
1199 print "\n";
1200 print "\tbuild2ve: Create packages in VEs, creating it if needed\n";
1201 print "\t and send those packages to a SSH host once built\n";
1202 print "\n";
1203 print "\tcms2vm: cms2build + build2vm\n";
1204 print "\n";
1205 print "\tcms2ve: cms2build + build2ve\n";
1206 print "\n";
1207 print "\tlaunchvm: Launch one virtual machine\n";
1208 print "\n";
1209 print "\tlaunchve: Launch one virtual environment\n";
1210 print "\n";
1211 print "\tscript2vm: Launch one virtual machine if needed \n";
1212 print "\t and executes a script on it \n";
1213 print "\n";
1214 print "\tscript2ve: Execute a script in a virtual environment \n";
1215 print "\n";
1216 print "\tnewvm: Create a new virtual machine\n";
1217 print "\n";
1218 print "\tnewve: Create a new virtual environment\n";
1219 print "\n";
1220 print "\tnewver: Create a new version of the project derived \n";
1221 print "\t from the current one \n";
1222 print "\n";
1223 print "\tnewproj: Create a new project and a template set of \n";
1224 print "\t configuration files under pbconf \n";
1225 print "\n";
1226}
Note: See TracBrowser for help on using the repository browser.