fix: provisions DB on container startup

This commit is contained in:
2025-05-16 13:15:28 -05:00
parent cc4942a537
commit 7bc8720377
9 changed files with 231 additions and 2 deletions

View File

@@ -7,5 +7,8 @@ RUN apt-get update && \
docker-php-ext-install ldap
COPY --chown=www-data:www-data . /var/www
COPY --chmod=0775 ./bash/entrypoint.sh /usr/local/bin/
COPY ./bash/vhost.conf /etc/apache2/sites-enabled/vhost.conf
RUN rm /etc/apache2/sites-enabled/000-default.conf
ENTRYPOINT [ "/usr/local/bin/entrypoint.sh" ]

15
bash/entrypoint.sh Normal file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
# Sleep for a second to ensure DB is awake and ready
SLEEP_TIME=$(shuf -i 2-5 -n 1)
echo "> Sleeping for ${SLEEP_TIME} seconds to wait for the database"
echo "> If there are errors after the migration runs, it's possible another container (scheduler, worker, etc.) already ran the migrations"
sleep $SLEEP_TIME
# Provision database
php /var/www/bin/console doctrine:migrations:migrate --no-interaction
php /var/www/bin/console doctrine:fixtures:load --no-interaction
# Start Apache in the foreground
echo "Starting Apache..."
exec apachectl -D FOREGROUND

View File

@@ -15,6 +15,7 @@
"doctrine/doctrine-migrations-bundle": "^3.4",
"doctrine/orm": "^3.3",
"dragonmantank/cron-expression": "^3.4",
"league/pipeline": "^1.1",
"nesbot/carbon": "^3.9",
"nihilarr/parse-torrent-name": "^0.0.1",
"nyholm/psr7": "*",

58
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "06913c92f22c0b45934c395138318bb4",
"content-hash": "33f1579a34e7ada02bb4738a9b39f493",
"packages": [
{
"name": "1tomany/rich-bundle",
@@ -1792,6 +1792,62 @@
],
"time": "2025-01-26T21:29:45+00:00"
},
{
"name": "league/pipeline",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/pipeline.git",
"reference": "9069ddfdbd5582f8a563e00cffdbeffb9a0acd01"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/pipeline/zipball/9069ddfdbd5582f8a563e00cffdbeffb9a0acd01",
"reference": "9069ddfdbd5582f8a563e00cffdbeffb9a0acd01",
"shasum": ""
},
"require": {
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0 || ^10.0 || ^11.5"
},
"type": "library",
"autoload": {
"psr-4": {
"League\\Pipeline\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Frank de Jonge",
"email": "info@frenky.net",
"role": "Author"
},
{
"name": "Woody Gilk",
"email": "woody.gilk@gmail.com",
"role": "Maintainer"
}
],
"description": "A plug and play pipeline implementation.",
"keywords": [
"composition",
"design pattern",
"pattern",
"pipeline",
"sequential"
],
"support": {
"issues": "https://github.com/thephpleague/pipeline/issues",
"source": "https://github.com/thephpleague/pipeline/tree/1.1.0"
},
"time": "2025-02-06T08:48:15+00:00"
},
{
"name": "nesbot/carbon",
"version": "3.9.1",

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Command;
use App\User\Framework\Pipeline\GettingStarted\AddPreferencesToDatabase;
use App\User\Framework\Pipeline\GettingStarted\GettingStartedInput;
use App\User\Framework\Pipeline\GettingStarted\MigrateDatabase;
use App\User\Framework\Repository\PreferencesRepository;
use League\Pipeline\Pipeline;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'provision',
description: 'Provision the application for a fresh install. This command migrate the database the latest version and seed it with some required data. This command will do nothing if the app is already provisioned.',
)]
class ProvisionCommand extends Command
{
private PreferencesRepository $preferencesRepository;
private LoggerInterface $logger;
public function __construct(
PreferencesRepository $preferencesRepository,
LoggerInterface $logger,
\Symfony\Component\HttpKernel\KernelInterface $kernel
) {
parent::__construct();
$this->preferencesRepository = $preferencesRepository;
$this->logger = $logger;
$this->kernel = $kernel;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$pipelineInput = new GettingStartedInput(
kernel: $this->kernel,
preferenceRepository: $this->preferencesRepository,
logger: $this->logger,
);
$pipeline = (new Pipeline)
->pipe(new MigrateDatabase)
->pipe(new AddPreferencesToDatabase);
$pipeline->process($pipelineInput);
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
return Command::SUCCESS;
}
}

View File

@@ -6,10 +6,19 @@ use App\User\Action\Command\RegisterUserCommand;
use App\User\Action\Handler\RegisterUserHandler;
use App\User\Framework\Entity\User;
use App\User\Framework\Form\RegistrationFormType;
use App\User\Framework\Pipeline\GettingStarted\AddPreferencesToDatabase;
use App\User\Framework\Pipeline\GettingStarted\GettingStartedInput;
use App\User\Framework\Pipeline\GettingStarted\MigrateDatabase;
use App\User\Framework\Repository\PreferencesRepository;
use App\User\Framework\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use League\Pipeline\Pipeline;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController
@@ -45,8 +54,12 @@ class RegistrationController extends AbstractController
}
#[Route(path: '/getting-started', name: 'app_getting_started')]
public function gettingStarted(Request $request, Security $security): Response
public function gettingStarted(Request $request, Security $security, UserRepository $userRepository, PreferencesRepository $preferencesRepository, KernelInterface $kernel, LoggerInterface $logger): Response
{
if ((new ArrayCollection($userRepository->findAll()))->count() !== 0) {
return $this->redirectToRoute('app_index');
}
$form = $this->createForm(RegistrationFormType::class, new User());
$form->handleRequest($request);

View File

@@ -0,0 +1,36 @@
<?php
namespace App\User\Framework\Pipeline\GettingStarted;
use App\User\Framework\Repository\PreferencesRepository;
use League\Pipeline\StageInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class AddPreferencesToDatabase implements StageInterface
{
/** @var GettingStartedInput $input */
public function __invoke($input)
{
if ($input->preferenceRepository->count() > 0) {
$input->logger->info('[AddPreferencesToDatabase] > Preferences already exist, skipping...');
return $input;
}
$app = new Application($input->kernel);
$app->setAutoExit(false);
$commandInput = new ArrayInput([
'command' => 'doctrine:fixtures:load',
'--no-interaction',
]);
$output = new BufferedOutput();
$app->run($commandInput, $output);
return $input;
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\User\Framework\Pipeline\GettingStarted;
use App\User\Framework\Repository\PreferencesRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class GettingStartedInput
{
public function __construct(
public readonly KernelInterface $kernel,
public readonly PreferencesRepository $preferenceRepository,
public readonly LoggerInterface $logger
) {}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\User\Framework\Pipeline\GettingStarted;
use League\Pipeline\StageInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
class MigrateDatabase implements StageInterface
{
/** @var GettingStartedInput $input */
public function __invoke($input)
{
$app = new Application($input->kernel);
$app->setAutoExit(false);
$commandInput = new ArrayInput([
'command' => 'doctrine:migrations:migrate',
'--no-interaction' => true,
]);
$output = new BufferedOutput();
$app->run($commandInput, $output);
$input->logger->info('[MigrateDatabase] > Migrating database...', ['output' => $output->fetch()]);
return $input;
}
}