Changeset 142 for devel/pb/bin/pb


Ignore:
Timestamp:
Sep 13, 2007, 10:16:54 AM (13 years ago)
Author:
Bruno Cornec
Message:

Add function script2vm and launchvm (separate) to improve VM management support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb/bin/pb

    r141 r142  
    122122    pb_build2vm();
    123123} elsif ($action =~ /^launchvm$/) {
    124     pb_launchvm();
     124    pb_launchvm($ENV{'PBVM'});
     125} elsif ($action =~ /^script2vm$/) {
     126    pb_script2vm($pbscript);
    125127} elsif ($action =~ /^clean$/) {
    126128} else {
     
    372374    my $cmt = shift;
    373375    my $vm = shift || undef;
     376    my $vmexist = shift || 0;           # 0 is FALSE
    374377    my $host = shift || "sshhost";
    375378    my $login = shift || "sshlogin";
     
    406409    if ($cmt eq "VMs") {
    407410        $src="$src $ENV{'PBDESTDIR'}/pbscript $ENV{'PBCONF'}/$ENV{'PBPROJ'}.pb $ENV{'PBDESTDIR'}/$pbprojver-$pbprojtag.pb $ENV{'PBETC'}";
     411    } elsif ($cmt eq "Script") {
     412        $src="$src $ENV{'PBDESTDIR'}/pbscript";
    408413    } elsif ($cmt eq "Packages") {
    409414        # Get package list from file made during build2pkg
     
    432437    my $tdir;
    433438    my $bdir;
    434     if ($cmt eq "Sources") {
     439    if (($cmt eq "Sources") || ($cmt eq "Script")) {
    435440        $tdir = "$sshdir->{$ENV{'PBPROJ'}}/src";
    436441    } elsif ($cmt eq "VMs") {
     
    468473        print KEEP "$odir/$over/*\n";
    469474        close(KEEP);
    470         #pb_system("ssh -q -p $port $mac \"rm -rf dirname("$sshdir->{$ENV{'PBPROJ'}}"); sudo /usr/bin/poweroff\"","VM cleanup and halt on $mac");
     475        if (! $vmexist) {
     476            pb_system("ssh -q -p $port $mac \"rm -rf $tdir; sudo /usr/bin/poweroff \"; sleep 120 ; if [-d /proc/$vmexist]; then kill $vmexist; fi ; sleep 10","VM cleanup and halt on $mac");
     477        }
    471478        pb_send2ssh("Packages","$odir"."_"."$over");
    472479        pb_rm_rf("$ENV{'PBBUILDDIR'}/$odir");
     
    474481}
    475482
    476 sub launchvm {
    477     my ($vm,$all) = pb_get_vm();
     483sub pb_script2vm {
     484    my $pbscript=shift;
    478485
    479486    # Prepare the script to be executed on the VM
    480487    # in $ENV{'PBDESTDIR'}/pbscript
    481     copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
    482     chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
     488    if ((defined $pbscript ) && ($pbscript ne "$ENV{'PBDESTDIR'}/pbscript")) {
     489        copy($pbscript,"$ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
     490        chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
     491    }
     492
     493    my ($vm,$all) = pb_get_vm();
    483494
    484495    foreach my $v (@$vm) {
    485         # Launch the VMs
    486         my ($ptr,$vmopt,$vmport,$vmpath) = pb_conf_get("vmtype","vmopt","vmport","vmpath");
    487         my $vmtype = $ptr->{$ENV{'PBPROJ'}};
    488         if (defined $vmopt->{$ENV{'PBPROJ'}}) {
    489             $ENV{'PBVMOPT'} = $vmopt->{$ENV{'PBPROJ'}};
     496        # Launch the VM
     497        my $vmexist = pb_launchvm($v);
     498        if (! $vmexist) {
     499            pb_system("sleep 300","Waiting for it to come up");
     500        }
     501
     502        # Gather all required files to send them to the VM
     503        # and launch the build thourgh pbscript
     504        pb_send2ssh("Script","$v",$vmexist,"vmhost","vmlogin","pbrc","vmport");
     505    }
     506}
     507
     508sub pb_launchvm {
     509    my $vm = shift;
     510
     511    die "No VM defined, unable to launch" if (not defined $vm);
     512    # Keep only the first VM in case many were given
     513    $vm =~ s/,.*//;
     514
     515    # Launch the VMs
     516    my ($ptr,$vmopt,$vmport,$vmpath) = pb_conf_get("vmtype","vmopt","vmport","vmpath");
     517    my $vmtype = $ptr->{$ENV{'PBPROJ'}};
     518    if (defined $vmopt->{$ENV{'PBPROJ'}}) {
     519        $ENV{'PBVMOPT'} = $vmopt->{$ENV{'PBPROJ'}};
     520    } else {
     521        $ENV{'PBVMOPT'} = "";
     522    }
     523
     524    my $cmd;
     525    my $vmcmd;      # has to be used for pb_check_ps
     526    my $vmm;        # has to be used for pb_check_ps
     527    if ($vmtype eq "qemu") {
     528        my $arch = `uname -m`;
     529        chomp($arch);
     530        my $qemucmd32;
     531        my $qemucmd64;
     532        if ($arch eq "x86_64") {
     533            $qemucmd32 = "/usr/bin/qemu-system-i386";
     534            $qemucmd64 = "/usr/bin/qemu";
    490535        } else {
    491             $ENV{'PBVMOPT'} = "";
    492         }
    493 
    494         my $cmd;
    495         if ($vmtype eq "qemu") {
    496             my $arch = `uname -m`;
    497             chomp($arch);
    498             my $qemucmd;
    499             my $qemucmd32;
    500             my $qemucmd64;
    501             if ($arch eq "x86_64") {
    502                 $qemucmd32 = "/usr/bin/qemu-system-i386";
    503                 $qemucmd64 = "/usr/bin/qemu";
    504             } else {
    505                 $qemucmd32 = "/usr/bin/qemu";
    506                 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
    507             }
    508             if ($v =~ /_64/) {
    509                 $qemucmd = "$qemucmd64 -no-kqemu";
    510             } else {
    511                 $qemucmd = "$qemucmd32";
    512             }
    513             if (! -f "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu") {
    514                 print "Unable to find VM $vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
    515                 next;
    516             }
    517             $cmd = "$qemucmd $ENV{'PBVMOPT'} -redir tcp:$vmport->{$ENV{'PBPROJ'}}:10.0.2.15:22 $vmpath->{$ENV{'PBPROJ'}}/$v.qemu"
    518         } elsif ($vmtype eq "xen") {
    519         } elsif ($vmtype eq "vmware") {
     536            $qemucmd32 = "/usr/bin/qemu";
     537            $qemucmd64 = "/usr/bin/qemu-system-x86_64";
     538        }
     539        if ($vm =~ /_64/) {
     540            $vmcmd = "$qemucmd64 -no-kqemu";
    520541        } else {
    521             die "VM of type $vmtype not supported. Report to the dev team";
    522         }
     542            $vmcmd = "$qemucmd32";
     543        }
     544        $vmm = "$vmpath->{$ENV{'PBPROJ'}}/$vm.qemu";
     545        if (! -f "$vmm") {
     546            print "Unable to find VM $vmm";
     547            return;
     548        }
     549        $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$vmport->{$ENV{'PBPROJ'}}:10.0.2.15:22 $vmm"
     550    } elsif ($vmtype eq "xen") {
     551    } elsif ($vmtype eq "vmware") {
     552    } else {
     553        die "VM of type $vmtype not supported. Report to the dev team";
     554    }
     555    my $vmexist = pb_check_ps($vmcmd,$vmm);
     556    if (! $vmexist) {
    523557        pb_system("$cmd &","Launching the VM");
    524         pb_system("sleep 300","Waiting for it to come up");
    525 
    526         # Gather all required files to send them to the VM and launch the build thourgh pbscript
    527         pb_send2ssh("VMs","$v","vmhost","vmlogin","pbrc","vmport");
    528     }
    529 }
     558    }
     559    return($vmexist);
     560}
     561
    530562sub pb_build2vm {
    531     my ($vm,$all) = pb_get_vm();
    532 
    533563    # Prepare the script to be executed on the VM
    534564    # in $ENV{'PBDESTDIR'}/pbscript
     
    553583    chmod 0755,"$ENV{'PBDESTDIR'}/pbscript";
    554584
     585    my ($vm,$all) = pb_get_vm();
     586
    555587    # Send tar files when we do a global generation
    556588    pb_build2ssh() if ($all == 1);
    557589
    558590    foreach my $v (@$vm) {
    559         # Launch the VMs
    560         my ($ptr,$vmopt,$vmport,$vmpath) = pb_conf_get("vmtype","vmopt","vmport","vmpath");
    561         my $vmtype = $ptr->{$ENV{'PBPROJ'}};
    562         if (defined $vmopt->{$ENV{'PBPROJ'}}) {
    563             $ENV{'PBVMOPT'} = $vmopt->{$ENV{'PBPROJ'}};
    564         } else {
    565             $ENV{'PBVMOPT'} = "";
    566         }
    567 
    568         my $cmd;
    569         if ($vmtype eq "qemu") {
    570             my $arch = `uname -m`;
    571             chomp($arch);
    572             my $qemucmd;
    573             my $qemucmd32;
    574             my $qemucmd64;
    575             if ($arch eq "x86_64") {
    576                 $qemucmd32 = "/usr/bin/qemu-system-i386";
    577                 $qemucmd64 = "/usr/bin/qemu";
    578             } else {
    579                 $qemucmd32 = "/usr/bin/qemu";
    580                 $qemucmd64 = "/usr/bin/qemu-system-x86_64";
    581             }
    582             if ($v =~ /_64/) {
    583                 $qemucmd = "$qemucmd64 -no-kqemu";
    584             } else {
    585                 $qemucmd = "$qemucmd32";
    586             }
    587             if (! -f "$vmpath->{$ENV{'PBPROJ'}}/$v.qemu") {
    588                 print "Unable to find VM $vmpath->{$ENV{'PBPROJ'}}/$v.qemu";
    589                 next;
    590             }
    591             $cmd = "$qemucmd $ENV{'PBVMOPT'} -redir tcp:$vmport->{$ENV{'PBPROJ'}}:10.0.2.15:22 $vmpath->{$ENV{'PBPROJ'}}/$v.qemu"
    592         } elsif ($vmtype eq "xen") {
    593         } elsif ($vmtype eq "vmware") {
    594         } else {
    595             die "VM of type $vmtype not supported. Report to the dev team";
    596         }
    597         pb_system("$cmd &","Launching the VM");
    598         pb_system("sleep 300","Waiting for it to come up");
    599 
    600         # Gather all required files to send them to the VM and launch the build thourgh pbscript
    601         pb_send2ssh("VMs","$v","vmhost","vmlogin","pbrc","vmport");
     591        # Launch the VM
     592        my $vmexist = pb_launchvm($v);
     593        if (! $vmexist) {
     594            pb_system("sleep 300","Waiting for it to come up");
     595        }
     596
     597        # Gather all required files to send them to the VM
     598        # and launch the build thourgh pbscript
     599        pb_send2ssh("VMs","$v",$vmexist,"vmhost","vmlogin","pbrc","vmport");
    602600    }
    603601}
     
    643641}
    644642
     643sub pb_check_ps {
     644    my $vmcmd = shift;
     645    my $vmm = shift;
     646    my $vmexist = 0;        # FALSE by default
     647
     648    open(PS, "ps auxhww|") || die "Unable to call ps";
     649    while (<PS>) {
     650        next if (! /$vmcmd/);
     651        next if (! /$vmm/);
     652        my ($void1, $void2);
     653        ($void1, $vmexist, $void2) = split(/ +/);
     654        last;
     655    }
     656    return($vmexist);
     657}
     658
     659
    645660sub pb_extract_build_files {
    646661
     
    675690    print "-v : Verbose mode\n";
    676691    print "\n";
    677     print "-m machine : Name of the Virtual Machines you want\n";
    678     print "             to build on (space separated). All if none precised\n";
     692    print "-m machine : Name of the Virtual Machines (VM) you want\n";
     693    print "             to build on (coma separated). All if none precised\n";
    679694    print "             (or use the env variable PBVM)       \n";
    680695    print "\n";
     
    703718    print "\tcms2pkg:   cms2build + build2pkg\n";
    704719    print "\n";
    705     print "\tbuild2vm:  Create packages in a Virtual Machine  \n";
    706     print "\t           adn send them to a SSH host once built\n";
    707     print "\t           VM supported are QEMU                 \n";
    708     print "\n";
    709     print "\tlaunchvm:  Launch a virtual machine and execute potentially\n";
    710     print "\t           a script on it\n";
     720    print "\tbuild2vm:  Create packages in VMs, launching them if needed\n";
     721    print "\t           and send those packages to a SSH host once built\n";
     722    print "\t           VM type supported are QEMU            \n";
     723    print "\n";
     724    print "\tlaunchvm:  Launch one virtual machine\n";
     725    print "\n";
     726    print "\tscript2vm: Launch one virtual machine if needed        \n";
     727    print "\t           and executes a script on it                 \n";
    711728    print "\n";
    712729    print "\tcms2vm:    cms2build + build2vm\n";
Note: See TracChangeset for help on using the changeset viewer.