Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/docker container cleanup #98

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Asm/Ansible/Command/AbstractAnsibleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ abstract class AbstractAnsibleCommand

private array $baseOptions;

private bool $useStdoutForError;

/**
* @param ProcessBuilderInterface $processBuilder
* @param LoggerInterface|null $logger
Expand All @@ -44,6 +46,7 @@ public function __construct(ProcessBuilderInterface $processBuilder, LoggerInter
$this->options = [];
$this->parameters = [];
$this->baseOptions = [];
$this->useStdoutForError = false;
$this->setLogger($logger ?? new NullLogger());
}

Expand Down Expand Up @@ -183,7 +186,7 @@ protected function runProcess(?callable $callback): int|string

// if no callback is set, and the process is not successful, we return the output
if (false === $process->isSuccessful()) {
return $process->getErrorOutput();
return $this->useStdoutForError ? $process->getOutput() : $process->getErrorOutput();
}

// if no callback is set, and the process is successful, we return the output
Expand All @@ -210,4 +213,14 @@ protected function getProcessCommandline(Process $process): string

return sprintf('%s %s', implode(' ', $vars), $commandline);
}

/**
* in case ansible explicitly is in json mode, this will be set to be able to get error outputs
*
* @return void
*/
protected function useStdoutForError(): void
{
$this->useStdoutForError = true;
}
}
1 change: 1 addition & 0 deletions Asm/Ansible/Command/AnsiblePlaybook.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public function colors(bool $colors = true): AnsiblePlaybookInterface
public function json(): AnsiblePlaybookInterface
{
$this->processBuilder->setEnv('ANSIBLE_STDOUT_CALLBACK', 'json');
$this->useStdoutForError();

return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions .docker/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM php:8.3-cli

WORKDIR /app

ENV ANSIBLE_VERSION 2.9.17

# composer
Expand Down
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Executables (local)
DOCKER_COMP = docker compose

# Docker containers
PHP_CONT = $(DOCKER_COMP) exec php-ansible

# Executables
PHP = $(PHP_CONT) php
COMPOSER = $(PHP_CONT) composer

# Misc
.DEFAULT_GOAL = help

## —— php-ansible Makefile 🎵 ——————————————————————————————————
help: ## Outputs this help screen
@grep -E '(^[a-zA-Z0-9\./_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'

## —— Docker 🐳 ————————————————————————————————————————————————————————————————
build: ## Builds the Docker images
@$(DOCKER_COMP) build --pull --no-cache

up: ## Start the docker hub in detached mode (no logs)
@$(DOCKER_COMP) up --detach

start: build up ## Build and start the containers

down: ## Stop the docker hub
@$(DOCKER_COMP) down --remove-orphans

logs: ## Show live logs
@$(DOCKER_COMP) logs --tail=0 --follow

sh: ## Connect to the php-fpm container
@$(PHP_CONT) sh

bash: ## Connect to the php-fpm container via bash so up and down arrows go to previous commands
@$(PHP_CONT) bash

test: ## Start tests with phpunit, pass the parameter "c=" to add options to phpunit, example: make test c="--group e2e --stop-on-failure"
@$(eval c ?=)
@$(PHP_CONT) vendor/bin/phpunit -c phpunit.xml.dist $(c)

analyze: ## Start analysis with phpstan, pass the parameter "c=" to add options to phpstan. Default config ist always used, example: make analyze c="--group e2e"
@$(eval c ?=)
@$(PHP_CONT) vendor/bin/phpstan --configuration=phpstan.neon $(c)

codestyle: ## Start codestyle analysis with phpcs, pass the parameter "c=" to add options to phpcs. Default config ist always used. Example: make codestyle c="--parallel=2"
@$(eval c ?=)
@$(PHP_CONT) vendor/bin/phpcs --standard=phpcs.xml.dist $(c)

codestyle-fix: ## Start codestyle analysis with phpcbf, pass the parameter "c=" to add options to phpcbf. Default config ist always used. Example: make codestyle c="--parallel=2"
@$(eval c ?=)
@$(PHP_CONT) vendor/bin/phpcbf --standard=phpcs.xml.dist $(c)

psalm: ## Start code analysis with psalm, pass the parameter "c=" to add options to psalm. Default config ist always used. Example: make codestyle c="--level=2"
@$(eval c ?=)
@$(DOCKER_COMP) exec -e APP_ENV=dev php vendor/bin/psalm --config=psalm.xml $(c)

## —— Composer 🧙 ——————————————————————————————————————————————————————————————
composer: ## Run composer, pass the parameter "c=" to run a given command, example: make composer c='req phpstan/phpstan'
@$(eval c ?=)
@$(COMPOSER) $(c)

vendor: ## Install vendors according to the current composer.lock file
vendor: c=install --prefer-dist --no-dev --no-progress --no-scripts --no-interaction
vendor: composer
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ $ansible

## Development

You can use the provided docker image with ```docker compose up``` which uses a default php-cli docker image and ansible 2.x. See the ```.docker/Dockerfile``` for more info.

Composer install: ```docker exec -u <YOUR_UID> -w /var/php-ansible -it php-ansible composer install```

You can run code or the tests within the container: ```docker exec -u <YOUR_UID> -w /var/php-ansible -it php-ansible php ./vendor/bin/phpunit --testdox```
You can use the provided docker image with ```make build``` which uses a default php-cli docker image and ansible 2.x. See the ```Dockerfile``` for more info.
Start the container with ```make up```.
Composer install: ```make vendor```
You can run code or the tests within the container: ```make test c="--testdox"```

## Thank you for your contributions!

Expand Down
32 changes: 32 additions & 0 deletions Tests/Asm/Ansible/Command/AnsiblePlaybookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,38 @@ public function testReturnsErrorOutputIfProcessWasNotSuccessful(): void
self::assertEquals('error output', $playbook->execute());
}

public function testReturnsErrorOutputIfProcessWasNotSuccessfulInJsonMode(): void
{
$builder = $this->createMock(ProcessBuilderInterface::class);
$builder
->expects(self::once())
->method('setArguments')
->willReturnSelf();
$builder
->expects(self::once())
->method('getProcess')
->willReturn($process = $this->createMock(Process::class));
$process
->expects(self::once())
->method('run');
$process
->expects(self::once())
->method('isSuccessful')
->willReturn(false);
$process
->expects(self::once())
->method('getOutput')
->willReturn('{ "error": "error output" }');
$process
->expects(self::never())
->method('getErrorOutput');

$playbook = new AnsiblePlaybook($builder);
$playbook->json();

self::assertEquals('{ "error": "error output" }', $playbook->execute());
}

public function testReturnsNormalOutputIfProcessWasSuccessful(): void
{
$builder = $this->createMock(ProcessBuilderInterface::class);
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml → compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
php-ansible:
build: ./.docker
build: .
container_name: php-ansible
volumes:
- ./:/var/php-ansible
- ./:/app
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<psalm
errorLevel="2"
resolveFromConfigFile="1"
phpVersion="8.0"
phpVersion="8.3"
>
<projectFiles>
<directory name="Asm" />
Expand Down