From 5668db95104c877739fbecd89de3496b3bff5721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Sat, 24 Feb 2024 08:58:20 +0100 Subject: [PATCH] finish: Move docker related stuff to docker NS --- .castor/docker.php | 127 +++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/.castor/docker.php b/.castor/docker.php index 22d40b0..8fe1892 100644 --- a/.castor/docker.php +++ b/.castor/docker.php @@ -20,7 +20,7 @@ use function Castor\run; use function Castor\variable; -#[AsTask(description: 'Displays some help and available urls for the current project')] +#[AsTask(description: 'Displays some help and available urls for the current project', namespace: '')] function about(): void { io()->section('About this project'); @@ -75,7 +75,7 @@ function up(): void docker_compose(['up', '--remove-orphans', '--detach', '--no-build']); } catch (ExceptionInterface $e) { io()->error('An error occured while starting the infrastructure.'); - io()->note('Did you forget to run "castor infra:build"?'); + io()->note('Did you forget to run "castor docker:build"?'); io()->note('Or you forget to login to the registry?'); throw $e; @@ -145,7 +145,7 @@ function generate_certificates( if (file_exists("$sslDir/cert.pem") && !$force) { io()->comment('SSL certificates already exists.'); - io()->note('Run "castor infra:generate-certificates --force" to generate new certificates.'); + io()->note('Run "castor docker:generate-certificates --force" to generate new certificates.'); return; } @@ -196,14 +196,14 @@ function generate_certificates( run(['infrastructure/docker/services/router/generate-ssl.sh'], quiet: true); io()->success('Successfully generated self-signed SSL certificates in infrastructure/docker/services/router/certs/*.pem.'); - io()->comment('Consider installing mkcert to generate locally trusted SSL certificates and run "castor infra:generate-certificates --force".'); + io()->comment('Consider installing mkcert to generate locally trusted SSL certificates and run "castor docker: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', aliases: ['start-workers'])] +#[AsTask(description: 'Starts the workers', namespace: 'docker:worker', name: 'start', aliases: ['start-workers'])] function workers_start(): void { $workers = get_workers(); @@ -226,7 +226,7 @@ function workers_start(): void ], quiet: true); } -#[AsTask(description: 'Stops the workers', namespace: 'infra:worker', name: 'stop', aliases: ['stop-workers'])] +#[AsTask(description: 'Stops the workers', namespace: 'docker:worker', name: 'stop', aliases: ['stop-workers'])] function workers_stop(): void { $workers = get_workers(); @@ -308,63 +308,6 @@ function create_default_context(): Context return new Context($data, pty: 'dev' === $data['env']); } -function docker_exit_code( - string $runCommand, - Context $c = null, - string $service = 'builder', - bool $noDeps = true, - string $workDir = null, - bool $withBuilder = true, -): int { - $c = ($c ?? context())->withAllowFailure(); - - $process = docker_compose_run( - runCommand: $runCommand, - c: $c, - service: $service, - noDeps: $noDeps, - workDir: $workDir, - withBuilder: $withBuilder, - ); - - return $process->getExitCode() ?? 0; -} - -function docker_compose_run( - string $runCommand, - Context $c = null, - string $service = 'builder', - bool $noDeps = true, - string $workDir = null, - bool $portMapping = false, - bool $withBuilder = true, -): Process { - $command = [ - 'run', - '--rm', - ]; - - 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 */ @@ -409,6 +352,64 @@ function docker_compose(array $subCommand, Context $c = null, bool $withBuilder return run($command, context: $c); } +function docker_compose_run( + string $runCommand, + Context $c = null, + string $service = 'builder', + bool $noDeps = true, + string $workDir = null, + bool $portMapping = false, + bool $withBuilder = true, +): Process { + $command = [ + 'run', + '--rm', + ]; + + 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); +} + +function docker_exit_code( + string $runCommand, + Context $c = null, + string $service = 'builder', + bool $noDeps = true, + string $workDir = null, + bool $withBuilder = true, +): int { + $c = ($c ?? context())->withAllowFailure(); + + $process = docker_compose_run( + runCommand: $runCommand, + c: $c, + service: $service, + noDeps: $noDeps, + workDir: $workDir, + withBuilder: $withBuilder, + ); + + return $process->getExitCode() ?? 0; +} + + // 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