Alternc  latest
Alternc logiel libre pour l'hébergement
m_lxc.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  ----------------------------------------------------------------------
5  LICENSE
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License (GPL)
9  as published by the Free Software Foundation; either version 2
10  of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  To read the license please visit http://www.gnu.org/copyleft/gpl.html
18  ----------------------------------------------------------------------
19 */
20 
21 include_once(dirname(__FILE__) . '/vm.class.php');
22 
23 /**
24  * Manage AlternC's virtual machine (Containers) start/stop using our own inetd-based protocol.
25  *
26  * @copyright AlternC-Team 2000-2017 https://alternc.com/
27  */
28 class m_lxc implements vm {
29 
30  public $IP;
31  public $KEY;
32  public $PORT;
33  public $maxtime;
34  public $TIMEOUT = 5;
35  public $error = array();
36 
37 
38  /**
39  * Constructor, initialize the class informations from AlternC's variables
40  */
41  function m_lxc() {
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  }
47 
48 
49  /**
50  * HOOK: add the "Console Access" to AlternC's main menu
51  */
52  function hook_menu() {
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  }
64 
65 
66  /**
67  * HOOK: remove VM history for AlternC account
68  */
69  function hook_admin_del_member() {
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  }
75 
76 
77  /**
78  * Send a message to a remote VM manager instance
79  * $params are the parameters to send as serialized data
80  * to the listening server.
81  * Return the unserialized response data, if the message has been sent successfully
82  * or FALSE if an error occurred. In that case $error[] is set.
83  */
84  private function sendMessage($params) {
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  }
119 
120 
121  /**
122  * START a Virtual Machine on the remote VM manager
123  * for user $login having hashed password $pass and uid $uid
124  */
125  public function start($login = FALSE, $pass = FALSE, $uid = FALSE) {
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  }
160 
161 
162  /**
163  *
164  */
165  public function getvm($login = FALSE) {
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  }
176 
177 
178  /**
179  * Stop the currently running VM
180  */
181  public function stop() {
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  }
191 
192 } /* class m_lxc */
193 
$hooks
Definition: bootstrap.php:74
global $db
Definition: bootstrap.php:26
$mem
Definition: bootstrap.php:71
$msg
Definition: bootstrap.php:75
$cuid
Definition: bootstrap.php:43
$p
Definition: bro_editor.php:46
$res
Definition: index.php:111
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
$errstr
Definition: change.php:62
Manage AlternC's virtual machine (Containers) start/stop using our own inetd-based protocol.
Definition: m_lxc.php:28
$error
Definition: m_lxc.php:35
m_lxc()
Constructor, initialize the class informations from AlternC's variables.
Definition: m_lxc.php:41
hook_menu()
HOOK: add the "Console Access" to AlternC's main menu.
Definition: m_lxc.php:52
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...
Definition: m_lxc.php:125
$IP
Definition: m_lxc.php:30
stop()
Stop the currently running VM.
Definition: m_lxc.php:181
$KEY
Definition: m_lxc.php:31
$PORT
Definition: m_lxc.php:32
getvm($login=FALSE)
Definition: m_lxc.php:165
hook_admin_del_member()
HOOK: remove VM history for AlternC account.
Definition: m_lxc.php:69
$TIMEOUT
Definition: m_lxc.php:34
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
$maxtime
Definition: m_lxc.php:33
Definition: vm.class.php:2
$uid
$login