<commit_before><?php
/*
KVM-VDI
<NAME>
2016-10-24
Vilnius, Lithuania.
*/
function SQL_connect(){
    include (dirname(__FILE__).'/config.php');
    $mysql_connection=mysqli_connect($mysql_host,$mysql_user,$mysql_pass);
    mysqli_select_db($mysql_connection, $mysql_db);
    return $mysql_connection;
}
function add_SQL_line($sql_line){
    $mysql_connection=SQL_connect();
    mysqli_query($mysql_connection, $sql_line) or die (mysqli_error($mysql_connection));
    mysqli_close($mysql_connection);
    return 0;
}
//##############################################################################
function get_SQL_line($sql_line){
    $mysql_connection=SQL_connect();
    $result = mysqli_fetch_row(mysqli_query($mysql_connection, $sql_line));
    mysqli_close($mysql_connection);
    return $result;
}
//##############################################################################
function get_SQL_array($sql_line){
    $query_array=array();
    $mysql_connection=SQL_connect();
    $q_string = mysqli_query($mysql_connection, $sql_line)or die (mysqli_error($mysql_connection));
    while ($row=mysqli_fetch_array($q_string)){
        $query_array[]=$row;
    }
    mysqli_close($mysql_connection);
    return $query_array;
}

function ssh_connect($address){
    include ('config.php');
    $tmp = explode(":", $address);
    $ip=$tmp[0];
    $port=(int)$tmp[1];
    global $connection;
    $connection = ssh2_connect($ip, $port, array('hostkey'=>'ssh-rsa'));
    if (!$connection)
	return 'BAD_SSH_ADDRESS';
    if (!ssh2_auth_pubkey_file($connection, $ssh_user, $ssh_key_path.'id_rsa.pub',$ssh_key_path.'id_rsa'))
	return 'BAD_SSH_CREDENTIALS';

}
function ssh_command($command,$blocking){
    global $connection;
    write_log("Executing: " . preg_replace('%\\"password.*?,%i', '"password\":\"*****\",',$command));
    $reply = ssh2_exec($connection,$command);
    $errorReply = ssh2_fetch_stream($reply, SSH2_STREAM_STDERR);
    stream_set_blocking($reply, $blocking);
    stream_set_blocking($errorReply, $blocking);
    $output= stream_get_contents($reply);
    $error=stream_get_contents($errorReply);
    if (!empty($output))
        return $output;
    if (!empty($error)){
        $output=$error . $output;
        return $output;
    }
}
//#############################################################################
function reload_vm_info(){
    $x=0;
    $sql_reply=get_SQL_array("SELECT * FROM hypervisors WHERE maintenance=0");
    while ($x<sizeof($sql_reply)){
	$ip=$sql_reply[$x]['ip'];
	$port=$sql_reply[$x]['port'];
	$hyper_id=$sql_reply[$x]['id'];
    	$reply=ssh_connect($ip . ":" . $port);
	if (!$reply){//if connection is successfull
        $output=ssh_command("sudo virsh list --all |tail -n +3|head -n -1|awk '{print $2" . '" "' . "$3}'",true);
        $vms_in_db=get_SQL_array("SELECT * FROM vms WHERE hypervisor='$hyper_id'");
        $vms=array();
        $PlainVMS=array();
        $output=str_replace("\n"," ",$output);
	    $vms=explode(" ",$output);
	    $y=0;
	    while ($vms[$y]){
            $PlainVMS[]="'" . $vms[$y] . "'";
    		$vms_reply=get_SQL_line("SELECT id,maintenance FROM vms WHERE name='$vms[$y]' AND hypervisor='$hyper_id'"); 
		if (!$vms_reply[1])
		    $maint="false";
		else
		    $maint=$vms_reply[1];
    	$state=$vms[$y+1];
        if (empty($vms_reply[0]))//New VM is found
            add_SQL_line("INSERT INTO  vms (name,hypervisor,state,maintenance) VALUES ('$vms[$y]','$hyper_id','$state','$maint')");
        else
            add_SQL_line("UPDATE vms SET name='$vms[$y]', hypervisor='$hyper_id', state='$state', maintenance='$maint' WHERE id='$vms_reply[0]'");
        $y=$y+2;
	    }

	}
	else
	    add_SQL_line("UPDATE hypervisors SET maintenance='1' WHERE id='$hyper_id'");
	++$x;
    }
    $PlainVMS = join(', ', $PlainVMS);
    if (!empty($PlainVMS))
    //remove all VMS, that do not exist on hypervisor, but still are in database
       $TrashVMS=add_SQL_line("DELETE FROM vms WHERE hypervisor='$hyper_id' AND name NOT IN ($PlainVMS)");
}
//##############################################################################
function check_session(){
    if (session_status() == PHP_SESSION_NONE) 
	session_start();
    if (isset($_SESSION['logged']))<commit_msg>Slightly remaking VM reload<commit_after><?php
/*
KVM-VDI
<NAME>
2016-10-27
Vilnius, Lithuania.
*/
function SQL_connect(){
    include (dirname(__FILE__).'/config.php');
    $mysql_connection=mysqli_connect($mysql_host,$mysql_user,$mysql_pass);
    mysqli_select_db($mysql_connection, $mysql_db);
    return $mysql_connection;
}
function add_SQL_line($sql_line){
    $mysql_connection=SQL_connect();
    mysqli_query($mysql_connection, $sql_line) or die (mysqli_error($mysql_connection));
    mysqli_close($mysql_connection);
    return 0;
}
//##############################################################################
function get_SQL_line($sql_line){
    $mysql_connection=SQL_connect();
    $result = mysqli_fetch_row(mysqli_query($mysql_connection, $sql_line));
    mysqli_close($mysql_connection);
    return $result;
}
//##############################################################################
function get_SQL_array($sql_line){
    $query_array=array();
    $mysql_connection=SQL_connect();
    $q_string = mysqli_query($mysql_connection, $sql_line)or die (mysqli_error($mysql_connection));
    while ($row=mysqli_fetch_array($q_string)){
        $query_array[]=$row;
    }
    mysqli_close($mysql_connection);
    return $query_array;
}

function ssh_connect($address){
    include ('config.php');
    $tmp = explode(":", $address);
    $ip=$tmp[0];
    $port=(int)$tmp[1];
    global $connection;
    $connection = ssh2_connect($ip, $port, array('hostkey'=>'ssh-rsa'));
    if (!$connection)
	return 'BAD_SSH_ADDRESS';
    if (!ssh2_auth_pubkey_file($connection, $ssh_user, $ssh_key_path.'id_rsa.pub',$ssh_key_path.'id_rsa'))
	return 'BAD_SSH_CREDENTIALS';

}
function ssh_command($command,$blocking){
    global $connection;
    write_log("Executing: " . preg_replace('%\\"password.*?,%i', '"password\":\"*****\",',$command));
    $reply = ssh2_exec($connection,$command);
    $errorReply = ssh2_fetch_stream($reply, SSH2_STREAM_STDERR);
    stream_set_blocking($reply, $blocking);
    stream_set_blocking($errorReply, $blocking);
    $output= stream_get_contents($reply);
    $error=stream_get_contents($errorReply);
    if (!empty($error))
        write_log($error);
    if (!empty($output))
        return $output;
    if (!empty($error)){
        $output=$error . $output;
        return $output;
    }
}
//#############################################################################
function reload_vm_info(){
    $x=0;
    $sql_reply=get_SQL_array("SELECT * FROM hypervisors WHERE maintenance=0");
    $error_return=0;
    while ($x<sizeof($sql_reply)){
        $ip=$sql_reply[$x]['ip'];
        $port=$sql_reply[$x]['port'];
        $hyper_id=$sql_reply[$x]['id'];
        $reply=ssh_connect($ip . ":" . $port);
        if (!$reply){//if connection is successfull
            $output=ssh_command("sudo virsh list --all |tail -n +3|head -n -1|awk '{print $2" . '" "' . "$3}'",true);
            $vms_in_db=get_SQL_array("SELECT * FROM vms WHERE hypervisor='$hyper_id'");
            $vms=array();
            $PlainVMS=array();
            $output=str_replace("\n"," ",$output);
            $vms=explode(" ",$output);
            $y=0;
            $error_reply=0;
            if (strpos($output, 'error:') !== false){
                $error_reply=1;
                write_log($output);
                $error_return=$error_return.'ERROR:'.$hyper_id.',';
            }
            while ($vms[$y]&&!$error_reply){
                $PlainVMS[]="'" . $vms[$y] . "'";
                $vms_reply=get_SQL_line("SELECT id,maintenance FROM vms WHERE name='$vms[$y]' AND hypervisor='$hyper_id'"); 
                if (!$vms_reply[1])
                    $maint="false";
                else
                    $maint=$vms_reply[1];
                $state=$vms[$y+1];
                if (empty($vms_reply[0]))//New VM is found
                    add_SQL_line("INSERT INTO  vms (name,hypervisor,state,maintenance) VALUES ('$vms[$y]','$hyper_id','$state','$maint')");
                else
                    add_SQL_line("UPDATE vms SET name='$vms[$y]', hypervisor='$hyper_id', state='$state', maintenance='$maint' WHERE id='$vms_reply[0]'");
                $y=$y+2;
            }
            $PlainVMS = join(', ', $PlainVMS);
            if (!empty($PlainVMS)&&!$error_reply)//remove all VMS, that do not exist on hypervisor, but still are in database
                $TrashVMS=add_SQL_line("DELETE FROM vms WHERE hypervisor='$hyper_id' AND name NOT IN ($PlainVMS)");
        }
        else//put hypervisor to maintenance if cannot connect to it
            add_SQL_line("UPDATE hypervisors SET maintenance='1' WHERE id='$hyper_id'");
        ++$x;
    }
    return $error_return;
}
//##############################################################################
function check_session(){
    if (session_status() == PHP_SESSION_NONE) 
	session_start();
    if (isset($_SESSION['logged']))
{'fraction_non_alphanumeric': 0.23348643919510062, 'fraction_numerical': 0.006889763779527559, 'mean_word_length': 4.0027352297593, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '202177', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2947, 'n_words': 490}