Add project management in compose.#App definition

I found an issue when during tests execution : there was orphan.
It's because #App doesn't give way to specify the compose project,
by default it's the directory where you launch your app but in our
definition, it will always be source.

The problem is that if we launch two differents docker-compose in the same
server, his project name will be source for both and it will create
orphans problems on cleanup (by docker-compose down).

This case is exactly what we do in tests so I've add the field name
to specify the projet name and avoid that issue.

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2021-06-26 16:06:46 +02:00
parent ad1eb75893
commit 020c458921
4 changed files with 31 additions and 17 deletions

View File

@ -16,8 +16,10 @@ import "alpha.dagger.io/docker/compose"
| Name | Type | Description | | Name | Type | Description |
| ------------- |:-------------: |:-------------: | | ------------- |:-------------: |:-------------: |
|*name* | `*"source" \| string` |App name (use as COMPOSE_PROJECT_NAME) |
|*registries* | `[]` |Image registries | |*registries* | `[]` |Image registries |
|*run.command* | `"""\n if [ -n "$DOCKER_HOSTNAME" ]; then\n \tssh -i /key -fNT -o "StreamLocalBindUnlink=yes" -L "$(pwd)"/docker.sock:/var/run/docker.sock -p "$DOCKER_PORT" "$DOCKER_USERNAME"@"$DOCKER_HOSTNAME"\n \texport DOCKER_HOST="unix://$(pwd)/docker.sock"\n fi\n \n # Extend session duration\n echo "Host *\\nServerAliveInterval 240" \>\> "$HOME"/.ssh/config\n chmod 600 "$HOME"/.ssh/config\n \n # Move compose\n if [ -d "$SOURCE_DIR" ]; then\n \tif [ -f docker-compose.yaml ]; then\n \t\tcp docker-compose.yaml "$SOURCE_DIR"/docker-compose.yaml\n \tfi\n \tcd "$SOURCE_DIR"\n fi\n \n docker-compose build\n docker-compose up -d\n """` |Command to execute | |*run.command* | `"""\n if [ -n "$DOCKER_HOSTNAME" ]; then\n \tssh -i /key -fNT -o "StreamLocalBindUnlink=yes" -L "$(pwd)"/docker.sock:/var/run/docker.sock -p "$DOCKER_PORT" "$DOCKER_USERNAME"@"$DOCKER_HOSTNAME"\n \texport DOCKER_HOST="unix://$(pwd)/docker.sock"\n fi\n \n # Extend session duration\n echo "Host *\\nServerAliveInterval 240" \>\> "$HOME"/.ssh/config\n chmod 600 "$HOME"/.ssh/config\n \n # Move compose\n if [ -d "$SOURCE_DIR" ]; then\n \tif [ -f docker-compose.yaml ]; then\n \t\tcp docker-compose.yaml "$SOURCE_DIR"/docker-compose.yaml\n \tfi\n \tcd "$SOURCE_DIR"\n fi\n \n docker-compose build\n docker-compose up -d\n """` |Command to execute |
|*run.env.COMPOSE_PROJECT_NAME* | `*"source" \| string` |- |
|*run.package."docker-compose"* | `true` |- | |*run.package."docker-compose"* | `true` |- |
|*run.registries* | `[]` |Image registries | |*run.registries* | `[]` |Image registries |

View File

@ -32,6 +32,9 @@ import (
source?: dagger.#Artifact @dagger(input) source?: dagger.#Artifact @dagger(input)
composeFile?: string @dagger(input) composeFile?: string @dagger(input)
// App name (use as COMPOSE_PROJECT_NAME)
name: *"source" | string @dagger(input)
// Image registries // Image registries
registries: [...{ registries: [...{
target?: string target?: string
@ -74,6 +77,7 @@ import (
} }
env: { env: {
COMPOSE_HTTP_TIMEOUT: strconv.FormatInt(200, 10) COMPOSE_HTTP_TIMEOUT: strconv.FormatInt(200, 10)
COMPOSE_PROJECT_NAME: name
if source != _|_ { if source != _|_ {
SOURCE_DIR: "source" SOURCE_DIR: "source"
} }

View File

@ -11,6 +11,9 @@ import (
// docker-compose up context // docker-compose up context
context: dagger.#Artifact context: dagger.#Artifact
// App name (use as COMPOSE_PROJECT_NAME)
name: *"source" | string
ssh: { ssh: {
// ssh host // ssh host
host: string @dagger(input) host: string @dagger(input)
@ -71,6 +74,7 @@ import (
DOCKER_HOSTNAME: ssh.host DOCKER_HOSTNAME: ssh.host
DOCKER_USERNAME: ssh.user DOCKER_USERNAME: ssh.user
DOCKER_PORT: strconv.FormatInt(ssh.port, 10) DOCKER_PORT: strconv.FormatInt(ssh.port, 10)
COMPOSE_PROJECT_NAME: name
if ssh.keyPassphrase != _|_ { if ssh.keyPassphrase != _|_ {
SSH_ASKPASS: "/get_passphrase" SSH_ASKPASS: "/get_passphrase"
DISPLAY: "1" DISPLAY: "1"

View File

@ -14,6 +14,8 @@ TestSSH: {
} }
TestCompose: { TestCompose: {
name: "compose_test"
up: #App & { up: #App & {
ssh: { ssh: {
key: TestSSH.key key: TestSSH.key
@ -21,22 +23,26 @@ TestCompose: {
user: TestSSH.user user: TestSSH.user
} }
source: repo source: repo
"name": name
} }
verify: docker.#Command & { verify: docker.#Command & {
ssh: up.run.ssh ssh: up.run.ssh
command: #""" command: #"""
docker container ls | grep "api" | grep "Up" docker container ls | grep "\#(name)_api" | grep "Up"
"""# """#
} }
cleanup: #CleanupCompose & { cleanup: #CleanupCompose & {
context: up.run context: up.run
"name": name
ssh: verify.ssh ssh: verify.ssh
} }
} }
TestInlineCompose: { TestInlineCompose: {
name: "inline_test"
up: #App & { up: #App & {
ssh: { ssh: {
key: TestSSH.key key: TestSSH.key
@ -44,6 +50,7 @@ TestInlineCompose: {
user: TestSSH.user user: TestSSH.user
} }
source: repo source: repo
"name": name
composeFile: #""" composeFile: #"""
version: "3" version: "3"
@ -54,22 +61,19 @@ TestInlineCompose: {
PORT: 7000 PORT: 7000
ports: ports:
- 7000:7000 - 7000:7000
networks:
default:
name: mix-context
"""# """#
} }
verify: docker.#Command & { verify: docker.#Command & {
ssh: up.run.ssh ssh: up.run.ssh
command: #""" command: #"""
docker container ls | grep "api-mix" | grep "Up" docker container ls | grep "\#(name)_api-mix" | grep "Up"
"""# """#
} }
cleanup: #CleanupCompose & { cleanup: #CleanupCompose & {
context: up.run context: up.run
"name": name
ssh: verify.ssh ssh: verify.ssh
} }
} }