Changeset 320 in ProjectBuilder for devel/pb/bin
- Timestamp:
- Feb 13, 2008, 6:26:07 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
devel/pb/bin/pb
r319 r320 27 27 my %opts; # CLI Options 28 28 my $action; # action to realize 29 my $test = "FALSE"; 30 my $option = ""; 31 my @pkgs; 29 my $test = "FALSE"; # Not used 30 my $force = 0; # Force VE/VM rebuild 31 my $option = ""; # Not used 32 my @pkgs; # list of packages 32 33 my $pbtag; # Global Tag variable 33 34 my $pbver; # Global Version variable … … 36 37 my %pbtag; # per package 37 38 my $pbrev; # Global REVISION variable 38 my @date = pb_get_date();39 my $pbdate = strftime("%Y-%m-%d", @date);40 39 my $pbaccount; # Login to use to connect to the VM 41 40 my $pbport; # Port to use to connect to the VM … … 43 42 my $iso; # ISO iage for the VM to create 44 43 45 getopts('a:hi:l:m:P:p:qr:s:tvV:',\%opts); 44 my @date = pb_get_date(); 45 my $pbdate = strftime("%Y-%m-%d", @date); 46 47 getopts('a:fhi:l:m:P:p:qr:s:tvV:',\%opts); 46 48 47 49 my ($projectbuilderver,$projectbuilderrev) = pb_version_init(); … … 54 56 #$debug = $opts{'v'}; 55 57 pb_log(0,"Debug value: $debug\n"); 58 } 59 if (defined $opts{'f'}) { 60 $force=1; 56 61 } 57 62 if (defined $opts{'q'}) { … … 78 83 # Handle virtual machines if any 79 84 if (defined $opts{'m'}) { 80 $ENV{'PBV M'} = $opts{'m'};85 $ENV{'PBV'} = $opts{'m'}; 81 86 } 82 87 if (defined $opts{'s'}) { … … 136 141 } elsif ($action =~ /^pkg2ssh$/) { 137 142 pb_pkg2ssh(); 143 } elsif ($action =~ /^build2ve$/) { 144 pb_build2v("ve"); 138 145 } elsif ($action =~ /^build2vm$/) { 139 pb_build2vm(); 146 pb_build2v("vm"); 147 } elsif ($action =~ /^cms2ve$/) { 148 pb_cms2build(); 149 pb_build2v("ve"); 140 150 } elsif ($action =~ /^cms2vm$/) { 141 151 pb_cms2build(); 142 pb_build2v m();152 pb_build2v("vm"); 143 153 } elsif ($action =~ /^launchvm$/) { 144 pb_launchvm($ENV{'PBVM'},0); 154 pb_launchv("vm",$ENV{'PBV'},0); 155 } elsif ($action =~ /^launchve$/) { 156 pb_launchv("ve",$ENV{'PBV'},0); 145 157 } elsif ($action =~ /^script2vm$/) { 146 pb_script2vm($pbscript); 158 pb_script2v($pbscript,"vm"); 159 } elsif ($action =~ /^script2ve$/) { 160 pb_script2v($pbscript,"ve"); 147 161 } elsif ($action =~ /^newver$/) { 148 162 pb_newver(); 149 } elsif ($action =~ /^new chroot$/) {150 pb_launch chroot($ENV{'PBCHROOT'},1);163 } elsif ($action =~ /^newve$/) { 164 pb_launchv("ve",$ENV{'PBV'},1); 151 165 } elsif ($action =~ /^newvm$/) { 152 pb_launchv m($ENV{'PBVM'},1);166 pb_launchv("vm",$ENV{'PBV'},1); 153 167 } elsif ($action =~ /^newproj$/) { 154 168 # Nothing to do - already done in pb_env_init … … 168 182 169 183 # declare packager for filtering 170 my ($tmp) = pb_conf_get("p ackager");184 my ($tmp) = pb_conf_get("pbpackager"); 171 185 my $pbpackager = $tmp->{$ENV{'PBPROJ'}}; 172 186 … … 227 241 my %build; 228 242 229 my ($ptr) = pb_conf_get_if("vmlist"," chrootlist");243 my ($ptr) = pb_conf_get_if("vmlist","velist"); 230 244 foreach my $d (split(/,/,$ptr->{$ENV{'PBPROJ'}})) { 231 245 my ($name,$ver,$arch) = split(/-/,$d); … … 240 254 # Find all build files first relatively to PBROOT 241 255 # Find also all specific files referenced in the .pb conf file 242 my %bfiles = {};243 my %pkgfiles = {};256 my %bfiles = (); 257 my %pkgfiles = (); 244 258 pb_log(2,"DEBUG dir: $ENV{'PBCONF'}/$pbpkg\n"); 245 259 $build{"$ddir-$dver"} = "yes"; … … 347 361 $pkg = { } if (not defined $pkg); 348 362 349 # declare packager 350 my ($tmp) = pb_conf_get("packager"); 351 my $pbpackager = $tmp->{$ENV{'PBPROJ'}}; 363 # declare packager (via env var in VM/VE 364 my $pbpackager; 365 if (not defined $ENV{'PBPACKAGER'}) { 366 my ($tmp) = pb_conf_get("pbpackager"); 367 $pbpackager = $tmp->{$ENV{'PBPROJ'}}; 368 } else { 369 $pbpackager = $ENV{'PBPACKAGER'}; 370 } 352 371 353 372 chdir "$ENV{'PBBUILDDIR'}"; … … 466 485 467 486 sub pb_build2ssh { 468 pb_send2 ssh("Sources");487 pb_send2target("Sources"); 469 488 } 470 489 471 490 sub pb_pkg2ssh { 472 pb_send2 ssh("Packages");491 pb_send2target("Packages"); 473 492 } 474 493 475 494 # By default deliver to the the public site hosting the 476 # ftp structure (or whatever) or a VM 477 sub pb_send2 ssh{495 # ftp structure (or whatever) or a VM/VE 496 sub pb_send2target { 478 497 479 498 my $cmt = shift; 480 my $v m= shift || undef;499 my $v = shift || undef; 481 500 my $vmexist = shift || 0; # 0 is FALSE 482 501 my $vmpid = shift || 0; # 0 is FALSE 483 my $host = shift || "sshhost"; 484 my $login = shift || "sshlogin"; 485 my $dir = shift || "sshdir"; 486 my $port = shift || "sshport"; 487 my $tmout = shift || "vmtmout"; 502 503 my $host = "sshhost"; 504 my $login = "sshlogin"; 505 my $dir = "sshdir"; 506 my $port = "sshport"; 507 my $tmout = "sshtmout"; 508 my $path = "sshpath"; 509 my $conf = "sshconf"; 510 my $rebuild = "sshrebuild"; 511 if ($cmt eq "vm") { 512 $login = "vmlogin"; 513 $dir = "pbdir"; 514 $tmout = "vmtmout"; 515 $rebuild = "vmrebuild"; 516 # Specific VM 517 $host = "vmhost"; 518 $port = "vmport"; 519 } elsif ($cmt eq "ve") { 520 $login = "velogin"; 521 $dir = "pbdir"; 522 $tmout = "vetmout"; 523 # Specific VE 524 $path = "vepath"; 525 $conf = "veconf"; 526 $rebuild = "verebuild"; 527 } 488 528 my $cmd = ""; 489 529 … … 493 533 494 534 # Get the running distro to consider 495 my ($odir,$over ) = (undef, undef);496 if (defined $v m) {497 ($odir,$over ) = split(/_/,$vm);535 my ($odir,$over,$oarch) = (undef, undef); 536 if (defined $v) { 537 ($odir,$over,$oarch) = split(/-/,$v); 498 538 } 499 539 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($odir,$over); … … 511 551 ($pbver,$pbtag) = split(/-/,$vertag); 512 552 513 if (($cmt eq "Sources") || ($cmt eq " VMs")) {553 if (($cmt eq "Sources") || ($cmt eq "vm") || ($cmt eq "ve")) { 514 554 $src = "$src $ENV{'PBDESTDIR'}/$pbpkg-$pbver.tar.gz"; 515 555 if ($cmd eq "") { … … 520 560 } 521 561 } 522 if ( $cmt eq "VMs") {523 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PB CONF'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";562 if (($cmt eq "vm") || ($cmt eq "ve")) { 563 $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBROOT'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}"; 524 564 } elsif ($cmt eq "Script") { 525 565 $src="$src $ENV{'PBDESTDIR'}/pbscript"; … … 538 578 } 539 579 } 540 # Remove potential leading spaces (cause p bwith basename)580 # Remove potential leading spaces (cause problem with basename) 541 581 $src =~ s/^ *//; 542 582 my $basesrc = ""; … … 545 585 } 546 586 547 pb_log(2,"Sources handled ($cmt): $src\n"); 548 my ($sshhost,$sshlogin,$sshdir,$sshport,$vmtmout,$testver) = pb_conf_get($host,$login,$dir,$port,$tmout,"testver"); 549 my $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}"; 550 # Overwrite account value if passed as parameter 551 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount); 587 pb_log(0,"Sources handled ($cmt): $src\n"); 588 my ($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf) = pb_conf_get($host,$login,$dir,$port,$tmout,$rebuild,$path,$conf); 589 pb_log(2,"ssh: ".Dumper(($sshhost,$sshlogin,$sshdir,$sshport,$vtmout,$vrebuild,$vepath,$veconf))."\n"); 590 # Not mandatory 591 my ($testver) = pb_conf_get_if("testver"); 592 593 my $mac; 594 # Useless for VE 595 if ($cmt ne "ve") { 596 $mac = "$sshlogin->{$ENV{'PBPROJ'}}\@$sshhost->{$ENV{'PBPROJ'}}"; 597 # Overwrite account value if passed as parameter 598 $mac = "$pbaccount\@$sshhost->{$ENV{'PBPROJ'}}" if (defined $pbaccount); 599 } 600 552 601 my $tdir; 553 602 my $bdir; 554 603 if (($cmt eq "Sources") || ($cmt eq "Script")) { 555 604 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src"; 556 } elsif ( $cmt eq "VMs") {557 $tdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/delivery";558 $bdir = dirname("$sshdir->{$ENV{'PBPROJ'}}")."/build";605 } elsif (($cmt eq "vm") || ($cmt eq "ve")) { 606 $tdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/delivery"; 607 $bdir = $sshdir->{$ENV{'PBPROJ'}}."/$ENV{'PBPROJ'}/build"; 559 608 # Remove a potential $ENV{'HOME'} as bdir should be relative to pb's home 560 609 $bdir =~ s|\$ENV.+\}/||; 561 610 } elsif ($cmt eq "Packages") { 562 611 $tdir = "$sshdir->{$ENV{'PBPROJ'}}/$ddir/$dver"; 563 if ((defined $testver ->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} eq "true")) {612 if ((defined $testver) && (defined $testver->{$ENV{'PBPROJ'}}) && ($testver->{$ENV{'PBPROJ'}} eq "true")) { 564 613 # This is a test pkg => target dir is under test 565 614 $tdir .= "/test"; … … 568 617 return; 569 618 } 570 my $nport = $sshport->{$ENV{'PBPROJ'}}; 571 $nport = "$pbport" if (defined $pbport); 619 620 # Useless for VE 621 my $nport; 622 if ($cmt ne "ve") { 623 $nport = $sshport->{$ENV{'PBPROJ'}}; 624 $nport = "$pbport" if (defined $pbport); 625 } 572 626 573 627 # Remove a potential $ENV{'HOME'} as tdir should be relative to pb's home 574 628 $tdir =~ s|\$ENV.+\}/||; 575 629 576 my $tm = $vmtmout->{$ENV{'PBPROJ'}}; 577 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"); 578 pb_system("cd $ENV{'PBBUILDDIR'} ; scp -p -P $nport $src $mac:$tdir 2> /dev/null","$cmt delivery in $tdir on $mac"); 579 pb_system("ssh -q -p $nport $mac \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $mac if needed"); 580 if ($cmt eq "VMs") { 630 my $tm = $vtmout->{$ENV{'PBPROJ'}}; 631 632 # ssh communication if not VE 633 my ($shcmd,$cpcmd,$cptarget,$cp2target); 634 if ($cmt ne "ve") { 635 $shcmd = "ssh -q -p $nport $mac"; 636 $cpcmd = "scp -p -P $nport"; 637 $cptarget = "$mac:$tdir"; 638 $cp2target = "$mac:$bdir"; 639 } else { 640 my $tp = $vepath->{$ENV{'PBPROJ'}}; 641 $shcmd = "sudo chroot $tp/$v /bin/su - $sshlogin->{$ENV{'PBPROJ'}} -c "; 642 $cpcmd = "cp -a "; 643 $cptarget = "$tp/$tdir"; 644 $cp2target = "$tp/$bdir"; 645 } 646 647 pb_system("$shcmd \"mkdir -p $tdir ; cd $tdir ; echo \'for i in $basesrc; do if [ -f \$i ]; then rm -f \$i; fi; done\ ; $cmd' | bash\"","Preparing $tdir on $cptarget"); 648 pb_system("cd $ENV{'PBBUILDDIR'} ; $cpcmd $src $cptarget 2> /dev/null","$cmt delivery in $cptarget"); 649 # For VE we need to change the owner manually - To be tested if needed 650 #if ($cmt eq "ve") { 651 #pb_system("cd $cptarget ; sudo chown -R $sshlogin->{$ENV{'PBPROJ'}} .","$cmt chown in $cptarget to $sshlogin->{$ENV{'PBPROJ'}}"); 652 #} 653 pb_system("$shcmd \"echo \'cd $tdir ; if [ -f pbscript ]; then ./pbscript; fi\' | bash\"","Executing pbscript on $cptarget if needed"); 654 if (($cmt eq "vm") || ($cmt eq "ve")) { 581 655 # Get back info on pkg produced, compute their name and get them from the VM 582 pb_system(" scp -p -P $nport $mac:$bdir/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $bdir on $mac");656 pb_system("$cpcmd $cp2target/pbgen-$pbprojver-$pbprojtag $ENV{'PBBUILDDIR'} 2> /dev/null","Get package names in $cp2target"); 583 657 open(KEEP,"$ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to read $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag"; 584 658 my $src = <KEEP>; … … 587 661 $src =~ s/^ *//; 588 662 pb_mkdir_p("$ENV{'PBBUILDDIR'}/$odir/$over"); 589 # Change pgben to make the next send2 sshhappy663 # Change pgben to make the next send2target happy 590 664 my $made = ""; 591 665 open(KEEP,"> $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag") || die "Unable to write $ENV{'PBBUILDDIR'}/pbgen-$pbprojver-$pbprojtag"; 592 666 foreach my $p (split(/ +/,$src)) { 593 667 my $j = basename($p); 594 pb_system(" scp -p -P $nport $mac:\'$bdir/$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $bdir from $mac");668 pb_system("$cpcmd $cp2target/\'$p\' $ENV{'PBBUILDDIR'}/$odir/$over 2> /dev/null","Package recovery of $j in $cp2target"); 595 669 $made="$made $odir/$over/$j" if (($dtype ne "rpm") || ($j !~ /.src.rpm$/)); 596 670 } 597 671 print KEEP "$made\n"; 598 672 close(KEEP); 599 pb_system(" ssh -q -p $nport $mac \"rm -rf $tdir $bdir\"","VM cleanup on $mac");600 pb_send2 ssh("Packages","$odir"."_"."$over");601 if ( ! $vmexist) {602 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 $vmhalt (pid $vmpid)");673 pb_system("$shcmd \"rm -rf $tdir $bdir\"","$cmt cleanup"); 674 pb_send2target("Packages","$odir"."_"."$over"); 675 if ((! $vmexist) && ($cmt eq "vm")) { 676 pb_system("$shcmd \"sudo /sbin/halt -p \"; sleep $tm ; echo \'if [ -d /proc/$vmpid ]; then kill -9 $vmpid; fi \' | bash ; sleep 10","VM $v halt (pid $vmpid)"); 603 677 } 604 678 pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir"); … … 606 680 } 607 681 608 sub pb_script2v m{682 sub pb_script2v { 609 683 my $pbscript=shift; 684 my $vtype=shift; 610 685 611 686 # Prepare the script to be executed on the VM … … 616 691 } 617 692 618 my ($vm,$all) = pb_get_vm(); 693 my ($vm,$all) = pb_get_v($vtype); 694 my ($vmexist,$vmpid) = (undef,undef); 619 695 620 696 foreach my $v (@$vm) { 697 # Launch the VM/VE 698 if ($vtype eq "vm") { 699 ($vmexist,$vmpid) = pb_launchv($vtype,$v,0); 700 701 # Skip that VM if something went wrong 702 next if (($vmpid == 0) && ($vmexist ==0)); 703 } 704 705 # Gather all required files to send them to the VM 706 # and launch the build through pbscript 707 pb_send2target("Script","$v",$vmexist,$vmpid); 708 709 } 710 } 711 712 sub pb_launchv { 713 my $vtype = shift; 714 my $v = shift; 715 my $create = shift || 0; # By default do not create a VM 716 717 die "No VM/VE defined, unable to launch" if (not defined $v); 718 # Keep only the first VM in case many were given 719 $v =~ s/,.*//; 720 721 # Which is our local arch ? (standardize on i386 for those platforms) 722 my $arch = `uname -m`; 723 chomp($arch); 724 $arch =~ s/i.86/i386/; 725 726 # Launch the VMs/VEs 727 if ($vtype eq "vm") { 728 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0)); 729 730 my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout","vmsize"); 731 732 my $vmtype = $ptr->{$ENV{'PBPROJ'}}; 733 if (not defined $ENV{'PBVMOPT'}) { 734 $ENV{'PBVMOPT'} = ""; 735 } 736 if (defined $vmopt->{$ENV{'PBPROJ'}}) { 737 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/); 738 } 739 my $nport = $vmport->{$ENV{'PBPROJ'}}; 740 $nport = "$pbport" if (defined $pbport); 741 742 my $cmd; 743 my $vmcmd; # has to be used for pb_check_ps 744 my $vmm; # has to be used for pb_check_ps 745 if ($vmtype eq "qemu") { 746 my $qemucmd32; 747 my $qemucmd64; 748 if ($arch eq "x86_64") { 749 $qemucmd32 = "/usr/bin/qemu-system-i386"; 750 $qemucmd64 = "/usr/bin/qemu"; 751 } else { 752 $qemucmd32 = "/usr/bin/qemu"; 753 $qemucmd64 = "/usr/bin/qemu-system-x86_64"; 754 } 755 if ($v =~ /x86_64/) { 756 $vmcmd = "$qemucmd64 -no-kqemu"; 757 } else { 758 $vmcmd = "$qemucmd32"; 759 } 760 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu"; 761 if ($create != 0) { 762 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d"; 763 } 764 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm" 765 } elsif ($vmtype eq "xen") { 766 } elsif ($vmtype eq "vmware") { 767 } else { 768 die "VM of type $vmtype not supported. Report to the dev team"; 769 } 770 my ($tmpcmd,$void) = split(/ +/,$cmd); 771 my $vmexist = pb_check_ps($tmpcmd,$vmm); 772 my $vmpid = 0; 773 if (! $vmexist) { 774 if ($create != 0) { 775 if (($vmtype eq "qemu") || ($vmtype eq "xen")) { 776 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM"); 777 } elsif ($vmtype eq "vmware") { 778 } else { 779 } 780 } 781 if (! -f "$vmm") { 782 pb_log(0,"Unable to find VM $vmm\n"); 783 } else { 784 pb_system("$cmd &","Launching the VM $vmm"); 785 pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $v to come up"); 786 $vmpid = pb_check_ps($tmpcmd,$vmm); 787 } 788 } else { 789 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n"); 790 } 791 return($vmexist,$vmpid); 792 # VE here 793 } else { 794 # Get VE context 795 my ($ptr,$vepath,$vetmout,$verebuild,$veconf) = pb_conf_get("vetype","vepath","vetmout","verebuild","veconf"); 796 my $vetype = $ptr->{$ENV{'PBPROJ'}}; 797 798 # Get distro context 799 my ($name,$ver,$darch) = split(/-/,$v); 800 chomp($darch); 801 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($name,$ver); 802 803 if ($vetype eq "chroot") { 804 # Architecture consistency 805 if ($arch ne $darch) { 806 die "Unable to launch a VE of architecture $darch on a $arch platform" if (not (($darch eq "x86_64") && ($arch =~ /i?86/))); 807 } 808 809 if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($force == 1)) { 810 # We have to rebuild the chroot 811 if ($dtype eq "rpm") { 812 pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE"); 813 # Once setup we need to install some packages, the pb account, ... 814 pb_system("sudo /usr/sbin/mock --install --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE"); 815 #pb_system("sudo /usr/sbin/mock --init --resultdir=\"/tmp\" --configdir=\"$veconf->{$ENV{'PBPROJ'}}\" --basedir=\"$vepath->{$ENV{'PBPROJ'}}\" -r $v","Creating the mock VE"); 816 } elsif ($dtype eq "deb") { 817 pb_system("","Creating the pbuilder VE"); 818 } elsif ($dtype eq "ebuild") { 819 die "Please teach the dev team how to build gentoo chroot"; 820 } else { 821 die "Unknown distribution type $dtype. Report to dev team"; 822 } 823 } 824 # Nothing more to do for VE. No real launch 825 } else { 826 die "VE of type $vetype not supported. Report to the dev team"; 827 } 828 } 829 } 830 831 sub pb_build2v { 832 833 my $vtype = shift; 834 835 # Prepare the script to be executed on the VM/VE 836 # in $ENV{'PBDESTDIR'}/pbscript 837 my ($ntp) = pb_conf_get($vtype."ntp"); 838 my $vntp = $ntp->{$ENV{'PBPROJ'}}; 839 840 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript"; 841 print SCRIPT "#!/bin/bash\n"; 842 print SCRIPT "echo ... Execution needed\n"; 843 print SCRIPT "# This is in directory delivery\n"; 844 print SCRIPT "# Setup the variables required for building\n"; 845 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n"; 846 print SCRIPT "# Preparation for pb\n"; 847 print SCRIPT "mv .pbrc \$HOME\n"; 848 print SCRIPT "cd ..\n"; 849 # Force new date to be in the future compared to the date of the tar file by adding 1 minute 850 my @date=pb_get_date(); 851 $date[1]++; 852 my $upddate = strftime("%m%d%H%M%Y", @date); 853 print SCRIPT "echo Setting up date on $vntp...\n"; 854 # Or use ntpdate if available TBC 855 print SCRIPT "sudo date $upddate\n"; 856 # This exports avoids pb_env_init to deal with PBCONF stuff 857 print SCRIPT "export PBCONF=/tmp\n"; 858 print SCRIPT "export PBVER=$ENV{'PBVER'}\n"; 859 print SCRIPT "export PBTAG=$ENV{'PBTAG'}\n"; 860 print SCRIPT "export PBPACKAGER=\"$ENV{'PBPACKAGER'}\"\n"; 861 print SCRIPT "# Build\n"; 862 # Get list of packages to build 863 my $ptr = pb_get_pkg($defpkgdir,$extpkgdir); 864 @pkgs = @$ptr; 865 my $p = join(' ',@pkgs) if (@pkgs); 866 print SCRIPT "echo Building packages on $vtype...\n"; 867 print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n"; 868 close(SCRIPT); 869 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript"; 870 871 my ($v,$all) = pb_get_v($vtype); 872 873 # Send tar files when we do a global generation 874 pb_build2ssh() if ($all == 1); 875 876 my ($vmexist,$vmpid) = (undef,undef); 877 878 foreach my $v (@$v) { 879 if ($vtype eq "vm") { 621 880 # Launch the VM 622 my ($vmexist,$vmpid) = pb_launchvm($v,0); 623 624 # Skip that VM if it something went wrong 625 next if (($vmpid == 0) && ($vmexist ==0)); 626 627 # Gather all required files to send them to the VM 628 # and launch the build thourgh pbscript 629 pb_send2ssh("Script","$v",$vmexist,$vmpid,"vmhost","vmlogin","$ENV{'PBDESTDIR'}/pbrc","vmport","vmtmout"); 630 631 } 632 } 633 634 sub pb_launchchroot { 635 # Virtual env. 636 my $ve = shift; 637 my $create = shift || 0; # By default do not create a chroot 638 639 #die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0)); 640 #die "No VM defined, unable to launch" if (not defined $ve); 641 642 # Keep only the first chroot in case many were given 643 $ve =~ s/,.*//; 644 645 # Launch the chroot 646 my ($ptr,$veopt,$veport,$vepath,$vetmout,$vesize) = pb_conf_get("vetype","veopt","veport","vepath","vetmout","vesize"); 647 my $vetype = $ptr->{$ENV{'PBPROJ'}}; 648 if (not defined $ENV{'PBCHROOTOPT'}) { 649 $ENV{'PBCHROOTOPT'} = ""; 650 } 651 if (defined $veopt->{$ENV{'PBPROJ'}}) { 652 $ENV{'PBVMOPT'} .= " $veopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $veopt->{$ENV{'PBPROJ'}}/); 653 } 654 my $nport = $veport->{$ENV{'PBPROJ'}}; 655 $nport = "$pbport" if (defined $pbport); 656 657 my $cmd; 658 my $vecmd; # has to be used for pb_check_ps 659 my $vem; # has to be used for pb_check_ps 660 if ($vetype eq "qemu") { 661 my $arch = `uname -m`; 662 chomp($arch); 663 my $qemucmd32; 664 my $qemucmd64; 665 if ($arch eq "x86_64") { 666 $qemucmd32 = "/usr/bin/qemu-system-i386"; 667 $qemucmd64 = "/usr/bin/qemu"; 668 } else { 669 $qemucmd32 = "/usr/bin/qemu"; 670 $qemucmd64 = "/usr/bin/qemu-system-x86_64"; 671 } 672 if ($ve =~ /_64/) { 673 $vecmd = "$qemucmd64 -no-kqemu"; 674 } else { 675 $vecmd = "$qemucmd32"; 676 } 677 $vem = "$vepath->{$ENV{'PBPROJ'}}/$ve.qemu"; 678 if ($create != 0) { 679 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d"; 680 } 681 $cmd = "$vecmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vem" 682 } elsif ($vetype eq "xen") { 683 } elsif ($vetype eq "vmware") { 684 } else { 685 die "VM of type $vetype not supported. Report to the dev team"; 686 } 687 my ($tmpcmd,$void) = split(/ +/,$cmd); 688 my $veexist = pb_check_ps($tmpcmd,$vem); 689 my $vepid = 0; 690 if (! $veexist) { 691 if ($create != 0) { 692 pb_system("/usr/bin/qemu-img create -f qcow2 $vem $vesize->{$ENV{'PBPROJ'}}","Creating the QEMU VM"); 693 } 694 if (! -f "$vem") { 695 pb_log(0,"Unable to find VM $vem\n"); 696 } else { 697 pb_system("$cmd &","Launching the VM $vem"); 698 pb_system("sleep $vetmout->{$ENV{'PBPROJ'}}","Waiting for VM $ve to come up"); 699 $vepid = pb_check_ps($tmpcmd,$vem); 700 } 701 } else { 702 pb_log(0,"Found an existing VM $vem (pid $veexist)\n"); 703 } 704 return($veexist,$vepid); 705 } 706 707 sub pb_launchvm { 708 my $vm = shift; 709 my $create = shift || 0; # By default do not create a VM 710 711 die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0)); 712 die "No VM defined, unable to launch" if (not defined $vm); 713 # Keep only the first VM in case many were given 714 $vm =~ s/,.*//; 715 716 # Launch the VMs 717 my ($ptr,$vmopt,$vmport,$vmpath,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmport","vmpath","vmtmout","vmsize"); 718 my $vmtype = $ptr->{$ENV{'PBPROJ'}}; 719 if (not defined $ENV{'PBVMOPT'}) { 720 $ENV{'PBVMOPT'} = ""; 721 } 722 if (defined $vmopt->{$ENV{'PBPROJ'}}) { 723 $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/); 724 } 725 my $nport = $vmport->{$ENV{'PBPROJ'}}; 726 $nport = "$pbport" if (defined $pbport); 727 728 my $cmd; 729 my $vmcmd; # has to be used for pb_check_ps 730 my $vmm; # has to be used for pb_check_ps 731 if ($vmtype eq "qemu") { 732 my $arch = `uname -m`; 733 chomp($arch); 734 my $qemucmd32; 735 my $qemucmd64; 736 if ($arch eq "x86_64") { 737 $qemucmd32 = "/usr/bin/qemu-system-i386"; 738 $qemucmd64 = "/usr/bin/qemu"; 739 } else { 740 $qemucmd32 = "/usr/bin/qemu"; 741 $qemucmd64 = "/usr/bin/qemu-system-x86_64"; 742 } 743 if ($vm =~ /_64/) { 744 $vmcmd = "$qemucmd64 -no-kqemu"; 745 } else { 746 $vmcmd = "$qemucmd32"; 747 } 748 $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$vm.qemu"; 749 if ($create != 0) { 750 $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d"; 751 } 752 $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm" 753 } elsif ($vmtype eq "xen") { 754 } elsif ($vmtype eq "vmware") { 755 } else { 756 die "VM of type $vmtype not supported. Report to the dev team"; 757 } 758 my ($tmpcmd,$void) = split(/ +/,$cmd); 759 my $vmexist = pb_check_ps($tmpcmd,$vmm); 760 my $vmpid = 0; 761 if (! $vmexist) { 762 if ($create != 0) { 763 if (($vmtype eq "qemu") || ($vmtype eq "xen")) { 764 pb_system("/usr/bin/qemu-img create -f qcow2 $vmm $vmsize->{$ENV{'PBPROJ'}}","Creating the QEMU VM"); 765 } elsif ($vmtype eq "vmware") { 766 } else { 767 } 768 } 769 if (! -f "$vmm") { 770 pb_log(0,"Unable to find VM $vmm\n"); 771 } else { 772 pb_system("$cmd &","Launching the VM $vmm"); 773 pb_system("sleep $vmtmout->{$ENV{'PBPROJ'}}","Waiting for VM $vm to come up"); 774 $vmpid = pb_check_ps($tmpcmd,$vmm); 775 } 776 } else { 777 pb_log(0,"Found an existing VM $vmm (pid $vmexist)\n"); 778 } 779 return($vmexist,$vmpid); 780 } 781 782 sub pb_build2vm { 783 # Prepare the script to be executed on the VM 784 # in $ENV{'PBDESTDIR'}/pbscript 785 my ($ntp) = pb_conf_get("vmntp"); 786 my $vmntp = $ntp->{$ENV{'PBPROJ'}}; 787 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript"; 788 print SCRIPT "#!/bin/bash\n"; 789 print SCRIPT "echo ... Execution needed\n"; 790 print SCRIPT "# This is in directory delivery\n"; 791 print SCRIPT "# Setup the variables required for building\n"; 792 print SCRIPT "export PBPROJ=$ENV{'PBPROJ'}\n"; 793 print SCRIPT "# Preparation for pb\n"; 794 print SCRIPT "mkdir -p ../pbconf\n"; 795 print SCRIPT "mv $ENV{'PBPROJ'}.pb ../pbconf\n"; 796 print SCRIPT "mv .pbrc \$HOME\n"; 797 print SCRIPT "cd ..\n"; 798 # Force new date to be in the future compared to the date of the tar file by adding 1 minute 799 my @date=(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst()); 800 $date[1]++; 801 my $upddate = strftime("%m%d%H%M%Y", @date); 802 print SCRIPT "echo Setting up date on $vmntp...\n"; 803 # Or use ntpdate if available 804 print SCRIPT "sudo date $upddate\n"; 805 print SCRIPT "export PBROOT=\`pwd\`\n"; 806 print SCRIPT "# Build\n"; 807 my $p = ""; 808 $p = join(' ',@ARGV) if (defined $ARGV[0]); 809 print SCRIPT "echo Building packages on VM...\n"; 810 print SCRIPT "pb -p $ENV{'PBPROJ'} build2pkg $p\n"; 811 close(SCRIPT); 812 chmod 0755,"$ENV{'PBDESTDIR'}/pbscript"; 813 814 my ($vm,$all) = pb_get_vm(); 815 816 # Send tar files when we do a global generation 817 pb_build2ssh() if ($all == 1); 818 819 foreach my $v (@$vm) { 820 # Launch the VM 821 my ($vmexist,$vmpid) = pb_launchvm($v,0); 881 my ($vmexist,$vmpid) = pb_launchv($vtype,$v,0); 822 882 823 883 # Skip that VM if it something went wrong 824 884 next if (($vmpid == 0) && ($vmexist == 0)); 825 826 # Gather all required files to send them to the VM827 828 pb_send2ssh("VMs","$v",$vmexist,$vmpid,"vmhost","vmlogin","$ENV{'PBDESTDIR'}/pbrc","vmport","vmtmout");829 885 } 886 # Gather all required files to send them to the VM/VE 887 # and launch the build thourgh pbscript 888 pb_send2target($vtype,"$v",$vmexist,$vmpid); 889 } 830 890 } 831 891 … … 858 918 859 919 # 860 # Return the list of VMs we are working on920 # Return the list of VMs/VEs we are working on 861 921 # $all is a flag to know if we return all of them 862 922 # or only some (if all we publish also tar files in addition to pkgs 863 923 # 864 sub pb_get_vm { 865 866 my @vm; 924 sub pb_get_v { 925 926 my $vtype = shift; 927 my @v; 867 928 my $all = 0; 868 869 # Get VM list 870 if ((not defined $ENV{'PBVM'}) || ($ENV{'PBVM'} =~ /^all$/)) { 871 my ($ptr) = pb_conf_get("vmlist"); 872 my $ptr2 = $ptr->{$ENV{'PBPROJ'}}; 873 $ptr2 =~ s/,/_64,/g; 874 $ptr2 =~ s/$/_64/g; 875 $ENV{'PBVM'} = "$ptr->{$ENV{'PBPROJ'}},$ptr2"; 929 my $vlist; 930 my $pbv = 'PBV'; 931 932 if ($vtype eq "vm") { 933 $vlist = "vmlist"; 934 } elsif ($vtype eq "ve") { 935 $vlist = "velist"; 936 } 937 # Get VM/VE list 938 if ((not defined $ENV{$pbv}) || ($ENV{$pbv} =~ /^all$/)) { 939 my ($ptr) = pb_conf_get($vlist); 940 $ENV{$pbv} = $ptr->{$ENV{'PBPROJ'}}; 876 941 $all = 1; 877 942 } 878 pb_log(2,"VMs: $ENV{'PBVM'}\n"); 879 @vm = split(/,/,$ENV{'PBVM'}); 880 return(\@vm,$all); 943 pb_log(2,"$vtype: $ENV{$pbv}\n"); 944 @v = split(/,/,$ENV{$pbv}); 945 return(\@v,$all); 946 } 947 948 sub pb_setup_v { 949 # Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo 950 # Needs to use root account to connect to the VM/VE 951 952 # pb will take your local public SSH key to access 953 # the pb account in the VM later on 954 955 my $vtype = shift; 956 957 my $file = "$ENV{'HOME'}/.ssh/id_dsa.pub"; 958 die "Unable to find your public ssh key as $file"; 959 960 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript"; 961 print SCRIPT << 'EOF'; 962 #!/usr/bin/perl -w 963 964 $file="/etc/passwd"; 965 open(PBFILE,$file) || die "Unable to open $file"; 966 my $found = 0; 967 while (<PBFILE>) { 968 $found = 1 if (/^pb:/); 969 } 970 close(PBFILE); 971 972 if ( $found == 0 ) { 973 if ( ! -d "/home" ) { 974 mkdir "/home"; 975 } 976 system "groupadd pb"; 977 system "useradd pb -g pb -m -d /home/pb"; 978 } 979 980 # For root 981 mkdir ".ssh",0700; 982 system 'cp /tmp/pbkey .ssh/authorized_keys'; 983 chmod 0600,".ssh/authorized_keys"; 984 985 # For pb 986 chdir "/home/pb"; 987 mkdir ".ssh",0700; 988 system 'cp /tmp/pbkey .ssh/authorized_keys'; 989 chmod 0600,".ssh/authorized_keys"; 990 system 'chown -R pb:pb .ssh'; 991 992 # No passwd for pb only keys 993 $file="/etc/shadow"; 994 open(PBFILE,$file) || die "Unable to open $file"; 995 open(PBOUT,"> $file.new") || die "Unable to open $file.new"; 996 while (<PBFILE>) { 997 s/^pb:\!\!:/pb:*:/; 998 s/^pb:\!:/pb:*:/; #SLES 9 e.g. 999 print PBOUT $_; 1000 } 1001 close(PBFILE); 1002 close(PBOUT); 1003 rename("$file.new",$file); 1004 chmod 0640,$file; 1005 1006 # pb has to be added to portage group on gentoo 1007 unlink "/tmp/pbkey"; 1008 1009 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init(); 1010 print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n"; 1011 1012 # Get and install pb 1013 if ( $ddir eq "fedora" ) { 1014 system "yum clean all"; 1015 system "yum update -y"; 1016 my $arch=`uname -m`; 1017 my $opt = ""; 1018 chomp($arch); 1019 if ($arch eq "x86_64") { 1020 $opt="--exclude=*.i?86"; 1021 } 1022 1023 system "yum -y $opt install rpm-build wget patch ntp sudo perl-DateManip perl-ExtUtils-MakeMaker"; 1024 } elsif (( $dfam eq "rh" ) || ($ddir eq "sles") || (($ddir eq "suse") && (($dver eq "10.1") || ($dver eq "10.0"))) || (($ddir eq "mandrake") && ($dver eq "10.1"))) { 1025 # Suppose pkg are installed already 1026 system "rpm -e lsb 2>&1 > /dev/null"; 1027 system "rm -rf DateManip* ; wget http://search.cpan.org/CPAN/authors/id/S/SB/SBECK/Date-Manip-5.46.tar.gz ; tar xvfz Date-Manip-5.46.tar.gz ; cd Date-Manip* ; perl Makefile.PL ; make ; make install ; cd .. "; 1028 } elsif ($ddir eq "suse") { 1029 # New OpenSuSE 1030 system "export TERM=linux ; liste=\"\" ; for i in make wget patch sudo perl-DateManip perl-File-HomeDir xntp; do rpm -q \$i 1> /dev/null 2> /dev/null ; if [ \$\? != 0 ]; then liste=\"\$liste \$i\"; fi; done; echo \"Liste: \$liste\" ; if [ \"\$liste\" != \"\" ]; then yast2 -i \$liste ; fi"; 1031 } elsif ( $dfam eq "md" ) { 1032 system "urpmi.update -a ; urpmi --auto rpm-build wget sudo patch ntp-client perl-DateManip"; 1033 } elsif ( $dfam eq "du" ) { 1034 if (( $dver eq "3.1" ) && ($ddir eq "debian")) { 1035 system "apt-get update; apt-get -y install wget patch ssh sudo debian-builder dh-make fakeroot ntpdate libdate-manip-perl"; 1036 } else { 1037 system "apt-get update; apt-get -y install wget patch openssh-server dpkg-dev sudo debian-builder dh-make fakeroot ntpdate rses5-dev libdate-manip-perl"; 1038 } 1039 } elsif ( $dfam eq "gen" ) { 1040 system "emerge -u system ; emerge wget sudo ntp DateManip"; 1041 } else { 1042 print "No pkg to install\n"; 1043 } 1044 1045 # Adapt sudoers 1046 $file="/etc/sudoers"; 1047 open(PBFILE,$file) || die "Unable to open $file"; 1048 open(PBOUT,"> $file.new") || die "Unable to open $file.new"; 1049 while (<PBFILE>) { 1050 next if (/^pb /); 1051 s/Defaults[ \t]+requiretty//; 1052 print PBOUT $_; 1053 } 1054 close(PBFILE); 1055 print PBOUT "pb ALL=(ALL) NOPASSWD:ALL\n"; 1056 close(PBOUT); 1057 rename("$file.new",$file); 1058 chmod 0440,$file; 1059 1060 # Suse wants sudoers as 640 1061 if (($ddir eq "sles") || (($ddir eq "suse")) && ($dver ne "10.3")) { 1062 chmod 0640,$file; 1063 } 1064 1065 # Sync date 1066 system "/usr/sbin/ntpdate ntp.pool.org"; 1067 1068 system "rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd .."; 1069 EOF 1070 close(SCRIPT); 881 1071 } 882 1072 … … 959 1149 print "-m machine: Name of the Virtual Machines (VM) you want\n"; 960 1150 print " to build on (coma separated). All if none precised\n"; 961 print " (or use the env variable PBV M) \n";1151 print " (or use the env variable PBV) \n"; 962 1152 print "\n"; 963 1153 print "-s script: Name of the script you want\n"; … … 1003 1193 print "\t VM type supported are QEMU \n"; 1004 1194 print "\n"; 1005 print "\tbuild2 chroot: Create packages in a chroot, creating it if needed\n";1195 print "\tbuild2ve: Create packages in VEs, creating it if needed\n"; 1006 1196 print "\t and send those packages to a SSH host once built\n"; 1007 1197 print "\n"; 1008 1198 print "\tcms2vm: cms2build + build2vm\n"; 1009 1199 print "\n"; 1200 print "\tcms2ve: cms2build + build2ve\n"; 1201 print "\n"; 1010 1202 print "\tlaunchvm: Launch one virtual machine\n"; 1203 print "\n"; 1204 print "\tlaunchve: Launch one virtual environment\n"; 1011 1205 print "\n"; 1012 1206 print "\tscript2vm: Launch one virtual machine if needed \n"; 1013 1207 print "\t and executes a script on it \n"; 1014 1208 print "\n"; 1209 print "\tscript2ve: Execute a script in a virtual environment \n"; 1210 print "\n"; 1015 1211 print "\tnewvm: Create a new virtual machine\n"; 1016 1212 print "\n"; 1017 print "\tnew chroot: Create a new chrootenvironment\n";1213 print "\tnewve: Create a new virtual environment\n"; 1018 1214 print "\n"; 1019 1215 print "\tnewver: Create a new version of the project derived \n";
Note:
See TracChangeset
for help on using the changeset viewer.