2013-04-12 21:52:27 +00:00
< ? php
2015-09-25 15:42:00 +00:00
/*
----------------------------------------------------------------------
LICENSE
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License ( GPL )
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
To read the license please visit http :// www . gnu . org / copyleft / gpl . html
----------------------------------------------------------------------
2017-10-06 21:42:39 +00:00
*/
2013-09-17 17:36:58 +00:00
2013-04-21 09:53:34 +00:00
include_once ( dirname ( __FILE__ ) . '/vm.class.php' );
2013-04-12 21:52:27 +00:00
2013-09-17 17:36:58 +00:00
/**
2017-10-08 17:31:34 +00:00
* Manage AlternC ' s virtual machine ( Containers ) start / stop using our own inetd - based protocol .
2017-10-06 21:42:39 +00:00
*
2017-10-08 17:31:34 +00:00
* @ copyright AlternC - Team 2000 - 2017 https :// alternc . com /
2013-09-17 17:36:58 +00:00
*/
class m_lxc implements vm {
2015-09-25 15:42:00 +00:00
public $IP ;
public $KEY ;
public $PORT ;
public $maxtime ;
public $TIMEOUT = 5 ;
public $error = array ();
2017-10-06 21:42:39 +00:00
2015-09-25 15:42:00 +00:00
/**
* Constructor , initialize the class informations from AlternC ' s variables
*/
function m_lxc () {
$this -> IP = variable_get ( 'lxc_ip' , '' , " IP address of the Alternc's LXC server. If empty, no LXC server. " , array ( 'desc' => 'IP address' , 'type' => 'ip' ));
$this -> PORT = variable_get ( 'lxc_port' , '6504' , " Port of the Alternc's LXC server " , array ( 'desc' => 'Port' , 'type' => 'integer' ));
$this -> KEY = variable_get ( 'lxc_key' , '' , " Shared key with the Alternc's LXC server " , array ( 'desc' => 'Shared key' , 'type' => 'string' ));
$this -> maxtime = variable_get ( 'lxc_maxtime' , '4' , " How many hours do we allow to have a server before shutting it down " , array ( 'desc' => 'Max time' , 'type' => 'integer' ));
2013-10-17 14:56:31 +00:00
}
2013-04-22 13:37:48 +00:00
2017-10-06 21:42:39 +00:00
2015-09-25 15:42:00 +00:00
/**
* HOOK : add the " Console Access " to AlternC ' s main menu
*/
function hook_menu () {
if ( empty ( $this -> IP ))
return ; // No menu if no server
$obj = array (
'title' => _ ( " Console access " ),
'link' => 'vm.php' ,
'pos' => 95 ,
2017-10-06 21:42:39 +00:00
);
2015-09-25 15:42:00 +00:00
return $obj ;
2013-04-22 13:37:48 +00:00
}
2013-09-17 17:36:58 +00:00
2017-10-06 21:42:39 +00:00
2015-09-25 15:42:00 +00:00
/**
* HOOK : remove VM history for AlternC account
*/
function hook_admin_del_member () {
2017-08-17 01:32:18 +00:00
global $db , $msg , $cuid ;
$msg -> log ( " lxc " , " alternc_del_member " );
2016-05-17 16:57:01 +00:00
$db -> query ( " DELETE FROM vm_history WHERE uid= ? " , array ( $cuid ));
2015-09-25 15:42:00 +00:00
return true ;
2013-04-22 13:37:48 +00:00
}
2013-04-22 14:45:45 +00:00
2017-10-06 21:42:39 +00:00
2015-09-25 15:42:00 +00:00
/**
* Send a message to a remote VM manager instance
* $params are the parameters to send as serialized data
* to the listening server .
* Return the unserialized response data , if the message has been sent successfully
* or FALSE if an error occurred . In that case $error [] is set .
*/
private function sendMessage ( $params ) {
global $L_FQDN , $hooks ;
$fp = @ fsockopen ( $this -> IP , $this -> PORT , $errno , $errstr , $this -> TIMEOUT );
if ( ! $fp ) {
$this -> error [] = 'Unable to connect' ;
return FALSE ;
}
// Authenticate:
$params [ 'server' ] = $L_FQDN ;
$params [ 'key' ] = $this -> KEY ;
// MySQL Host for this user ?
$moreparams = $hooks -> invoke ( " lxc_params " , array ( $params ));
foreach ( $moreparams as $p ) {
foreach ( $p as $k => $v ) {
$params [ $k ] = $v ;
}
}
2018-10-16 21:04:46 +00:00
$message = serialize ( $params );
if ( fwrite ( $fp , $message . " \n " ) < 0 ) {
2015-09-25 15:42:00 +00:00
$this -> error [] = 'Unable to send data' ;
return FALSE ;
}
$resp = fgets ( $fp , 8192 );
fclose ( $fp );
$data = @ unserialize ( $resp );
if ( isset ( $data [ 'error' ]) && $data [ 'error' ] > 0 ) {
$this -> error [] = $data [ 'msg' ];
return FALSE ;
} else {
return $resp ;
}
}
2013-04-22 13:37:48 +00:00
2017-10-06 21:42:39 +00:00
2015-09-25 15:42:00 +00:00
/**
* START a Virtual Machine on the remote VM manager
* for user $login having hashed password $pass and uid $uid
*/
public function start ( $login = FALSE , $pass = FALSE , $uid = FALSE ) {
2017-08-17 01:32:18 +00:00
global $mem , $db , $msg , $mysql ;
2015-09-25 15:42:00 +00:00
if ( $this -> getvm () !== FALSE ) {
2017-10-06 16:04:36 +00:00
$msg -> raise ( " ERROR " , 'lxc' , _ ( 'VM already started' ));
2015-09-25 15:42:00 +00:00
return FALSE ;
}
unset ( $this -> error );
$login = $login ? $login : $mem -> user [ 'login' ];
$pass = $pass ? $pass : $mem -> user [ 'pass' ];
$uid = $uid ? $uid : $mem -> user [ 'uid' ];
2018-10-16 21:04:46 +00:00
$message = array ( 'action' => 'start' , 'login' => $login , 'pass' => $pass , 'uid' => $uid );
$message [ 'mysql_host' ] = $mysql -> dbus -> Host ;
2015-09-25 15:42:00 +00:00
2018-10-16 21:04:46 +00:00
$res = $this -> sendMessage ( $message );
2015-09-25 15:42:00 +00:00
if ( $res === FALSE ) {
return $this -> error ;
} else {
$data = unserialize ( $res );
$error = ( int ) $data [ 'error' ];
$hostname = $data [ 'hostname' ];
2018-10-16 21:04:46 +00:00
$message = $data [ 'msg' ];
2015-09-25 15:42:00 +00:00
$date_start = 'NOW()' ;
$uid = $mem -> user [ 'uid' ];
if ( $error != 0 ) {
2018-10-16 21:04:46 +00:00
$msg -> raise ( " ERROR " , 'lxc' , _ ( $message ));
2015-09-25 15:42:00 +00:00
return FALSE ;
}
2016-05-17 16:57:01 +00:00
$db -> query ( " INSERT INTO vm_history (ip,date_start,uid,serialized_object) VALUES (?, ?, ?, ?); " , array ( $hostname , $date_start , $uid , $res ));
2015-09-25 15:42:00 +00:00
return $res ;
}
}
2013-04-22 13:37:48 +00:00
2017-10-06 21:42:39 +00:00
2015-09-25 15:42:00 +00:00
/**
*
*/
public function getvm ( $login = FALSE ) {
global $mem ;
$login = $login ? $login : $mem -> user [ 'login' ];
2018-10-16 21:04:46 +00:00
$message = array ( 'action' => 'get' , 'login' => $login );
$res = $this -> sendMessage ( $message );
2015-09-25 15:42:00 +00:00
if ( ! $res ) {
return FALSE ;
}
return unserialize ( $res );
}
2013-09-17 17:36:58 +00:00
2017-10-06 21:42:39 +00:00
2015-09-25 15:42:00 +00:00
/**
* Stop the currently running VM
*/
public function stop () {
$vm = $this -> getvm ();
if ( $vm === FALSE ) {
return FALSE ;
}
if ( $this -> sendMessage ( array ( 'action' => 'stop' , 'vm' => $vm [ 'vm' ])) === FALSE ) {
return FALSE ;
}
return TRUE ;
}
2013-09-17 17:36:58 +00:00
2017-10-06 21:42:39 +00:00
} /* class m_lxc */
2013-09-17 17:36:58 +00:00