From 22ca6d6529dde9c60d38fa5128e0910a7c0a6d3f Mon Sep 17 00:00:00 2001 From: arnaudmm Date: Tue, 12 May 2015 15:51:26 +0200 Subject: [PATCH 01/14] dashboard view --- app/src/Controller.php | 108 +++++++++++++++++++++++++++++++- app/views/layout/dashboard.twig | 41 ++++++++++++ app/views/layout/global.twig | 32 ++++++++++ 3 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 app/views/layout/dashboard.twig create mode 100644 app/views/layout/global.twig diff --git a/app/src/Controller.php b/app/src/Controller.php index 343ca97..2ebce84 100644 --- a/app/src/Controller.php +++ b/app/src/Controller.php @@ -48,14 +48,118 @@ public function __construct(Application $app) public function index(Request $request, Response $response, array $args) { $layout = $request->query->get('layout', 'grid'); - in_array($layout, ['grid', 'list']) or $layout = 'grid'; + + $instances = $this->app['manager']->getAll(); + + $allProcesses = []; + $hosts = []; + $groups = []; + $stopped = 0; + + //Building datas for layout + foreach ($instances as $key => $value) { + $processes = $value->getAllProcessInfo(); + foreach ($processes as $process) { + $process['host'] = $key; + $allProcesses[] = $process; + $hosts[] = $process['host']; + $groups[] = $process['group']; + if ($process['state'] == 'STOPPED') { + $stopped++; + } + } + } + + $hosts = array_unique($hosts); + $groups = array_unique($groups); + sort($hosts); + sort($groups); + + switch($layout) { + case 'grid': + break; + case 'list': + break; + case 'dashboard': + break; + case 'global': + $filter = $request->query->get('filter', 'none'); + $filter_value = $request->query->get('filter_value', 'none'); + $sort = $request->query->get('sort', 'none'); + + switch($filter) { + case 'group': + $allProcesses = $this->filter_processes($filter, $filter_value, $allProcesses); + break; + case 'host': + $allProcesses = $this->filter_processes($filter, $filter_value, $allProcesses); + break; + case 'state': + $allProcesses = $this->filter_processes($filter, $filter_value, $allProcesses); + break; + } + + switch($sort) { + case 'group': + usort($allProcesses, array($this,'group_sort')); + break; + case 'host': + usort($allProcesses, array($this,'host_sort')); + break; + case 'state': + usort($allProcesses, array($this,'state_sort')); + break; + case 'start': + usort($allProcesses, array($this,'start_sort')); + break; + } + + break; + default: + $layout = 'grid'; + break; + } $template = $this->app['twig']->loadTemplate(sprintf('layout/%s.twig', $layout)); - $response->setContent($template->render(['instances' => $this->app['manager']->getAll()])); + + $response->setContent($template->render([ + 'instances' => $instances, + 'hosts' => $hosts, + 'allProcesses' => $allProcesses, + 'stopped' => $stopped, + 'groups' => $groups + ])); return $response; } + private function group_sort($a, $b) { + return strcmp($a["group"], $b["group"]); + } + + private function host_sort($a, $b) { + return strcmp($a["host"], $b["host"]); + } + + private function start_sort($a, $b) { + return $a["start"] > $b["start"]; + } + + private function state_sort($a, $b) { + return $a["state"] > $b["state"]; + } + + private function filter_processes($key, $value, $processes) { + $tokeep = []; + foreach ($processes as $process) { + //TODO key exist ? + if ($process[$key] == $value) { + $tokeep[] = $process; + } + } + return $tokeep; + } + /** * Starts all processes in an instance * diff --git a/app/views/layout/dashboard.twig b/app/views/layout/dashboard.twig new file mode 100644 index 0000000..6437567 --- /dev/null +++ b/app/views/layout/dashboard.twig @@ -0,0 +1,41 @@ +{% extends 'template.twig' %} + +{% block content %} +
+
+
+

Total processes {{ allProcesses|length }}

+ View all processes +
+
+
+
+

Stopped {{ stopped }}

+ View stopped processes +
+
+
+
+
+
+

Hosts {{ hosts|length }}

+
    + {% for host in hosts %} +
  • {{ host }}
  • + {% endfor %} +
+
+
+
+
+

Groups {{ groups|length }}

+
    + {% for group in groups %} +
  • {{ group }}
  • + {% endfor %} +
+
+
+
+{% endblock %} + diff --git a/app/views/layout/global.twig b/app/views/layout/global.twig new file mode 100644 index 0000000..af60264 --- /dev/null +++ b/app/views/layout/global.twig @@ -0,0 +1,32 @@ +{% extends 'template.twig' %} + +{% block content %} +
+
+
+ + + + + + + + + + {% for process in allProcesses %} + + + + + + + + + {% else %} + + {% endfor %} +
HostGroupNameStateStartControls
{{ process.host }}{{ process.group }}{{ process.name }}{{ process.statename }}{{ process|state_ago }}{% include 'actions.twig' with {'instance': process.host, 'process': process.name, 'group': process.group} only %}
No processes
+
+
+
+{% endblock %} From 1aa51a7335cbc04768ab64ee3b2d92bbc5271320 Mon Sep 17 00:00:00 2001 From: arnaudmm Date: Fri, 29 May 2015 10:04:31 +0200 Subject: [PATCH 02/14] More revelant to display "not running" processes instead of "stopped" processes since other states may be problematic like FATAL --- app/src/Controller.php | 18 ++++++++---------- app/views/layout/dashboard.twig | 18 +++++++++--------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/src/Controller.php b/app/src/Controller.php index 2ebce84..cda061e 100644 --- a/app/src/Controller.php +++ b/app/src/Controller.php @@ -54,7 +54,7 @@ public function index(Request $request, Response $response, array $args) $allProcesses = []; $hosts = []; $groups = []; - $stopped = 0; + $notRunning = 0; //Building datas for layout foreach ($instances as $key => $value) { @@ -62,18 +62,16 @@ public function index(Request $request, Response $response, array $args) foreach ($processes as $process) { $process['host'] = $key; $allProcesses[] = $process; - $hosts[] = $process['host']; - $groups[] = $process['group']; - if ($process['state'] == 'STOPPED') { - $stopped++; + array_key_exists($process['host'], $hosts) ? $hosts[$process['host']] += 1 : $hosts[$process['host']] = 1 ; + array_key_exists($process['group'], $groups) ? $groups[$process['group']] += 1 : $groups[$process['group']] = 1; + if ($process['statename'] != 'RUNNING') { + $notRunning++; } } } - $hosts = array_unique($hosts); - $groups = array_unique($groups); - sort($hosts); - sort($groups); + ksort($hosts); + ksort($groups); switch($layout) { case 'grid': @@ -126,7 +124,7 @@ public function index(Request $request, Response $response, array $args) 'instances' => $instances, 'hosts' => $hosts, 'allProcesses' => $allProcesses, - 'stopped' => $stopped, + 'notRunning' => $notRunning, 'groups' => $groups ])); diff --git a/app/views/layout/dashboard.twig b/app/views/layout/dashboard.twig index 6437567..a2605ac 100644 --- a/app/views/layout/dashboard.twig +++ b/app/views/layout/dashboard.twig @@ -2,36 +2,36 @@ {% block content %}
-
+

Total processes {{ allProcesses|length }}

View all processes
-
+
-

Stopped {{ stopped }}

+

Not Running {{ notRunning }}

View stopped processes
-
+

Hosts {{ hosts|length }}

    - {% for host in hosts %} -
  • {{ host }}
  • + {% for host,number in hosts %} +
  • {{ number }} {{ host }}
  • {% endfor %}
-
+

Groups {{ groups|length }}

    - {% for group in groups %} -
  • {{ group }}
  • + {% for group,number in groups %} +
  • {{ number }} {{ group }}
  • {% endfor %}
From 0c5461812b102fed8d026eaa8b2c7ea1dadf8e8d Mon Sep 17 00:00:00 2001 From: arnaudmm Date: Fri, 29 May 2015 10:51:19 +0200 Subject: [PATCH 03/14] No more warning if username/password not provided in configuration --- app/src/Manager.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/Manager.php b/app/src/Manager.php index aa1be05..e8fba9c 100644 --- a/app/src/Manager.php +++ b/app/src/Manager.php @@ -58,8 +58,9 @@ public function get($instance) $config = $this->config[$instance]; $transport = new StreamSocketTransport; - - $transport->setHeader('Authorization', 'Basic '.base64_encode(sprintf('%s:%s', $config['username'], $config['password']))); + if (array_key_exists('username', $config) && array_key_exists('username', $config)) { + $transport->setHeader('Authorization', 'Basic '.base64_encode(sprintf('%s:%s', $config['username'], $config['password']))); + } $client = new Client($config['url'], $transport); $connector = new XmlRpc($client); From 3dac19968023cf3e1cfafc824eb026efcec91b96 Mon Sep 17 00:00:00 2001 From: arnaudmm Date: Fri, 29 May 2015 15:50:29 +0200 Subject: [PATCH 04/14] Display error message when a host is not connected --- app/src/Controller.php | 9 ++++++++- app/views/layout/dashboard.twig | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/src/Controller.php b/app/src/Controller.php index cda061e..c48b289 100644 --- a/app/src/Controller.php +++ b/app/src/Controller.php @@ -54,11 +54,13 @@ public function index(Request $request, Response $response, array $args) $allProcesses = []; $hosts = []; $groups = []; + $unreachableHosts = []; $notRunning = 0; //Building datas for layout foreach ($instances as $key => $value) { - $processes = $value->getAllProcessInfo(); + if($value->isConnected()) { + $processes = $value->getAllProcessInfo(); foreach ($processes as $process) { $process['host'] = $key; $allProcesses[] = $process; @@ -68,6 +70,10 @@ public function index(Request $request, Response $response, array $args) $notRunning++; } } + } + else { + $unreachableHosts[] = $key; + } } ksort($hosts); @@ -125,6 +131,7 @@ public function index(Request $request, Response $response, array $args) 'hosts' => $hosts, 'allProcesses' => $allProcesses, 'notRunning' => $notRunning, + 'unreachableHosts' => $unreachableHosts, 'groups' => $groups ])); diff --git a/app/views/layout/dashboard.twig b/app/views/layout/dashboard.twig index a2605ac..4512483 100644 --- a/app/views/layout/dashboard.twig +++ b/app/views/layout/dashboard.twig @@ -1,6 +1,20 @@ {% extends 'template.twig' %} {% block content %} + {% if unreachableHosts is not empty %} +
+
+ +
+
+ {% endif %}
From 0a67531ebfdbd56925cdd9ffe7ef830495c4f740 Mon Sep 17 00:00:00 2001 From: arnaudmm Date: Fri, 29 May 2015 17:03:17 +0200 Subject: [PATCH 05/14] New route and controller method to control group process --- app/src/Controller.php | 90 +++++++++++++++++++++++++++++++++++++++++- public/index.php | 5 +++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/app/src/Controller.php b/app/src/Controller.php index c48b289..9268173 100644 --- a/app/src/Controller.php +++ b/app/src/Controller.php @@ -47,7 +47,7 @@ public function __construct(Application $app) */ public function index(Request $request, Response $response, array $args) { - $layout = $request->query->get('layout', 'grid'); + $layout = $request->query->get('layout', 'dashboard'); $instances = $this->app['manager']->getAll(); @@ -183,6 +183,94 @@ public function startAll(Request $request, Response $response, array $args) return new RedirectResponse('/'); } + /** + * Starts all processes with specific group + * + * @param Request $request + * @param Response $response + * @param array $args + * + * @return Response + */ + public function startGroup(Request $request, Response $response, array $args) + { + $group = $args['group']; + + $instances = $this->app['manager']->getAll(); + + foreach ($instances as $name => $instance) { + if($instance->isConnected()) { + $processes = $instance->getAllProcessInfo(); + foreach ($processes as $process) { + if ($process['group'] == $group) { + $instance->startProcess($process['name'], false); + } + } + } + } + + return new RedirectResponse('/'); + } + + /** + * Restarts all processes with specific group + * + * @param Request $request + * @param Response $response + * @param array $args + * + * @return Response + */ + public function restartGroup(Request $request, Response $response, array $args) + { + $group = $args['group']; + + $instances = $this->app['manager']->getAll(); + + foreach ($instances as $name => $instance) { + if($instance->isConnected()) { + $processes = $instance->getAllProcessInfo(); + foreach ($processes as $process) { + if ($process['group'] == $group) { + $instance->stopProcess($process['name'], false); + $instance->startProcess($process['name'], false); + } + } + } + } + + return new RedirectResponse('/'); + } + + /** + * Stops all processes with specific group + * + * @param Request $request + * @param Response $response + * @param array $args + * + * @return Response + */ + public function stopGroup(Request $request, Response $response, array $args) + { + $group = $args['group']; + + $instances = $this->app['manager']->getAll(); + + foreach ($instances as $name => $instance) { + if($instance->isConnected()) { + $processes = $instance->getAllProcessInfo(); + foreach ($processes as $process) { + if ($process['group'] == $group) { + $instance->stopProcess($process['name'], false); + } + } + } + } + + return new RedirectResponse('/'); + } + /** * Restarts all processes in an instance * diff --git a/public/index.php b/public/index.php index f75354b..1f9a3ed 100644 --- a/public/index.php +++ b/public/index.php @@ -22,6 +22,10 @@ $app->get('/', 'controller::index'); +$app->get('/group/start/{group}', 'controller::startGroup'); +$app->get('/group/restart/{group}', 'controller::restartGroup'); +$app->get('/group/stop/{group}', 'controller::stopGroup'); + $app->get('/start/{instance}', 'controller::startAll'); $app->get('/restart/{instance}', 'controller::restartAll'); $app->get('/stop/{instance}', 'controller::stopAll'); @@ -30,4 +34,5 @@ $app->get('/restart/{instance}/{process}', 'controller::restartProcess'); $app->get('/stop/{instance}/{process}', 'controller::stopProcess'); + $app->run(); From fbb14ee66d1bd7d3b305f5c6f114588a031c1415 Mon Sep 17 00:00:00 2001 From: arnaudmm Date: Fri, 29 May 2015 17:15:32 +0200 Subject: [PATCH 06/14] wait for stop when restart --- app/src/Controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/Controller.php b/app/src/Controller.php index 9268173..4e2d11f 100644 --- a/app/src/Controller.php +++ b/app/src/Controller.php @@ -232,7 +232,7 @@ public function restartGroup(Request $request, Response $response, array $args) $processes = $instance->getAllProcessInfo(); foreach ($processes as $process) { if ($process['group'] == $group) { - $instance->stopProcess($process['name'], false); + $instance->stopProcess($process['name'], true); $instance->startProcess($process['name'], false); } } @@ -284,7 +284,7 @@ public function restartAll(Request $request, Response $response, array $args) { $instance = $this->app['manager']->get($args['instance']); - $instance->stopAllProcesses(false); + $instance->stopAllProcesses(true); $instance->startAllProcesses(false); return new RedirectResponse('/'); @@ -339,7 +339,7 @@ public function restartProcess(Request $request, Response $response, array $args { $instance = $this->app['manager']->get($args['instance']); - $instance->stopProcess($args['process'], false); + $instance->stopProcess($args['process'], true); $instance->startProcess($args['process'], false); return new RedirectResponse('/'); From 565c0a997f07e65a19a8e7944e2ce15078f59a4f Mon Sep 17 00:00:00 2001 From: arnaudmm Date: Mon, 1 Jun 2015 14:30:25 +0200 Subject: [PATCH 07/14] link title to root --- app/views/template.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/template.twig b/app/views/template.twig index aa3c744..04d0138 100644 --- a/app/views/template.twig +++ b/app/views/template.twig @@ -33,7 +33,7 @@ - Supervisor Monitor + Supervisor Monitor