source: ProjectBuilder/0.8.11/pb/bin/pb@ 284

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

Removes suse_10.0 bzip2 usage as wrong

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