From 020c458921898a1fafd8649053483eaddadcb7d6 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Sat, 26 Jun 2021 16:06:46 +0200 Subject: [PATCH] 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 --- docs/reference/universe/docker/compose.md | 14 ++++++++------ stdlib/docker/compose/compose.cue | 4 ++++ stdlib/docker/compose/tests/cleanup.cue | 10 +++++++--- stdlib/docker/compose/tests/compose.cue | 20 ++++++++++++-------- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/docs/reference/universe/docker/compose.md b/docs/reference/universe/docker/compose.md index 3cec19a8..2a74308c 100644 --- a/docs/reference/universe/docker/compose.md +++ b/docs/reference/universe/docker/compose.md @@ -14,12 +14,14 @@ import "alpha.dagger.io/docker/compose" ### compose.#App Inputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*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.package."docker-compose"* | `true` |- | -|*run.registries* | `[]` |Image registries | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*name* | `*"source" \| string` |App name (use as COMPOSE_PROJECT_NAME) | +|*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.env.COMPOSE_PROJECT_NAME* | `*"source" \| string` |- | +|*run.package."docker-compose"* | `true` |- | +|*run.registries* | `[]` |Image registries | ### compose.#App Outputs diff --git a/stdlib/docker/compose/compose.cue b/stdlib/docker/compose/compose.cue index bff531aa..77067ec2 100644 --- a/stdlib/docker/compose/compose.cue +++ b/stdlib/docker/compose/compose.cue @@ -32,6 +32,9 @@ import ( source?: dagger.#Artifact @dagger(input) composeFile?: string @dagger(input) + // App name (use as COMPOSE_PROJECT_NAME) + name: *"source" | string @dagger(input) + // Image registries registries: [...{ target?: string @@ -74,6 +77,7 @@ import ( } env: { COMPOSE_HTTP_TIMEOUT: strconv.FormatInt(200, 10) + COMPOSE_PROJECT_NAME: name if source != _|_ { SOURCE_DIR: "source" } diff --git a/stdlib/docker/compose/tests/cleanup.cue b/stdlib/docker/compose/tests/cleanup.cue index 58b8a59b..938abc88 100644 --- a/stdlib/docker/compose/tests/cleanup.cue +++ b/stdlib/docker/compose/tests/cleanup.cue @@ -11,6 +11,9 @@ import ( // docker-compose up context context: dagger.#Artifact + // App name (use as COMPOSE_PROJECT_NAME) + name: *"source" | string + ssh: { // ssh host host: string @dagger(input) @@ -68,9 +71,10 @@ import ( "/entrypoint.sh", ] env: { - DOCKER_HOSTNAME: ssh.host - DOCKER_USERNAME: ssh.user - DOCKER_PORT: strconv.FormatInt(ssh.port, 10) + DOCKER_HOSTNAME: ssh.host + DOCKER_USERNAME: ssh.user + DOCKER_PORT: strconv.FormatInt(ssh.port, 10) + COMPOSE_PROJECT_NAME: name if ssh.keyPassphrase != _|_ { SSH_ASKPASS: "/get_passphrase" DISPLAY: "1" diff --git a/stdlib/docker/compose/tests/compose.cue b/stdlib/docker/compose/tests/compose.cue index 32d4caf5..b6befc6f 100644 --- a/stdlib/docker/compose/tests/compose.cue +++ b/stdlib/docker/compose/tests/compose.cue @@ -14,6 +14,8 @@ TestSSH: { } TestCompose: { + name: "compose_test" + up: #App & { ssh: { key: TestSSH.key @@ -21,22 +23,26 @@ TestCompose: { user: TestSSH.user } source: repo + "name": name } verify: docker.#Command & { - ssh: up.run.ssh + ssh: up.run.ssh command: #""" - docker container ls | grep "api" | grep "Up" + docker container ls | grep "\#(name)_api" | grep "Up" """# } cleanup: #CleanupCompose & { context: up.run + "name": name ssh: verify.ssh } } TestInlineCompose: { + name: "inline_test" + up: #App & { ssh: { key: TestSSH.key @@ -44,6 +50,7 @@ TestInlineCompose: { user: TestSSH.user } source: repo + "name": name composeFile: #""" version: "3" @@ -54,22 +61,19 @@ TestInlineCompose: { PORT: 7000 ports: - 7000:7000 - - networks: - default: - name: mix-context """# } verify: docker.#Command & { - ssh: up.run.ssh + ssh: up.run.ssh command: #""" - docker container ls | grep "api-mix" | grep "Up" + docker container ls | grep "\#(name)_api-mix" | grep "Up" """# } cleanup: #CleanupCompose & { context: up.run + "name": name ssh: verify.ssh } }