Skip to content

Commit

Permalink
try and track both swarm and single node docker
Browse files Browse the repository at this point in the history
  • Loading branch information
mhzawadi committed Dec 20, 2024
1 parent ee7db6d commit 7eed0e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
47 changes: 30 additions & 17 deletions docker-api/docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,40 @@ public function __construct($socket = '/var/run/docker.sock', $host = 'localhost
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/info");
$host_info = json_decode(curl_exec($ch), true);
if($host_info['Swarm']['LocalNodeState'] == 'active'){
$this->swarm = 'services';
}else{
$this->swarm = 'containers/json?{\"status\":[\"running\"]}';
}
curl_close($ch);
$this->swarm = $host_info['Swarm']['LocalNodeState'];
}

public function get_containers(){

if($this->swarm === 'active'){
$containers = $this->docker_swarm();
}
$containers = $this->docker_node($containers);

return $containers;
}

private function docker_node($node_containers){
set_time_limit(0);
$ch = $this->connection;
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $this->SOCKET);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/$this->swarm");
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/containers/json?{\"status\":[\"running\"]}");
$containers = json_decode(curl_exec($ch), true);
curl_close($ch);
if($this->swarm === 'services'){
return $this->docker_swarm($containers);
}else{
return $this->docker_node($containers);
}
}

private function docker_node($containers){
$count = count($containers);
$container_list = array();
$d = 0;
$container_list = json_decode($node_containers, true);
$start = count($container_list);
$d = $start;
for($c = 0; $c < $count; $c++){
if( (array_key_exists('dashboard.ignore', $containers[$c]['Labels']) === false) ||
( array_key_exists('traefik.enable', $containers[$c]['Labels']) &&
$containers[$c]['Labels']['traefik.enable'] === false)
$containers[$c]['Labels']['traefik.enable'] === false) ||
(array_key_exists('com.docker.stack.namespace', $containers[$c]['Labels']) === false)
){
$container_list[$d]['name'] = str_replace('/', '', $containers[$c]['Names'][0]);
foreach($containers[$c]['Labels'] as $key => $label){
Expand Down Expand Up @@ -90,7 +93,17 @@ private function docker_node($containers){
return json_encode($container_list);
}

private function docker_swarm($containers){
private function docker_swarm(){
set_time_limit(0);
$ch = $this->connection;
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $this->SOCKET);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
curl_setopt($ch, CURLOPT_URL, "http://$this->HOST/services");
$containers = json_decode(curl_exec($ch), true);
curl_close($ch);

$count = count($containers);
$container_list = array();
$d = 0;
Expand Down
6 changes: 4 additions & 2 deletions src/controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public function __construct($user_agent){
$this->bookmark = new bookmark($this->setting_obj['useOrdering']);
$this->bookmark_view = new bookmark_view($this->bookmark);
$this->category_view = new category_view($this->bookmark);
$this->docker = new docker();
$this->application->store_docker($this->docker->get_data());
if($this->setting_obj['dockerApps'] === '1'){
$this->docker = new docker();
$this->application->store_docker($this->docker->get_data());
}
if(file_exists('../../user_data/db.sqlite')){
$this->flame = new flame();
$this->flame->import_apps($this->application);
Expand Down

0 comments on commit 7eed0e3

Please sign in to comment.