2013-04-12 21:52:27 +00:00
|
|
|
<?php
|
|
|
|
|
2013-04-21 09:53:34 +00:00
|
|
|
include_once(dirname(__FILE__) . '/vm.class.php');
|
2013-04-12 21:52:27 +00:00
|
|
|
# include('vm.php'); // This one fails ...
|
|
|
|
|
|
|
|
class m_lxc implements vm
|
|
|
|
{
|
2013-04-19 07:16:31 +00:00
|
|
|
public $IP;
|
|
|
|
public $PORT;
|
|
|
|
public $TIMEOUT = 5;
|
|
|
|
public $error = array();
|
|
|
|
|
|
|
|
function m_lxc() {
|
|
|
|
$this->IP = variable_get('lxc_ip', '', "IP address of the Alternc's LXC server. If empty, no LXC server.");
|
|
|
|
$this->PORT = variable_get('lxc_port', '6504', "Port of the Alternc's LXC server");
|
2013-04-21 09:48:05 +00:00
|
|
|
$this->KEY = variable_get('lxc_key', '', "Shared key with the Alternc's LXC server");
|
2013-04-19 07:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function hook_menu() {
|
|
|
|
if ( empty($this->IP)) return ; # No menu if no server
|
|
|
|
|
|
|
|
$obj = array(
|
2013-04-21 15:49:56 +00:00
|
|
|
'title' => _("Console access"),
|
2013-04-19 07:16:31 +00:00
|
|
|
'ico' => 'images/ssh.png',
|
|
|
|
'link' => 'vm.php',
|
|
|
|
'pos' => 95,
|
|
|
|
) ;
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2013-04-21 09:48:05 +00:00
|
|
|
function hook_admin_del_member() {
|
|
|
|
global $db,$err,$cuid;
|
|
|
|
$err->log("lxc","alternc_del_member");
|
|
|
|
$db->query("DELETE FROM vm_history WHERE uid='$cuid'");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-15 13:42:04 +00:00
|
|
|
|
2013-04-21 15:49:56 +00:00
|
|
|
private function sendMessage($action, $user = FALSE, $password=FALSE, $uid=FALSE)
|
2013-04-12 21:52:27 +00:00
|
|
|
{
|
2013-04-15 13:42:04 +00:00
|
|
|
$fp = fsockopen($this->IP, $this->PORT, $errno, $errstr, $this->TIMEOUT);
|
|
|
|
if (!$fp)
|
|
|
|
{
|
|
|
|
$this->error[] = 'Unable to connect';
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$msg = sprintf("%s|%s|%s|%d\n", $action, $user, $password, $uid);
|
|
|
|
if (fwrite ($fp, $msg) < 0)
|
|
|
|
{
|
|
|
|
$this->error[] = 'Unable to send data';
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-21 15:02:42 +00:00
|
|
|
$resp = '';
|
|
|
|
#while (($resp .= fgets($fp, 4096)) !== FALSE);
|
2013-04-15 13:42:04 +00:00
|
|
|
$resp = fgets($fp, 4096);
|
|
|
|
fclose ($fp);
|
2013-04-12 21:52:27 +00:00
|
|
|
|
2013-04-21 15:02:42 +00:00
|
|
|
|
|
|
|
return $resp;
|
|
|
|
|
|
|
|
if (stripos($resp, 'error') > 0)
|
2013-04-15 13:42:04 +00:00
|
|
|
{
|
2013-04-21 15:02:42 +00:00
|
|
|
$data = unserialize($resp);
|
|
|
|
$this->error[] = $data['msg'];
|
2013-04-15 13:42:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-21 15:02:42 +00:00
|
|
|
return $resp;
|
2013-04-15 13:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function start($login = FALSE, $pass = FALSE, $uid = FALSE)
|
|
|
|
{
|
2013-04-21 15:49:56 +00:00
|
|
|
|
|
|
|
global $mem, $db, $err;
|
|
|
|
|
|
|
|
if ($this->getvm() !== FALSE)
|
|
|
|
{
|
|
|
|
$err->raise('lxc', _('VM already started'));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-21 09:53:34 +00:00
|
|
|
|
|
|
|
$user = $login ? $login : $mem->user['login'];
|
|
|
|
$pass = $pass ? $pass : $mem->user['pass'];
|
2013-04-21 15:02:42 +00:00
|
|
|
$uid = $uid ? $uid : $mem->user['uid'];
|
2013-04-21 09:53:34 +00:00
|
|
|
|
|
|
|
$res = $this->sendMessage('start', $user, $pass, $uid);
|
2013-04-15 13:42:04 +00:00
|
|
|
if ($res === FALSE)
|
2013-04-22 07:20:49 +00:00
|
|
|
return $this->error;
|
2013-04-15 13:42:04 +00:00
|
|
|
else
|
2013-04-21 15:02:42 +00:00
|
|
|
{
|
|
|
|
$data = unserialize($res);
|
|
|
|
$error = $data['error'];
|
|
|
|
$hostname = $data['hostname'];
|
|
|
|
$msg = $data['msg'];
|
|
|
|
$date_start = 'NOW()';
|
|
|
|
$uid = $mem->user['uid'];
|
|
|
|
|
2013-04-21 15:49:56 +00:00
|
|
|
if ((int)$data['error'] != 0)
|
|
|
|
{
|
|
|
|
$err->raise('lxc', _($data['msg']));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-04-21 15:02:42 +00:00
|
|
|
$db->query("INSERT INTO vm_history (ip,date_start,uid,serialized_object) VALUES ('$hostname', $date_start, '$uid', '$res')");
|
|
|
|
|
2013-04-15 13:42:04 +00:00
|
|
|
return $res;
|
2013-04-21 15:02:42 +00:00
|
|
|
}
|
2013-04-12 21:52:27 +00:00
|
|
|
}
|
|
|
|
|
2013-04-21 09:53:34 +00:00
|
|
|
|
2013-04-12 21:52:27 +00:00
|
|
|
public function monit()
|
|
|
|
{
|
2013-04-21 15:02:42 +00:00
|
|
|
echo "1 / 5 used ";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getvm()
|
|
|
|
{
|
|
|
|
global $db, $mem;
|
|
|
|
|
|
|
|
$uid = $mem->user['uid'];
|
|
|
|
$res = array();
|
|
|
|
|
|
|
|
$res = $db->query("SELECT * FROM vm_history WHERE date_end IS NULL AND uid= '$uid' ORDER BY id DESC LIMIT 1");
|
|
|
|
|
|
|
|
if ($db->next_record())
|
|
|
|
{
|
|
|
|
$db->Record['serialized_object'] = unserialize($db->Record['serialized_object']);
|
|
|
|
return $db->Record;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
2013-04-12 21:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function stop()
|
|
|
|
{
|
2013-04-21 15:49:56 +00:00
|
|
|
global $db, $mem;
|
|
|
|
|
|
|
|
$vm = $this->getvm();
|
|
|
|
|
|
|
|
if ($vm === FALSE)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
$vm_id = $vm['serialized_object']['vm'];
|
|
|
|
$uid = $mem->user['uid'];
|
|
|
|
$vid = $vm['id'];
|
|
|
|
|
|
|
|
if ($this->sendMessage('stop', $vm_id, FALSE, FALSE) === FALSE)
|
|
|
|
return FALSE;
|
2013-04-12 21:52:27 +00:00
|
|
|
|
2013-04-21 15:49:56 +00:00
|
|
|
return $db->query("UPDATE vm_history SET date_end = NOW() WHERE uid = '$uid' AND id = '$vid' LIMIT 1");
|
2013-04-12 21:52:27 +00:00
|
|
|
}
|
|
|
|
}
|