2013-04-12 21:52:27 +00:00
|
|
|
<?php
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
/*
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
AlternC - Web Hosting System
|
|
|
|
Copyright (C) 2000-2013 by the AlternC Development Team.
|
|
|
|
https://alternc.org/
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
Purpose of file: Manage LXC-based virtual machine through an inetd-based protocol
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage AlternC's virtual machine start/stop using our own inetd-based protocol.
|
|
|
|
*/
|
|
|
|
class m_lxc implements vm {
|
|
|
|
|
|
|
|
|
2013-04-19 07:16:31 +00:00
|
|
|
public $IP;
|
|
|
|
public $PORT;
|
|
|
|
public $TIMEOUT = 5;
|
|
|
|
public $error = array();
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor, initialize the class informations from AlternC's variables
|
|
|
|
*/
|
2013-04-19 07:16:31 +00:00
|
|
|
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-22 14:45:45 +00:00
|
|
|
$this->maxtime = variable_get('lxc_maxtime', '4', "How many hours do we allow to have a server before shutting it down");
|
2013-04-19 07:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
/**
|
|
|
|
* HOOK: add the "Console Access" to AlternC's main menu
|
|
|
|
*/
|
2013-04-19 07:16:31 +00:00
|
|
|
function hook_menu() {
|
2013-09-17 17:36:58 +00:00
|
|
|
if ( empty($this->IP)) return ; // No menu if no server
|
2013-04-19 07:16:31 +00:00
|
|
|
|
|
|
|
$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-09-17 17:36:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HOOK: remove VM history for AlternC account
|
|
|
|
*/
|
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-09-17 17:36:58 +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.
|
|
|
|
*/
|
2013-04-22 13:37:48 +00:00
|
|
|
private function sendMessage($params) {
|
2013-09-17 17:36:58 +00:00
|
|
|
global $L_FQDN;
|
2013-04-22 13:37:48 +00:00
|
|
|
$fp = fsockopen($this->IP, $this->PORT, $errno, $errstr, $this->TIMEOUT);
|
2013-09-17 17:36:58 +00:00
|
|
|
if (!$fp) {
|
2013-04-22 13:37:48 +00:00
|
|
|
$this->error[] = 'Unable to connect';
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-09-17 17:36:58 +00:00
|
|
|
// Authenticate:
|
|
|
|
$params['server']=$L_FQDN;
|
|
|
|
$params['key']=$this->KEY;
|
2013-04-22 13:37:48 +00:00
|
|
|
|
|
|
|
$msg = sprintf("%s\n", serialize($params) );
|
2013-09-17 17:36:58 +00:00
|
|
|
if (fwrite ($fp, $msg) < 0) {
|
2013-04-22 13:37:48 +00:00
|
|
|
$this->error[] = 'Unable to send data';
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
$resp = '';
|
|
|
|
$resp = fgets($fp, 4096);
|
|
|
|
fclose ($fp);
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
$data = @unserialize($resp);
|
2013-04-22 13:37:48 +00:00
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
if (isset($data['error']) && $data['error']>0) {
|
2013-04-22 13:37:48 +00:00
|
|
|
$this->error[] = $data['msg'];
|
|
|
|
return FALSE;
|
2013-09-17 17:36:58 +00:00
|
|
|
} else {
|
2013-04-22 13:37:48 +00:00
|
|
|
return $resp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 17:36:58 +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) {
|
2013-04-22 13:37:48 +00:00
|
|
|
global $mem, $db, $err, $mysql;
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
if ($this->getvm() !== FALSE) {
|
2013-04-22 13:37:48 +00:00
|
|
|
$err->raise('lxc', _('VM already started'));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$login = $login ? $login : $mem->user['login'];
|
|
|
|
$pass = $pass ? $pass : $mem->user['pass'];
|
|
|
|
$uid = $uid ? $uid : $mem->user['uid'];
|
|
|
|
|
|
|
|
$msgg = array('action'=>'start', 'login'=>$login, 'pass' => $pass, 'uid'=> $uid);
|
|
|
|
$msgg['mysql_host'] = $mysql->dbus->Host;
|
|
|
|
|
|
|
|
$res = $this->sendMessage($msgg);
|
2013-09-17 17:36:58 +00:00
|
|
|
if ($res === FALSE) {
|
2013-04-22 13:37:48 +00:00
|
|
|
return $this->error;
|
2013-09-17 17:36:58 +00:00
|
|
|
} else {
|
2013-04-22 13:37:48 +00:00
|
|
|
$data = unserialize($res);
|
|
|
|
$error = $data['error'];
|
|
|
|
$hostname = $data['hostname'];
|
|
|
|
$msg = $data['msg'];
|
|
|
|
$date_start = 'NOW()';
|
|
|
|
$uid = $mem->user['uid'];
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
if ((int)$data['error'] != 0) {
|
2013-04-22 13:37:48 +00:00
|
|
|
$err->raise('lxc', _($data['msg']));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
$db->query("INSERT INTO vm_history (ip,date_start,uid,serialized_object) VALUES ('$hostname', $date_start, '$uid', '$res')");
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getvm($login = FALSE) {
|
2013-04-22 14:45:45 +00:00
|
|
|
global $db, $mem, $cuid;
|
2013-04-22 13:37:48 +00:00
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
$login = $login ? $login : $mem->user['login'];
|
|
|
|
$msgg = array('action'=>'get', 'login'=>$login);
|
|
|
|
$res = $this->sendMessage($msgg);
|
|
|
|
if (!$res) return FALSE;
|
|
|
|
return unserialize($res);
|
2013-04-22 14:45:45 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 13:37:48 +00:00
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
/**
|
|
|
|
* Stop the currently running VM
|
|
|
|
*/
|
|
|
|
public function stop() {
|
2013-04-22 13:37:48 +00:00
|
|
|
global $db, $mem;
|
|
|
|
$vm = $this->getvm();
|
|
|
|
if ($vm === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-09-17 17:36:58 +00:00
|
|
|
if ($this->sendMessage(array('action' => 'stop', 'vm' => $vm['vm'])) === FALSE)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
2013-04-22 13:37:48 +00:00
|
|
|
}
|
2013-09-17 17:36:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // class m_lxc
|