#!/usr/bin/perl
use warnings;
use strict;

use File::Path;
use File::Slurp;
use Data::Dumper;
use Getopt::Long;
use Sys::Hostname;
use Net::OpenSSH;
use Socket;
use threads;
use YAML  qw (LoadFile Load Dump DumpFile);

use FindBin qw($Bin);
use lib "$Bin/../lib";
use lib "/opt/eig_linux/lib";
use BackupServer;

my ($userbox, $dest, $job, $job_file);
my $key = "/root/.ssh/backupkey";
my $source = hostname;
my $threshold = '.98';
my $wanted_resize_percent = '.83';
my $minimumresize = '.2';
my $resize = 0;
my $force = 0;
my $create = 0;
my $reset_failures = 0;
my $usage = 0;
my $cron = 0;

GetOptions(
    "userbox=s"         => \$userbox,
    "dest=s"            => \$dest,
    "force"             => \$force,
    "resize"            => \$resize,
    "create"            => \$create,
    "cron"            => \$cron,
    "minimum-resize=f"    => \$minimumresize,
    "reset-failures"    => \$reset_failures,
    "usage|help|h"      => \$usage,
) || usage("Invalid options given");

usage() if $usage;
die "Please run this script from a screen!" unless ($ENV{STY} || $cron);
usage("Please specify userbox") unless $userbox;

#setup or import from the job file
$job_file = "/var/spool/backup_migrations/$userbox";
if ( -f $job_file ){
    $job = LoadFile($job_file);
    $dest = $job->{'dest'};
    $resize = $job->{'resize'};
    $minimumresize = $job->{'minimumresize'};
} else {
    $job->{'hostname'} = $userbox;
    usage("Please specify dest")  unless $dest;
    $job->{'dest'} = $dest;
    $job->{'resize'} = $resize;
    $job->{'minimumresize'} = $minimumresize;
}
$job->{'status'} = 'queued';
$job->{'error'} = "";
$job->{'failures'} = 0 if (not defined $job->{'failures'} or $reset_failures == 1);
die "Refusing to run due to high amount of failures ($job->{'failures'} $reset_failures)\n" if $job->{'failures'} >= 5;

#get which partitions we need to migrate.
my $mounts;
my $bk = new BackupServer;
my $partitions = $bk->get_partitions();
foreach my $id (keys %$partitions){
    next unless ($partitions->{$id}->{'chostname'} eq $userbox);
    $mounts->{$partitions->{$id}->{'cmount'}} = $partitions->{$id};
    logger("Found the following mount: $partitions->{$id}->{'cmount'}",'INFO');
}
logger("No mounts found for $userbox",'FATAL') if (scalar keys %$mounts == 0);
if ($create){
    logger("Creating $userbox migration to $dest in backupmaster",'INFO');
    my $res = $bk->call_backupmaster("backupmigration?source=$source&cbox=$userbox&dest=$dest&action=create&resize=$resize&minimumresize=$minimumresize");
    logger("BM return: $res",'INFO');
    logger("Backupmaster failed to create server",'FATAL') unless  $res =~ /success/;
}
update_job(qw(status error));
make_pid();

#Setup ssh connection
my %opts = (
    'user' => 'root',
    'key_path' => $key,
    master_opts  =>
        [-o => 'StrictHostKeyChecking=no',
         -o => 'GSSAPIAuthentication=no',
         -o => 'ConnectTimeout=10']
        );
my $ssh;

#setup remote partitions if they need it.
if (!check_remote_mounts()) {
    #time to figure out some parition data.
    logger("Remote partitions are not set up yet. Setting them up.",'INFO');
    foreach my $mount (keys %$mounts){
        my $mountpoint = "/home/$userbox/$mounts->{$mount}->{'cmount'}";
        if (qx(/bin/mountpoint $mountpoint) =~ m/is a mountpoint/) {
            my (undef, undef, $used) = split /\s++/, qx(df -P $mountpoint | sed 1d);
            if ($resize){
                my $percent_adjusted_homesize = ($used/($wanted_resize_percent * 0.9321)/1024/1024/1024);
                my $minimum_size = $mounts->{$mount}->{'homesize'} * $minimumresize;
                $mounts->{$mount}->{'homesize'} = $minimum_size >= $percent_adjusted_homesize ? $minimum_size : $percent_adjusted_homesize;
                $job->{$mounts->{$mount}->{'cmount'}} = sprintf "%.03f", ($mounts->{$mount}->{'homesize'});
            } else {
                #Default to the size of the LV
                chomp(my $size =  qx(lvs /dev/backup/$mounts->{$mount}->{'plabel'} --units t --no-headings -o lvsize));
                if ($size =~ m/\s([0-9]+\.[0-9]+)t/){
                    $job->{$mounts->{$mount}->{'cmount'}} = $1;
                    $mounts->{$mount}->{'homesize'} = sprintf "%.03f", ($1 / 0.9555 );
                } else {
                    logger("Unable to find lv size of $mounts->{$mount}->{'plabel'}",'FATAL');
                }
            }
            logger("Wanted lv size of $mounts->{$mount}->{'cmount'} will be $mounts->{$mount}->{'homesize'}",'INFO');
        } else {
            logger("$mountpoint is not a mountpoint!",'FATAL');
        }
    }
    setup_remote_backup_migration_config('add',$mounts);
    my @actions =  ('/opt/eig_linux/bin/updatelocal',
            '/opt/eig_linux/bin/partitiondrives run',
            '/opt/eig_linux/bin/formatthemall run',
            '/opt/eig_linux/bin/setupexports run', );
    foreach my $action (@actions){ run_remote($action) };
}
setup_remote_backup_migration_config('del',$mounts);
logger("Remote mounts failed to setup!",'FATAL') if !check_remote_mounts();

#Time to run the rsyncs
logger("Rsyncs kicking off for $userbox -> $dest",'INFO');
$job->{'status'} = 'running';
update_job(qw(status));
my $rsync_tries = 0;
until  ( check_usage() && ! $force ){
    undef $ssh;
    logger("3 rsyncs and still not matching!\nPlease check manually",'FATAL') if ($rsync_tries >= 3);
    my @threads;
    for my $mount  (keys %$mounts) {
        my $mountpoint = "/home/$userbox/$mounts->{$mount}->{'cmount'}";
        my $rsync = qq(rsync -aHX --delete -e "ssh -o 'StrictHostKeyChecking=no' -o 'ConnectTimeout=10' -o 'GSSAPIAuthentication=no' -i $key"  $mountpoint/ $dest:$mountpoint/ );
        push @threads, async {
            my $start = time;
            logger("Starting rsync for $mounts->{$mount}->{'cmount'}\n$rsync",'INFO');
            system($rsync);
            my $elapsed = time - $start;
            logger("Finished rsync for $mount with rc $?\nElapsed Time: $elapsed",'INFO');
        };
    }
    $_->join() for @threads;
    sleep 300 if $rsync_tries >=1;
    $rsync_tries++;
    $force = 0;
}
logger("Rsyncs finished successfully",'INFO');

#This will send an api request off to backup master to flip.
logger("Flipping $userbox box to new backup server $dest in backupmaster",'INFO');
my $res = $bk->call_backupmaster("backupmigration?source=$source&cbox=$userbox&dest=$dest&action=finalize");
logger("BM return: $res",'INFO');
logger("Backupmaster failed to flip server",'FATAL') unless  $res =~ /success/;
run_remote('/opt/eig_linux/bin/updatelocal');
run_remote('puppet agent -t --tags firewall,sshd_eig',{'allow-errors' => 1});
$job->{'status'} = 'complete';
$job->{'pid'} = undef;
update_job();
mkdir "/var/log/backup_migrations/completed" unless -d "/var/log/backup_migrations/completed";
system("mv -v $job_file /var/log/backup_migrations/completed/");

#thar be subs
sub check_remote_mounts{
    for my $mount (keys %$mounts) {
        my $res = run_remote("/bin/mountpoint /home/$userbox/$mount",{'allow-errors' => 1});
        return 0 unless $res =~ m/is a mountpoint/;
    }
    return 1;
}
sub setup_remote_backup_migration_config{
    my $action = shift;
    my $data = shift;
    my $remote_raw  = run_remote('cat -v /opt/eig_linux/conf/backup_migrations.yaml',{'quiet' => 1});
    my $remote = Load($remote_raw);
    if ($action eq "add"){
        logger("Adding data to the remote partitions config",'INFO');
        for my $key (keys %$data){
            $remote->{$key} = $data->{$key};
        }
    } elsif ($action eq "del") {
        logger("Deleting data to the remote partitions config",'INFO');
        for my $key (keys %$data){
        if ($remote->{$key}->{'chostname'} eq $userbox) {
                delete $remote->{$key};
        }
        }
    }
    $remote_raw  = Dump($remote);
    run_remote("echo '$remote_raw'> /opt/eig_linux/conf/backup_migrations.yaml",{'quiet' => 1});
    run_remote('/opt/eig_linux/bin/updatelocal');
}
sub run_remote{
    my $action = shift;
    my $opts = shift;
    if (not defined $ssh){
        $ssh = Net::OpenSSH->new($dest, %opts);
        logger("Failed to connect ot ssh successfullly with error " . $ssh->error,'FATAL') if $ssh->error;
    }
    logger ("Running '$action' on $dest",'INFO') unless $opts->{'quiet'};
    my $res = $ssh->capture({timeout => 300}, $action);
    logger($res,'INFO') if $res && !$opts->{'quiet'};
    logger("$action failed to run successfullly with error " . $ssh->error,'FATAL') if $ssh->error && !$opts->{'allow-errors'};
    return $res;
}
sub logger {
    my $msg = shift;
    my $level = shift;
    mkpath('/var/log/backup_migrations/server_logs') unless ( -d '/var/log/backup_migrations/server_logs' );
    open(my $LOG, '>>', "/var/log/backup_migrations/server_logs/$userbox.log");
    if ($level =~ m/fatal/i){
        $job->{'error'} = $msg;
        $job->{'status'} = 'failed';
        $job->{'failures'}++;
        update_job(qw(status error));
        print $LOG localtime(time) . ":FATAL: $msg\n";
        die $msg;
    } else {
        print $LOG localtime(time) . ":$level: $msg\n";
        print localtime(time) . ": $msg\n";
    }
    close($LOG);
}
sub update_job {
    my @fields = @_;
    mkdir ('/var/spool/backup_migrations') unless ( -d '/var/spool/backup_migrations' );
    DumpFile($job_file,$job) or logger ("Unable to dump to $job_file",'FATAL');
    if (@fields){
        my $vars = "";
        for my $field (@fields){
            my $encoded_field = $job->{$field};
            $encoded_field  =~ s/([^^A-Za-z0-9\-_.!~*'()])/ sprintf "%%%02x", ord $1 /eg;
            $vars .= "&$field=$encoded_field";
        }

    logger("backupmigration?source=$source&cbox=$userbox&dest=$dest&action=update$vars",'INFO');
    my $res = $bk->call_backupmaster("backupmigration?source=$source&cbox=$userbox&dest=$dest&action=update$vars");
    logger("BM return: $res",'INFO');
    logger("Backupmaster failed to update  server",'ERROR') && die  unless  $res =~ /success/;
    }
}
sub check_usage {
    my $rc = 1;
    for my $mount (keys %$mounts){
        my $mountpoint = "/home/$userbox/$mounts->{$mount}->{'cmount'}";
        my $sourceusage = (split (/\s++/, qx(df -P $mountpoint | tail -1)))[2];
        my $destusage = (split (/\s++/, run_remote("df -P $mountpoint | tail -1")))[2];
        my $percent = $destusage/$sourceusage;
        my $msg = sprintf "%s at %.02f of source\ $sourceusage/$destusage", $mounts->{$mount}->{'cmount'}, $percent;
        logger($msg,'INFO');
        $rc = 0 if ($percent <= $threshold);
    }
    return $rc;
}
sub usage {
    my $msg = shift // "";
    my $help  = q(
Usage:
        --userbox=USERBOX       The customer userbox we are migration. Required.
        --dest=DESTINATION      Destination backup server to migrate the userbox to. Required if no job is already setup
        --resize                Flag for whether or not to reize the partitions to 83% usage
        --create                Creates the job and submits a request to backup master to create the migratoin.
        --minimum-resize=.2     Decimal percent for the minimum we will resize a drive to in relation to the homesize of the userbox.
                                This is .2 (20%) by default.  Boxes that are not expecting growth can resize more aggressively then those that are not.
        --force                 If this is set we will finalize the migration regardles of the QA check after one rsync
        --reset-failures        Resets the failure count in the job
        --help                  Prints this message.

);
    print "$help" . "$msg\n";
    exit;
}
sub make_pid {
    my $alive;
    if ($job->{'pid'}) {
        $alive = (kill 0, $job->{'pid'});
        logger("$userbox has an active migraiton as pid $job->{'pid'}\n",1) if ( $alive );
    }
    $job->{'pid'} = $$;
    update_job();
}
