forked from RaspAP/SamplePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocker.php
More file actions
154 lines (131 loc) · 5 KB
/
Docker.php
File metadata and controls
154 lines (131 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace RaspAP\Plugins\Docker;
use RaspAP\Plugins\PluginInterface;
use RaspAP\UI\Sidebar;
require_once __DIR__ . '/DockerService.php';
if (!defined('RASPI_DOCKER_CONFIG')) {
define('RASPI_DOCKER_CONFIG', '/etc/raspap/docker');
}
class Docker implements PluginInterface
{
private string $pluginPath;
private string $pluginName;
private string $templateMain;
private string $serviceStatus;
private string $label;
private string $icon;
private string $binPath;
public function __construct(string $pluginPath, string $pluginName)
{
$this->pluginPath = $pluginPath;
$this->pluginName = $pluginName;
$this->templateMain = 'main';
$this->serviceStatus = 'up';
$this->label = 'Docker';
$this->icon = 'fab fa-docker';
$this->binPath = '/usr/bin/docker';
$this->loadData();
}
public function initialize(Sidebar $sidebar): void
{
$sidebar->addItem('Docker', $this->icon, 'plugin__Docker', 77);
}
public function handlePageAction(string $page): bool
{
if (strpos($page, '/plugin__' . $this->getName()) !== 0) {
return false;
}
$status = new \RaspAP\Messages\StatusMessage();
if (!file_exists($this->binPath)) {
$status->addMessage('Docker binary not found. See https://docs.docker.com/engine/install/debian/ to install Docker.', 'warning');
$daemonStatus = 'inactive';
$containers = [];
$images = [];
$systemDf = [];
$dockerVersion = '';
} else {
$dockerService = new DockerService();
$daemonStatus = $dockerService->getDaemonStatus();
$containers = $dockerService->getContainers();
$images = $dockerService->getImages();
$systemDf = $dockerService->getSystemDf();
$dockerVersion = $dockerService->getDockerVersion();
$composeProjects = $dockerService->getComposeProjects();
$volumes = $dockerService->getVolumes();
if (!RASPI_MONITOR_ENABLED && isset($_POST['saveCompose'])) {
$project = trim($_POST['compose_project'] ?? '');
$yaml = $_POST['compose_yaml'] ?? '';
if ($project !== '' && $yaml !== '') {
$dockerService->saveComposeFile($project, $yaml);
}
}
}
$this->serviceStatus = ($daemonStatus === 'active') ? 'up' : 'down';
// Read version and plugin_uri from manifest (single source of truth)
$manifest = $this->readManifest();
$pluginVersion = $manifest['version'] ?? 'unknown';
$pluginUri = $manifest['plugin_uri'] ?? 'https://github.com/RaspAP/';
$__template_data = [
'title' => $this->label,
'description' => _('A Docker container management plugin for RaspAP'),
'author' => 'RaspAP',
'uri' => 'https://github.com/RaspAP/',
'pluginUri' => $pluginUri,
'pluginVersion' => $pluginVersion,
'icon' => $this->icon,
'serviceStatus' => $this->serviceStatus,
'serviceName' => 'docker',
'action' => 'plugin__' . $this->getName(),
'pluginName' => $this->getName(),
'daemonStatus' => $daemonStatus,
'containers' => $containers,
'systemDf' => $systemDf,
'dockerVersion' => $dockerVersion,
'images' => $images,
'composeProjects' => $composeProjects ?? [],
'volumes' => $volumes ?? [],
];
echo $this->renderTemplate($this->templateMain, compact('status', '__template_data'));
return true;
}
public function renderTemplate(string $templateName, array $__data = []): string
{
$templateFile = $this->pluginPath . '/' . $this->getName() . '/templates/' . $templateName . '.php';
if (!file_exists($templateFile)) {
return '';
}
extract($__data);
ob_start();
include $templateFile;
return ob_get_clean();
}
public function persistData(): void
{
file_put_contents("/tmp/plugin__{$this->getName()}.data", serialize($this));
}
public static function loadData(): ?self
{
$file = "/tmp/plugin__" . self::getName() . ".data";
if (!file_exists($file)) {
return null;
}
$data = unserialize(file_get_contents($file));
if ($data instanceof self) {
return $data;
}
return null;
}
public static function getName(): string
{
return basename(str_replace('\\', '/', static::class));
}
private function readManifest(): array
{
$path = __DIR__ . '/manifest.json';
if (!file_exists($path)) {
return [];
}
$data = json_decode(file_get_contents($path), true);
return is_array($data) ? $data : [];
}
}