On peut démarrer et éteindre des vms \o/

This commit is contained in:
François Serman 2013-04-21 15:49:56 +00:00
parent 184afe88ea
commit 9d6374fd8b
2 changed files with 69 additions and 25 deletions

View File

@ -3,32 +3,52 @@
require_once("../class/config.php");
include_once("head.php");
echo '<h1>youpi gestion des VM</h1>';
$fields = array (
"action" => array ("request", "string", FALSE),
"login" => array ("request", "string", FALSE),
"pass" => array("request", "string", FALSE),
"uid" => array("request", "integer", FALSE),
"action" => array ("get", "string", ''),
);
getFields($fields);
if (in_array($action, array('start', 'stop', 'monit')))
{
#$res = call_user_func(array($lxc, $action));
if (in_array($action, array('start', 'stop', 'monit'))) {
$res = $hooks->invoke($action, array(), 'lxc');
var_dump ($res);
printvar($res);
}
$infos = $lxc->getvm();
printvar($infos);
?>
<h1>Dev only</h1>
<form method="post">
<p>Action: <select name="action"><option value="monit">Monitoring</option><option value="start">Start</option><option value="stop">Stop</option></select></p>
<p>Login: <input type="text" name="login" value="<?php echo $mem->user['login']; ?>" /></p>
<p>Pass (hash): <input type="text" name="pass" value="<?php echo $mem->user['pass']; ?>" /></p>
<p>Uid: <input type="text" name="uid" value="<?php echo $mem->user['uid']; ?>" /></p>
<p><input type="submit" name="tester" /></p>
<h3><?php __('Console access'); ?></h3>
<hr/>
<br/>
</form>
<?php if (isset($res) && ! $res['lxc']) { ?>
<div>
<span class="error">
<?php echo $err->errstr(); ?>
</span>
</div>
<br/>
<br/>
<?php } //isset $res ?>
<div>
<?php if (empty($infos)) {
echo '<span class="error">';
__("You can start a virtual machine.");
echo "<a href='vm.php?action=start'>"._("Click here to do so.")."</a>";
echo '</span>';
} else {
echo "<table class='tedit'>";
echo "<tr><th>"._("Hostname")."</th><td>".$infos['serialized_object']['hostname']."</td></tr>";
echo "<tr><th>"._("Start time")."</th><td>".$infos['date_start']."</td></tr>";
echo "<tr><td colspan='2'><a href='vm.php?action=stop'>"._("Click here to stop the machine")."</a></td></tr>";
echo "</table>";
} // empty infos ?>
</div>
<?php
include_once("foot.php");
?>

View File

@ -21,7 +21,7 @@ class m_lxc implements vm
if ( empty($this->IP)) return ; # No menu if no server
$obj = array(
'title' => _("Virtual server"),
'title' => _("Console access"),
'ico' => 'images/ssh.png',
'link' => 'vm.php',
'pos' => 95,
@ -38,7 +38,7 @@ class m_lxc implements vm
}
private function sendMessage($action, $user, $password, $uid)
private function sendMessage($action, $user = FALSE, $password=FALSE, $uid=FALSE)
{
$fp = fsockopen($this->IP, $this->PORT, $errno, $errstr, $this->TIMEOUT);
if (!$fp)
@ -75,7 +75,14 @@ class m_lxc implements vm
public function start($login = FALSE, $pass = FALSE, $uid = FALSE)
{
global $mem, $db;
global $mem, $db, $err;
if ($this->getvm() !== FALSE)
{
$err->raise('lxc', _('VM already started'));
return FALSE;
}
$user = $login ? $login : $mem->user['login'];
$pass = $pass ? $pass : $mem->user['pass'];
@ -93,6 +100,12 @@ class m_lxc implements vm
$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;
@ -125,9 +138,20 @@ class m_lxc implements vm
public function stop()
{
global $mem;
printvar($mem);
echo "lxc::stop";
global $db, $mem;
$vm = $this->getvm();
if ($vm === FALSE)
return TRUE;
$vm_id = $vm['serialized_object']['vm'];
$uid = $mem->user['uid'];
$vid = $vm['id'];
if ($this->sendMessage('stop', $vm_id, FALSE, FALSE) === FALSE)
return FALSE;
return $db->query("UPDATE vm_history SET date_end = NOW() WHERE uid = '$uid' AND id = '$vid' LIMIT 1");
}
}