#!/usr/bin/perl
use strict;
use warnings;
use Log::Log4perl;
use XML::LibXML;

our %conf = (
    fail	=>	0,
    logging	=>	{
	enabled		=>	1,
	filename	=>	"/var/log/mgiration_hook.log",
    },
    timers	=>	{
	virsh		=>	{
	    shutdown		=>	{
	        sleep			=>	30,
		counter			=>	20,
	    },
	    start		=>	{
		sleep			=>	5,
		counter			=>	20,
	    },
	},
    },
    guest	=>	{
        1		=>	{
            cpuset		=>	'0-9',
            numatune		=>	1,
        },
        2		=>	{
            cpuset		=>	'20-29',
            numatune		=>	0,
        },
        3		=>	{
            cpuset		=>	'10-19',
            numatune		=>	0,
        },
        4		=>	{
            cpuset		=>	'30-39',
            numatune		=>	1,
        },
    },
    node_mem_limit	=>	130000000,
    memory_kib	=>	31461376,
    vcpus	=>	10,
    max		=>	{
        vg		=>	2,
        lv_per_vg	=>	2,
    },
);

my %log_conf = (
    "log4perl.rootLogger"                               =>      "DEBUG, LOG",
    "log4perl.appender.LOG"                             =>      "Log::Log4perl::Appender::File",
    "log4perl.appender.LOG.filename"                    =>      "$conf{logging}->{filename}",
    "log4perl.appender.LOG.mode"                        =>      "append",
    "log4perl.appender.LOG.layout"                      =>      "Log::Log4perl::Layout::PatternLayout::Multiline",
    "log4perl.appender.LOG.layout.ConversionPattern"    =>      "[%d] %p - %m%n",
);

my %new = ();

our $log;
if($conf{logging}->{enabled}){
    Log::Log4perl::init(\%log_conf);
    $log = Log::Log4perl->get_logger();
}
our %volumes;
my $hostname = '';
my $use_array;
my @vgarrays;
my @out;

$hostname = $ARGV[0] if(defined($ARGV[0]) && $ARGV[0] ne '-');
my $vg_hostname = $hostname;
$vg_hostname =~ s/\./\-/g;
loggg("#####  #####  Starting run. ARGS: @ARGV  #####  #####");


############### Migrate ###############
# This is called by QEMU automagically on an incoming migration. We reconfigure the incoming guest on the fly to
# work with the new hardware. If first time run, will also attempt to configure LVM.
if(defined($ARGV[1]) && defined($ARGV[2]) && $ARGV[1] eq 'migrate' && $ARGV[2] eq 'begin'){
    check_memory();
    get_volumes();

    my $originalxml = do { local $/; <STDIN> };
    my $dom = XML::LibXML->load_xml(string => $originalxml);

    # Only run this code if incoming migration is from reseller migration script
    # For regular manual migrations, this hook should not do anything.
    my ($eigmig) = $dom->findnodes('/domain/metadata');
    exit(0) if(!$eigmig);

    loggg("##### Incoming XML:");
    loggg($originalxml);

    # Set up local storage only if no volumes detected already
    if(!$volumes{wrong} && !$volumes{count}->{vg} && !$volumes{count}->{pv}){
        @out=`virsh list --all`;
        my $count=0;
        if(!grep(/^\s+[\d\-]+\s+\S+/, @out)){
            # Theres no VM's so move on to checking disk for known raw state
            @out=`fdisk -l /dev/sd{b,c} 2>/dev/null`;
            foreach my $line (@out){
                $count++ if($line =~ /^Disk label type: gpt$/);
            }
            # 2 GPT disks, as expected
            if($count == 2){
                loggg("Setting up local storage.");
                my $str;
                $str .= `wipefs -a /dev/sd{b,c}`;
                $str .= `pvcreate /dev/sd{b,c}`;
                $str .= `vgcreate vg_array1 /dev/sdb`;
                $str .= `vgcreate vg_array2 /dev/sdc`;
                loggg($str) if($str);
                # Now we should have volumes, so re-build our volume struct
                get_volumes();
                check_volumes();
            } else {
                bail("Disks not as expected. Set up manually. Aborting.");
            }
        }
    }

    # Find what VG to put LV on
    $use_array = get_next_vg();
    bail("Could not find suitable VG for new LV.") if(!$use_array);
    bail("LV already exists") if(grep(/^lv_root_$vg_hostname$/,@{$volumes{map}->{vg}->{$use_array}->{lv}}));
    loggg("Creating LV lv_root_$vg_hostname on $use_array");
    @out=`lvcreate -L 2T -n lv_root_$vg_hostname $use_array`;
    $new{device}='/dev/'.$use_array.'/lv_root_'.$vg_hostname;
    bail ("Did not create LV properly.") if(!-e $new{device});

    # Find bridge to use
    @out=`brctl show`;
    foreach my $line (@out){
        $new{bridge}=$1 if($line =~ /^(\S+)\s+\S+\s+\S+\s+(eth\d+|team\S+)$/);
        last if($new{bridge});
    }
    bail("Could not find bridge.") if(!$new{bridge});

    # Change disk:
    my ($disk_source) = $dom->findnodes('/domain/devices/disk/source');
    $disk_source->setAttribute('dev',$new{device});  

    # Remove metadata
    $eigmig->unbindNode() if($eigmig);

    # Remove seclabel
    my ($seclabel) = $dom->findnodes('/domain/seclabel');
    $seclabel->unbindNode() if($seclabel);

    # Change interface:
    my ($interface) = $dom->findnodes('/domain/devices/interface/source');
    $interface->setAttribute('bridge',$new{bridge}) if($interface);

    loggg("##### Modified XML:");
    loggg($dom->toString(0));
    print($dom->toString(0));
    bail("Bailing due to configuration") if($conf{fail});
}


########### Fix CPU post run ##############
# This is in the hook script because of similar code, but this section is not run by QEMU as a hook, rather
# it is called from the source server upon suspected completion of the migration.
# Here we reconfigure the guest CPU to get our performance back
if($hostname && scalar(@ARGV) >= 2 && $ARGV[1] eq 'post'){
    check_memory();
    get_volumes();

    my $originalxml = `virsh dumpxml $hostname`;

    loggg("##### Post run to fix CPU");
    my $dom = XML::LibXML->load_xml(string => $originalxml);

    # Reset CPU model
    my ($cpu_old) = $dom->findnodes('/domain/cpu');
    $cpu_old->unbindNode() if($cpu_old);
    my ($domain) = $dom->findnodes('/domain');
    $domain->addNewChild(undef,'cpu');
    my ($cpu_new) = $dom->findnodes('/domain/cpu');
    $cpu_new->setAttribute('mode','host-passthrough');
    $cpu_new->addNewChild(undef,'model');
    my ($cpu_model) = $dom->findnodes('/domain/cpu/model');
    $cpu_model->setAttribute('fallback','allow');

    # Set vCPU's:
    my ($vcpu_old) = $dom->findnodes('/domain/vcpu');
    $vcpu_old->unbindNode() if($vcpu_old);
    $domain->addNewChild(undef,'vcpu');
    my ($vcpu_new) = $dom->findnodes('/domain/vcpu');
    $vcpu_new->setAttribute('placement','static');
    $vcpu_new->setAttribute('cpuset',$conf{guest}->{$volumes{count}->{lv}}->{cpuset});
    $vcpu_new->appendTextNode($conf{vcpus});

    # Numatune
    my ($numa_old) = $dom->findnodes('/domain/numatune');
    $numa_old->unbindNode() if($numa_old);
    $domain->addNewChild(undef,'numatune');
    my ($numa_new) = $dom->findnodes('/domain/numatune');
    $numa_new->addNewChild(undef,'memory');
    my ($numa_memory) = $dom->findnodes('/domain/numatune/memory');
    $numa_memory->setAttribute('mode','preferred');
    $numa_memory->setAttribute('nodeset',$conf{guest}->{$volumes{count}->{lv}}->{numatune});

    # Change memory
    my ($memory_old) = $dom->findnodes('/domain/memory');
    my ($curmem_old) = $dom->findnodes('/domain/currentMemory');
    $memory_old->unbindNode() if($memory_old);
    $curmem_old->unbindNode() if($curmem_old);
    $domain->addNewChild(undef,'memory');
    $domain->addNewChild(undef,'currentMemory');
    my ($memory_new) = $dom->findnodes('/domain/memory');
    my ($curmem_new) = $dom->findnodes('/domain/currentMemory');
    $memory_new->setAttribute('unit','KiB');
    $curmem_new->setAttribute('unit','KiB');
    $memory_new->appendTextNode($conf{memory_kib});
    $curmem_new->appendTextNode($conf{memory_kib});

    # Write out tmp xml
    open(my $wewlad, '>', "/tmp/${hostname}_cpufix.xml");
    binmode $wewlad;
    $dom->toFH($wewlad);
    close($wewlad);

    # Restart guest into new configuration
    print("Shutting down...");
    my $lastthing = virsh_shutdown();
    $lastthing = virsh_shutdown("virsh destroy --graceful ${hostname}") if(defined($ARGV[2]) && $ARGV[2] eq 'destroy' && !$lastthing);
    bail("Could not shutdown $hostname in time") if(!$lastthing);
    print("\nUndefine ${hostname}.\n");
    system("virsh undefine ${hostname} >/dev/null 2>&1");
    print("Define /tmp/${hostname}_cpufix.xml\n");
    system("virsh define /tmp/${hostname}_cpufix.xml >/dev/null 2>&1");
    print("Starting ${hostname}.\n");
    $lastthing = virsh_start();
    bail("Could not start $hostname in time. Check \"/tmp/${hostname}_cpufix.xml\" and try to start manually.") if(!$lastthing);
} 

# Sub to wait for shutdown to complete
sub virsh_shutdown{
    bail("I dont know hostname") if(!$hostname);
    my $command=shift;
    $command="virsh shutdown ${hostname}" if(!$command);
    loggg("command=\"$command\"");
    system($command.' >/dev/null 2>&1');
    my $counter = 0;
    while(++$counter <= $conf{timers}->{virsh}->{shutdown}->{counter} && virsh_state($hostname) != 5){
        print('.');
        sleep($conf{timers}->{virsh}->{shutdown}->{sleep});
    }
    return(1) if(virsh_state($hostname) == 5);
    return(0)
}

# Sub to wait for start to complete
sub virsh_start{
    bail("I dont know hostname") if(!$hostname);
    my $command="virsh start ${hostname}";
    system($command.' >/dev/null 2>&1');
    my $counter = 0;
    while(++$counter <= $conf{timers}->{virsh}->{start}->{counter} && virsh_state($hostname) != 1){
        print('.');
        sleep($conf{timers}->{virsh}->{start}->{sleep});
    }
    return(1) if(virsh_state($hostname) == 1);
    return(0)
}

# Return the state of a guest
sub virsh_state{
    my $servername=shift;
    my $state='none';
    bail("I dont know hostname") if(!$servername);
    my @output=`virsh list --all 2>/dev/null`;
    if("@output" =~ /\s*[\d\-]+\s+${servername}\s+\b(.*)\b/){
        $state=$1 if($1);
        return(1) if($state eq 'running');
        return(2) if($state eq 'idle');
        return(3) if($state eq 'paused');
        return(4) if($state eq 'shutdown' || $state eq 'in shutdown');
        return(5) if($state eq 'shut off' || $state eq 'shutoff');
        return(6) if($state eq 'crashed');
        return(7) if($state eq 'dying');
        return(8) if($state eq 'pmsuspended');
    }
    return(0);
}

# Sub to build a struct to map LVM layout
sub get_volumes{
    $volumes{wrong}=0;
    $volumes{count}->{pv}=0;
    $volumes{count}->{lv}=0;    
    $volumes{count}->{vg}=0;    

    my @output = `vgs --noheadings`;
    shift(@output) if(@output && $output[0]=~/^\s+VG\s+#PV\s+#LV\s+#SN\s+/);
    if(scalar(@output)>0){
        foreach my $line (@output){
            if($line=~/\s+(\S+)\s+/){
                my $vg=$1;
                $volumes{wrong}++ if($vg!~/^vg_array[1-$conf{max}->{vg}]$/);
                $volumes{map}->{vg}->{$vg}=();
                $volumes{map}->{vg}->{$vg}->{count}->{lv}=0;
                $volumes{count}->{vg}++;
            }
        }
    }

    @output = `pvs --noheadings`;
    shift(@output) if(@output && $output[0]=~/^\s+PV\s+VG\s+/);
    if(scalar(@output)>0){
        foreach my $line (@output){
            if($line=~/\s+(\S+)\s+(\S+)\s+lvm\S+/){
                my $pv=$1;
                my $vg=$2;
                $volumes{wrong}++ if($pv!~/^\/dev\/sd[bc]$/);
                push(@{$volumes{map}->{vg}->{$vg}->{pv}},$pv);
                $volumes{map}->{vg}->{$vg}->{count}->{pv}++;
                $volumes{count}->{pv}++;
            }
        }
    }

    @output = `lvs --noheadings`;
    shift(@output) if(@output && $output[0]=~/^\s+LV\s+VG\s+/);
    if(scalar(@output)>0){
        foreach my $line (@output){
            if($line=~/\s+(\S+)\s+(\S+)\s+/){
                my $lv=$1;
                my $vg=$2;
                $volumes{wrong}++ if($lv!~/^lv_root_\S+/);
                push(@{$volumes{map}->{vg}->{$vg}->{lv}},$lv);
                $volumes{map}->{vg}->{$vg}->{count}->{lv}++;
                $volumes{wrong}++ if($volumes{map}->{vg}->{$vg}->{count}->{lv}>$conf{max}->{lv_per_vg});
                $volumes{count}->{lv}++;
            }
        }
    }
}

# Make sure memory is enough
sub check_memory{
    open(my $mems, "<", "/proc/meminfo");
    while(my $line = <$mems>){
        if($line =~ /^MemTotal:\s+(\d+)\s+kB/){
            bail("Memory not as expected") if($1<$conf{node_mem_limit});
        }
    }
    close($mems);
}

# Sub to bail out
sub bail{
    my $msg=shift;
    loggg($msg);
    die($msg);
}

# Sub to find out what next VG to put LV on should be
sub get_next_vg{
    check_volumes();
    foreach my $vgarray (sort(keys %{$volumes{map}->{vg}})){
        return($vgarray) if($volumes{map}->{vg}->{$vgarray}->{count}->{lv}<$conf{max}->{lv_per_vg});
    }
}

# Do the logging stuff
sub loggg{
    my $msg=shift;
    $log->info($msg) if($msg && $conf{logging}->{enabled});
}

# Some sanity checks
sub check_volumes{
    bail("LVM doesnt appear as expected.") if($volumes{wrong});
    bail("Volume groups not as expected.") if($volumes{count}->{vg} != $conf{max}->{vg});
    bail("No room for new volume.") if($volumes{count}->{lv} >= ($conf{max}->{vg}*$conf{max}->{lv_per_vg}));
} 
