Skip to content

Commit

Permalink
Merge pull request #297 from jolicode/worker-profile
Browse files Browse the repository at this point in the history
Add support for profile & service in up,down, and logs command
  • Loading branch information
pyrech committed Apr 5, 2024
2 parents 7c7b50f + b9da302 commit 7c4afee
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 230 deletions.
156 changes: 79 additions & 77 deletions .castor/docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Castor\Attribute\AsOption;
use Castor\Attribute\AsTask;
use Castor\Context;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Exception\ExceptionInterface;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -61,29 +62,57 @@ function open_project(): void
}

#[AsTask(description: 'Builds the infrastructure', aliases: ['build'])]
function build(): void
{
function build(
?string $service = null,
?string $profile = null,
): void {
io()->title('Building infrastructure');

$command = [];

if ($profile) {
$command[] = '--profile';
$command[] = $profile;
}

$userId = variable('user_id');
$phpVersion = variable('php_version');

$command = [
...$command,
'build',
'--build-arg', "USER_ID={$userId}",
'--build-arg', "PHP_VERSION={$phpVersion}",
];

if ($service) {
$command[] = $service;
}

docker_compose($command, withBuilder: true);
}

/**
* @param list<string> $profiles
*/
#[AsTask(description: 'Builds and starts the infrastructure', aliases: ['up'])]
function up(): void
{
io()->title('Starting infrastructure');
function up(
?string $service = null,
#[AsOption(mode: InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED)]
array $profiles = [],
): void {
if (!$service && !$profiles) {
io()->title('Starting infrastructure');
}

$command = ['up', '--detach', '--no-build'];

if ($service) {
$command[] = $service;
}

try {
docker_compose(['up', '--detach', '--no-build']);
docker_compose($command, profiles: $profiles);
} catch (ExceptionInterface $e) {
io()->error('An error occured while starting the infrastructure.');
io()->note('Did you forget to run "castor docker:build"?');
Expand All @@ -93,12 +122,26 @@ function up(): void
}
}

/**
* @param list<string> $profiles
*/
#[AsTask(description: 'Stops the infrastructure', aliases: ['stop'])]
function stop(): void
{
io()->title('Stopping infrastructure');
function stop(
?string $service = null,
#[AsOption(mode: InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED)]
array $profiles = [],
): void {
if (!$service || !$profiles) {
io()->title('Stopping infrastructure');
}

$command = ['stop'];

if ($service) {
$command[] = $service;
}

docker_compose(['stop']);
docker_compose($command, profiles: $profiles);
}

#[AsTask(description: 'Opens a shell (bash) into a builder container', aliases: ['builder'])]
Expand All @@ -113,10 +156,22 @@ function builder(): void
docker_compose_run('bash', c: $c);
}

/**
* @param list<string> $profiles
*/
#[AsTask(description: 'Displays infrastructure logs', aliases: ['logs'])]
function logs(): void
{
docker_compose(['logs', '-f', '--tail', '150'], c: context()->withTty());
function logs(
?string $service = null,
#[AsOption(mode: InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED)]
array $profiles = [],
): void {
$command = ['logs', '-f', '--tail', '150'];

if ($service) {
$command[] = $service;
}

docker_compose($command, c: context()->withTty(), profiles: $profiles);
}

#[AsTask(description: 'Lists containers status', aliases: ['ps'])]
Expand Down Expand Up @@ -225,49 +280,15 @@ function workers_start(): void
{
io()->title('Starting workers');

$workers = get_workers();

if (!$workers) {
return;
}

run([
'docker',
'update',
'--restart=unless-stopped',
...$workers,
], quiet: true);

run([
'docker',
'start',
...$workers,
], quiet: true);
up(profiles: ['worker']);
}

#[AsTask(description: 'Stops the workers', namespace: 'docker:worker', name: 'stop', aliases: ['stop-workers'])]
function workers_stop(): void
{
io()->title('Stopping workers');

$workers = get_workers();

if (!$workers) {
return;
}

run([
'docker',
'update',
'--restart=no',
...$workers,
]);

run([
'docker',
'stop',
...$workers,
]);
stop(profiles: ['worker']);
}

#[AsContext(default: true)]
Expand Down Expand Up @@ -305,7 +326,7 @@ function create_default_context(): Context
// If the directory does not exist, we create it. Otherwise, docker
// will do, as root, and the user will not be able to write in it.
if (!is_dir($composerCacheDir)) {
mkdir($composerCacheDir, 0777, true);
mkdir($composerCacheDir, 0o777, true);
}
}

Expand Down Expand Up @@ -355,11 +376,13 @@ function create_ci_context(): Context
}

/**
* @param array<string> $subCommand
* @param list<string> $subCommand
* @param list<string> $profiles
*/
function docker_compose(array $subCommand, ?Context $c = null, bool $withBuilder = false): Process
function docker_compose(array $subCommand, ?Context $c = null, bool $withBuilder = false, array $profiles = []): Process
{
$c ??= context();
$profiles = $profiles ?: ['default'];

$domains = [variable('root_domain'), ...variable('extra_domains')];
$domains = '`' . implode('`) || Host(`', $domains) . '`';
Expand All @@ -381,6 +404,10 @@ function docker_compose(array $subCommand, ?Context $c = null, bool $withBuilder
'compose',
'-p', variable('project_name'),
];
foreach ($profiles as $profile) {
$command[] = '--profile';
$command[] = $profile;
}

foreach (variable('docker_compose_files') as $file) {
$command[] = '-f';
Expand Down Expand Up @@ -466,28 +493,3 @@ function run_in_docker_or_locally_for_mac(string $command, ?Context $c = null):
docker_compose_run($command, c: $c);
}
}

/**
* Find worker containers for the current project.
*
* @return array<string>
*/
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);
}
2 changes: 1 addition & 1 deletion .castor/qa.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function all(): int
$phpstan = phpstan();
// $phpunit = phpunit();

return max($cs, $phpstan/*, $phpunit*/);
return max($cs, $phpstan/* , $phpunit */);
}

#[AsTask(description: 'Installs tooling')]
Expand Down
4 changes: 2 additions & 2 deletions castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ function start(): void
{
io()->title('Starting the stack');

workers_stop();
// workers_stop();
generate_certificates(force: false);
build();
up();
cache_clear();
install();
migrate();
workers_start();
// workers_start();

notify('The stack is now up and running.');
io()->success('The stack is now up and running.');
Expand Down
5 changes: 3 additions & 2 deletions infrastructure/docker/docker-compose.worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ x-services-templates:
#- rabbitmq
volumes:
- "../..:/var/www:cached"
labels:
- "docker-starter.worker.${PROJECT_NAME}=true"
profiles:
- default
- worker

# services:
# worker_messenger:
Expand Down
6 changes: 6 additions & 0 deletions infrastructure/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
- "/var/run/docker.sock:/var/run/docker.sock"
- "./services/router/certs:/etc/ssl/certs"
network_mode: host
profiles:
- default

frontend:
build:
Expand All @@ -19,6 +21,8 @@ services:
- "../..:/var/www:cached"
environment:
- "PHP_VERSION=${PHP_VERSION}"
profiles:
- default
labels:
- "traefik.enable=true"
- "traefik.http.routers.${PROJECT_NAME}-frontend.rule=Host(${PROJECT_DOMAINS})"
Expand All @@ -34,3 +38,5 @@ services:
- POSTGRES_PASSWORD=app
volumes:
- postgres-data:/var/lib/postgresql/data
profiles:
- default
2 changes: 1 addition & 1 deletion tools/php-cs-fixer/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "project",
"require": {
"friendsofphp/php-cs-fixer": "^3.47.1"
"friendsofphp/php-cs-fixer": "^3.52.1"
},
"config": {
"platform": {
Expand Down

0 comments on commit 7c4afee

Please sign in to comment.