Skip to content

Commit 7eed0e3

Browse files
committed
try and track both swarm and single node docker
1 parent ee7db6d commit 7eed0e3

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

docker-api/docker.php

+30-17
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,40 @@ public function __construct($socket = '/var/run/docker.sock', $host = 'localhost
2222
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
2323
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/info");
2424
$host_info = json_decode(curl_exec($ch), true);
25-
if($host_info['Swarm']['LocalNodeState'] == 'active'){
26-
$this->swarm = 'services';
27-
}else{
28-
$this->swarm = 'containers/json?{\"status\":[\"running\"]}';
29-
}
25+
curl_close($ch);
26+
$this->swarm = $host_info['Swarm']['LocalNodeState'];
3027
}
28+
3129
public function get_containers(){
30+
31+
if($this->swarm === 'active'){
32+
$containers = $this->docker_swarm();
33+
}
34+
$containers = $this->docker_node($containers);
35+
36+
return $containers;
37+
}
38+
39+
private function docker_node($node_containers){
3240
set_time_limit(0);
3341
$ch = $this->connection;
3442
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $this->SOCKET);
3543
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
3644
curl_setopt($ch, CURLOPT_TIMEOUT, 1000000);
3745
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
38-
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/$this->swarm");
46+
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/containers/json?{\"status\":[\"running\"]}");
3947
$containers = json_decode(curl_exec($ch), true);
4048
curl_close($ch);
41-
if($this->swarm === 'services'){
42-
return $this->docker_swarm($containers);
43-
}else{
44-
return $this->docker_node($containers);
45-
}
46-
}
4749

48-
private function docker_node($containers){
4950
$count = count($containers);
50-
$container_list = array();
51-
$d = 0;
51+
$container_list = json_decode($node_containers, true);
52+
$start = count($container_list);
53+
$d = $start;
5254
for($c = 0; $c < $count; $c++){
5355
if( (array_key_exists('dashboard.ignore', $containers[$c]['Labels']) === false) ||
5456
( array_key_exists('traefik.enable', $containers[$c]['Labels']) &&
55-
$containers[$c]['Labels']['traefik.enable'] === false)
57+
$containers[$c]['Labels']['traefik.enable'] === false) ||
58+
(array_key_exists('com.docker.stack.namespace', $containers[$c]['Labels']) === false)
5659
){
5760
$container_list[$d]['name'] = str_replace('/', '', $containers[$c]['Names'][0]);
5861
foreach($containers[$c]['Labels'] as $key => $label){
@@ -90,7 +93,17 @@ private function docker_node($containers){
9093
return json_encode($container_list);
9194
}
9295

93-
private function docker_swarm($containers){
96+
private function docker_swarm(){
97+
set_time_limit(0);
98+
$ch = $this->connection;
99+
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $this->SOCKET);
100+
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
101+
curl_setopt($ch, CURLOPT_TIMEOUT, 1000000);
102+
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
103+
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/services");
104+
$containers = json_decode(curl_exec($ch), true);
105+
curl_close($ch);
106+
94107
$count = count($containers);
95108
$container_list = array();
96109
$d = 0;

src/controller/DashboardController.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ public function __construct($user_agent){
3434
$this->bookmark = new bookmark($this->setting_obj['useOrdering']);
3535
$this->bookmark_view = new bookmark_view($this->bookmark);
3636
$this->category_view = new category_view($this->bookmark);
37-
$this->docker = new docker();
38-
$this->application->store_docker($this->docker->get_data());
37+
if($this->setting_obj['dockerApps'] === '1'){
38+
$this->docker = new docker();
39+
$this->application->store_docker($this->docker->get_data());
40+
}
3941
if(file_exists('../../user_data/db.sqlite')){
4042
$this->flame = new flame();
4143
$this->flame->import_apps($this->application);

0 commit comments

Comments
 (0)