<?php

namespace App\Console\Commands;
use App\Virtual;
use Illuminate\Console\Command;
Use \Carbon\Carbon;
use App\Server;

class DeleteVirtuals extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'virtuals:delete';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Goes throught database and create virtuals';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */

    public function handle()
    {
        echo date('Y-m-d H:i:s') . 'Checking database for  virtuals to delete' . PHP_EOL; 
        $virtualsdelete = Virtual::where([
            ['ready', '=', '4'],
        ])->get();

        //Change for use of private keys
        //$usekey="-i $server->private";
        $usekey="";

        foreach($virtualsdelete as $virtual) {
        $server = Server::where('id', $virtual->server_id)->first();

        $tmp = exec("ping -n 1 -w 1 $server->ip");
        $name = $virtual->user_name . '_' . $virtual->system; 
            if(!strpos($tmp, "100% loss")){
                echo ("Server ".$server->ip." is online ". "trying to delete ".$name .PHP_EOL);
                if ($server->type=="HyperV") {
                    //HyperV delete
                    $delete=$server->scripts."\delete.ps1";
                    $tmp = exec("ssh $usekey $usekey -oNumberOfPasswordPrompts=0 administrator@$server->ip powershell.exe Test-Path $delete && echo Found || echo Not");
                    if ($tmp=="Not")
                    {
                        echo("Failed to connect to $server->ip");
                        continue;
                    }                    
                    $tmp = "powershell.exe ".$delete." ".$name;
                    $tmp = Shell_Exec("ssh $usekey administrator@$server->ip $tmp & exit");
                    echo($tmp);

                    if(!strpos($tmp, "not found")){
                            echo('Done');
                            Virtual::where('id', $virtual->id)->delete();
                        } 
                    } else {
                    //ESXi delete
                    $del=$server->scripts."/delete.sh";
                    $tmp = exec("ssh $usekey -oNumberOfPasswordPrompts=0 root@$server->ip [ -f $del ] && echo Found || echo Not");
                    if ($tmp=="Not")
                    {
                        echo("Failed to connect to $server->ip");
                        continue;
                    }
                    if ($tmp == "Found") {
                        $tmp=exec("ssh $usekey root@$server->ip $del $name $server->clones & exit");
                        if(!strpos($tmp, "not found")){                        
                            echo('Done');
                            Virtual::where('id', $virtual->id)->delete();
                        } else {
                            echo("Eroor when deleting $name".PHP_EOL);
                        }  
                    }
                }
            }
        }  
    }
}
