Improuve Container States

Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com>
This commit is contained in:
Jean-Yves 2024-10-21 11:18:30 +02:00
parent a1bc150612
commit 4798489435
No known key found for this signature in database
GPG key ID: 644C8B9C4CABAEF7
7 changed files with 156 additions and 132 deletions

View file

@ -5,6 +5,7 @@ namespace AIO\Container;
use AIO\Data\ConfigurationManager;
use AIO\Docker\DockerActionManager;
use AIO\ContainerDefinitionFetcher;
use GuzzleHttp\Exception\GuzzleException;
readonly class Container {
public function __construct(
@ -112,20 +113,13 @@ readonly class Container {
return $this->volumes;
}
public function GetRunningState() : ContainerState {
return $this->dockerActionManager->GetContainerRunningState($this);
/** @throws GuzzleException */
public function GetContainerState() : ContainerState {
return $this->dockerActionManager->GetContainerState($this);
}
public function GetRestartingState() : ContainerState {
return $this->dockerActionManager->GetContainerRestartingState($this);
}
public function GetUpdateState() : VersionState {
return $this->dockerActionManager->GetContainerUpdateState($this);
}
public function GetStartingState() : ContainerState {
return $this->dockerActionManager->GetContainerStartingState($this);
public function GetUpdateState() : UpdateState {
return $this->dockerActionManager->GetUpdateState($this);
}
/**

View file

@ -2,11 +2,35 @@
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';
enum ContainerState {
case DoesNotExist;
case Restarting;
case Healthy;
case Starting;
case Stopped;
case Unhealthy;
public function isStopped(): bool {
return $this == self::Stopped;
}
public function isStarting(): bool {
return $this == self::Starting;
}
public function isRestarting(): bool {
return $this == self::Restarting;
}
public function isHealthy(): bool {
return $this == self::Healthy;
}
public function isUnhealthy(): bool {
return $this == self::Unhealthy;
}
public function isRunning(): bool {
return $this->isHealthy() || $this->isUnhealthy() || $this->isStarting() || $this->isRestarting();
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace AIO\Container;
enum UpdateState {
case Outdated;
case Latest;
public function isUpdatableAvailable(): bool {
return $this == self::Outdated;
}
}

View file

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

View file

@ -5,6 +5,7 @@ namespace AIO\Controller;
use AIO\Container\ContainerState;
use AIO\ContainerDefinitionFetcher;
use AIO\Docker\DockerActionManager;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use AIO\Data\ConfigurationManager;
@ -19,6 +20,7 @@ readonly class DockerController {
) {
}
/** @throws GuzzleException */
private function PerformRecursiveContainerStart(string $id, bool $pullImage = true) : void {
$container = $this->containerDefinitionFetcher->GetContainerById($id);
@ -28,7 +30,7 @@ readonly class DockerController {
// Don't start if container is already running
// This is expected to happen if a container is defined in depends_on of multiple containers
if ($container->GetRunningState() === ContainerState::Running) {
if ($container->GetContainerState()->isRunning()) {
error_log('Not starting ' . $id . ' because it was already started.');
return;
}
@ -240,6 +242,7 @@ readonly class DockerController {
$this->PerformRecursiveContainerStop($id);
}
/** @throws GuzzleException */
public function StartDomaincheckContainer() : void
{
# Don't start if domain is already set
@ -254,10 +257,10 @@ readonly class DockerController {
$domaincheckContainer = $this->containerDefinitionFetcher->GetContainerById($id);
$apacheContainer = $this->containerDefinitionFetcher->GetContainerById(self::TOP_CONTAINER);
// Don't start if apache is already running
if ($apacheContainer->GetRunningState() === ContainerState::Running) {
if ($apacheContainer->GetContainerState()->isRunning()) {
return;
// Don't start if domaincheck is already running
} elseif ($domaincheckContainer->GetRunningState() === ContainerState::Running) {
} elseif ($domaincheckContainer->GetContainerState()->isRunning()) {
$domaincheckWasStarted = apcu_fetch($cacheKey);
// Start domaincheck again when 10 minutes are over by not returning here
if($domaincheckWasStarted !== false && is_string($domaincheckWasStarted)) {

View file

@ -3,10 +3,13 @@
namespace AIO\Docker;
use AIO\Container\Container;
use AIO\Container\VersionState;
use AIO\Container\UpdateState;
use AIO\Container\ContainerState;
use AIO\Data\ConfigurationManager;
use AssertionError;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use AIO\ContainerDefinitionFetcher;
use http\Env\Response;
@ -35,50 +38,7 @@ readonly class DockerActionManager {
return $container->GetContainerName() . ':' . $tag;
}
public function GetContainerRunningState(Container $container) : ContainerState
{
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier())));
try {
$response = $this->guzzleClient->get($url);
} catch (RequestException $e) {
if ($e->getCode() === 404) {
return ContainerState::ImageDoesNotExist;
}
throw $e;
}
$responseBody = json_decode((string)$response->getBody(), true);
if ($responseBody['State']['Running'] === true) {
return ContainerState::Running;
} else {
return ContainerState::Stopped;
}
}
public function GetContainerRestartingState(Container $container) : ContainerState
{
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier())));
try {
$response = $this->guzzleClient->get($url);
} catch (RequestException $e) {
if ($e->getCode() === 404) {
return ContainerState::ImageDoesNotExist;
}
throw $e;
}
$responseBody = json_decode((string)$response->getBody(), true);
if ($responseBody['State']['Restarting'] === true) {
return ContainerState::Restarting;
} else {
return ContainerState::NotRestarting;
}
}
public function GetContainerUpdateState(Container $container) : VersionState
{
public function GetUpdateState(Container $container): UpdateState {
$tag = $container->GetImageTag();
if ($tag === '%AIO_CHANNEL%') {
$tag = $this->GetCurrentChannel();
@ -86,47 +46,66 @@ readonly class DockerActionManager {
$runningDigests = $this->GetRepoDigestsOfContainer($container->GetIdentifier());
if ($runningDigests === null) {
return VersionState::Different;
return UpdateState::Outdated;
}
$remoteDigest = $this->dockerHubManager->GetLatestDigestOfTag($container->GetContainerName(), $tag);
if ($remoteDigest === null) {
return VersionState::Equal;
return UpdateState::Latest;
}
foreach($runningDigests as $runningDigest) {
if ($runningDigest === $remoteDigest) {
return VersionState::Equal;
}
}
return VersionState::Different;
return in_array($remoteDigest, $runningDigests, true) ? UpdateState::Latest : UpdateState::Outdated;
}
public function GetContainerStartingState(Container $container) : ContainerState
{
$runningState = $this->GetContainerRunningState($container);
if ($runningState === ContainerState::Stopped || $runningState === ContainerState::ImageDoesNotExist) {
return $runningState;
}
$containerName = $container->GetIdentifier();
$internalPort = $container->GetInternalPort();
if($internalPort === '%APACHE_PORT%') {
$internalPort = $this->configurationManager->GetApachePort();
} elseif($internalPort === '%TALK_PORT%') {
$internalPort = $this->configurationManager->GetTalkPort();
}
if ($internalPort !== "" && $internalPort !== 'host') {
$connection = @fsockopen($containerName, (int)$internalPort, $errno, $errstr, 0.2);
if ($connection) {
fclose($connection);
return ContainerState::Running;
} else {
return ContainerState::Starting;
/** @throws GuzzleException */
public function GetContainerState(Container $container): ContainerState {
$url = $this->BuildApiUrl(sprintf('containers/%s/json', urlencode($container->GetIdentifier())));
try {
$response = $this->guzzleClient->get($url);
} catch (GuzzleException $e) {
if ($e->getCode() === 404) {
return ContainerState::DoesNotExist;
}
} else {
return ContainerState::Running;
throw $e;
}
$body = json_decode($response->getBody()->getContents(), true);
assert(is_array($body));
assert(is_array($body['State']));
$state = match ($body['State']['Status']) {
'running' => ContainerState::Healthy,
'created' => ContainerState::Starting,
'restarting' => ContainerState::Restarting,
'paused', 'removing', 'exited', 'dead' => ContainerState::Stopped,
default => throw new AssertionError()
};
if ($state->isHealthy() && is_array($body['State']['Health'])) {
$state = match ($body['State']['Health']['Status']) {
'starting' => ContainerState::Starting,
'unhealthy' => ContainerState::Unhealthy,
default => $state,
};
}
if ($state->isHealthy()) {
$containerName = $container->GetIdentifier();
$internalPort = $container->GetInternalPort();
if ($internalPort === '%APACHE_PORT%') {
$internalPort = $this->configurationManager->GetApachePort();
} elseif ($internalPort === '%TALK_PORT%') {
$internalPort = $this->configurationManager->GetTalkPort();
}
if (is_numeric($internalPort)) {
$connection = @fsockopen($containerName, intval($internalPort), $errno, $errstr, 0.2);
if ($connection) {
fclose($connection);
} else {
$state = ContainerState::Starting;
}
}
}
return $state;
}
public function DeleteContainer(Container $container) : void {
@ -167,7 +146,7 @@ readonly class DockerActionManager {
try {
$this->guzzleClient->post($url);
} catch (RequestException $e) {
throw new \Exception("Could not start container " . $container->GetIdentifier() . ": " . $e->getMessage());
throw new Exception("Could not start container " . $container->GetIdentifier() . ": " . $e->getMessage());
}
}
@ -404,7 +383,7 @@ readonly class DockerActionManager {
} else {
$secret = $this->configurationManager->GetSecret($out[1]);
if ($secret === "") {
throw new \Exception("The secret " . $out[1] . " is empty. Cannot substitute its value. Please check if it is defined in secrets of containers.json.");
throw new Exception("The secret " . $out[1] . " is empty. Cannot substitute its value. Please check if it is defined in secrets of containers.json.");
}
$replacements[1] = $secret;
}
@ -573,7 +552,7 @@ readonly class DockerActionManager {
]
);
} catch (RequestException $e) {
throw new \Exception("Could not create container " . $container->GetIdentifier() . ": " . $e->getMessage());
throw new Exception("Could not create container " . $container->GetIdentifier() . ": " . $e->getMessage());
}
}
@ -609,17 +588,16 @@ readonly class DockerActionManager {
$this->guzzleClient->post($url);
} catch (RequestException $e) {
if ($imageIsThere === false) {
throw new \Exception("Could not pull image " . $imageName . ". Please run 'sudo docker exec -it nextcloud-aio-mastercontainer docker pull " . $imageName . "' in order to find out why it failed.");
throw new Exception("Could not pull image " . $imageName . ". Please run 'sudo docker exec -it nextcloud-aio-mastercontainer docker pull " . $imageName . "' in order to find out why it failed.");
}
}
}
private function isContainerUpdateAvailable(string $id) : string
{
private function isContainerUpdateAvailable(string $id): string {
$container = $this->containerDefinitionFetcher->GetContainerById($id);
$updateAvailable = "";
if ($container->GetUpdateState() === VersionState::Different) {
if ($container->GetUpdateState() === UpdateState::Outdated) {
$updateAvailable = '1';
}
foreach ($container->GetDependsOn() as $dependency) {
@ -718,7 +696,7 @@ readonly class DockerActionManager {
}
return null;
} catch (\Exception $e) {
} catch (Exception $e) {
return null;
}
}
@ -747,7 +725,7 @@ readonly class DockerActionManager {
$tag = 'latest';
}
return $tag;
} catch (\Exception $e) {
} catch (Exception $e) {
error_log('Could not get current channel ' . $e->getMessage());
}
@ -778,9 +756,9 @@ readonly class DockerActionManager {
return true;
}
public function sendNotification(Container $container, string $subject, string $message, string $file = '/notify.sh') : void
{
if ($this->GetContainerStartingState($container) === ContainerState::Running) {
/** @throws GuzzleException */
public function sendNotification(Container $container, string $subject, string $message, string $file = '/notify.sh'): void {
if ($this->GetContainerState($container)->isHealthy()) {
$containerName = $container->GetIdentifier();
@ -867,7 +845,7 @@ readonly class DockerActionManager {
} catch (RequestException $e) {
// 409 is undocumented and gets thrown if the network already exists.
if ($e->getCode() !== 409) {
throw new \Exception("Could not create the nextcloud-aio network: " . $e->getMessage());
throw new Exception("Could not create the nextcloud-aio network: " . $e->getMessage());
}
}
@ -961,19 +939,24 @@ readonly class DockerActionManager {
}
}
public function isLoginAllowed() : bool {
/**
* @throws GuzzleException
* @throws Exception
*/
public function isLoginAllowed(): bool {
$id = 'nextcloud-aio-apache';
$apacheContainer = $this->containerDefinitionFetcher->GetContainerById($id);
if ($this->GetContainerStartingState($apacheContainer) === ContainerState::Running) {
if ($this->GetContainerState($apacheContainer)->isRunning()) {
return false;
}
return true;
}
public function isBackupContainerRunning() : bool {
/** @throws GuzzleException */
public function isBackupContainerRunning(): bool {
$id = 'nextcloud-aio-borgbackup';
$backupContainer = $this->containerDefinitionFetcher->GetContainerById($id);
if ($this->GetContainerRunningState($backupContainer) === ContainerState::Running) {
if ($this->GetContainerState($backupContainer)->isRunning()) {
return true;
}
return false;
@ -991,7 +974,7 @@ readonly class DockerActionManager {
}
return str_replace('T', ' ', (string)$imageOutput['Created']);
} catch (\Exception $e) {
} catch (Exception $e) {
return null;
}
}

View file

@ -41,19 +41,20 @@
{% endif %}
{% for container in containers %}
{% if container.GetDisplayName() != '' and container.GetRunningState().value == 'running' %}
{% set runingState = container.GetContainerState() %}
{% if container.GetDisplayName() != '' and runingState.isRunning() %}
{% set isAnyRunning = true %}
{% endif %}
{% if container.GetDisplayName() != '' and container.GetRestartingState().value == 'restarting' %}
{% if container.GetDisplayName() != '' and runingState.isRestarting() %}
{% set isAnyRestarting = true %}
{% endif %}
{% if container.GetIdentifier() == 'nextcloud-aio-watchtower' and container.GetRunningState().value == 'running' %}
{% if container.GetIdentifier() == 'nextcloud-aio-watchtower' and container.GetContainerState().isRunning() %}
{% set isWatchtowerRunning = true %}
{% endif %}
{% if container.GetIdentifier() == 'nextcloud-aio-domaincheck' and container.GetRunningState().value == 'running' %}
{% if container.GetIdentifier() == 'nextcloud-aio-domaincheck' and container.GetContainerState().isRunning() %}
{% set isDomaincheckRunning = true %}
{% endif %}
{% if container.GetIdentifier() == 'nextcloud-aio-apache' and container.GetStartingState().value == 'starting' %}
{% if container.GetIdentifier() == 'nextcloud-aio-apache' and runingState.isStarting() %}
{% set isApacheStarting = true %}
{% endif %}
{% endfor %}
@ -262,14 +263,29 @@
{% for container in containers %}
{% if container.GetDisplayName() != '' %}
<li>
{% if container.GetStartingState().value == 'starting' %}
{% set runningState = container.GetContainerState() %}
{% if runningState.isStarting() %}
<span class="status running"></span>
<span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Starting</a>)
{% if container.GetDocumentation() != '' %}
(<a href="{{ container.GetDocumentation() }}">docs</a>)
{% endif %}
</span>
{% elseif container.GetRunningState().value == 'running' %}
{% elseif runningState.isUnhealthy() %}
<span class="status running"></span>
<span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Unhealthy</a>)
{% if container.GetDocumentation() != '' %}
(<a href="{{ container.GetDocumentation() }}">docs</a>)
{% endif %}
</span>
{% elseif runningState.isRestarting() %}
<span class="status running"></span>
<span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Restarting</a>)
{% if container.GetDocumentation() != '' %}
(<a href="{{ container.GetDocumentation() }}">docs</a>)
{% endif %}
</span>
{% elseif runningState.isHealthy() %}
<span class="status success"></span>
<span>{{ container.GetDisplayName() }} (<a href="/api/docker/logs?id={{ container.GetIdentifier() }}" target="_blank" rel="noopener">Running</a>)
{% if container.GetDocumentation() != '' %}