Compare commits

...

3 Commits

5 changed files with 89 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
<?php <?php
/* /*
* DeploymentService.php 2026-03-23 thomas * DeploymentService.php 2026-03-27 thomas
* *
* Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de> * Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de>
* Alle Rechte vorbehalten. * Alle Rechte vorbehalten.
@@ -41,7 +41,7 @@ abstract class DeploymentService implements DeploymentInterface
->setName($project->name) ->setName($project->name)
->setWorkingDir($project->projectDir) ->setWorkingDir($project->projectDir)
->setComposeFiles([$this->filesDir . '/compose.yaml', $this->filesDir . '/compose.prod.yaml']) ->setComposeFiles([$this->filesDir . '/compose.yaml', $this->filesDir . '/compose.prod.yaml'])
->daemonize(true) ->wait(true)
->up(['--build']); ->up(['--build']);
} }

View File

@@ -8,6 +8,7 @@
namespace App\Service\Docker; namespace App\Service\Docker;
use App\Service\Docker\Logger\LoggerService;
use App\Service\Process\CleanProcess; use App\Service\Process\CleanProcess;
use Dotenv\Dotenv; use Dotenv\Dotenv;
use Exception; use Exception;
@@ -24,6 +25,8 @@ class DockerCompose
protected bool $daemonize = false; protected bool $daemonize = false;
protected bool $wait = false;
protected string $dockerHost = ''; protected string $dockerHost = '';
protected string $workingDir = ''; protected string $workingDir = '';
@@ -41,10 +44,14 @@ class DockerCompose
*/ */
public function up(array $optionalArgs = []): void public function up(array $optionalArgs = []): void
{ {
$log = new LoggerService($this->workingDir);
$log->clearLog();
$log->log('Starting Docker Compose up command at ' . date('Y-m-d H:i:s') . ' with optional arguments: ' . implode(', ', $this->getExtraOptions($optionalArgs)));
$process = $this->process('up', $optionalArgs); $process = $this->process('up', $optionalArgs);
$process->run(function ($type, $buffer): void $process->run(function ($type, $buffer) use ($log): void
{ {
echo $buffer; $log->log($buffer);
}); });
// if(!$process->isSuccessful()) // if(!$process->isSuccessful())
@@ -161,6 +168,24 @@ class DockerCompose
$extraOptions[] = '-d'; $extraOptions[] = '-d';
} }
if($this->wait)
{
if(($key = array_search('-d', $extraOptions, true)) !== false)
{
unset($extraOptions[$key]);
}
if(($key = array_search('--detach', $extraOptions, true)) !== false)
{
unset($extraOptions[$key]);
}
if(!in_array('--wait', $extraOptions, true))
{
$extraOptions[] = '--wait';
}
}
return $extraOptions; return $extraOptions;
} }
@@ -248,6 +273,22 @@ class DockerCompose
} }
/**
* Sets the wait state for the current instance.
*
* This method allows enabling or disabling the wait behavior by updating
* the internal wait property. It returns the instance to allow method chaining.
*
* @param bool $wait The value to set for the wait property.
* @return self The current instance with the updated wait state.
*/
public function wait(bool $wait): self
{
$this->wait = $wait;
return $this;
}
/** /**
* Sets and merges Docker Compose files. * Sets and merges Docker Compose files.
* *

View File

@@ -0,0 +1,39 @@
<?php
/*
* LoggerService.php 2026-03-27 thomas
*
* Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de>
* Alle Rechte vorbehalten.
*/
namespace App\Service\Docker\Logger;
use Symfony\Component\Filesystem\Filesystem;
class LoggerService
{
const string LOG_FILE = 'docker.log';
public function __construct(
protected string $logDir,
){}
public function log(string $message): void
{
new Filesystem()->appendToFile($this->logDir . DIRECTORY_SEPARATOR . self::LOG_FILE, $message . PHP_EOL);
}
public function clearLog(): void
{
new Filesystem()->remove($this->logDir . DIRECTORY_SEPARATOR . self::LOG_FILE);
}
public function getLog(): string
{
return new Filesystem()->readFile($this->logDir . DIRECTORY_SEPARATOR . self::LOG_FILE);
}
}

View File

@@ -110,7 +110,7 @@ class GitService
public function fetchRepo(string $dir, array $options = []): void public function fetchRepo(string $dir, array $options = []): void
{ {
Process::fromShellCommandline( Process::fromShellCommandline(
command: 'git --all --tags', command: 'git fetch --all --tags',
cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir, cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir,
)->mustRun(); )->mustRun();
} }

View File

@@ -1,6 +1,6 @@
<?php <?php
/* /*
* CleanProcess.php 2026-03-27 thomas * CleanProcess.php 2026-04-9 thomas
* *
* Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de> * Copyright (c) 2026 Thomas Schneider <thomas@inter-mundos.de>
* Alle Rechte vorbehalten. * Alle Rechte vorbehalten.
@@ -27,8 +27,9 @@ class CleanProcess extends Process
private function parseEnvVars(string $keySelector, array &$env): void private function parseEnvVars(string $keySelector, array &$env): void
{ {
$preservedKeys = []; $preservedKeys = [];
$envVars = getenv($keySelector) ?: ($_SERVER[$keySelector] ?? $_ENV[$keySelector] ?? false);
if(empty(getenv($keySelector))) if(empty($envVars))
{ {
return; return;
} }
@@ -39,7 +40,7 @@ class CleanProcess extends Process
$preservedKeys += ['SSH_AUTH_SOCK']; $preservedKeys += ['SSH_AUTH_SOCK'];
} }
$vars = array_fill_keys(explode(',', getenv($keySelector)), false); $vars = array_fill_keys(explode(',', $envVars), false);
$env = [...array_diff_key($vars, array_flip($preservedKeys)), ...$env]; $env = [...array_diff_key($vars, array_flip($preservedKeys)), ...$env];
} }