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

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

pb_env_init now creates a pbconf template dir if asked for (newproj option)

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