diff --git a/.castor.php b/.castor.php deleted file mode 100644 index 10a7056..0000000 --- a/.castor.php +++ /dev/null @@ -1,99 +0,0 @@ - $projectName, - 'project_directory' => 'application', - 'root_domain' => $projectName . '.local', - 'extra_domains' => [ - 'www.' . $projectName . '.local', - ], - 'php_version' => '8.1', - 'docker_compose_files' => [ - 'docker-compose.yml', - 'docker-compose.worker.yml', - 'docker-compose.builder.yml', - ], - 'services_to_build_first' => [ - 'php-base', - 'builder', - ], - 'dinghy' => false, - 'macos' => false, - 'power_shell' => false, - 'user_id' => 1000, - 'root_dir' => __DIR__, - 'start_workers' => false, - 'composer_cache_dir' => '~/.composer/cache', - ]); - - if (file_exists($context['root_dir'] . '/infrastructure/docker/docker-compose.override.yml')) { - $context['docker_compose_files'][] = 'docker-compose.override.yml'; - } - -// $composerDirectory = exec('composer global config cache-dir -q')->getOutput(); -// -// if ($composerDirectory) { -// $context['composer_cache_dir'] = trim($composerDirectory); -// } - - $platform = php_uname('s'); - - if (stripos($platform, 'darwin') !== false) { - $context['macos'] = true; - } - - return $context; -} - -function docker_compose(Context $context, string $command): \Symfony\Component\Process\Process -{ - $domains = [$context['root_domain'], ...$context['extra_domains']]; - $domains = implode(' ', $domains); - - $environment = [ - 'PROJECT_NAME' => $context['project_name'], - 'PROJECT_DIRECTORY' => $context['project_directory'], - 'PROJECT_ROOT_DOMAIN' => $context['root_domain'], - 'PROJECT_DOMAINS' => $domains, - 'PROJECT_START_WORKERS' => $context['start_workers'], - 'COMPOSER_CACHE_DIR' => $context['composer_cache_dir'], - 'PHP_VERSION' => $context['php_version'], - ]; - - $dockerComposeFiles = implode(' -f ', array_map(function ($file) use ($context) { - return $context['root_dir'] . '/infrastructure/docker/' . $file; - }, $context['docker_compose_files'])); - - $command = "docker compose -p {$context['project_name']} -f $dockerComposeFiles $command"; - - return exec($command, environment: $environment); -} - -#[Task(description: 'Build the infrastructure')] -function build(Context $context) { - $command = "build --build-arg PROJECT_NAME={$context['project_name']} --build-arg USER_ID={$context['user_id']} --build-arg PHP_VERSION={$context['php_version']}"; - - foreach ($context['services_to_build_first'] as $service) { - $serviceCommand = "$command $service"; - - docker_compose($context, $serviceCommand); - } - - return docker_compose($context, $command); -} - -#[Task(description: 'Build and start the infrastructure')] -function up(Context $context) { - build($context); - - return docker_compose($context, 'up --remove-orphans --detach'); -} diff --git a/.castor/infra.php b/.castor/infra.php new file mode 100644 index 0000000..fe4eb77 --- /dev/null +++ b/.castor/infra.php @@ -0,0 +1,223 @@ +error('An error occured while starting the infrastructure.'); + io()->note('Did you forget to run "castor infra:build"?'); + io()->note('Or you forget to login to the registry?'); + + throw $e; + } +} + +#[AsTask(description: 'Stops the infrastructure', aliases: ['stop'])] +function stop(): void +{ + docker_compose(['stop']); +} + +#[AsTask(description: 'Displays infrastructure logs')] +function logs(): void +{ + docker_compose(['logs', '-f', '--tail', '150'], c: get_context()->withTty()); +} + +#[AsTask(description: 'Lists containers status')] +function ps(): void +{ + docker_compose(['ps'], withBuilder: false); +} + +#[AsTask(description: 'Cleans the infrastructure (remove container, volume, networks)')] +function destroy( + #[AsOption(description: 'Force the destruction without confirmation', shortcut: 'f')] + bool $force = false, +): void { + if (!$force) { + io()->warning('This will permanently remove all containers, volumes, networks... created for this project.'); + io()->note('You can use the --force option to avoid this confirmation.'); + if (!io()->confirm('Are you sure?', false)) { + io()->comment('Aborted.'); + + return; + } + } + + docker_compose(['down', '--remove-orphans', '--volumes', '--rmi=local'], withBuilder: true); + $files = finder() + ->in(variable('root_dir') . '/infrastructure/docker/services/router/etc/ssl/certs/') + ->name('*.pem') + ->files() + ; + fs()->remove($files); +} + +#[AsTask(description: 'Generates SSL certificates (with mkcert if available or self-signed if not)')] +function generate_certificates( + #[AsOption(description: 'Force the certificates re-generation without confirmation', shortcut: 'f')] + bool $force = false, +): void { + if (file_exists(variable('root_dir') . '/infrastructure/docker/services/router/etc/ssl/certs/cert.pem') && !$force) { + io()->comment('SSL certificates already exists.'); + io()->note('Run "castor infra:generate-certificates --force" to generate new certificates.'); + + return; + } + + if ($force) { + if (file_exists($f = variable('root_dir') . '/infrastructure/docker/services/router/etc/ssl/certs/cert.pem')) { + io()->comment('Removing existing certificates in infrastructure/docker/services/router/etc/ssl/certs/*.pem.'); + unlink($f); + } + + if (file_exists($f = variable('root_dir') . '/infrastructure/docker/services/router/etc/ssl/certs/key.pem')) { + unlink($f); + } + } + + $finder = new ExecutableFinder(); + $mkcert = $finder->find('mkcert'); + + if ($mkcert) { + $pathCaRoot = capture(['mkcert', '-CAROOT']); + + if (!is_dir($pathCaRoot)) { + io()->warning('You must have mkcert CA Root installed on your host with "mkcert -install" command.'); + + return; + } + + $rootDomain = variable('root_domain'); + + run([ + 'mkcert', + '-cert-file', 'infrastructure/docker/services/router/etc/ssl/certs/cert.pem', + '-key-file', 'infrastructure/docker/services/router/etc/ssl/certs/key.pem', + $rootDomain, + "*.{$rootDomain}", + ...variable('extra_domains'), + ]); + + io()->success('Successfully generated SSL certificates with mkcert.'); + + if ($force) { + io()->note('Please restart the infrastructure to use the new certificates with "castor up" or "castor start".'); + } + + return; + } + + run(['infrastructure/docker/services/router/generate-ssl.sh']); + + io()->success('Successfully generated self-signed SSL certificates in infrastructure/docker/services/router/etc/ssl/certs/*.pem.'); + io()->comment('Consider installing mkcert to generate locally trusted SSL certificates and run "castor infra:generate-certificates --force".'); + + if ($force) { + io()->note('Please restart the infrastructure to use the new certificates with "castor up" or "castor start".'); + } +} + +#[AsTask(description: 'Starts the workers', namespace: 'infra:worker', name: 'start')] +function workers_start(): void +{ + $workers = get_workers(); + + if (!$workers) { + return; + } + + run([ + 'docker', + 'update', + '--restart=unless-stopped', + ...$workers, + ], quiet: true); + + run([ + 'docker', + 'start', + ...$workers, + ], quiet: true); +} + +#[AsTask(description: 'Stops the workers', namespace: 'infra:worker', name: 'stop')] +function workers_stop(): void +{ + $workers = get_workers(); + + if (!$workers) { + return; + } + + run([ + 'docker', + 'update', + '--restart=no', + ...$workers, + ]); + + run([ + 'docker', + 'stop', + ...$workers, + ]); +} + +/** + * Find worker containers for the current project. + * + * @return array + */ +function get_workers(): array +{ + $command = [ + 'docker', + 'ps', + '-a', + '--filter', 'label=docker-starter.worker.' . variable('project_name'), + '--quiet', + ]; + + $out = capture($command); + if (!$out) { + return []; + } + + $workers = explode("\n", $out); + + return array_map('trim', $workers); +} diff --git a/.castor/utils.php b/.castor/utils.php new file mode 100644 index 0000000..b626838 --- /dev/null +++ b/.castor/utils.php @@ -0,0 +1,194 @@ +section('About this project'); + + io()->comment('Run castor to display all available commands.'); + io()->comment('Run castor about to display this project help.'); + io()->comment('Run castor help [command] to display Castor help.'); + + io()->section('Available URLs for this project:'); + $urls = [variable('root_domain'), ...variable('extra_domains')]; + + $payload = @file_get_contents(sprintf('http://%s:8080/api/http/routers', variable('root_domain'))); + if ($payload) { + $routers = json_decode($payload, true); + $projectName = variable('project_name'); + foreach ($routers as $router) { + if (!preg_match("{^{$projectName}-(.*)@docker$}", $router['name'])) { + continue; + } + if ("frontend-{$projectName}" === $router['service']) { + continue; + } + if (!preg_match('{^Host\\(`(?P.*)`\\)$}', $router['rule'], $matches)) { + continue; + } + $hosts = explode('`, `', $matches['hosts']); + $urls = [...$urls, ...$hosts]; + } + } + io()->listing(array_map(fn ($url) => "https://{$url}", $urls)); +} + +#[AsTask(description: 'Opens a shell (bash) into a builder container')] +function builder(string $user = 'app'): void +{ + $c = get_context() + ->withTimeout(null) + ->withTty() + ->withEnvironment($_ENV + $_SERVER) + ->withQuiet() + ->withAllowFailure() + ; + docker_compose_run('bash', c: $c, user: $user); +} + +#[AsContext(default: true)] +function create_default_context(): Context +{ + $data = create_default_variables() + [ + 'docker_compose_files' => [ + 'docker-compose.yml', + 'docker-compose.worker.yml', + ], + 'macos' => false, + 'power_shell' => false, + 'user_id' => posix_geteuid(), + 'root_dir' => dirname(__DIR__), + 'env' => $_SERVER['CI'] ?? false ? 'ci' : 'dev', + ]; + + if (file_exists($data['root_dir'] . '/infrastructure/docker/docker-compose.override.yml')) { + $data['docker_compose_files'][] = 'docker-compose.override.yml'; + } + + $data['composer_cache_dir'] = cache('composer_cache_dir', fn () => capture(['composer', 'global', 'config', 'cache-dir', '-q'], onFailure: sys_get_temp_dir() . '/castor/composer')); + + $platform = strtolower(php_uname('s')); + if (str_contains($platform, 'darwin')) { + $data['macos'] = true; + $data['docker_compose_files'][] = 'docker-compose.docker-for-x.yml'; + } elseif (in_array($platform, ['win32', 'win64'])) { + $data['docker_compose_files'][] = 'docker-compose.docker-for-x.yml'; + $data['power_shell'] = true; + } + + if ($data['user_id'] > 256000) { + $data['user_id'] = 1000; + } + + if (0 === $data['user_id']) { + log('Running as root? Fallback to fake user id.', 'warning'); + $data['user_id'] = 1000; + } + + return new Context($data, pty: 'dev' === $data['env']); +} + +function docker_compose_run( + string $runCommand, + Context $c = null, + string $service = 'builder', + string $user = 'app', + bool $noDeps = true, + string $workDir = null, + bool $portMapping = false, + bool $withBuilder = true, +): Process { + $command = [ + 'run', + '--rm', + '-u', $user, + ]; + + if ($noDeps) { + $command[] = '--no-deps'; + } + + if ($portMapping) { + $command[] = '--service-ports'; + } + + if (null !== $workDir) { + $command[] = '-w'; + $command[] = $workDir; + } + + $command[] = $service; + $command[] = '/bin/sh'; + $command[] = '-c'; + $command[] = "exec {$runCommand}"; + + return docker_compose($command, c: $c, withBuilder: $withBuilder); +} + +/** + * @param array $subCommand + */ +function docker_compose(array $subCommand, Context $c = null, bool $withBuilder = false): Process +{ + $c ??= get_context(); + + $domains = [variable('root_domain'), ...variable('extra_domains')]; + $domains = '`' . implode('`, `', $domains) . '`'; + + $c = $c + ->withTimeout(null) + ->withEnvironment([ + 'PROJECT_NAME' => variable('project_name'), + 'PROJECT_DIRECTORY' => variable('project_directory'), + 'PROJECT_ROOT_DOMAIN' => variable('root_domain'), + 'PROJECT_DOMAINS' => $domains, + 'COMPOSER_CACHE_DIR' => variable('composer_cache_dir'), + 'PHP_VERSION' => variable('php_version'), + ], false) + ; + + $command = [ + 'docker', + 'compose', + '-p', variable('project_name'), + ]; + + foreach (variable('docker_compose_files') as $file) { + $command[] = '-f'; + $command[] = variable('root_dir') . '/infrastructure/docker/' . $file; + } + if ($withBuilder) { + $command[] = '-f'; + $command[] = variable('root_dir') . '/infrastructure/docker/docker-compose.builder.yml'; + } + + $command = array_merge($command, $subCommand); + + return run($command, context: $c); +} + +// Mac users have a lot of problems running Yarn / Webpack on the Docker stack +// so this func allow them to run these tools on their host +function run_in_docker_or_locally_for_mac(string $command, Context $c = null): void +{ + $c ??= get_context(); + + if (variable('macos')) { + run($command, context: $c->withPath(variable('root_dir'))); + } else { + docker_compose_run($command, c: $c); + } +} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6045ed8..83fc1b3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,29 +1,29 @@ -name: CI +name: Continuous Integration 'on': push: - branches: - - main - pull_request: null + branches: [ "main" ] + pull_request: + branches: [ "main" ] schedule: - - - cron: '0 0 * * MON' + - cron: '0 0 * * MON' + +permissions: + contents: read jobs: - check-python-cs: - name: Check Python CS + check-cs: + name: Check Coding Standards runs-on: ubuntu-latest steps: - - - uses: actions/checkout@v3 - - - uses: actions/setup-python@v4 - with: - python-version: '3.x' - - - run: 'pip install pycodestyle' - - - run: 'pycodestyle --ignore=E501,W605,E722 invoke.py tasks.py' + - name: Checkout + uses: actions/checkout@v3 + + - name: PHP-CS-Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --config=.php-cs-fixer.php --diff --dry-run + ci: name: Test with PHP ${{ matrix.php-version }} strategy: @@ -31,6 +31,10 @@ jobs: matrix: php-version: ['7.4', '8.0', '8.1', '8.2'] runs-on: ubuntu-latest + env: + DS_PHP_VERSION: ${{ matrix.php-version }} + BUILDKIT_PROGRESS: plain + CI: 1 steps: - uses: actions/checkout@v3 @@ -38,21 +42,20 @@ jobs: # Setup - - uses: actions/setup-python@v4 + name: Setup PHP + uses: shivammathur/setup-php@v2 with: - python-version: '3.9' - - - run: 'pip3 install pipenv' - - - name: Set PHP version - run: sed -i "s/^php_version = .*/php_version = '${{ matrix.php-version }}'/" invoke.py + php-version: 8.2 + tools: jolicode/castor - # Install the stack and run the tests + # - + # name: 'Download castor' + # run: 'wget https://github.com/jolicode/castor/releases/latest/download/castor.linux-amd64.phar && chmod +x castor.linux-amd64.phar && sudo mv castor.linux-amd64.phar /usr/local/bin/castor' - - run: 'pipenv install --deploy --dev' - - - run: 'pipenv run inv start && sleep 5' + name: 'Build and start the infrastructure' + run: 'castor start && sleep 5' + - name: 'Test the infrastructure is working' run: | @@ -60,6 +63,7 @@ jobs: set -o pipefail curl --fail --insecure --silent -H "Host: app.test" https://127.0.0.1 | grep "Hello world" curl --fail --insecure --silent -H "Host: app.test" https://127.0.0.1 | grep "${{ matrix.php-version }}" + - name: 'Test communication with DB' run: | diff --git a/.gitignore b/.gitignore index 904027b..d7e4e80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -*.pyc +/.castor.stub.php /infrastructure/docker/docker-compose.override.yml /infrastructure/docker/services/router/etc/ssl/certs/*.pem diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..29a627d --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,27 @@ +ignoreVCSIgnored(true) + ->ignoreDotFiles(false) + ->in(__DIR__) + ->append([ + __FILE__, + ]) +; + +return (new PhpCsFixer\Config()) + ->setRiskyAllowed(true) + ->setRules([ + '@PHP81Migration' => true, + '@PhpCsFixer' => true, + '@Symfony' => true, + '@Symfony:risky' => true, + 'php_unit_internal_class' => false, // From @PhpCsFixer but we don't want it + 'php_unit_test_class_requires_covers' => false, // From @PhpCsFixer but we don't want it + 'phpdoc_add_missing_param_annotation' => false, // From @PhpCsFixer but we don't want it + 'concat_space' => ['spacing' => 'one'], + 'ordered_class_elements' => true, // Symfony(PSR12) override the default value, but we don't want + 'blank_line_before_statement' => true, // Symfony(PSR12) override the default value, but we don't want + ]) + ->setFinder($finder) +; diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c820da..3a3c329 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 4.0.0 (not released yet) +* Migrate from Invoke to Castor + ## 3.11.0 (2023-05-30) * Use docker stages to build images diff --git a/Pipfile b/Pipfile deleted file mode 100644 index ff0a9a0..0000000 --- a/Pipfile +++ /dev/null @@ -1,19 +0,0 @@ -[[source]] -name = "pypi" -url = "https://pypi.org/simple" -verify_ssl = true - -[packages] -docker = "*" -invoke = ">=1.4" -# https://github.com/docker/compose/issues/6839 -colorama = {version = ">=0.4, <1"} -pypiwin32 = {version = "*", sys_platform = "== 'win32'"} -pywin32 = {version = "*", sys_platform = "== 'win32'"} - -[requires] -python_version = "3" - -[scripts] -# Alias fab to invoke to avoid BC break -fab = "invoke" diff --git a/Pipfile.lock b/Pipfile.lock deleted file mode 100644 index 9f7a634..0000000 --- a/Pipfile.lock +++ /dev/null @@ -1,186 +0,0 @@ -{ - "_meta": { - "hash": { - "sha256": "0ea30d8b12fff958eb3aede7557a633c7643f12d08887df1ae9c8e4a80640993" - }, - "pipfile-spec": 6, - "requires": { - "python_version": "3" - }, - "sources": [ - { - "name": "pypi", - "url": "https://pypi.org/simple", - "verify_ssl": true - } - ] - }, - "default": { - "certifi": { - "hashes": [ - "sha256:0f0d56dc5a6ad56fd4ba36484d6cc34451e1c6548c61daad8c320169f91eddc7", - "sha256:c6c2e98f5c7869efca1f8916fed228dd91539f9f1b444c314c06eef02980c716" - ], - "markers": "python_version >= '3.6'", - "version": "==2023.5.7" - }, - "charset-normalizer": { - "hashes": [ - "sha256:04afa6387e2b282cf78ff3dbce20f0cc071c12dc8f685bd40960cc68644cfea6", - "sha256:04eefcee095f58eaabe6dc3cc2262f3bcd776d2c67005880894f447b3f2cb9c1", - "sha256:0be65ccf618c1e7ac9b849c315cc2e8a8751d9cfdaa43027d4f6624bd587ab7e", - "sha256:0c95f12b74681e9ae127728f7e5409cbbef9cd914d5896ef238cc779b8152373", - "sha256:0ca564606d2caafb0abe6d1b5311c2649e8071eb241b2d64e75a0d0065107e62", - "sha256:10c93628d7497c81686e8e5e557aafa78f230cd9e77dd0c40032ef90c18f2230", - "sha256:11d117e6c63e8f495412d37e7dc2e2fff09c34b2d09dbe2bee3c6229577818be", - "sha256:11d3bcb7be35e7b1bba2c23beedac81ee893ac9871d0ba79effc7fc01167db6c", - "sha256:12a2b561af122e3d94cdb97fe6fb2bb2b82cef0cdca131646fdb940a1eda04f0", - "sha256:12d1a39aa6b8c6f6248bb54550efcc1c38ce0d8096a146638fd4738e42284448", - "sha256:1435ae15108b1cb6fffbcea2af3d468683b7afed0169ad718451f8db5d1aff6f", - "sha256:1c60b9c202d00052183c9be85e5eaf18a4ada0a47d188a83c8f5c5b23252f649", - "sha256:1e8fcdd8f672a1c4fc8d0bd3a2b576b152d2a349782d1eb0f6b8e52e9954731d", - "sha256:20064ead0717cf9a73a6d1e779b23d149b53daf971169289ed2ed43a71e8d3b0", - "sha256:21fa558996782fc226b529fdd2ed7866c2c6ec91cee82735c98a197fae39f706", - "sha256:22908891a380d50738e1f978667536f6c6b526a2064156203d418f4856d6e86a", - "sha256:3160a0fd9754aab7d47f95a6b63ab355388d890163eb03b2d2b87ab0a30cfa59", - "sha256:322102cdf1ab682ecc7d9b1c5eed4ec59657a65e1c146a0da342b78f4112db23", - "sha256:34e0a2f9c370eb95597aae63bf85eb5e96826d81e3dcf88b8886012906f509b5", - "sha256:3573d376454d956553c356df45bb824262c397c6e26ce43e8203c4c540ee0acb", - "sha256:3747443b6a904001473370d7810aa19c3a180ccd52a7157aacc264a5ac79265e", - "sha256:38e812a197bf8e71a59fe55b757a84c1f946d0ac114acafaafaf21667a7e169e", - "sha256:3a06f32c9634a8705f4ca9946d667609f52cf130d5548881401f1eb2c39b1e2c", - "sha256:3a5fc78f9e3f501a1614a98f7c54d3969f3ad9bba8ba3d9b438c3bc5d047dd28", - "sha256:3d9098b479e78c85080c98e1e35ff40b4a31d8953102bb0fd7d1b6f8a2111a3d", - "sha256:3dc5b6a8ecfdc5748a7e429782598e4f17ef378e3e272eeb1340ea57c9109f41", - "sha256:4155b51ae05ed47199dc5b2a4e62abccb274cee6b01da5b895099b61b1982974", - "sha256:49919f8400b5e49e961f320c735388ee686a62327e773fa5b3ce6721f7e785ce", - "sha256:53d0a3fa5f8af98a1e261de6a3943ca631c526635eb5817a87a59d9a57ebf48f", - "sha256:5f008525e02908b20e04707a4f704cd286d94718f48bb33edddc7d7b584dddc1", - "sha256:628c985afb2c7d27a4800bfb609e03985aaecb42f955049957814e0491d4006d", - "sha256:65ed923f84a6844de5fd29726b888e58c62820e0769b76565480e1fdc3d062f8", - "sha256:6734e606355834f13445b6adc38b53c0fd45f1a56a9ba06c2058f86893ae8017", - "sha256:6baf0baf0d5d265fa7944feb9f7451cc316bfe30e8df1a61b1bb08577c554f31", - "sha256:6f4f4668e1831850ebcc2fd0b1cd11721947b6dc7c00bf1c6bd3c929ae14f2c7", - "sha256:6f5c2e7bc8a4bf7c426599765b1bd33217ec84023033672c1e9a8b35eaeaaaf8", - "sha256:6f6c7a8a57e9405cad7485f4c9d3172ae486cfef1344b5ddd8e5239582d7355e", - "sha256:7381c66e0561c5757ffe616af869b916c8b4e42b367ab29fedc98481d1e74e14", - "sha256:73dc03a6a7e30b7edc5b01b601e53e7fc924b04e1835e8e407c12c037e81adbd", - "sha256:74db0052d985cf37fa111828d0dd230776ac99c740e1a758ad99094be4f1803d", - "sha256:75f2568b4189dda1c567339b48cba4ac7384accb9c2a7ed655cd86b04055c795", - "sha256:78cacd03e79d009d95635e7d6ff12c21eb89b894c354bd2b2ed0b4763373693b", - "sha256:80d1543d58bd3d6c271b66abf454d437a438dff01c3e62fdbcd68f2a11310d4b", - "sha256:830d2948a5ec37c386d3170c483063798d7879037492540f10a475e3fd6f244b", - "sha256:891cf9b48776b5c61c700b55a598621fdb7b1e301a550365571e9624f270c203", - "sha256:8f25e17ab3039b05f762b0a55ae0b3632b2e073d9c8fc88e89aca31a6198e88f", - "sha256:9a3267620866c9d17b959a84dd0bd2d45719b817245e49371ead79ed4f710d19", - "sha256:a04f86f41a8916fe45ac5024ec477f41f886b3c435da2d4e3d2709b22ab02af1", - "sha256:aaf53a6cebad0eae578f062c7d462155eada9c172bd8c4d250b8c1d8eb7f916a", - "sha256:abc1185d79f47c0a7aaf7e2412a0eb2c03b724581139193d2d82b3ad8cbb00ac", - "sha256:ac0aa6cd53ab9a31d397f8303f92c42f534693528fafbdb997c82bae6e477ad9", - "sha256:ac3775e3311661d4adace3697a52ac0bab17edd166087d493b52d4f4f553f9f0", - "sha256:b06f0d3bf045158d2fb8837c5785fe9ff9b8c93358be64461a1089f5da983137", - "sha256:b116502087ce8a6b7a5f1814568ccbd0e9f6cfd99948aa59b0e241dc57cf739f", - "sha256:b82fab78e0b1329e183a65260581de4375f619167478dddab510c6c6fb04d9b6", - "sha256:bd7163182133c0c7701b25e604cf1611c0d87712e56e88e7ee5d72deab3e76b5", - "sha256:c36bcbc0d5174a80d6cccf43a0ecaca44e81d25be4b7f90f0ed7bcfbb5a00909", - "sha256:c3af8e0f07399d3176b179f2e2634c3ce9c1301379a6b8c9c9aeecd481da494f", - "sha256:c84132a54c750fda57729d1e2599bb598f5fa0344085dbde5003ba429a4798c0", - "sha256:cb7b2ab0188829593b9de646545175547a70d9a6e2b63bf2cd87a0a391599324", - "sha256:cca4def576f47a09a943666b8f829606bcb17e2bc2d5911a46c8f8da45f56755", - "sha256:cf6511efa4801b9b38dc5546d7547d5b5c6ef4b081c60b23e4d941d0eba9cbeb", - "sha256:d16fd5252f883eb074ca55cb622bc0bee49b979ae4e8639fff6ca3ff44f9f854", - "sha256:d2686f91611f9e17f4548dbf050e75b079bbc2a82be565832bc8ea9047b61c8c", - "sha256:d7fc3fca01da18fbabe4625d64bb612b533533ed10045a2ac3dd194bfa656b60", - "sha256:dd5653e67b149503c68c4018bf07e42eeed6b4e956b24c00ccdf93ac79cdff84", - "sha256:de5695a6f1d8340b12a5d6d4484290ee74d61e467c39ff03b39e30df62cf83a0", - "sha256:e0ac8959c929593fee38da1c2b64ee9778733cdf03c482c9ff1d508b6b593b2b", - "sha256:e1b25e3ad6c909f398df8921780d6a3d120d8c09466720226fc621605b6f92b1", - "sha256:e633940f28c1e913615fd624fcdd72fdba807bf53ea6925d6a588e84e1151531", - "sha256:e89df2958e5159b811af9ff0f92614dabf4ff617c03a4c1c6ff53bf1c399e0e1", - "sha256:ea9f9c6034ea2d93d9147818f17c2a0860d41b71c38b9ce4d55f21b6f9165a11", - "sha256:f645caaf0008bacf349875a974220f1f1da349c5dbe7c4ec93048cdc785a3326", - "sha256:f8303414c7b03f794347ad062c0516cee0e15f7a612abd0ce1e25caf6ceb47df", - "sha256:fca62a8301b605b954ad2e9c3666f9d97f63872aa4efcae5492baca2056b74ab" - ], - "markers": "python_version >= '3.7'", - "version": "==3.1.0" - }, - "colorama": { - "hashes": [ - "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", - "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6" - ], - "index": "pypi", - "version": "==0.4.6" - }, - "docker": { - "hashes": [ - "sha256:134cd828f84543cbf8e594ff81ca90c38288df3c0a559794c12f2e4b634ea19e", - "sha256:dcc088adc2ec4e7cfc594e275d8bd2c9738c56c808de97476939ef67db5af8c2" - ], - "index": "pypi", - "version": "==6.1.2" - }, - "idna": { - "hashes": [ - "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4", - "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2" - ], - "markers": "python_version >= '3.5'", - "version": "==3.4" - }, - "invoke": { - "hashes": [ - "sha256:a6cc1f06f75bacd0b1e11488fa3bf3e62f85e31f62e2c0172188613ba5b070e2", - "sha256:bfc904df1c9e9fe1a881933de661fe054b8db616ff2c4cf78e00407fe473ba5d" - ], - "index": "pypi", - "version": "==2.1.2" - }, - "packaging": { - "hashes": [ - "sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61", - "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f" - ], - "markers": "python_version >= '3.7'", - "version": "==23.1" - }, - "pypiwin32": { - "hashes": [ - "sha256:67adf399debc1d5d14dffc1ab5acacb800da569754fafdc576b2a039485aa775", - "sha256:71be40c1fbd28594214ecaecb58e7aa8b708eabfa0125c8a109ebd51edbd776a" - ], - "markers": "sys_platform == 'win32'", - "version": "==223" - }, - "pywin32": { - "sys_platform": "== 'win32'", - "version": "*" - }, - "requests": { - "hashes": [ - "sha256:10e94cc4f3121ee6da529d358cdaeaff2f1c409cd377dbc72b825852f2f7e294", - "sha256:239d7d4458afcb28a692cdd298d87542235f4ca8d36d03a15bfc128a6559a2f4" - ], - "markers": "python_version >= '3.7'", - "version": "==2.30.0" - }, - "urllib3": { - "hashes": [ - "sha256:61717a1095d7e155cdb737ac7bb2f4324a858a1e2e6466f6d03ff630ca68d3cc", - "sha256:d055c2f9d38dc53c808f6fdc8eab7360b6fdbbde02340ed25cfbcd817c62469e" - ], - "markers": "python_version >= '3.7'", - "version": "==2.0.2" - }, - "websocket-client": { - "hashes": [ - "sha256:3f09e6d8230892547132177f575a4e3e73cfdf06526e20cc02aa1c3b47184d40", - "sha256:cdf5877568b7e83aa7cf2244ab56a3213de587bbe0ce9d8b9600fc77b455d89e" - ], - "markers": "python_version >= '3.7'", - "version": "==1.5.1" - } - }, - "develop": {} -} diff --git a/README.dist.md b/README.dist.md index 04d404f..4cd7832 100644 --- a/README.dist.md +++ b/README.dist.md @@ -8,33 +8,24 @@ A Docker environment is provided and requires you to have these tools available: * Docker * Bash - * pipenv (see [these instructions](https://pipenv.pypa.io/en/latest/installation/) for how to install) + * PHP >= 8.1 + * [Castor](https://github.com/jolicode/castor#installation) -Install and run `pipenv` to install the required tools: +#### Castor -```bash -pipenv --three install -``` - -You can configure your current shell to be able to use Invoke commands directly -(without having to prefix everything by `pipenv run`) - -```bash -pipenv shell -``` - -Optionally, in order to improve your usage of invoke scripts, you can install console autocompletion script. +Once castor is installed, in order to improve your usage of castor scripts, you +can install console autocompletion script. If you are using bash: ```bash -invoke --print-completion-script=bash > /etc/bash_completion.d/invoke +castor completion | sudo tee /etc/bash_completion.d/castor ``` -If you are using something else, please refer to your shell documentation. -You may need to use `invoke --print-completion-script=zsh > /to/somewhere`. +If you are using something else, please refer to your shell documentation. You +may need to use `castor completion > /to/somewhere`. -Invoke supports completion for `bash`, `zsh` & `fish` shells. +Castor supports completion for `bash`, `zsh` & `fish` shells. ### Docker environment @@ -68,7 +59,7 @@ Using dinghy? Run `dinghy ip` to get the IP of the VM. Launch the stack by running this command: ```bash -inv start +castor start ``` > Note: the first start of the stack should take a few minutes. @@ -80,8 +71,8 @@ installed on your computer - see below). ### SSL certificates This stack no longer embeds self-signed SSL certificates. Instead they will be -generated the first time you start the infrastructure (`inv start`) or if you -run `inv generate-certificates`. So *HTTPS will work out of the box*. +generated the first time you start the infrastructure (`castor start`) or if you +run `castor infra:generate-certificates`. So *HTTPS will work out of the box*. If you have `mkcert` installed on your computer, it will be used to generate locally trusted certificates. See [`mkcert` documentation](https://github.com/FiloSottile/mkcert#installation) @@ -92,9 +83,9 @@ If you don't have `mkcert`, then self-signed certificates will instead be generated with openssl. You can configure [infrastructure/docker/services/router/openssl.cnf](infrastructure/docker/services/router/openssl.cnf) to tweak certificates. -You can run `inv generate-certificates --force` to recreate new certificates +You can run `castor infra:generate-certificates --force` to recreate new certificates if some were already generated. Remember to restart the infrastructure to make -use of the new certificates with `inv up` or `inv start`. +use of the new certificates with `castor up` or `castor start`. ### Builder @@ -103,21 +94,9 @@ Start the builder which will give you access to a container with all these tools available: ```bash -inv builder -``` - -Note: You can add as many Invoke commands as you want. If a command should be -ran by the builder, don't forget to use `with Builder(c):`: -``` -@task -def mycommand(c): - """ - My documentation - """ - with Builder(c): - docker_compose_run(c, 'echo "HelloWorld") +castor builder ``` ### Other tasks -Checkout `inv -l` to have the list of available Invoke tasks. +Checkout `castor` to have the list of available tasks. diff --git a/README.md b/README.md index 396e54d..ee9d241 100644 --- a/README.md +++ b/README.md @@ -3,60 +3,78 @@

-Collection of Dockerfile and docker-compose configurations wrapped in an easy-to-use command line, oriented for PHP projects. + Collection of Dockerfile and docker-compose configurations wrapped in an easy-to-use command line, oriented for PHP projects.

## What is Docker Starter Kit -This repository provides a Docker infrastructure for your PHP projects with built-in support for HTTPS, custom domain, databases, workers... -and is used as a foundation for our projects here at [JoliCode](https://jolicode.com/). +This repository provides a Docker infrastructure for your PHP projects with +built-in support for HTTPS, custom domain, databases, workers... and is used as +a foundation for our projects at [JoliCode](https://jolicode.com/). Learn more about it and why we created and open-sourced this starter-kit in our blog posts: [in English 🇬🇧](https://jolicode.com/blog/introducing-our-docker-starter-kit) or [in French 🇫🇷](https://jolicode.com/blog/presentation-de-notre-starter-kit-docker). > **Warning** -> You are reading the README of version 3 that uses [Invoke](https://www.pyinvoke.org/). +> You are reading the README of version 4 that uses [castor](https://github.com/jolicode/castor). +* If you are using [Invoke](https://www.pyinvoke.org/), you can read the [dedicated README](https://github.com/jolicode/docker-starter/tree/v3.0.0); * If you are using [Fabric](https://www.fabfile.org/), you can read the [dedicated README](https://github.com/jolicode/docker-starter/tree/v2.0.0); -* If you want to migrate from docker-starter v2.x to v3.0, you can read the [dedicated guide](./UPGRADE-3.0.md); ## Project configuration Before executing any command, you need to configure a few parameters in the -`invoke.py` file: +`castor.php` file, in the `create_default_parameters()` function: -* `project_name` (**required**): This will be used to prefix all docker -objects (network, images, containers); +* `project_name` (**required**): This will be used to prefix all docker objects +(network, images, containers); -* `root_domain` (optional, default: `project_name + '.test'`): This is the -root domain where the application will be available; +* `root_domain` (optional, default: `project_name + '.test'`): This is the root +domain where the application will be available; -* `extra_domains` (optional): This contains extra domains where the -application will be available; +* `extra_domains` (optional): This contains extra domains where the application +will be available; * `project_directory` (optional, default: `application`): This is the host -directory containing your PHP application. +directory containing your PHP application; -For example: +* `php_version` (optional, default: `8.2`): This is PHP version. -```py -project_name = 'foobar' -root_domain = project_name + '.test' -extra_domains = ['api.' + root_domain] -project_directory = 'application' -``` +For example: -Will give you `https://foobar.test` and `https://api.foobar.test` pointing at your `application/` directory. +```php +function create_default_parameters(): Context +{ + $projectName = 'app'; + $tld = 'test'; + + return [ + 'project_name' => $projectName, + 'root_domain' => "{$projectName}.{$tld}", + 'extra_domains' => [ + "www.{$projectName}.{$tld}", + "admin.{$projectName}.{$tld}", + "api.{$projectName}.{$tld}", + ], + 'php_version' => 8.2, + ]; +) +``` + +Will give you `https://app.test`, `https://www.app.test`, +`https://api.app.test` and `https://admin.app.test` pointing at your +`application/` directory. > **Note** -> Some Invoke tasks have been added for DX purposes. Checkout and adapt +> Some castor tasks have been added for DX purposes. Checkout and adapt the tasks `install`, `migrate` and `cache_clear` to your project. ## Usage documentation -We provide a [README.dist.md](./README.dist.md) to bootstrap your project documentation, with everything you need -to know to start and interact with the infrastructure. +We provide a [README.dist.md](./README.dist.md) to bootstrap your project +documentation, with everything you need to know to start and interact with the +infrastructure. To use this README.dist.md as a base for your project's README.md: @@ -67,21 +85,22 @@ mv README.{dist.md,md} Some files will not be needed for your project and should be deleted: ```bash -rm -rf .github/ CHANGELOG.md CONTRIBUTING.md LICENSE UPGRADE-3.0.md +rm -rf .github/ CHANGELOG.md CONTRIBUTING.md LICENSE UPGRADE-4.0.md ``` -Also, in order to improve your usage of invoke scripts, you can install console autocompletion script. +Also, in order to improve your usage of castor scripts, you can install console +autocompletion script. If you are using bash: ```bash -invoke --print-completion-script=bash > /etc/bash_completion.d/invoke +castor completion | sudo tee /etc/bash_completion.d/castor ``` -If you are using something else, please refer to your shell documentation. -You may need to use `invoke --print-completion-script=zsh > /to/somewhere`. +If you are using something else, please refer to your shell documentation. You +may need to use `castor completion > /to/somewhere`. -Invoke supports completion for `bash`, `zsh` & `fish` shells. +Castor supports completion for `bash`, `zsh` & `fish` shells. ## Cookbooks @@ -866,7 +885,7 @@ services: Note: `172.17.0.1` is the default IP of the `docker0` interface. It can be different on some installations. You can see this IP thanks to the following command `ip address show docker0`. Since `docker-compose.yml` file supports -environment variables you may script this with Invoke. +environment variables you may script this with Castor. diff --git a/UPGRADE-3.0.md b/UPGRADE-3.0.md deleted file mode 100644 index 8dd29c8..0000000 --- a/UPGRADE-3.0.md +++ /dev/null @@ -1,292 +0,0 @@ -How to upgrade from 2.x to 3.0 -============================== - -This guide will cover the migration from Fabric to Invoke. -At anytime, you can refer the final form of the starter kit by looking -at the [main repository](https://github.com/jolicode/docker-starter) - -**WARNING**: Migrating from 2.x to 3.0 can be a fastidious task since a lot of -parts have changed. Moreover, this project is a starter kit. It means you -usually start with it, then you make your own choices and implementations. So if -you are comfortable with your current project **we recommend you to stick on -2.x**. If you want to migrate from 2.x to 3.0, here are the tasks you will have -to perform. - -## Fabric to Invoke - -1. Rename the `fabfile.py` to `tasks.py`: - - ```bash - mv fabfile.py tasks.py - ``` - -1. Extract the configuration to a dedicated file `invoke.py`: - - 1. The variables are not longer prefixed with `env.`: - - * `env.project_name` => `project_name`; - * `env.root_domain` => `root_domain`; - * `env.project_directory` => `project_directory`; - * `env.extra_domains` => `extra_domains`. - - 1. Append the following content to the file: - - ```py - # Usually, you should not edit the file above this point - docker_compose_files = [ - 'docker-compose.yml', - 'docker-compose.worker.yml', - ] - services_to_build_first = [ - 'php-base', - 'builder', - ] - dinghy = False - power_shell = False - user_id = 1000 - root_dir = '.' - start_workers = False - - - def __extract_runtime_configuration(config): - from invoke import run - from sys import platform - import os - import sys - from colorama import init, Fore - init(autoreset=True) - - config['root_dir'] = os.path.dirname(os.path.abspath(__file__)) - - try: - docker_kernel = run('docker version --format "{{.Server.KernelVersion}}"', hide=True).stdout - except: - docker_kernel = '' - - if platform == "darwin" and docker_kernel.find('linuxkit') != -1: - config['dinghy'] = True - elif platform in ["win32", "win64"]: - config['power_shell'] = True - # # Python can't set the vars correctly on PowerShell and local() always calls cmd.exe - shellProjectName = run('echo %PROJECT_NAME%', hide=True).stdout - if (shellProjectName != config['project_name']): - domains = '`' + '`, `'.join([config['root_domain']] + config['extra_domains']) + '`' - print(Fore.RED + 'Env vars not set (Windows detected)') - print(Fore.YELLOW + 'You must manually set environment variables on Windows:') - print('$Env:PROJECT_NAME="%s"' % config['project_name']) - print('$Env:PROJECT_DIRECTORY="%s"' % config['project_directory']) - print('$Env:PROJECT_ROOT_DOMAIN="%s"' % config['root_domain']) - print('$Env:PROJECT_DOMAINS="%s"' % domains) - sys.exit(0) - - if not config['power_shell']: - config['user_id'] = int(run('id -u', hide=True).stdout) - - if config['user_id'] > 256000: - config['user_id'] = 1000 - - return config - - - locals().update(__extract_runtime_configuration(locals())) - ``` - - 1. You can remove the similar part from `tasks.py` because `invoke.py` will be automatically imported. - - Don't forget to remove `set_local_configuration()` function and its function call. - -1. Edit `tasks.py`: - - 1. Fix `import`s: - - Remove all imports (unless you are using specific import) and add the following lines: - - ```py - from invoke import task - from shlex import quote - from colorama import Fore - ``` - - 1. Replace `run_in_docker_or_locally_for_dinghy()`, `docker_compose()` and `docker_composer_run()` functions: - - With the following content: - - ```py - def run_in_docker_or_locally_for_dinghy(c, command, no_deps=False): - """ - Mac users have a lot of problems running Yarn / Webpack on the Docker stack so this func allow them to run these tools on their host - """ - if c.dinghy: - with c.cd(c.project_directory): - c.run(command) - else: - docker_compose_run(c, command, no_deps=no_deps) - - - def docker_compose_run(c, command_name, service="builder", user="app", no_deps=False, workdir=None, port_mapping=False): - args = [ - 'run', - '--rm', - '-u %s' % quote(user), - ] - - if no_deps: - args.append('--no-deps') - - if port_mapping: - args.append('--service-ports') - - if workdir is not None: - args.append('-w %s' % quote(workdir)) - - docker_compose(c, '%s %s /bin/sh -c "exec %s"' % ( - ' '.join(args), - quote(service), - command_name - )) - - - def docker_compose(c, command_name): - domains = '`' + '`, `'.join([c.root_domain] + c.extra_domains) + '`' - - env = { - 'PROJECT_NAME': c.project_name, - 'PROJECT_DIRECTORY': c.project_directory, - 'PROJECT_ROOT_DOMAIN': c.root_domain, - 'PROJECT_DOMAINS': domains, - } - - cmd = 'docker-compose -p %s %s %s' % ( - c.project_name, - ' '.join('-f \'' + c.root_dir + '/infrastructure/docker/' + file + '\'' for file in c.docker_compose_files), - command_name - ) - - c.run(cmd, pty=True, env=env) - ``` - - 1. Use the context in all tasks - - Invoke needs a context (named `c`) in every single task. This context: - - * is configured for each task; - * contains the configuration; - * contains some methods to execute commands. - - You will need to update all tasks signature, and all tasks function call to pass the context: - - Before: - - ```py - @task - def up(): - build() - docker_compose('up --remove-orphans -d') - ``` - - After: - - ```py - @task - def up(c): - build(c) - docker_compose(c, 'up --remove-orphans -d') - ``` - - 1. Do not use `env.` anymore: - - Before: - - ```py - @task - def up(c): - print(env.project_name) - ``` - - After: - - ```py - @task - def up(c): - print(c.project_name) - ``` - - 1. Do not use `local()` anymore: - - Before: - - ```py - @task - def start(c): - machine_running = local('dinghy status', capture=True) - ``` - - After: - - ```py - @task - def start(c): - machine_running = c.local('dinghy status').stdout - ``` - - 1. Add `Builder` class and use it: - - The Builder class is the new `@with_builder` decorator - - ```py - class Builder: - def __init__(self, c): - self.c = c - - def __enter__(self): - self.docker_compose_files = self.c.docker_compose_files - self.c.docker_compose_files = ['docker-compose.builder.yml'] + self.docker_compose_files - - def __exit__(self, type, value, traceback): - self.c.docker_compose_files = self.docker_compose_files - ``` - - Before: - - ```py - @task - @with_builder - def install(): - docker_compose_run('composer install -n --prefer-dist --optimize-autoloader') - ``` - - After: - - ```py - @task - def install(c): - with Builder(c): - docker_compose_run(c, 'composer install -n --prefer-dist --optimize-autoloader') - ``` - - 1. Update colors - - Fabric provided a nice helper for colors. Now we use colorama. - - Before: - - ```py - print green('You can now browse:') - for domain in [c.root_domain] + c.extra_domains: - print yellow("* https://" + domain) - ``` - - After: - - ```py - @task - print(Fore.GREEN + 'You can now browse:') - for domain in [c.root_domain] + c.extra_domains: - print(Fore.YELLOW + "* https://" + domain) - ``` - -## Alpine to Debian - -We have changed PHP base images from Alpine to Debian. The best guide to process -this migration is to replicate what have been done in following pull request: -https://github.com/jolicode/docker-starter/pull/67 diff --git a/application/public/index.php b/application/public/index.php index 8eae0fb..3e18401 100644 --- a/application/public/index.php +++ b/application/public/index.php @@ -1,3 +1,3 @@ + */ +function create_default_variables(): array +{ + $projectName = 'app'; + $tld = 'test'; + + return [ + 'project_name' => $projectName, + 'root_domain' => "{$projectName}.{$tld}", + 'extra_domains' => [ + "www.{$projectName}.{$tld}", + ], + 'php_version' => $_SERVER['DS_PHP_VERSION'] ?? '8.2', + 'project_directory' => 'application', + ]; +} + +#[AsTask(description: 'Builds and starts the infrastructure, then install the application (composer, yarn, ...)')] +function start(): void +{ + infra\workers_stop(); + infra\generate_certificates(false); + infra\build(); + infra\up(); + cache_clear(); + install(); + migrate(); + infra\workers_start(); + + notify('The stack is now up and running.'); + io()->success('The stack is now up and running.'); + + about(); +} + +#[AsTask(description: 'Installs the application (composer, yarn, ...)', namespace: 'app')] +function install(): void +{ + $basePath = sprintf('%s/%s', variable('root_dir'), variable('project_directory')); + + if (is_file("{$basePath}/composer.json")) { + docker_compose_run('composer install -n --prefer-dist --optimize-autoloader'); + } + if (is_file("{$basePath}/yarn.lock")) { + docker_compose_run('yarn'); + } elseif (is_file("{$basePath}/package.json")) { + docker_compose_run('npm install'); + } +} + +#[AsTask(description: 'Clear the application cache', namespace: 'app')] +function cache_clear(): void +{ + // docker_compose_run('rm -rf var/cache/ && bin/console cache:warmup'); +} + +#[AsTask(description: 'Migrates database schema', namespace: 'app:db')] +function migrate(): void +{ + // docker_compose_run('bin/console doctrine:database:create --if-not-exists'); + // docker_compose_run('bin/console doctrine:migration:migrate -n --allow-no-migration'); +} diff --git a/infrastructure/docker/docker-compose.worker.yml b/infrastructure/docker/docker-compose.worker.yml index 6c69081..bc3a2c3 100644 --- a/infrastructure/docker/docker-compose.worker.yml +++ b/infrastructure/docker/docker-compose.worker.yml @@ -12,12 +12,10 @@ x-services-templates: volumes: - "../../${PROJECT_DIRECTORY}:/home/app/application:cached" user: app - environment: - - "PROJECT_START_WORKERS=${PROJECT_START_WORKERS}" labels: - "docker-starter.worker.${PROJECT_NAME}" -#services: +# services: # worker_messenger: # <<: *worker_base # command: php -d memory_limit=1G /home/app/application/bin/console messenger:consume async --memory-limit=128M diff --git a/infrastructure/docker/services/php/Dockerfile b/infrastructure/docker/services/php/Dockerfile index cf822ac..7cebeab 100644 --- a/infrastructure/docker/services/php/Dockerfile +++ b/infrastructure/docker/services/php/Dockerfile @@ -69,10 +69,6 @@ CMD ["runsvdir", "-P", "/etc/service"] FROM php-base as worker -COPY worker/entrypoint.sh /var/lib/entrypoint.sh - -ENTRYPOINT ["/var/lib/entrypoint.sh"] - FROM php-base as builder ARG NODEJS_VERSION=18.x diff --git a/infrastructure/docker/services/php/worker/entrypoint.sh b/infrastructure/docker/services/php/worker/entrypoint.sh deleted file mode 100755 index 4665cae..0000000 --- a/infrastructure/docker/services/php/worker/entrypoint.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh -set -e - -if [ "$PROJECT_START_WORKERS" = "False" ]; then - echo "Worker not started" - exit 0 -fi - -exec "$@" diff --git a/invoke.py b/invoke.py deleted file mode 100644 index b63406b..0000000 --- a/invoke.py +++ /dev/null @@ -1,90 +0,0 @@ -# This will be used to prefix all docker objects (network, images, containers) -project_name = 'app' -# This is the root domain where the app will be available -# The "frontend" container will receive all the traffic -root_domain = project_name + '.test' -# This contains extra domains where the app will be available -# The "frontend" container will receive all the traffic -extra_domains = [] -# This is the host directory containing your PHP application -project_directory = 'application' - -# Usually, you should not edit the file below this point -php_version = '8.1' -docker_compose_files = [ - 'docker-compose.yml', - 'docker-compose.worker.yml', -] -dinghy = False -macos = False -power_shell = False -user_id = 1000 -root_dir = '.' -start_workers = False -composer_cache_dir = '~/.composer/cache' - - -def __extract_runtime_configuration(config): - from invoke import run - from sys import platform - import os - import sys - import inspect - from colorama import init, Fore - init(autoreset=True) - - config['root_dir'] = os.path.dirname(inspect.getfile(lambda: None)) - - if os.path.exists(config['root_dir'] + '/infrastructure/docker/docker-compose.override.yml'): - config['docker_compose_files'] += ['docker-compose.override.yml'] - - config['composer_cache_dir'] = composer_cache_dir - composer_cache_dir_from_host = run('composer global config cache-dir -q', warn=True, hide=True).stdout - if composer_cache_dir_from_host: - config['composer_cache_dir'] = composer_cache_dir_from_host.strip() - - if platform == "darwin": - config['macos'] = True - - try: - docker_kernel = run('docker version --format "{{.Server.KernelVersion}}"', hide=True).stdout - except: - docker_kernel = '' - - if docker_kernel.find('boot2docker') != -1: - config['dinghy'] = True - else: - config['docker_compose_files'] += ['docker-compose.docker-for-x.yml'] - elif platform in ["win32", "win64"]: - config['docker_compose_files'] += ['docker-compose.docker-for-x.yml'] - config['power_shell'] = True - # Python can't set the vars correctly on PowerShell and local() always calls cmd.exe - shellProjectName = run('echo %PROJECT_NAME%', hide=True).stdout - - if (shellProjectName.rstrip() != config['project_name']): - domains = '`' + '`, `'.join([config['root_domain']] + config['extra_domains']) + '`' - print(Fore.RED + 'Env vars not set (Windows detected)') - print(Fore.YELLOW + 'You must manually set environment variables on Windows:') - # This list should be in sync with the one in docker_compose.py, docker_compose() function - print('$Env:PROJECT_NAME="%s"' % config['project_name']) - print('$Env:PROJECT_DIRECTORY="%s"' % config['project_directory']) - print('$Env:PROJECT_ROOT_DOMAIN="%s"' % config['root_domain']) - print("$Env:PROJECT_DOMAINS='%s'" % domains) - print('$Env:COMPOSER_CACHE_DIR="%s"' % config['composer_cache_dir']) - print('$Env:PHP_VERSION="%s"' % config['php_version']) - sys.exit(1) - - if not config['power_shell']: - config['user_id'] = int(run('id -u', hide=True).stdout) - - if config['user_id'] > 256000: - config['user_id'] = 1000 - - if config['user_id'] == 0: - print(Fore.YELLOW + 'Running as root? Fallback to fake user id.') - config['user_id'] = 1000 - - return config - - -locals().update(__extract_runtime_configuration(locals())) diff --git a/tasks.py b/tasks.py deleted file mode 100644 index 73588b6..0000000 --- a/tasks.py +++ /dev/null @@ -1,334 +0,0 @@ -from invoke import task -from shlex import quote -from colorama import Fore -from distutils.spawn import find_executable -import json -import os -import re -import requests -import subprocess - - -@task -def build(c): - """ - Build the infrastructure - """ - command = 'build' - command += ' --build-arg PROJECT_NAME=%s' % c.project_name - command += ' --build-arg USER_ID=%s' % c.user_id - command += ' --build-arg PHP_VERSION=%s' % c.php_version - - with Builder(c): - docker_compose(c, command) - - -@task -def up(c): - """ - Build and start the infrastructure - """ - build(c) - - docker_compose(c, 'up --remove-orphans --detach') - - -@task -def start(c): - """ - Build and start the infrastructure, then install the application (composer, yarn, ...) - """ - if c.dinghy: - machine_running = c.run('dinghy status', hide=True).stdout - if machine_running.splitlines()[0].strip() != 'VM: running': - c.run('dinghy up --no-proxy') - c.run('docker-machine ssh dinghy "echo \'nameserver 8.8.8.8\' | sudo tee -a /etc/resolv.conf && sudo /etc/init.d/docker restart"') - - stop_workers(c) - generate_certificates(c) - up(c) - cache_clear(c) - install(c) - migrate(c) - start_workers(c) - - print(Fore.GREEN + 'The stack is now up and running.') - help(c) - - -@task -def install(c): - """ - Install the application (composer, yarn, ...) - """ - with Builder(c): - if os.path.isfile(c.root_dir + '/' + c.project_directory + '/composer.json'): - docker_compose_run(c, 'composer install -n --prefer-dist --optimize-autoloader', no_deps=True) - if os.path.isfile(c.root_dir + '/' + c.project_directory + '/yarn.lock'): - run_in_docker_or_locally_for_mac(c, 'yarn', no_deps=True) - elif os.path.isfile(c.root_dir + '/' + c.project_directory + '/package.json'): - run_in_docker_or_locally_for_mac(c, 'npm install', no_deps=True) - - -@task -def cache_clear(c): - """ - Clear the application cache - """ - # with Builder(c): - # docker_compose_run(c, 'rm -rf var/cache/ && php bin/console cache:warmup', no_deps=True) - - -@task -def migrate(c): - """ - Migrate database schema - """ - # with Builder(c): - # docker_compose_run(c, 'php bin/console doctrine:database:create --if-not-exists') - # docker_compose_run(c, 'php bin/console doctrine:migration:migrate -n --allow-no-migration') - - -@task -def builder(c, user="app"): - """ - Open a shell (bash) into a builder container - """ - with Builder(c): - docker_compose_run(c, 'bash', user=user, bare_run=True) - - -@task -def logs(c): - """ - Display infrastructure logs - """ - docker_compose(c, 'logs -f --tail=150') - - -@task -def ps(c): - """ - List containers status - """ - docker_compose(c, 'ps --all') - - -@task -def stop(c): - """ - Stop the infrastructure - """ - docker_compose(c, 'stop') - - -@task -def start_workers(c): - """ - Start the workers - """ - workers = get_workers(c) - - if (len(workers) == 0): - return - - c.start_workers = True - c.run('docker update --restart=unless-stopped %s' % (' '.join(workers)), hide='both') - docker_compose(c, 'up --remove-orphans --detach') - - -@task -def stop_workers(c): - """ - Stop the workers - """ - workers = get_workers(c) - - if (len(workers) == 0): - return - - c.start_workers = False - c.run('docker update --restart=no %s' % (' '.join(workers)), hide='both') - c.run('docker stop %s' % (' '.join(workers)), hide='both') - - -@task -def destroy(c, force=False): - """ - Clean the infrastructure (remove container, volume, networks) - """ - - if not force: - ok = confirm_choice('Are you sure? This will permanently remove all containers, volumes, networks... created for this project.') - if not ok: - return - - with Builder(c): - docker_compose(c, 'down --remove-orphans --volumes --rmi=local') - c.run('rm -f infrastructure/docker/services/router/etc/ssl/certs/*.pem') - - -@task(default=True) -def help(c): - """ - Display some help and available urls for the current project - """ - - print('Run ' + Fore.GREEN + 'inv help' + Fore.RESET + ' to display this help.') - print('') - - print('Run ' + Fore.GREEN + 'inv --help' + Fore.RESET + ' to display invoke help.') - print('') - - print('Run ' + Fore.GREEN + 'inv -l' + Fore.RESET + ' to list all the available tasks.') - c.run('inv --list') - - print(Fore.GREEN + 'Available URLs for this project:' + Fore.RESET) - for domain in [c.root_domain] + c.extra_domains: - print("* " + Fore.YELLOW + "https://" + domain + Fore.RESET) - - try: - response = json.loads(requests.get('http://%s:8080/api/http/routers' % (c.root_domain)).text) - gen = (router for router in response if re.match("^%s-(.*)@docker$" % (c.project_name), router['name'])) - for router in gen: - if router['service'] != 'frontend-%s' % (c.project_name): - host = re.search('Host\(\`(?P.*)\`\)', router['rule']).group('host') - if host: - scheme = 'https' if 'https' in router['using'] else router['using'][0] - print("* " + Fore.YELLOW + scheme + "://" + host + Fore.RESET) - print('') - except: - pass - - -@task -def generate_certificates(c, force=False): - """ - Generate SSL certificates (with mkcert if available or self-signed if not) - """ - with Builder(c): - if (os.path.isfile(c.root_dir + '/infrastructure/docker/services/router/etc/ssl/certs/cert.pem') and not force): - print(Fore.GREEN + 'SSL certificates found in infrastructure/docker/services/router/etc/ssl/certs/*.pem.') - print('Run "inv generate-certificates --force" to generate new certificates.') - return - - if force: - if os.path.isfile(c.root_dir + '/infrastructure/docker/services/router/etc/ssl/certs/cert.pem'): - print('Removing existing certificates in infrastructure/docker/services/router/etc/ssl/certs/*.pem.') - os.remove('infrastructure/docker/services/router/etc/ssl/certs/cert.pem') - - if os.path.isfile(c.root_dir + '/infrastructure/docker/services/router/etc/ssl/certs/key.pem'): - os.remove('infrastructure/docker/services/router/etc/ssl/certs/key.pem') - - if find_executable('mkcert') is not None: - path_caroot = c.run('mkcert -CAROOT', hide=True).stdout.strip() - - if not os.path.isdir(path_caroot): - print(Fore.RED + 'You must have mkcert CA Root installed on your host with `mkcert -install` command') - return - - c.run('mkcert -cert-file infrastructure/docker/services/router/etc/ssl/certs/cert.pem -key-file infrastructure/docker/services/router/etc/ssl/certs/key.pem %s "*.%s" %s' % (c.root_domain, c.root_domain, ' '.join(c.extra_domains))) - print(Fore.GREEN + 'Successfully generated SSL certificates with mkcert.') - if force: - print('Please restart the infrastructure to use the new certificates with "inv up" or "inv start".') - return - - c.run('infrastructure/docker/services/router/generate-ssl.sh') - - print(Fore.GREEN + 'Successfully generated self-signed SSL certificates in infrastructure/docker/services/router/etc/ssl/certs/*.pem.') - print(Fore.YELLOW + 'Consider installing mkcert to generate locally trusted SSL certificates and run "inv generate-certificates --force".') - if force: - print('Please restart the infrastructure to use the new certificates with "inv up" or "inv start".') - - -def run_in_docker_or_locally_for_mac(c, command, no_deps=False): - """ - Mac users have a lot of problems running Yarn / Webpack on the Docker stack so this func allow them to run these tools on their host - """ - if c.macos: - with c.cd(c.project_directory): - c.run(command) - else: - docker_compose_run(c, command, no_deps=no_deps) - - -def docker_compose_run(c, command_name, service="builder", user="app", no_deps=False, workdir=None, port_mapping=False, bare_run=False): - args = [ - 'run', - '--rm', - '-u %s' % quote(user), - ] - - if no_deps: - args.append('--no-deps') - - if port_mapping: - args.append('--service-ports') - - if workdir is not None: - args.append('-w %s' % quote(workdir)) - - docker_compose(c, '%s %s /bin/sh -c "exec %s"' % ( - ' '.join(args), - quote(service), - command_name - ), bare_run=bare_run) - - -def docker_compose(c, command_name, bare_run=False): - domains = '`' + '`, `'.join([c.root_domain] + c.extra_domains) + '`' - - # This list should be in sync with the one in invoke.py - env = { - 'PROJECT_NAME': c.project_name, - 'PROJECT_DIRECTORY': c.project_directory, - 'PROJECT_ROOT_DOMAIN': c.root_domain, - 'PROJECT_DOMAINS': domains, - 'PROJECT_START_WORKERS': str(c.start_workers), - 'COMPOSER_CACHE_DIR': c.composer_cache_dir, - 'PHP_VERSION': c.php_version, - } - - cmd = 'docker compose -p %s %s %s' % ( - c.project_name, - ' '.join('-f "' + c.root_dir + '/infrastructure/docker/' + file + '"' for file in c.docker_compose_files), - command_name - ) - - # bare_run bypass invoke run() function - # see https://github.com/pyinvoke/invoke/issues/744 - # Use it ONLY for task where you need to interact with the container like builder - if (bare_run): - env.update(os.environ) - subprocess.run(cmd, shell=True, env=env) - else: - c.run(cmd, pty=not c.power_shell, env=env) - - -def get_workers(c): - """ - Find worker containers for the current project - """ - cmd = c.run('docker ps -a --filter "label=docker-starter.worker.%s" --quiet' % c.project_name, hide='both') - if re.compile('^NAMES').search(cmd.stdout): - return list(filter(None, cmd.stdout.rsplit("\n")))[1::] - - return list(filter(None, cmd.stdout.rsplit("\n"))) - - -def confirm_choice(message): - confirm = input('%s [y]es or [N]o: ' % message) - - return re.compile('^y').search(confirm) - - -class Builder: - def __init__(self, c): - self.c = c - - def __enter__(self): - self.docker_compose_files = self.c.docker_compose_files - self.c.docker_compose_files = ['docker-compose.builder.yml'] + self.docker_compose_files - - def __exit__(self, type, value, traceback): - self.c.docker_compose_files = self.docker_compose_files