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

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

pb working for gentoo with mindi

  • Property svn:executable set to *
File size: 32.3 KB
Line 
1#!/usr/bin/perl -w
2#
3# Project Builder main application
4#
5# $Id$
6#
7# Copyright B. Cornec 2007
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13use Getopt::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 qw (pb_distro_init);
24use ProjectBuilder::Version qw (pb_version_init);
25use ProjectBuilder::Base qw (pb_conf_read pb_conf_get pb_cms_init pb_mkdir_p pb_system pb_rm_rf pb_get_filters pb_filter_file pb_filter_file_pb pb_cms_export pb_cms_log pb_cms_isdiff pb_cms_copy pb_cms_checkout);
26
27my %opts; # CLI Options
28my $action; # action to realize
29my $test = "FALSE";
30my $option = "";
31my @pkgs;
32my $pbtag; # Global Tag variable
33my $pbver; # Global Version variable
34my $pbscript; # Name of the script
35my %pbver; # per package
36my %pbtag; # per package
37my $pbrev; # Global REVISION variable
38my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
39my $pbdate = strftime("%Y-%m-%d", @date);
40my $pbdatecvs = strftime("%Y-%m-%d %H:%M:%S", @date);
41my $debug = 0;
42my $pbaccount; # Login to use to connect to the VM
43my $pbport; # Port to use to connect to the VM
44my $newver; # New version to create
45my $iso; # ISO iage for the VM to create
46my $LOG = \*STDOUT;
47
48getopts('a:hi:l:m:P:p:qr:s:tvV:',\%opts);
49
50my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
51if (defined $opts{'h'}) {
52 pb_syntax();
53 exit(0);
54}
55if (defined $opts{'v'}) {
56 $debug++;
57}
58if (defined $opts{'q'}) {
59 $debug=-1;
60}
61if (defined $opts{'l'}) {
62 open(LOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
63 $LOG = *LOG;
64 $debug = 0 if ($debug == -1);
65 }
66# Handles test option
67if (defined $opts{'t'}) {
68 $test = "TRUE";
69 # Works only for SVN
70 $option = "-r BASE";
71}
72
73# Handle root of the project if defined
74if (defined $opts{'r'}) {
75 $ENV{'PBROOT'} = $opts{'r'};
76}
77# Handle virtual machines if any
78if (defined $opts{'m'}) {
79 $ENV{'PBVM'} = $opts{'m'};
80}
81if (defined $opts{'s'}) {
82 $pbscript = $opts{'s'};
83}
84if (defined $opts{'a'}) {
85 $pbaccount = $opts{'a'};
86}
87if (defined $opts{'P'}) {
88 $pbport = $opts{'P'};
89}
90if (defined $opts{'V'}) {
91 $newver = $opts{'V'};
92}
93if (defined $opts{'i'}) {
94 $iso = $opts{'i'};
95}
96
97# Get Action
98$action = shift @ARGV;
99die pb_syntax() if (not defined $action);
100
101my ($pbrc, $filteredfiles, $defpkgdir, $extpkgdir);
102my $pbinit = undef;
103$pbinit = 1 if ($action =~ /^newproj$/);
104
105# Handles project name if any
106# And get global params
107if (defined $opts{'p'}) {
108 ($ENV{'PBPROJ'},$debug,$LOG, $pbrc, $filteredfiles, $defpkgdir, $extpkgdir)
109 = pb_env_init($opts{'p'},$pbinit);
110} else {
111 ($ENV{'PBPROJ'},$debug,$LOG, $pbrc, $filteredfiles, $defpkgdir, $extpkgdir)
112 = pb_env_init(undef,$pbinit);
113}
114
115print $LOG "Project: $ENV{'PBPROJ'}\n" if ($debug >= 0);
116print $LOG "Action: $action\n" if ($debug >= 0);
117
118# Keep those project values to store them at the end each time
119my $pbprojtag = $ENV{'PBTAG'};
120my $pbprojver = $ENV{'PBVER'};
121
122# Act depending on action
123if ($action =~ /^cms2build$/) {
124 pb_cms2build();
125} elsif ($action =~ /^build2pkg$/) {
126 pb_build2pkg();
127} elsif ($action =~ /^cms2pkg$/) {
128 pb_cms2build();
129 pb_build2pkg();
130} elsif ($action =~ /^build2ssh$/) {
131 pb_build2ssh();
132} elsif ($action =~ /^cms2ssh$/) {
133 pb_cms2build();
134 pb_build2ssh();
135} elsif ($action =~ /^pkg2ssh$/) {
136 pb_pkg2ssh();
137} elsif ($action =~ /^build2vm$/) {
138 pb_build2vm();
139} elsif ($action =~ /^cms2vm$/) {
140 pb_cms2build();
141 pb_build2vm();
142} elsif ($action =~ /^launchvm$/) {
143 pb_launchvm($ENV{'PBVM'},0);
144} elsif ($action =~ /^script2vm$/) {
145 pb_script2vm($pbscript);
146} elsif ($action =~ /^newver$/) {
147 pb_newver();
148} elsif ($action =~ /^newvm$/) {
149 pb_launchvm($ENV{'PBVM'},1);
150} elsif ($action =~ /^newproj$/) {
151 # Nothing to do - already done in pb_env_init
152} elsif ($action =~ /^clean$/) {
153} else {
154 print $LOG "'$action' is not available\n";
155 pb_syntax();
156}
157
158sub pb_cms2build {
159
160 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
161 @pkgs = @$ptr;
162 my $cms=pb_cms_init($ENV{'PBPROJ'});
163
164 my ($pkgv, $pkgt) = pb_conf_read("$ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb","pkgver","pkgtag");
165
166 # declare packager for filtering
167 my ($tmp) = pb_conf_get("packager");
168 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
169
170 foreach my $pbpkg (@pkgs) {
171 $ENV{'PBPKG'} = $pbpkg;
172 $ENV{'PBVER'} = $pbprojver;
173 $ENV{'PBTAG'} = $pbprojtag;
174 if ((defined $pkgv) && (defined $pkgv->{$pbpkg})) {
175 $pbver = $pkgv->{$pbpkg};
176 $ENV{'PBVER'} = $pbver;
177 } else {
178 $pbver = $ENV{'PBVER'};
179 }
180 if ((defined $pkgt) && (defined $pkgt->{$pbpkg})) {
181 $pbtag = $pkgt->{$pbpkg};
182 $ENV{'PBTAG'} = $pbtag;
183 } else {
184 $pbtag = $ENV{'PBTAG'};
185 }
186
187 $pbrev = $ENV{'PBREVISION'};
188 print $LOG "\n";
189 print $LOG "Management of $pbpkg $pbver-$pbtag (rev $pbrev)\n";
190 die "Unable to get env var PBDESTDIR" if (not defined $ENV{'PBDESTDIR'});
191 # Clean up dest if necessary. The export will recreate it
192 my $dest = "$ENV{'PBDESTDIR'}/$pbpkg-$pbver";
193 pb_rm_rf($dest) if (-d $dest);
194
195 # Export CMS tree for the concerned package to dest
196 # And generate some additional files
197 $OUTPUT_AUTOFLUSH=1;
198
199 # computes in which dir we have to work
200 my $dir = $defpkgdir->{$pbpkg};
201 $dir = $extpkgdir->{$pbpkg} if (not defined $dir);
202 print "def:".Dumper($defpkgdir)." ext: ".Dumper($extpkgdir)." \n" if ($debug >= 1);
203 pb_cms_export($cms,$pbdatecvs,"$ENV{'PBROOT'}/$dir",$dest);
204
205 # Get project info on authors and log file
206 my $chglog = "$ENV{'PBCONF'}/$pbpkg/pbcl";
207 $chglog = "$ENV{'PBCONF'}/pbcl" if (! -f $chglog);
208 $chglog = undef if (! -f $chglog);
209
210 my $authors = "$ENV{'PBCONF'}/$pbpkg/pbauthors";
211 $authors = "$ENV{'PBCONF'}/pbauthors" if (! -f $authors);
212 $authors = "/dev/null" if (! -f $authors);
213
214 # Extract cms log history and store it
215 if ((defined $chglog) && (! -f "$dest/NEWS")) {
216 print $LOG "Generating NEWS file from $chglog\n";
217 copy($chglog,"$dest/NEWS") || die "Unable to create $dest/NEWS";
218 }
219 pb_cms_log($cms,"$ENV{'PBROOT'}/$dir",$dest,$chglog,$authors);
220
221 my %build;
222
223 my ($ptr) = pb_conf_get("vmlist");
224 foreach my $d (split(/,/,$ptr->{$ENV{'PBPROJ'}})) {
225 my ($name,$ver) = split(/_/,$d);
226 chomp($ver);
227 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver);
228 print $LOG "DEBUG: distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n" if ($debug >= 1);
229 print $LOG "DEBUG Filtering PBDATE => $pbdate, PBTAG => $pbtag, PBVER => $pbver\n" if ($debug >= 1);
230
231 # Filter build files from the less precise up to the most with overloading
232 # Filter all files found, keeping the name, and generating in dest
233
234 # Find all build files first relatively to PBROOT
235 my %bfiles;
236 print $LOG "DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n" if ($debug >= 1);
237 $build{"$ddir-$dver"} = "yes";
238 if (-d "$ENV{'PBCONF'}/$pbpkg/$dtype") {
239 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dtype") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dtype: $!";
240 foreach my $f (readdir(BDIR)) {
241 next if ($f =~ /^\./);
242 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dtype/$f";
243 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
244 }
245 closedir(BDIR);
246 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$dfam") {
247 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$dfam") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$dfam: $!";
248 foreach my $f (readdir(BDIR)) {
249 next if ($f =~ /^\./);
250 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$dfam/$f";
251 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
252 }
253 closedir(BDIR);
254 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir") {
255 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir: $!";
256 foreach my $f (readdir(BDIR)) {
257 next if ($f =~ /^\./);
258 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir/$f";
259 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
260 }
261 closedir(BDIR);
262 } elsif (-d "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") {
263 opendir(BDIR,"$ENV{'PBCONF'}/$pbpkg/$ddir-$dver") || die "Unable to open dir $ENV{'PBCONF'}/$pbpkg/$ddir-$dver: $!";
264 foreach my $f (readdir(BDIR)) {
265 next if ($f =~ /^\./);
266 $bfiles{$f} = "$ENV{'PBCONF'}/$pbpkg/$ddir-$dver/$f";
267 $bfiles{$f} =~ s~$ENV{'PBROOT'}~~;
268 }
269 closedir(BDIR);
270 } else {
271 $build{"$ddir-$dver"} = "no";
272 next;
273 }
274 print $LOG "DEBUG bfiles: ".Dumper(\%bfiles)."\n" if ($debug >= 1);
275
276 # Get all filters to apply
277 my $ptr = pb_get_filters($pbpkg, $dtype, $dfam, $ddir, $dver);
278
279 # Apply now all the filters on all the files concerned
280 # destination dir depends on the type of file
281 if (defined $ptr) {
282 foreach my $f (values %bfiles) {
283 pb_filter_file_pb("$ENV{'PBROOT'}/$f",$ptr,"$dest/pbconf/$ddir-$dver/".basename($f),$dtype,$pbsuf,$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$defpkgdir,$extpkgdir,$pbpackager,$chglog);
284 }
285 }
286 }
287 if ($debug >= 0) {
288 my @found;
289 my @notfound;
290 foreach my $b (keys %build) {
291 push @found,$b if ($build{$b} =~ /yes/);
292 push @notfound,$b if ($build{$b} =~ /no/);
293 }
294 print $LOG "Build files generated for ".join(',',@found)."\n";
295 print $LOG "No Build files found for ".join(',',@notfound)."\n";
296 }
297 # Get the generic filter (all.pbf) and
298 # apply those to the non-build files including those
299 # generated by pbinit if applicable
300
301 # Get only all.pbf filter
302 $ptr = pb_get_filters($pbpkg);
303
304 my $liste ="";
305 if (defined $filteredfiles->{$pbpkg}) {
306 foreach my $f (split(/,/,$filteredfiles->{$pbpkg})) {
307 pb_filter_file("$ENV{'PBROOT'}/$dir/$f",$ptr,"$dest/$f",$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
308 $liste = "$f $liste";
309 }
310 }
311 print $LOG "Files ".$liste."have been filtered\n";
312
313 # Prepare the dest directory for archive
314 if (-x "$ENV{'PBCONF'}/$pbpkg/pbinit") {
315 pb_filter_file("$ENV{'PBCONF'}/$pbpkg/pbinit",$ptr,"$ENV{'PBTMP'}/pbinit",$pbpkg,$pbver,$pbtag,$pbrev,$pbdate,$pbpackager);
316 chmod 0755,"$ENV{'PBTMP'}/pbinit";
317 pb_system("cd $dest ; $ENV{'PBTMP'}/pbinit","Executing init script from $ENV{'PBCONF'}/$pbpkg/pbinit");
318 }
319
320 # Archive dest dir
321 chdir "$ENV{'PBDESTDIR'}" || die "Unable to change dir to $ENV{'PBDESTDIR'}";
322 # Possibility to look at PBSRC to guess more the filename
323 pb_system("tar cfz $pbpkg-$pbver.tar.gz $pbpkg-$pbver","Creating $pbpkg tar files compressed");
324 print $LOG "Under $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz\n" if ($debug >= 0);
325
326 # Keep track of what is generated for default
327 open(LAST,"> $pbrc->{$ENV{'PBPROJ'}}") || die "Unable to create $pbrc->{$ENV{'PBPROJ'}}";
328 print LAST "pbroot $pbprojver-$pbprojtag = $ENV{'PBROOT'}\n";
329 close(LAST);
330
331 # Keep track of per package version
332 if (! -f "$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") {
333 open(PKG,">$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
334 print PKG "# Empty\n";
335 close(PKG);
336 }
337 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
338 $pkg = { } if (not defined $pkg);
339 if ((not defined $pkg->{$pbpkg}) || ($pkg->{$pbpkg} ne "$pbver-$pbtag")) {
340 $pkg->{$pbpkg} = "$pbver-$pbtag";
341 }
342
343 print $LOG "DEBUG pkg: ".Dumper($pkg)."\n" if ($debug >= 1);
344 open(PKG,"> $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb") || die "Unable to create $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb";
345 foreach my $p (keys %$pkg) {
346 print PKG "pbpkg $p = $pkg->{$p}\n";
347 }
348 close(PKG);
349
350 # Final cleanup
351 pb_rm_rf($dest) if (-d $dest);
352 }
353}
354
355sub pb_build2pkg {
356
357 # Get list of packages to build
358 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
359 @pkgs = @$ptr;
360
361 # Get the running distro to build on
362 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
363 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
364
365 # Get content saved in cms2build
366 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
367 $pkg = { } if (not defined $pkg);
368
369 # declare packager
370 my ($tmp) = pb_conf_get("packager");
371 my $pbpackager = $tmp->{$ENV{'PBPROJ'}};
372
373 chdir "$ENV{'PBBUILDDIR'}";
374 my $made = ""; # pkgs made during build
375 foreach my $pbpkg (@pkgs) {
376 my $vertag = $pkg->{$pbpkg};
377 # get the version of the current package - maybe different
378 ($pbver,$pbtag) = split(/-/,$vertag);
379
380 my $src="$ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
381 print $LOG "Source file: $src\n" if ($debug >= 0);
382
383 print $LOG "Working directory: $ENV{'PBBUILDDIR'}\n" if ($debug >= 0);
384 if ($dtype eq "rpm") {
385 foreach my $d ('RPMS','SRPMS','SPECS','SOURCES','BUILD') {
386 if (! -d "$ENV{'PBBUILDDIR'}/$d") {
387 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";
388 }
389 }
390
391 symlink "$src","$ENV{'PBBUILDDIR'}/SOURCES/".basename($src) || die "Unable to symlink $src in $ENV{'PBBUILDDIR'}/SOURCES";
392 # We need to first extract the spec file
393 my @specfile;
394 @specfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$ENV{'PBBUILDDIR'}/SPECS");
395
396 print $LOG "specfile: ".Dumper(\@specfile)."\n" if ($debug >= 1);
397 # set LANGUAGE to check for correct log messages
398 $ENV{'LANGUAGE'}="C";
399 #system("ls -R $ENV{'PBBUILDDIR'}") if ($debug >= 1);
400 foreach my $f (@specfile) {
401 if ($f =~ /\.spec$/) {
402 pb_system("rpmbuild --define \'packager $pbpackager\' --define \"_topdir $ENV{'PBBUILDDIR'}\" -ba $f","Building package with $f under $ENV{'PBBUILDDIR'}");
403 last;
404 }
405 }
406 $made="$made RPMS/*/$pbpkg-$pbver-$pbtag$pbsuf.*.rpm SRPMS/$pbpkg-$pbver-$pbtag$pbsuf.src.rpm";
407 if (-f "/usr/bin/rpmlint") {
408 pb_system("rpmlint $made","Checking validity of rpms with rpmlint");
409 }
410 } elsif ($dtype eq "deb") {
411 chdir "$ENV{'PBBUILDDIR'}" || die "Unable to chdir to $ENV{'PBBUILDDIR'}";
412 pb_system("tar xfz $src","Extracting sources");
413
414 chdir "$pbpkg-$pbver" || die "Unable to chdir to $pbpkg-$pbver";
415 symlink "pbconf/$ddir-$dver","debian" || die "Unable to symlink to pbconf/$ddir-$dver";
416 chmod 0755,"debian/rules";
417 pb_system("dpkg-buildpackage -us -uc -rfakeroot","Building package");
418 $made="$made $pbpkg"."_*.deb $pbpkg"."_*.dsc $pbpkg"."_*.tar.gz";
419 } elsif ($dtype eq "ebuild") {
420 my @ebuildfile;
421 # For gentoo we need to take pb as subsystem name
422 # We put every apps here under sys-apps. hope it's correct
423 # We use pb's home dir in order o have a single OVERLAY line
424 my $tmpd = "$ENV{'HOME'}/portage/pb/sys-apps/$pbpkg";
425 pb_mkdir_p($tmpd) if (! -d "$tmpd");
426 pb_mkdir_p("$ENV{'HOME'}/portage/distfiles") if (! -d "$ENV{'HOME'}/portage/distfiles");
427
428 # We need to first extract the ebuild file
429 @ebuildfile = pb_extract_build_files($src,"$pbpkg-$pbver/pbconf/$ddir-$dver/","$tmpd");
430
431 # Prepare the build env for gentoo
432 my $found = 0;
433 my $pbbd = $ENV{'HOME'};
434 $pbbd =~ s|/|\\/|g;
435 if (-r "/etc/make.conf") {
436 open(MAKE,"/etc/make.conf");
437 while (<MAKE>) {
438 $found = 1 if (/$pbbd\/portage/);
439 }
440 close(MAKE);
441 }
442 if ($found == 0) {
443 pb_system("sudo sh -c 'echo PORTDIR_OVERLAY=\"$ENV{'HOME'}/portage\" >> /etc/make.conf'");
444 }
445 #$found = 0;
446 #if (-r "/etc/portage/package.keywords") {
447 #open(KEYW,"/etc/portage/package.keywords");
448 #while (<KEYW>) {
449 #$found = 1 if (/portage\/pb/);
450 #}
451 #close(KEYW);
452 #}
453 #if ($found == 0) {
454 #pb_system("sudo sh -c \"echo portage/pb >> /etc/portage/package.keywords\"");
455 #}
456
457 # Build
458 foreach my $f (@ebuildfile) {
459 if ($f =~ /\.ebuild$/) {
460 move($f,"$tmpd/$pbpkg-$pbver.ebuild");
461 pb_system("cd $tmpd ; ebuild $pbpkg-$pbver.ebuild clean ; ebuild $pbpkg-$pbver.ebuild digest ; ebuild $pbpkg-$pbver.ebuild package");
462 # Now move it where pb expects it
463 pb_mkdir_p("$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
464 move("$tmpd/$pbpkg-$pbver.ebuild","$ENV{'PBBUILDDIR'}/portage/pb/sys-apps/$pbpkg");
465 }
466 }
467
468 $made="$made portage/pb/sys-apps/$pbpkg/$pbpkg-$pbver.ebuild";
469 } elsif ($dtype eq "slackware") {
470 $made="$made build-$pbpkg/$pbpkg-$pbver-*-$pbtag.tgz";
471 pb_mkdir_p("$ENV{'PBBUILDDIR'}/install") if (! -d "$ENV{'PBBUILDDIR'}/install");
472 } else {
473 die "Unknown dtype format $dtype";
474 }
475 }
476 # Keep track of what is generated so that we can get them back from VMs
477 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to create $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
478 print KEEP "$made\n";
479 close(KEEP);
480}
481
482sub pb_build2ssh {
483 pb_send2ssh("Sources");
484}
485
486sub pb_pkg2ssh {
487 pb_send2ssh("Packages");
488}
489
490# By default deliver to the the public site hosting the
491# ftp structure (or whatever) or a VM
492sub pb_send2ssh {
493
494 my $cmt = shift;
495 my $vm = shift || undef;
496 my $vmexist = shift || 0; # 0 is FALSE
497 my $vmpid = shift || 0; # 0 is FALSE
498 my $host = shift || "sshhost";
499 my $login = shift || "sshlogin";
500 my $dir = shift || "sshdir";
501 my $port = shift || "sshport";
502 my $tmout = shift || "vmtmout";
503 my $cmd = "";
504
505 # Get list of packages to build
506 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir);
507 @pkgs = @$ptr;
508
509 # Get the running distro to consider
510 my ($odir,$over) = (undef, undef);
511 if (defined $vm) {
512 ($odir,$over) = split(/_/,$vm);
513 }
514 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over);
515 print $LOG "DEBUG: distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n" if ($debug >= 1);
516
517 # Get content saved in cms2build
518 my ($pkg) = pb_conf_read("$ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb","pbpkg");
519 $pkg = { } if (not defined $pkg);
520
521 my $src = "";
522 chdir "$ENV{'PBBUILDDIR'}";
523 foreach my $pbpkg (@pkgs) {
524 my $vertag = $pkg->{$pbpkg};
525 # get the version of the current package - maybe different
526 ($pbver,$pbtag) = split(/-/,$vertag);
527
528 if (($cmt eq "Sources") || ($cmt eq "VMs")) {
529 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz";
530 if ($cmd eq "") {
531 $cmd = "ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
532 } else {
533 $cmd = "$cmd ; ln -sf $pbpkg-$pbver.tar.gz $pbpkg-latest.tar.gz";
534 }
535 }
536 }
537 if ($cmt eq "VMs") {
538 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
539 } elsif ($cmt eq "Script") {
540 $src="$src $ENV{'PBDESTDIR'}/pbscript";
541 } elsif ($cmt eq "Packages") {
542 # Get package list from file made during build2pkg
543 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
544 $src = <KEEP>;
545 chomp($src);
546 close(KEEP);
547 if ($dtype eq "rpm") {
548 # Also make a pbscript to generate yum/urpmi bases
549 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
550 } elsif ($dtype eq "deb") {
551 # Also make a pbscript to generate apt bases
552 # $src = "$src $ENV{'PBDESTDIR'}/pbscript"
553 }
554 }
555 # Remove potential leading spaces (cause pb with basename)
556 $src =~ s/^ *//;
557 my $basesrc = "";
558 foreach my $i (split(/ +/,$src)) {
559 $basesrc .= " ".basename($i);
560 }
561
562 print $LOG "Sources handled ($cmt): $src\n" if ($debug >= 0);
563 my ($sshhost,$sshlogin,$sshdir,$sshport,$vmtmout,$testver) = pb_conf_get($host,$login,$dir,$port,$tmout,"testver");
564 my $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}";
565 # Overwrite account value if passed as parameter
566 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount);
567 my $tdir;
568 my $bdir;
569 if (($cmt eq "Sources") || ($cmt eq "Script")) {
570 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
571 } elsif ($cmt eq "VMs") {
572 $tdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/delivery";
573 $bdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/build";
574 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home
575 $bdir =~ s|\$ENV.+\}/||;
576 } elsif ($cmt eq "Packages") {
577 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver";
578 if (defined $testver->{$ENV{'PBPROJ'}}) {
579 # This is a test pkg => target dir is under test
580 $tdir .= "/test";
581 }
582 } else {
583 return;
584 }
585 my $nport = $sshport->{$ENV{'PBPROJ'}};
586 $nport = "$pbport" if (defined $pbport);
587
588 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home
589 $tdir =~ s|\$ENV.+\}/||;
590
591 my $tm = $vmtmout->{$ENV{'PBPROJ'}};
592 pb_system("ssh -q -p $nport $mac \"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 $mac");
593 pb_system("cd $ENV{'PBBUILDDIR'} ; scp -p -P $nport $src $mac:$tdir 2> /dev/null","$cmt delivery in $tdir on $mac");
594 pb_system("ssh -q -p $nport $mac \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $mac if needed");
595 if ($cmt eq "VMs") {
596 # Get back info on pkg produced, compute their name and get them from the VM
597 pb_system("scp -p -P $nport $mac:$bdir/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $bdir on $mac");
598 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
599 my $src = <KEEP>;
600 chomp($src);
601 close(KEEP);
602 $src =~ s/^ *//;
603 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over");
604 # Change pgben to make the next send2ssh happy
605 my $made = "";
606 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag";
607 foreach my $p (split(/ +/,$src)) {
608 my $j = basename($p);
609 pb_system("scp -p -P $nport $mac:\'$bdir/$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $bdir from $mac");
610 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/));
611 }
612 print KEEP "$made\n";
613 close(KEEP);
614 pb_system("ssh -q -p $nport $mac \"rm -rf $tdir $bdir\"","VM cleanup on $mac");
615 pb_send2ssh("Packages","$odir"."_"."$over");
616 if (! $vmexist) {
617 pb_system("ssh -q -p $nport $mac \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $vm halt (pid $vmpid)");
618 }
619 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
620 }
621}
622
623sub pb_script2vm {
624 my $pbscript=shift;
625
626 # Prepare the script to be executed on the VM
627 # in $ENV{'PBDESTDIR'}/pbscript
628 if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
629 copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
630 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
631 }
632
633 my ($vm,$all) = pb_get_vm();
634
635 foreach my $v (@$vm) {
636 # Launch the VM
637 my ($vmexist,$vmpid) = pb_launchvm($v,0);
638
639 # Skip that VM if it something went wrong
640 next if (($vmpid == 0) && ($vmexist ==0));
641
642 # Gather all required files to send them to the VM
643 # and launch the build thourgh pbscript
644 pb_send2ssh("Script","$v",$vmexist,$vmpid,"vmhost","vmlogin","pbrc","vmport","vmtmout");
645
646 }
647}
648
649sub pb_launchvm {
650 my $vm = shift;
651 my $create = shift || 0; # By default do not create a VM
652
653 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
654 die "No VM defined, unable to launch" if (not defined $vm);
655 # Keep only the first VM in case many were given
656 $vm =~ s/,.*//;
657
658 # Launch the VMs
659 my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout","vmsize");
660 my $vmtype = $ptr->{$ENV{'PBPROJ'}};
661 if (not defined $ENV{'PBVMOPT'}) {
662 $ENV{'PBVMOPT'} = "";
663 }
664 if (defined $vmopt->{$ENV{'PBPROJ'}}) {
665 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}";
666 }
667 my $nport = $vmport->{$ENV{'PBPROJ'}};
668 $nport = "$pbport" if (defined $pbport);
669
670 my $cmd;
671 my $vmcmd; # has to be used for pb_check_ps
672 my $vmm; # has to be used for pb_check_ps
673 if ($vmtype eq "qemu") {
674 my $arch = `uname -m`;
675 chomp($arch);
676 my $qemucmd32;
677 my $qemucmd64;
678 if ($arch eq "x86_64") {
679 $qemucmd32 = "/usr/bin/qemu-system-i386";
680 $qemucmd64 = "/usr/bin/qemu";
681 } else {
682 $qemucmd32 = "/usr/bin/qemu";
683 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
684 }
685 if ($vm =~ /_64/) {
686 $vmcmd = "$qemucmd64 -no-kqemu";
687 } else {
688 $vmcmd = "$qemucmd32";
689 }
690 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$vm.qemu";
691 if ($create != 0) {
692 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
693 }
694 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
695 } elsif ($vmtype eq "xen") {
696 } elsif ($vmtype eq "vmware") {
697 } else {
698 die "VM of type $vmtype not supported. Report to the dev team";
699 }
700 my ($tmpcmd,$void) = split(/ +/,$cmd);
701 my $vmexist = pb_check_ps($tmpcmd,$vmm);
702 my $vmpid = 0;
703 if (! $vmexist) {
704 if ($create != 0) {
705 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM");
706 }
707 if (! -f "$vmm") {
708 print "Unable to find VM $vmm\n";
709 } else {
710 pb_system("$cmd &","Launching the VM $vmm");
711 pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $vm to come up");
712 $vmpid = pb_check_ps($tmpcmd,$vmm);
713 }
714 } else {
715 print "Found an existing VM $vmm (pid $vmexist)\n";
716 }
717 return($vmexist,$vmpid);
718}
719
720sub pb_build2vm {
721 # Prepare the script to be executed on the VM
722 # in $ENV{'PBDESTDIR'}/pbscript
723 my ($ntp) = pb_conf_get("vmntp");
724 my $vmntp = $ntp->{$ENV{'PBPROJ'}};
725 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
726 print SCRIPT "#!/bin/bash\n";
727 print SCRIPT "echo ... Execution needed\n";
728 print SCRIPT "# This is in directory delivery\n";
729 print SCRIPT "# Setup the variables required for building\n";
730 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n";
731 print SCRIPT "# Preparation for pb\n";
732 print SCRIPT "mkdir -p ../pbconf\n";
733 print SCRIPT "mv $ENV{'PBPROJ'}.pb ../pbconf\n";
734 print SCRIPT "mv .pbrc \$HOME\n";
735 print SCRIPT "cd ..\n";
736 # Force new date to be in the future compared to the date of the tar file by adding 1 minute
737 my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
738 $date[1]++;
739 my $upddate = strftime("%m%d%H%M%Y", @date);
740 print SCRIPT "echo Setting up date on $vmntp...\n";
741 # Or use ntpdate if available
742 print SCRIPT "sudo date $upddate\n";
743 print SCRIPT "export PBROOT=\`pwd\`\n";
744 print SCRIPT "# Build\n";
745 my $p = "";
746 $p = join(' ',@ARGV) if (defined $ARGV[0]);
747 print SCRIPT "echo Building packages on VM...\n";
748 print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n";
749 close(SCRIPT);
750 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
751
752 my ($vm,$all) = pb_get_vm();
753
754 # Send tar files when we do a global generation
755 pb_build2ssh() if ($all == 1);
756
757 foreach my $v (@$vm) {
758 # Launch the VM
759 my ($vmexist,$vmpid) = pb_launchvm($v,0);
760
761 # Skip that VM if it something went wrong
762 next if (($vmpid == 0) && ($vmexist == 0));
763
764 # Gather all required files to send them to the VM
765 # and launch the build thourgh pbscript
766 pb_send2ssh("VMs","$v",$vmexist,$vmpid,"vmhost","vmlogin","pbrc","vmport","vmtmout");
767 }
768}
769
770
771sub pb_newver {
772
773 die "-V Version parameter needed" if ((not defined $newver) || ($newver eq ""));
774 my $cms=pb_cms_init($ENV{'PBPROJ'});
775 if ($cms->{$ENV{'PBPROJ'}} ne "svn") {
776 die "Only SVN is supported at the moment";
777 }
778 my $res = pb_cms_isdiff($cms);
779 die "You need to have no differences before creating a new version" if ($res != 0);
780 my $cmsurl = pb_cms_getinfo($cms);
781 my $newurl = dirname($cmsurl)."/$newver";
782 pb_cms_copy($cms,$cmsurl,$newurl);
783 pb_cms_checkout($cms,$newurl,"$ENV{'PBROOT'}/../$newver");
784 my $oldver=basename($cmsurl);
785 open(FILE,"$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb") || die "Unable to open $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb";
786 open(OUT,"> $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new") || die "Unable to write to $ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new";
787 while(<FILE>) {
788 s/projver\s+$ENV{'PBPROJ'}\s*=\s*$oldver/projver $ENV{'PBPROJ'} = $newver/;
789 print OUT $_;
790 }
791 close(FILE);
792 close(OUT);
793 rename("$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb.new","$ENV{'PBROOT'}/../$newver/pbconf/$ENV{'PBPROJ'}.pb");
794 pb_cms_checkin($cms,"$ENV{'PBROOT'}/../$newver");
795}
796
797#
798# Return the list of packages we are working on
799#
800sub pb_get_pkg {
801
802my @pkgs;
803my $defpkgdir = shift;
804my $extpkgdir = shift;
805
806# Get packages list
807if (not defined $ARGV[0]) {
808 @pkgs = keys %$defpkgdir;
809} elsif ($ARGV[0] =~ /^all$/) {
810 @pkgs = keys %$defpkgdir;
811 push(@pkgs, keys %$extpkgdir);
812} else {
813 @pkgs = @ARGV;
814}
815print $LOG "Packages: ".join(',',@pkgs)."\n" if ($debug >= 0);
816return(\@pkgs);
817}
818
819#
820# Return the list of VMs we are working on
821# $all is a flag to know if we return all of them
822# or only some (if all we publish also tar files in addition to pkgs
823#
824sub pb_get_vm {
825
826my @vm;
827my $all = 0;
828
829# Get VM list
830if ((not defined $ENV{'PBVM'}) || ($ENV{'PBVM'} =~ /^all$/)) {
831 my ($ptr) = pb_conf_get("vmlist");
832 my $ptr2 = $ptr->{$ENV{'PBPROJ'}};
833 $ptr2 =~ s/,/_64,/g;
834 $ptr2 =~ s/$/_64/g;
835 $ENV{'PBVM'} = "$ptr->{$ENV{'PBPROJ'}},$ptr2";
836 $all = 1;
837}
838print $LOG "VMs: $ENV{'PBVM'}\n";
839@vm = split(/,/,$ENV{'PBVM'});
840return(\@vm,$all);
841}
842
843# Returns the pid of a running VM command using a specific VM file
844sub pb_check_ps {
845 my $vmcmd = shift;
846 my $vmm = shift;
847 my $vmexist = 0; # FALSE by default
848
849 open(PS, "ps auxhww|") || die "Unable to call ps";
850 while (<PS>) {
851 next if (! /$vmcmd/);
852 next if (! /$vmm/);
853 my ($void1, $void2);
854 ($void1, $vmexist, $void2) = split(/ +/);
855 last;
856 }
857 return($vmexist);
858}
859
860
861sub pb_extract_build_files {
862
863my $src=shift;
864my $dir=shift;
865my $ddir=shift;
866my @files;
867
868if ($src =~ /tar\.gz$/) {
869 pb_system("tar xfpz $src $dir","Extracting build files");
870} elsif ($src =~ /tar\.bz2$/) {
871 pb_system("tar xfpj $src $dir","Extracting build files");
872} else {
873 die "Unknown compression algorithm for $src";
874}
875opendir(DIR,"$dir") || die "Unable to open directory $dir";
876foreach my $f (readdir(DIR)) {
877 next if ($f =~ /^\./);
878 move("$dir/$f","$ddir") || die "Unable to move $dir/$f to $ddir";
879 print $LOG "mv $dir/$f $ddir\n" if ($debug >= 1);
880 push @files,"$ddir/$f";
881}
882closedir(DIR);
883# Not enough but still a first cleanup
884pb_rm_rf("$dir");
885return(@files);
886}
887
888sub pb_syntax {
889
890 print "pb (aka project-builder) Version $projectbuilderver-$projectbuilderrev\n";
891 print "\n";
892 print "Syntax: pb [-vhqt][-r pbroot][-p project][[-s script -a account -P port] -m \"mach-1[,...]\"][-i iso] <action> [<pkg1>...]\n";
893 print "\n";
894 print "-h : This help file\n";
895 print "-q : Quiet mode\n";
896 print "-t : Test mode (not done yet)\n";
897 print "-v : Verbose mode\n";
898 print "\n";
899 print "-m machine : Name of the Virtual Machines (VM) you want\n";
900 print " to build on (coma separated). All if none precised\n";
901 print " (or use the env variable PBVM) \n";
902 print "\n";
903 print "-s script : Name of the script you want\n";
904 print " to execute on the related VMs.\n";
905 print "\n";
906 print "-i iso : Name of the ISO image of the distribution you want\n";
907 print " to install on the related VMs.\n";
908 print "\n";
909 print "-a account : Name of the account to use\n";
910 print " to connect on the related VMs.\n";
911 print "\n";
912 print "-P port : Number of the port to use\n";
913 print " to connect on the related VMs.\n";
914 print "\n";
915 print "-p project : Name of the project you're working on\n";
916 print " (or use the env variable PBPROJ) \n";
917 print "\n";
918 print "-r pbroot : Path Name of project under the CMS \n";
919 print " (or use the env variable PBROOT) \n";
920 print "\n";
921 print "-V newver : New version of the project to create\n";
922 print " from the current one. \n";
923 print "\n";
924 print "<action> can be:\n";
925 print "\n";
926 print "\tcms2build: Create tar files for the project under your CMS\n";
927 print "\t CMS supported are SVN and CVS\n";
928 print "\t parameters are packages to build\n";
929 print "\t if not using default list\n";
930 print "\n";
931 print "\tbuild2pkg: Create packages for your running distribution \n";
932 print "\n";
933 print "\tcms2pkg: cms2build + build2pkg\n";
934 print "\n";
935 print "\tbuild2ssh: Send the tar files to a SSH host \n";
936 print "\n";
937 print "\tcms2ssh: cms2build + build2ssh\n";
938 print "\n";
939 print "\tpkg2ssh: Send the packages built to a SSH host \n";
940 print "\n";
941 print "\tbuild2vm: Create packages in VMs, launching them if needed\n";
942 print "\t and send those packages to a SSH host once built\n";
943 print "\t VM type supported are QEMU \n";
944 print "\n";
945 print "\tcms2vm: cms2build + build2vm\n";
946 print "\n";
947 print "\tlaunchvm: Launch one virtual machine\n";
948 print "\n";
949 print "\tscript2vm: Launch one virtual machine if needed \n";
950 print "\t and executes a script on it \n";
951 print "\n";
952 print "\tnewvm: Create a new virtual machine\n";
953 print "\n";
954 print "\tnewver: Create a new version of the project derived \n";
955 print "\t from the current one \n";
956 print "\n";
957 print "\tnewproj: Create a new project and a template set of \n";
958 print "\t configuration files under pbconf \n";
959 print "\n";
960}
Note: See TracBrowser for help on using the repository browser.