diff --git a/Dockhand/Dockhand.php b/Dockhand/Dockhand.php index 418abf92db..e10653df41 100644 --- a/Dockhand/Dockhand.php +++ b/Dockhand/Dockhand.php @@ -2,6 +2,108 @@ namespace App\SupportedApps\Dockhand; -class Dockhand extends \App\SupportedApps +class Dockhand extends \App\SupportedApps implements \App\EnhancedApps { + public $config; + + public function test() + { + $test = parent::appTest($this->url("api/environments"), $this->attrs()); + echo $test->status; + } + + public function livestats() + { + $status = "inactive"; + $running = 0; + $stopped = 0; + + foreach ($this->environmentIds() as $envId) { + $result = parent::execute( + $this->url("api/containers?env=" . urlencode($envId)), + $this->attrs() + ); + if (null === $result) { + continue; + } + $containers = json_decode($result->getBody()); + if (!is_array($containers)) { + continue; + } + $status = "active"; + foreach ($containers as $container) { + if (($container->state ?? "") === "running") { + $running++; + } else { + $stopped++; + } + } + } + + $data = [ + "running" => $running, + "stopped" => $stopped, + ]; + + return parent::getLiveStats($status, $data); + } + + private function environmentIds() + { + $configured = trim((string) $this->getConfigValue("environments", "")); + if ($configured !== "") { + return array_filter(array_map("trim", explode(",", $configured))); + } + + $result = parent::execute($this->url("api/environments"), $this->attrs()); + if (null === $result) { + return []; + } + $environments = json_decode($result->getBody()); + if (!is_array($environments)) { + return []; + } + + $ids = []; + foreach ($environments as $environment) { + if (isset($environment->id)) { + $ids[] = $environment->id; + } + } + return $ids; + } + + public function url($endpoint) + { + return parent::normaliseurl($this->config->url) . $endpoint; + } + + public function attrs() + { + $attrs = [ + "headers" => [ + "Accept" => "application/json", + ], + ]; + + // Dockhand instances with authentication disabled accept + // unauthenticated API requests, so the token is optional. + $token = $this->getConfigValue("token", null); + if (!empty($token)) { + $attrs["headers"]["Authorization"] = "Bearer " . $token; + } + + if ($this->getConfigValue("ignore_tls", false)) { + $attrs["verify"] = false; + } + + return $attrs; + } + + public function getConfigValue($key, $default = null) + { + return isset($this->config) && isset($this->config->$key) + ? $this->config->$key + : $default; + } } diff --git a/Dockhand/app.json b/Dockhand/app.json index 56fd0b65e1..e555cd854c 100644 --- a/Dockhand/app.json +++ b/Dockhand/app.json @@ -4,7 +4,7 @@ "website": "https://dockhand.pro", "license": "Apache License 2.0", "description": "Modern Docker management for everyone. Licensed under BSL 1.1 The code is naked. Deal with it. Converts to Apache 2.0 in 2029", - "enhanced": false, + "enhanced": true, "tile_background": "light", "icon": "dockhand.png" } diff --git a/Dockhand/config.blade.php b/Dockhand/config.blade.php new file mode 100644 index 0000000000..4652cae8cf --- /dev/null +++ b/Dockhand/config.blade.php @@ -0,0 +1,37 @@ +

{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')

+
+
+ + {!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!} +
+
+ + {!! Form::input('password', 'config[token]', isset($item) ? $item->getconfig()->token : null, ['placeholder' => 'dh_...', 'data-config' => 'token', 'class' => 'form-control config-item']) !!} +

Create one in Dockhand under Settings → API Tokens. Leave empty if authentication is disabled on your instance.

+
+
+ + {!! Form::text('config[environments]', isset($item) ? $item->getconfig()->environments : null, ['placeholder' => 'Leave blank for all environments', 'data-config' => 'environments', 'class' => 'form-control config-item']) !!} +

Comma separated list of environment IDs to count containers for.

+
+
+ +
+ {!! Form::hidden('config[ignore_tls]', 0, ['class' => 'config-item', 'data-config' => 'ignore_tls']) !!} + +
+
+
+ +
+
diff --git a/Dockhand/livestats.blade.php b/Dockhand/livestats.blade.php new file mode 100644 index 0000000000..f98751d19c --- /dev/null +++ b/Dockhand/livestats.blade.php @@ -0,0 +1,10 @@ +