Merge pull request #5372 from nextcloud/enh/noid/enum

Use enum instead of interface for state
This commit is contained in:
Simon L. 2024-10-08 10:57:01 +02:00 committed by GitHub
commit 32f8c6587c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 60 additions and 104 deletions

View file

@ -2,7 +2,6 @@
namespace AIO\Container; namespace AIO\Container;
use AIO\Container\State\IContainerState;
use AIO\Data\ConfigurationManager; use AIO\Data\ConfigurationManager;
use AIO\Docker\DockerActionManager; use AIO\Docker\DockerActionManager;
use AIO\ContainerDefinitionFetcher; use AIO\ContainerDefinitionFetcher;
@ -113,19 +112,19 @@ readonly class Container {
return $this->volumes; return $this->volumes;
} }
public function GetRunningState() : IContainerState { public function GetRunningState() : ContainerState {
return $this->dockerActionManager->GetContainerRunningState($this); return $this->dockerActionManager->GetContainerRunningState($this);
} }
public function GetRestartingState() : IContainerState { public function GetRestartingState() : ContainerState {
return $this->dockerActionManager->GetContainerRestartingState($this); return $this->dockerActionManager->GetContainerRestartingState($this);
} }
public function GetUpdateState() : IContainerState { public function GetUpdateState() : VersionState {
return $this->dockerActionManager->GetContainerUpdateState($this); return $this->dockerActionManager->GetContainerUpdateState($this);
} }
public function GetStartingState() : IContainerState { public function GetStartingState() : ContainerState {
return $this->dockerActionManager->GetContainerStartingState($this); return $this->dockerActionManager->GetContainerStartingState($this);
} }

View file

@ -0,0 +1,12 @@
<?php
namespace AIO\Container;
enum ContainerState: string {
case ImageDoesNotExist = 'image_does_not_exist';
case NotRestarting = 'not_restarting';
case Restarting = 'restarting';
case Running = 'running';
case Starting = 'starting';
case Stopped = 'stopped';
}

View file

@ -1,5 +0,0 @@
<?php
namespace AIO\Container\State;
interface IContainerState {}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class ImageDoesNotExistState implements IContainerState
{}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class NotRestartingState implements IContainerState
{}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class RestartingState implements IContainerState
{}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class RunningState implements IContainerState
{}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class StartingState implements IContainerState
{}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class StoppedState implements IContainerState
{}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class VersionDifferentState implements IContainerState
{}

View file

@ -1,6 +0,0 @@
<?php
namespace AIO\Container\State;
class VersionEqualState implements IContainerState
{}

View file

@ -0,0 +1,8 @@
<?php
namespace AIO\Container;
enum VersionState: string {
case Different = 'different';
case Equal = 'equal';
}

View file

@ -9,7 +9,6 @@ use AIO\Container\ContainerPort;
use AIO\Container\ContainerPorts; use AIO\Container\ContainerPorts;
use AIO\Container\ContainerVolume; use AIO\Container\ContainerVolume;
use AIO\Container\ContainerVolumes; use AIO\Container\ContainerVolumes;
use AIO\Container\State\RunningState;
use AIO\Data\ConfigurationManager; use AIO\Data\ConfigurationManager;
use AIO\Data\DataConst; use AIO\Data\DataConst;
use AIO\Docker\DockerActionManager; use AIO\Docker\DockerActionManager;

View file

@ -2,7 +2,7 @@
namespace AIO\Controller; namespace AIO\Controller;
use AIO\Container\State\RunningState; use AIO\Container\ContainerState;
use AIO\ContainerDefinitionFetcher; use AIO\ContainerDefinitionFetcher;
use AIO\Docker\DockerActionManager; use AIO\Docker\DockerActionManager;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
@ -28,7 +28,7 @@ readonly class DockerController {
// Don't start if container is already running // Don't start if container is already running
// This is expected to happen if a container is defined in depends_on of multiple containers // This is expected to happen if a container is defined in depends_on of multiple containers
if ($container->GetRunningState() instanceof RunningState) { if ($container->GetRunningState() === ContainerState::Running) {
error_log('Not starting ' . $id . ' because it was already started.'); error_log('Not starting ' . $id . ' because it was already started.');
return; return;
} }
@ -254,10 +254,10 @@ readonly class DockerController {
$domaincheckContainer = $this->containerDefinitionFetcher->GetContainerById($id); $domaincheckContainer = $this->containerDefinitionFetcher->GetContainerById($id);
$apacheContainer = $this->containerDefinitionFetcher->GetContainerById(self::TOP_CONTAINER); $apacheContainer = $this->containerDefinitionFetcher->GetContainerById(self::TOP_CONTAINER);
// Don't start if apache is already running // Don't start if apache is already running
if ($apacheContainer->GetRunningState() instanceof RunningState) { if ($apacheContainer->GetRunningState() === ContainerState::Running) {
return; return;
// Don't start if domaincheck is already running // Don't start if domaincheck is already running
} elseif ($domaincheckContainer->GetRunningState() instanceof RunningState) { } elseif ($domaincheckContainer->GetRunningState() === ContainerState::Running) {
$domaincheckWasStarted = apcu_fetch($cacheKey); $domaincheckWasStarted = apcu_fetch($cacheKey);
// Start domaincheck again when 10 minutes are over by not returning here // Start domaincheck again when 10 minutes are over by not returning here
if($domaincheckWasStarted !== false && is_string($domaincheckWasStarted)) { if($domaincheckWasStarted !== false && is_string($domaincheckWasStarted)) {

View file

@ -3,15 +3,8 @@
namespace AIO\Docker; namespace AIO\Docker;
use AIO\Container\Container; use AIO\Container\Container;
use AIO\Container\State\IContainerState; use AIO\Container\VersionState;
use AIO\Container\State\ImageDoesNotExistState; use AIO\Container\ContainerState;
use AIO\Container\State\StartingState;
use AIO\Container\State\RunningState;
use AIO\Container\State\RestartingState;
use AIO\Container\State\NotRestartingState;
use AIO\Container\State\VersionDifferentState;
use AIO\Container\State\StoppedState;
use AIO\Container\State\VersionEqualState;
use AIO\Data\ConfigurationManager; use AIO\Data\ConfigurationManager;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
@ -42,14 +35,14 @@ readonly class DockerActionManager {
return $container->GetContainerName() . ':' . $tag; return $container->GetContainerName() . ':' . $tag;
} }
public function GetContainerRunningState(Container $container) : IContainerState public function GetContainerRunningState(Container $container) : ContainerState
{ {
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier()))); $url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier())));
try { try {
$response = $this->guzzleClient->get($url); $response = $this->guzzleClient->get($url);
} catch (RequestException $e) { } catch (RequestException $e) {
if ($e->getCode() === 404) { if ($e->getCode() === 404) {
return new ImageDoesNotExistState(); return ContainerState::ImageDoesNotExist;
} }
throw $e; throw $e;
} }
@ -57,20 +50,20 @@ readonly class DockerActionManager {
$responseBody = json_decode((string)$response->getBody(), true); $responseBody = json_decode((string)$response->getBody(), true);
if ($responseBody['State']['Running'] === true) { if ($responseBody['State']['Running'] === true) {
return new RunningState(); return ContainerState::Running;
} else { } else {
return new StoppedState(); return ContainerState::Stopped;
} }
} }
public function GetContainerRestartingState(Container $container) : IContainerState public function GetContainerRestartingState(Container $container) : ContainerState
{ {
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier()))); $url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier())));
try { try {
$response = $this->guzzleClient->get($url); $response = $this->guzzleClient->get($url);
} catch (RequestException $e) { } catch (RequestException $e) {
if ($e->getCode() === 404) { if ($e->getCode() === 404) {
return new ImageDoesNotExistState(); return ContainerState::ImageDoesNotExist;
} }
throw $e; throw $e;
} }
@ -78,13 +71,13 @@ readonly class DockerActionManager {
$responseBody = json_decode((string)$response->getBody(), true); $responseBody = json_decode((string)$response->getBody(), true);
if ($responseBody['State']['Restarting'] === true) { if ($responseBody['State']['Restarting'] === true) {
return new RestartingState(); return ContainerState::Restarting;
} else { } else {
return new NotRestartingState(); return ContainerState::NotRestarting;
} }
} }
public function GetContainerUpdateState(Container $container) : IContainerState public function GetContainerUpdateState(Container $container) : VersionState
{ {
$tag = $container->GetImageTag(); $tag = $container->GetImageTag();
if ($tag === '%AIO_CHANNEL%') { if ($tag === '%AIO_CHANNEL%') {
@ -93,28 +86,26 @@ readonly class DockerActionManager {
$runningDigests = $this->GetRepoDigestsOfContainer($container->GetIdentifier()); $runningDigests = $this->GetRepoDigestsOfContainer($container->GetIdentifier());
if ($runningDigests === null) { if ($runningDigests === null) {
return new VersionDifferentState(); return VersionState::Different;
} }
$remoteDigest = $this->dockerHubManager->GetLatestDigestOfTag($container->GetContainerName(), $tag); $remoteDigest = $this->dockerHubManager->GetLatestDigestOfTag($container->GetContainerName(), $tag);
if ($remoteDigest === null) { if ($remoteDigest === null) {
return new VersionEqualstate(); return VersionState::Equal;
} }
foreach($runningDigests as $runningDigest) { foreach($runningDigests as $runningDigest) {
if ($runningDigest === $remoteDigest) { if ($runningDigest === $remoteDigest) {
return new VersionEqualState(); return VersionState::Equal;
} }
} }
return new VersionDifferentState(); return VersionState::Different;
} }
public function GetContainerStartingState(Container $container) : IContainerState public function GetContainerStartingState(Container $container) : ContainerState
{ {
$runningState = $this->GetContainerRunningState($container); $runningState = $this->GetContainerRunningState($container);
if ($runningState instanceof StoppedState) { if ($runningState === ContainerState::Stopped || $runningState === ContainerState::ImageDoesNotExist) {
return new StoppedState(); return $runningState;
} elseif ($runningState instanceof ImageDoesNotExistState) {
return new ImageDoesNotExistState();
} }
$containerName = $container->GetIdentifier(); $containerName = $container->GetIdentifier();
@ -129,12 +120,12 @@ readonly class DockerActionManager {
$connection = @fsockopen($containerName, (int)$internalPort, $errno, $errstr, 0.2); $connection = @fsockopen($containerName, (int)$internalPort, $errno, $errstr, 0.2);
if ($connection) { if ($connection) {
fclose($connection); fclose($connection);
return new RunningState(); return ContainerState::Running;
} else { } else {
return new StartingState(); return ContainerState::Starting;
} }
} else { } else {
return new RunningState(); return ContainerState::Running;
} }
} }
@ -628,7 +619,7 @@ readonly class DockerActionManager {
$container = $this->containerDefinitionFetcher->GetContainerById($id); $container = $this->containerDefinitionFetcher->GetContainerById($id);
$updateAvailable = ""; $updateAvailable = "";
if ($container->GetUpdateState() instanceof VersionDifferentState) { if ($container->GetUpdateState() === VersionState::Different) {
$updateAvailable = '1'; $updateAvailable = '1';
} }
foreach ($container->GetDependsOn() as $dependency) { foreach ($container->GetDependsOn() as $dependency) {
@ -789,7 +780,7 @@ readonly class DockerActionManager {
public function sendNotification(Container $container, string $subject, string $message, string $file = '/notify.sh') : void public function sendNotification(Container $container, string $subject, string $message, string $file = '/notify.sh') : void
{ {
if ($this->GetContainerStartingState($container) instanceof RunningState) { if ($this->GetContainerStartingState($container) === ContainerState::Running) {
$containerName = $container->GetIdentifier(); $containerName = $container->GetIdentifier();
@ -973,7 +964,7 @@ readonly class DockerActionManager {
public function isLoginAllowed() : bool { public function isLoginAllowed() : bool {
$id = 'nextcloud-aio-apache'; $id = 'nextcloud-aio-apache';
$apacheContainer = $this->containerDefinitionFetcher->GetContainerById($id); $apacheContainer = $this->containerDefinitionFetcher->GetContainerById($id);
if ($this->GetContainerStartingState($apacheContainer) instanceof RunningState) { if ($this->GetContainerStartingState($apacheContainer) === ContainerState::Running) {
return false; return false;
} }
return true; return true;
@ -982,7 +973,7 @@ readonly class DockerActionManager {
public function isBackupContainerRunning() : bool { public function isBackupContainerRunning() : bool {
$id = 'nextcloud-aio-borgbackup'; $id = 'nextcloud-aio-borgbackup';
$backupContainer = $this->containerDefinitionFetcher->GetContainerById($id); $backupContainer = $this->containerDefinitionFetcher->GetContainerById($id);
if ($this->GetContainerRunningState($backupContainer) instanceof RunningState) { if ($this->GetContainerRunningState($backupContainer) === ContainerState::Running) {
return true; return true;
} }
return false; return false;

View file

@ -40,19 +40,19 @@
{% endif %} {% endif %}
{% for container in containers %} {% for container in containers %}
{% if container.GetDisplayName() != '' and class(container.GetRunningState()) == 'AIO\\Container\\State\\RunningState' %} {% if container.GetDisplayName() != '' and container.GetRunningState().value == 'running' %}
{% set isAnyRunning = true %} {% set isAnyRunning = true %}
{% endif %} {% endif %}
{% if container.GetDisplayName() != '' and class(container.GetRestartingState()) == 'AIO\\Container\\State\\RestartingState' %} {% if container.GetDisplayName() != '' and container.GetRestartingState().value == 'restarting' %}
{% set isAnyRestarting = true %} {% set isAnyRestarting = true %}
{% endif %} {% endif %}
{% if container.GetIdentifier() == 'nextcloud-aio-watchtower' and class(container.GetRunningState()) == 'AIO\\Container\\State\\RunningState' %} {% if container.GetIdentifier() == 'nextcloud-aio-watchtower' and container.GetRunningState().value == 'running' %}
{% set isWatchtowerRunning = true %} {% set isWatchtowerRunning = true %}
{% endif %} {% endif %}
{% if container.GetIdentifier() == 'nextcloud-aio-domaincheck' and class(container.GetRunningState()) == 'AIO\\Container\\State\\RunningState' %} {% if container.GetIdentifier() == 'nextcloud-aio-domaincheck' and container.GetRunningState().value == 'running' %}
{% set isDomaincheckRunning = true %} {% set isDomaincheckRunning = true %}
{% endif %} {% endif %}
{% if container.GetIdentifier() == 'nextcloud-aio-apache' and class(container.GetStartingState()) == 'AIO\\Container\\State\\StartingState' %} {% if container.GetIdentifier() == 'nextcloud-aio-apache' and container.GetStartingState().value == 'starting' %}
{% set isApacheStarting = true %} {% set isApacheStarting = true %}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
@ -261,14 +261,14 @@
{% for container in containers %} {% for container in containers %}
{% if container.GetDisplayName() != '' %} {% if container.GetDisplayName() != '' %}
<li> <li>
{% if class(container.GetStartingState()) == 'AIO\\Container\\State\\StartingState' %} {% if container.GetStartingState().value == 'starting' %}
<span class="status running"></span> <span class="status running"></span>
<span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Starting</a>) <span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Starting</a>)
{% if container.GetDocumentation() != '' %} {% if container.GetDocumentation() != '' %}
(<a href="{{ container.GetDocumentation() }}">docs</a>) (<a href="{{ container.GetDocumentation() }}">docs</a>)
{% endif %} {% endif %}
</span> </span>
{% elseif class(container.GetRunningState()) == 'AIO\\Container\\State\\RunningState' %} {% elseif container.GetRunningState().value == 'running' %}
<span class="status success"></span> <span class="status success"></span>
<span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Running</a>) <span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Running</a>)
{% if container.GetDocumentation() != '' %} {% if container.GetDocumentation() != '' %}