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 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 start/stop using our own inetd-based protocol.

Definition at line 32 of file m_lxc.php.

Member Function Documentation

m_lxc::getvm (   $login = FALSE)

Definition at line 164 of file m_lxc.php.

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

Referenced by start(), and stop().

164  {
165  global $mem;
166 
167  $login = $login ? $login : $mem->user['login'];
168  $msgg = array('action' => 'get', 'login' => $login);
169  $res = $this->sendMessage($msgg);
170  if (!$res) {
171  return FALSE;
172  }
173  return unserialize($res);
174  }
$res
Definition: index.php:127
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 71 of file m_lxc.php.

References $cuid, $db, and $err.

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

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

Definition at line 54 of file m_lxc.php.

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

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

Definition at line 44 of file m_lxc.php.

References variable_get().

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

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

125  {
126  global $mem, $db, $err, $mysql;
127 
128  if ($this->getvm() !== FALSE) {
129  $err->raise('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  $msgg = array('action' => 'start', 'login' => $login, 'pass' => $pass, 'uid' => $uid);
139  $msgg['mysql_host'] = $mysql->dbus->Host;
140 
141  $res = $this->sendMessage($msgg);
142  if ($res === FALSE) {
143  return $this->error;
144  } else {
145  $data = unserialize($res);
146  $error = (int) $data['error'];
147  $hostname = $data['hostname'];
148  $msg = $data['msg'];
149  $date_start = 'NOW()';
150  $uid = $mem->user['uid'];
151 
152  if ($error != 0) {
153  $err->raise('lxc', _($msg));
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  }
$uid
getvm($login=FALSE)
Definition: m_lxc.php:164
$hostname
Definition: aws_edit.php:40
global $db
Definition: bootstrap.php:22
$error
Definition: m_lxc.php:39
$err
Definition: bootstrap.php:72
$res
Definition: index.php:127
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 179 of file m_lxc.php.

References getvm(), and sendMessage().

179  {
180  $vm = $this->getvm();
181  if ($vm === FALSE) {
182  return FALSE;
183  }
184  if ($this->sendMessage(array('action' => 'stop', 'vm' => $vm['vm'])) === FALSE) {
185  return FALSE;
186  }
187  return TRUE;
188  }
getvm($login=FALSE)
Definition: m_lxc.php:164
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 39 of file m_lxc.php.

Referenced by start().

m_lxc::$IP

Definition at line 34 of file m_lxc.php.

m_lxc::$KEY

Definition at line 35 of file m_lxc.php.

Referenced by sendMessage().

m_lxc::$maxtime

Definition at line 37 of file m_lxc.php.

m_lxc::$PORT

Definition at line 36 of file m_lxc.php.

m_lxc::$TIMEOUT = 5

Definition at line 38 of file m_lxc.php.


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