Alternc  latest
Alternc logiel libre pour l'hébergement
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

◆ getvm()

m_lxc::getvm (   $login = FALSE)

Definition at line 165 of file m_lxc.php.

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

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

Referenced by start(), and stop().

◆ hook_admin_del_member()

m_lxc::hook_admin_del_member ( )

HOOK: remove VM history for AlternC account.

Definition at line 69 of file m_lxc.php.

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

References $cuid, $db, and $msg.

◆ hook_menu()

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  'link' => 'vm.php',
59  'pos' => 95,
60  );
61 
62  return $obj;
63  }

◆ m_lxc()

m_lxc::m_lxc ( )

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

Definition at line 41 of file m_lxc.php.

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

References variable_get().

◆ sendMessage()

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 84 of file m_lxc.php.

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

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

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

◆ start() [1/2]

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 125 of file m_lxc.php.

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

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

◆ start() [2/2]

vm::start ( )
inherited

◆ stop()

m_lxc::stop ( )

Stop the currently running VM.

Implements vm.

Definition at line 181 of file m_lxc.php.

181  {
182  $vm = $this->getvm();
183  if ($vm === FALSE) {
184  return FALSE;
185  }
186  if ($this->sendMessage(array('action' => 'stop', 'vm' => $vm['vm'])) === FALSE) {
187  return FALSE;
188  }
189  return TRUE;
190  }

References getvm(), and sendMessage().

Member Data Documentation

◆ $error

m_lxc::$error = array()

Definition at line 35 of file m_lxc.php.

Referenced by start().

◆ $IP

m_lxc::$IP

Definition at line 30 of file m_lxc.php.

◆ $KEY

m_lxc::$KEY

Definition at line 31 of file m_lxc.php.

Referenced by sendMessage().

◆ $maxtime

m_lxc::$maxtime

Definition at line 33 of file m_lxc.php.

◆ $PORT

m_lxc::$PORT

Definition at line 32 of file m_lxc.php.

◆ $TIMEOUT

m_lxc::$TIMEOUT = 5

Definition at line 34 of file m_lxc.php.


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