Alternc  3.2
Alternc logiel libre pour l'h├ębergement
 All Data Structures Namespaces Files Functions Variables Pages
m_lxc Class Reference

Manage AlternC's virtual machine start/stop using our own inetd-based protocol. More...

+ Inheritance diagram for m_lxc:

Public Member Functions

 m_lxc ()
 Constructor, initialize the class informations from AlternC's variables.
 hook_menu ()
 HOOK: add the "Console Access" to AlternC's main menu.
 hook_admin_del_member ()
 HOOK: remove VM history for AlternC account.
 start ($login=FALSE, $pass=FALSE, $uid=FALSE)
 START a Virtual Machine on the remote VM manager for user $login having hashed password $pass and uid $uid.
 getvm ($login=FALSE)
 stop ()
 Stop the currently running VM.
- Public Member Functions inherited from vm
 start ()

Data Fields

 $IP
 $PORT
 $TIMEOUT = 5
 $error = array()

Private Member Functions

 sendMessage ($params)
 Send a message to a remote VM manager instance $params are the parameters to send as serialized data to the listening server.

Detailed Description

Manage AlternC's virtual machine start/stop using our own inetd-based protocol.

Definition at line 33 of file m_lxc.php.

Member Function Documentation

getvm (   $login = FALSE)

Definition at line 169 of file m_lxc.php.

References $cuid, $db, $mem, $res, and sendMessage().

Referenced by start(), and stop().

{
global $db, $mem, $cuid;
$login = $login ? $login : $mem->user['login'];
$msgg = array('action'=>'get', 'login'=>$login);
$res = $this->sendMessage($msgg);
if (!$res) return FALSE;
return unserialize($res);
}
hook_admin_del_member ( )

HOOK: remove VM history for AlternC account.

Definition at line 73 of file m_lxc.php.

References $cuid, $db, and $err.

{
global $db,$err,$cuid;
$err->log("lxc","alternc_del_member");
$db->query("DELETE FROM vm_history WHERE uid='$cuid'");
return true;
}
hook_menu ( )

HOOK: add the "Console Access" to AlternC's main menu.

Definition at line 56 of file m_lxc.php.

{
if ( empty($this->IP)) return ; // No menu if no server
$obj = array(
'title' => _("Console access"),
'ico' => 'images/ssh.png',
'link' => 'vm.php',
'pos' => 95,
) ;
return $obj;
}
m_lxc ( )

Constructor, initialize the class informations from AlternC's variables.

Definition at line 45 of file m_lxc.php.

References variable_get().

{
$this->IP = variable_get('lxc_ip', '', "IP address of the Alternc's LXC server. If empty, no LXC server.", array(array('desc'=>'IP address','type'=>'ip')));
$this->PORT = variable_get('lxc_port', '6504', "Port of the Alternc's LXC server", array(array('desc'=>'Port','type'=>'integer')));
$this->KEY = variable_get('lxc_key', '', "Shared key with the Alternc's LXC server", array(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(array('desc'=>'Max time','type'=>'integer')));
}
sendMessage (   $params)
private

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.

Definition at line 88 of file m_lxc.php.

References $hooks, $p, and isset.

Referenced by getvm(), start(), and stop().

{
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;
}
$msg = serialize($params);
if (fwrite ($fp, $msg."\n") < 0) {
$this->error[] = 'Unable to send data';
return FALSE;
}
$resp = '';
$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;
}
}
start (   $login = FALSE,
  $pass = FALSE,
  $uid = FALSE 
)

START a Virtual Machine on the remote VM manager for user $login having hashed password $pass and uid $uid.

Definition at line 129 of file m_lxc.php.

References $db, $err, $error, $mem, $res, $uid, getvm(), and sendMessage().

{
global $mem, $db, $err, $mysql;
if ($this->getvm() !== FALSE) {
$err->raise('lxc', _('VM already started'));
return FALSE;
}
unset($this->error);
$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);
if ($res === FALSE) {
return $this->error;
} else {
$data = unserialize($res);
$error = $data['error'];
$hostname = $data['hostname'];
$msg = $data['msg'];
$date_start = 'NOW()';
$uid = $mem->user['uid'];
if ((int)$data['error'] != 0) {
$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;
}
}
stop ( )

Stop the currently running VM.

Implements vm.

Definition at line 183 of file m_lxc.php.

References $db, $mem, getvm(), and sendMessage().

{
global $db, $mem;
$vm = $this->getvm();
if ($vm === FALSE)
return FALSE;
if ($this->sendMessage(array('action' => 'stop', 'vm' => $vm['vm'])) === FALSE)
return FALSE;
return TRUE;
}

Field Documentation

$error = array()

Definition at line 39 of file m_lxc.php.

Referenced by start().

$IP

Definition at line 36 of file m_lxc.php.

$PORT

Definition at line 37 of file m_lxc.php.

$TIMEOUT = 5

Definition at line 38 of file m_lxc.php.


The documentation for this class was generated from the following file: