Changeset 748 for devel/pb/bin/pb


Ignore:
Timestamp:
Mar 29, 2009, 6:52:33 PM (11 years ago)
Author:
Bruno Cornec
Message:
  • Adds snapshot support at build time and at use time for VMs
File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb/bin/pb

    r739 r748  
    3636my $action;                 # action to realize
    3737my $test = "FALSE";         # Not used
    38 my $force = 0;              # Force VE/VM rebuild
     38my $pbforce = 0;            # Force VE/VM rebuild
     39my $pbsnap = 0;             # Use snapshot mode for VM/VE
    3940my $option = "";            # Not used
    4041my @pkgs;                   # list of packages
     
    6364pb helps you build various packages directly from your project sources.
    6465Those sources could be handled by a CMS (Configuration Management System)
    65 such as Subversion, CVS, Mercurial... or being a simple reference to a compressed tar file.
     66such as Subversion, CVS, Git, Mercurial... or being a simple reference to a compressed tar file.
    6667It's based on a set of configuration files, a set of provided macros to help
    6768you keeping build files as generic as possible. For example, a single .spec
     
    7172=head1 SYNOPSIS
    7273
    73 pb [-vhq][-r pbroot][-p project][[-s script -a account -P port][-m mach-1[,...]]][-i iso] <action> [<pkg1> ...]
    74 
    75 pb [--verbose][--help][--man][--quiet][--revision pbroot][--project project][[--script script --account account --port port][--machine mach-1[,...]]][--iso iso] <action> [<pkg1> ...]
     74pb [-vhSq][-r pbroot][-p project][[-s script -a account -P port][-m mach-1[,...]]][-i iso] <action> [<pkg1> ...]
     75
     76pb [--verbose][--help][--man][--quiet][--snapshot][--revision pbroot][--project project][[--script script --account account --port port][--machine mach-1[,...]]][--iso iso] <action> [<pkg1> ...]
    7677
    7778=head1 OPTIONS
     
    9091
    9192Print a brief help message and exits.
     93
     94=item B<-S|--snapshot>
     95
     96Use the snapshot mode of VMs or VEs
    9297
    9398=item B<--man>
     
    214219
    215220Setup a virtual environment for pb usage
     221
     222=item B<snapvm>
     223
     224Snapshot a virtual machine for pb usage
     225
     226=item B<snapve>
     227
     228Snapshot a virtual environment for pb usage
    216229
    217230=item B<newver>
     
    314327# ---------------------------------------------------------------------------
    315328
    316 # Old syntax
    317 #getopts('a:fhi:l:m:P:p:qr:s:vV:',\%opts);
    318 
    319329my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
    320330
     
    326336        "man" => \$opts{'man'},
    327337        "verbose|v+" => \$opts{'v'},
     338        "snapshot|S" => \$opts{'S'},
    328339        "quiet|q" => \$opts{'q'},
    329340        "log-files|l=s" => \$opts{'l'},
     
    349360}
    350361if (defined $opts{'f'}) {
    351     $force=1;
     362    $pbforce=1;
    352363}
    353364if (defined $opts{'q'}) {
    354365    $pbdebug=-1;
     366}
     367if (defined $opts{'S'}) {
     368    $pbsnap=1;
    355369}
    356370if (defined $opts{'l'}) {
     
    444458    pb_setup_v("ve");
    445459} elsif ($action =~ /^setupvm$/) {
     460    pb_snap_v("vm");
     461} elsif ($action =~ /^snapve$/) {
     462    pb_snap_v("ve");
     463} elsif ($action =~ /^snapvm$/) {
    446464    pb_setup_v("vm");
    447465} elsif ($action =~ /^newproj$/) {
     
    10171035    my $vmexist = shift || 0;           # 0 is FALSE
    10181036    my $vmpid = shift || 0;             # 0 is FALSE
     1037    my $snapme = shift || 0;            # 0 is FALSE
    10191038
    10201039    pb_log(2,"DEBUG: pb_send2target($cmt,".Dumper($v).",$vmexist,$vmpid)\n");
     
    13651384    pb_log(2,"Before halt, vmexist: $vmexist, vmpid: $vmpid\n");
    13661385    if ((! $vmexist) && (($cmt eq "vm") || ($cmt eq "VMScript"))) {
     1386        # If in setupvm then takes a snapshot just before halting
     1387        if ($snapme != 0) {
     1388            my ($vmmonport,$vmtype) = pb_conf_get("vmmonport","vmtype");
     1389            # For monitoring control
     1390            if ((($vmtype->{$ENV{'PBPROJ'}}) eq "kvm") || (($vmtype->{$ENV{'PBPROJ'}}) eq "qemu")) {
     1391                use Net::Telnet;
     1392                my $t = new Net::Telnet (Host => "localhost", Port => $vmmonport->{$ENV{'PBPROJ'}}) || die "Unable to dialog on the monitor";
     1393                # move to monitor mode
     1394                my @lines = $t->cmd("c");
     1395                # Create a snapshot named pb
     1396                @lines = $t->cmd("savevm pb");
     1397                # Write the new status in the VM
     1398                @lines = $t->cmd("commit all");
     1399                # End
     1400                @lines = $t->cmd("quit");
     1401            }
     1402        }
    13671403        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)");
    13681404    }
     
    13721408    my $pbscript=shift;
    13731409    my $vtype=shift;
    1374     my $force=shift || 0;   # Force stop of VM. Default not
     1410    my $pbforce=shift || 0; # Force stop of VM. Default not
    13751411    my $vm1=shift || undef; # Only that VM to treat
     1412    my $snapme=shift || 0;  # Do we have to crate a snapshot
    13761413    my $vm;
    13771414    my $all;
    13781415
    1379     pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$force)\n");
     1416    pb_log(2,"DEBUG: pb_script2v($pbscript,$vtype,$pbforce)\n");
    13801417    # Prepare the script to be executed on the VM
    13811418    # in $ENV{'PBDESTDIR'}/pbscript
     
    13951432        # Launch the VM/VE
    13961433        if ($vtype eq "vm") {
    1397             ($vmexist,$vmpid) = pb_launchv($vtype,$v,0);
     1434            ($vmexist,$vmpid) = pb_launchv($vtype,$v,0,$snapme);
    13981435            pb_log(2,"DEBUG: After pb_launchv, vmexist: $vmexist, vmpid: $vmpid\n");
    13991436
     
    14021439
    14031440            # If force stopping the VM then reset vmexist
    1404             if ($force == 1) {
     1441            if ($pbforce == 1) {
    14051442                $vmpid = $vmexist;
    14061443                $vmexist = 0;
     
    14151452        # and launch the build through pbscript
    14161453        pb_log(2,"DEBUG: Before send2target, vmexist: $vmexist, vmpid: $vmpid\n");
    1417         pb_send2target(uc($vtype)."Script","$v",$vmexist,$vmpid);
     1454        pb_send2target(uc($vtype)."Script","$v",$vmexist,$vmpid,$snapme);
    14181455
    14191456    }
     
    14241461    my $v = shift;
    14251462    my $create = shift || 0;        # By default do not create a VM
     1463    my $snapme = shift || 0;        # By default do not create a VM
    14261464
    14271465    pb_log(2,"DEBUG: pb_launchv($vtype,$v,$create)\n");
     
    14361474        die "-i iso parameter needed" if (((not defined $iso) || ($iso eq "")) && ($create != 0));
    14371475
    1438         my ($ptr,$vmopt,$vmpath,$vmport,$vmtmout,$vmsize) = pb_conf_get("vmtype","vmopt","vmpath","vmport","vmtmout","vmsize");
     1476        my ($ptr,$vmpath,$vmport,$vmsize,$vmmonport) = pb_conf_get("vmtype","vmpath","vmport","vmsize","vmmonport");
     1477        my ($vmopt,$vmtmout,$vmsnap) = pb_conf_get_if("vmopt","vmtmout","vmsnap");
    14391478
    14401479        my $vmtype = $ptr->{$ENV{'PBPROJ'}};
     
    14521491        } elsif (defined $vmopt->{$ENV{'PBPROJ'}}) {
    14531492            $ENV{'PBVMOPT'} .= " $vmopt->{$ENV{'PBPROJ'}}" if ($ENV{'PBVMOPT'} !~ / $vmopt->{$ENV{'PBPROJ'}}/);
     1493        }
     1494        # Do not use snapshot feature at creation
     1495        if ($create != 0) {
     1496            if ((defined $vmsnap->{$v}) && ($vmsnap->{$v} =~ /true/i)) {
     1497                $ENV{'PBVMOPT'} .= " -snapshot";
     1498            } elsif ((defined $vmsnap->{$ENV{'PBPROJ'}}) && ($vmsnap->{$ENV{'PBPROJ'}} =~ /true/i)) {
     1499                $ENV{'PBVMOPT'} .= " -snapshot";
     1500            } elsif ($pbsnap eq 1) {
     1501                $ENV{'PBVMOPT'} .= " -snapshot";
     1502            }
     1503        } elsif ($snapme != 0) {
     1504            if (($vmtype eq "kvm") || ($vmtype eq "qemu")) {
     1505                # Configure the monitoring to automize the creation of the 'pb' snapshot
     1506                $ENV{'PBVMOPT'} .= "-serial mon:telnet::$vmmonport->{$ENV{'PBPROJ'}},server,nowait ";
     1507            }
    14541508        }
    14551509        if (defined $vmtmout->{$v}) {
     
    14861540                $ENV{'PBVMOPT'} .= " -cdrom $iso -boot d";
    14871541            }
    1488             $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 $vmm"
     1542            # Always redirect the network and always try to use a 'pb' snapshot
     1543            $cmd = "$vmcmd $ENV{'PBVMOPT'} -redir tcp:$nport:10.0.2.15:22 -loadvm pb $vmm"
    14891544        } elsif ($vmtype eq "xen") {
    14901545        } elsif ($vmtype eq "vmware") {
     
    15391594
    15401595            my ($verpmtype) = pb_conf_get("verpmtype");
    1541             if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($force == 1)) {
     1596            if (($create != 0) || ($verebuild->{$ENV{'PBPROJ'}} eq "true") || ($pbforce == 1)) {
    15421597                # We have to rebuild the chroot
    15431598                if ($dtype eq "rpm") {
     
    21622217
    21632218    # Force shutdown of VM exept if it was already launched
    2164     my $force = 0;
     2219    my $pbforce = 0;
    21652220    if ((! $vmexist) && ($vtype eq "vm")) {
    2166         $force = 1;
     2221        $pbforce = 1;
    21672222    }
    21682223   
    2169     pb_script2v($pbscript,$vtype,$force,$v);
     2224    pb_script2v($pbscript,$vtype,$pbforce,$v);
     2225}
     2226return;
     2227}
     2228
     2229# Function to create a snapshot named 'pb' for VMs/VEs
     2230sub pb_snap_v {
     2231
     2232my $vtype = shift;
     2233
     2234my ($vm,$all) = pb_get_v($vtype);
     2235
     2236# Script generated
     2237my $pbscript = "$ENV{'PBDESTDIR'}/snapv";
     2238
     2239foreach my $v (@$vm) {
     2240    # Name of the account to deal with for VM/VE
     2241    # Do not use the one passed potentially with -a
     2242    my ($pbac) = pb_conf_get($vtype."login");
     2243    my ($vmexist,$vmpid);
     2244
     2245    # Prepare the script to be executed on the VM/VE
     2246    # in $ENV{'PBDESTDIR'}/setupv
     2247    open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
     2248   
     2249    print SCRIPT << 'EOF';
     2250    #!/bin/bash
     2251    sleep 60
     2252EOF
     2253    close(SCRIPT);
     2254    chmod 0755,"$pbscript";
     2255
     2256    # Force shutdown of VM/VE
     2257    $pbforce = 1;
     2258    # Force snapshot of VM/VE
     2259    pb_script2v($pbscript,$vtype,$pbforce,$v,1);
    21702260}
    21712261return;
Note: See TracChangeset for help on using the changeset viewer.