Alternc  latest
Alternc logiel libre pour l'h├ębergement
 All Classes Namespaces Files Functions Variables Pages
m_lxc Class Reference

Manage AlternC's virtual machine (Containers) 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. More...
 
 hook_menu ()
 HOOK: add the "Console Access" to AlternC's main menu. More...
 
 hook_admin_del_member ()
 HOOK: remove VM history for AlternC account. More...
 
 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. More...
 
 getvm ($login=FALSE)
 
 stop ()
 Stop the currently running VM. More...
 
 start ()
 

Public Attributes

 $IP
 
 $KEY
 
 $PORT
 
 $maxtime
 
 $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. More...
 

Detailed Description

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

Definition at line 28 of file m_lxc.php.

Member Function Documentation

m_lxc::getvm (   $login = FALSE)

Definition at line 166 of file m_lxc.php.

References $login, $mem, $res, and sendMessage().

Referenced by start(), and stop().

166  {
167  global $mem;
168 
169  $login = $login ? $login : $mem->user['login'];
170  $msgg = array('action' => 'get', 'login' => $login);
171  $res = $this->sendMessage($msgg);
172  if (!$res) {
173  return FALSE;
174  }
175  return unserialize($res);
176  }
$login
$res
Definition: index.php:125
sendMessage($params)
Send a message to a remote VM manager instance $params are the parameters to send as serialized data ...
Definition: m_lxc.php:85
$mem
Definition: bootstrap.php:71
m_lxc::hook_admin_del_member ( )

HOOK: remove VM history for AlternC account.

Definition at line 70 of file m_lxc.php.

References $cuid, $db, and $msg.

70  {
71  global $db, $msg, $cuid;
72  $msg->log("lxc", "alternc_del_member");
73  $db->query("DELETE FROM vm_history WHERE uid= ?", array($cuid));
74  return true;
75  }
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_lxc::hook_menu ( )

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

Definition at line 52 of file m_lxc.php.

52  {
53  if (empty($this->IP))
54  return; // No menu if no server
55 
56  $obj = array(
57  'title' => _("Console access"),
58  'ico' => 'images/ssh.png',
59  'link' => 'vm.php',
60  'pos' => 95,
61  );
62 
63  return $obj;
64  }
m_lxc::m_lxc ( )

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

Definition at line 41 of file m_lxc.php.

References variable_get().

41  {
42  $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'));
43  $this->PORT = variable_get('lxc_port', '6504', "Port of the Alternc's LXC server", array('desc' => 'Port', 'type' => 'integer'));
44  $this->KEY = variable_get('lxc_key', '', "Shared key with the Alternc's LXC server", array('desc' => 'Shared key', 'type' => 'string'));
45  $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'));
46  }
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
m_lxc::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 85 of file m_lxc.php.

References $errstr, $hooks, $KEY, $msg, and $p.

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

85  {
86  global $L_FQDN, $hooks;
87  $fp = @fsockopen($this->IP, $this->PORT, $errno, $errstr, $this->TIMEOUT);
88  if (!$fp) {
89  $this->error[] = 'Unable to connect';
90  return FALSE;
91  }
92  // Authenticate:
93  $params['server'] = $L_FQDN;
94  $params['key'] = $this->KEY;
95  // MySQL Host for this user ?
96  $moreparams = $hooks->invoke("lxc_params", array($params));
97  foreach ($moreparams as $p) {
98  foreach ($p as $k => $v) {
99  $params[$k] = $v;
100  }
101  }
102 
103  $msg = serialize($params);
104  if (fwrite($fp, $msg . "\n") < 0) {
105  $this->error[] = 'Unable to send data';
106  return FALSE;
107  }
108  $resp = fgets($fp, 8192);
109  fclose($fp);
110 
111  $data = @unserialize($resp);
112 
113  if (isset($data['error']) && $data['error'] > 0) {
114  $this->error[] = $data['msg'];
115  return FALSE;
116  } else {
117  return $resp;
118  }
119  }
$hooks
Definition: bootstrap.php:74
$msg
Definition: config.php:155
$p
Definition: bro_editor.php:46
$KEY
Definition: m_lxc.php:31
$errstr
Definition: change.php:62
vm::start ( )
inherited
m_lxc::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 126 of file m_lxc.php.

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

126  {
127  global $mem, $db, $msg, $mysql;
128 
129  if ($this->getvm() !== FALSE) {
130  $msg->raise("ERROR", 'lxc', _('VM already started'));
131  return FALSE;
132  }
133  unset($this->error);
134 
135  $login = $login ? $login : $mem->user['login'];
136  $pass = $pass ? $pass : $mem->user['pass'];
137  $uid = $uid ? $uid : $mem->user['uid'];
138 
139  $msgg = array('action' => 'start', 'login' => $login, 'pass' => $pass, 'uid' => $uid);
140  $msgg['mysql_host'] = $mysql->dbus->Host;
141 
142  $res = $this->sendMessage($msgg);
143  if ($res === FALSE) {
144  return $this->error;
145  } else {
146  $data = unserialize($res);
147  $error = (int) $data['error'];
148  $hostname = $data['hostname'];
149  $msg = $data['msg'];
150  $date_start = 'NOW()';
151  $uid = $mem->user['uid'];
152 
153  if ($error != 0) {
154  $msg->raise("ERROR", 'lxc', _($msg));
155  return FALSE;
156  }
157  $db->query("INSERT INTO vm_history (ip,date_start,uid,serialized_object) VALUES (?, ?, ?, ?);", array($hostname, $date_start, $uid, $res));
158  return $res;
159  }
160  }
$uid
getvm($login=FALSE)
Definition: m_lxc.php:166
$login
global $db
Definition: bootstrap.php:22
$error
Definition: m_lxc.php:35
$res
Definition: index.php:125
$msg
Definition: config.php:155
sendMessage($params)
Send a message to a remote VM manager instance $params are the parameters to send as serialized data ...
Definition: m_lxc.php:85
$mem
Definition: bootstrap.php:71
m_lxc::stop ( )

Stop the currently running VM.

Implements vm.

Definition at line 182 of file m_lxc.php.

References getvm(), and sendMessage().

182  {
183  $vm = $this->getvm();
184  if ($vm === FALSE) {
185  return FALSE;
186  }
187  if ($this->sendMessage(array('action' => 'stop', 'vm' => $vm['vm'])) === FALSE) {
188  return FALSE;
189  }
190  return TRUE;
191  }
getvm($login=FALSE)
Definition: m_lxc.php:166
sendMessage($params)
Send a message to a remote VM manager instance $params are the parameters to send as serialized data ...
Definition: m_lxc.php:85

Member Data Documentation

m_lxc::$error = array()

Definition at line 35 of file m_lxc.php.

Referenced by start().

m_lxc::$IP

Definition at line 30 of file m_lxc.php.

m_lxc::$KEY

Definition at line 31 of file m_lxc.php.

Referenced by sendMessage().

m_lxc::$maxtime

Definition at line 33 of file m_lxc.php.

m_lxc::$PORT

Definition at line 32 of file m_lxc.php.

m_lxc::$TIMEOUT = 5

Definition at line 34 of file m_lxc.php.


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