From 9d6374fd8b174c16f02b52d1053154a747ae8146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Serman?= Date: Sun, 21 Apr 2013 15:49:56 +0000 Subject: [PATCH] =?UTF-8?q?On=20peut=20d=C3=A9marrer=20et=20=C3=A9teindre?= =?UTF-8?q?=20des=20vms=20\o/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bureau/admin/vm.php | 58 ++++++++++++++++++++++++++++-------------- bureau/class/m_lxc.php | 36 +++++++++++++++++++++----- 2 files changed, 69 insertions(+), 25 deletions(-) diff --git a/bureau/admin/vm.php b/bureau/admin/vm.php index bdc5d1ba..56680de1 100644 --- a/bureau/admin/vm.php +++ b/bureau/admin/vm.php @@ -3,32 +3,52 @@ require_once("../class/config.php"); include_once("head.php"); -echo '

youpi gestion des VM

'; - - $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); ?> -

Dev only

-
-

Action:

-

Login:

-

Pass (hash):

-

Uid:

-

+

+
+
-
+ +
+ + errstr(); ?> + +
+
+
+ + +
+'; + __("You can start a virtual machine."); + echo ""._("Click here to do so.").""; + echo ''; +} else { + echo ""; + echo ""; + echo ""; + echo ""; + echo "
"._("Hostname")."".$infos['serialized_object']['hostname']."
"._("Start time")."".$infos['date_start']."
"._("Click here to stop the machine")."
"; + + +} // empty infos ?> +
+ + diff --git a/bureau/class/m_lxc.php b/bureau/class/m_lxc.php index 92208e89..6dca0497 100644 --- a/bureau/class/m_lxc.php +++ b/bureau/class/m_lxc.php @@ -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"); } }