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

Add mongo-express command #19

Merged
merged 1 commit into from
Oct 31, 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
22 changes: 0 additions & 22 deletions .githooks/commit-msg

This file was deleted.

39 changes: 39 additions & 0 deletions .github/workflows/doc-links.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Documentation links

on:
workflow_dispatch:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
markdown-test-links:
name: Markdown test links
runs-on: ubuntu-latest
steps:
- name: Clone sources
uses: actions/checkout@v4
with:
path: sources

- name: Lauch localhost server
run: |
sudo npm install --global http-server
http-server ./sources &

- name: Set up Ruby 2.6
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

- name: Check links in Markdown files
run: |
gem install awesome_bot
cd sources
awesome_bot README.md --skip-save-results --allow-dupe --base-url http://localhost:8080/ --white-list ddev.site
25 changes: 11 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
name: Create Release
# example: gh workflow run release.yml -f tag_name=v1.1.4 -f draft=true
# example: gh workflow run release.yml -f tag_name=v1.1.4
on:
workflow_dispatch:
branches:
- main
inputs:
tag_name:
type: string
description: Tag name
required: true
draft:
type: boolean
description: Draft release
default: false
prerelease:
type: boolean
description: Prerelease
default: false

jobs:
create-release:
Expand Down Expand Up @@ -45,7 +35,7 @@ jobs:
- name: Check version ${{ env.VERSION_NUMBER }} consistency in files
# Check CHANGELOG.md
run: |

# Check top ## [VERSION_NUMBER](GITHUB_URL/releases/tag/VERSION_NUMBER) - yyyy-mm-dd in CHANGELOG.md
CURRENT_DATE=$(date +'%Y-%m-%d')
echo $CURRENT_DATE
Expand All @@ -58,7 +48,7 @@ jobs:
echo "CHANGELOG VERSION KO"
exit 1
fi

# Check top [_Compare with previous release_](GITHUB_URL/compare/LAST_TAG...VERSION_NUMBER) in CHANGELOG.md
COMPARISON=$(grep -oP "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/compare/\K(.*)$" CHANGELOG.md | head -1)
LAST_TAG=$(curl -Ls -o /dev/null -w %{url_effective} $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/latest | grep -oP "\/tag\/\K(.*)$")
Expand All @@ -73,7 +63,7 @@ jobs:
fi

- name: Create Tag ${{ github.event.inputs.tag_name }}
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
github-token: ${{ github.token }}
script: |
Expand All @@ -98,3 +88,10 @@ jobs:
tag_name: ${{ github.event.inputs.tag_name }}
draft: ${{ github.event.inputs.draft }}
prerelease: ${{ github.event.inputs.prerelease }}

test-release:
name: Test release
needs: [create-release]
uses: ./.github/workflows/tests.yml
with:
debug_enabled: false
14 changes: 10 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name: tests
on:
pull_request:
push:
branches: [ main ]
branches: [main]
paths-ignore:
- '**.md'
- "**.md"

schedule:
- cron: '44 08 * * *'
- cron: "44 08 * * *"

workflow_dispatch:
inputs:
Expand All @@ -16,13 +16,19 @@ on:
description: Debug with tmate
required: false
default: false
workflow_call:
inputs:
debug_enabled:
type: boolean
description: Debug with tmate
required: false
default: false

permissions:
actions: write

jobs:
tests:

strategy:
matrix:
ddev_version: [stable, HEAD]
Expand Down
42 changes: 31 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,58 @@

## What is ddev-mongo?

This repository provides Mongo and Mongo Express add-on to [DDEV](https://ddev.readthedocs.io).
This repository provides Mongo and Mongo Express add-on to [DDEV](https://ddev.readthedocs.io/en/stable/).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This repository provides Mongo and Mongo Express add-on to [DDEV](https://ddev.readthedocs.io/en/stable/).
This repository is an add-on that provides Mongo and Mongo Express for [DDEV](https://ddev.readthedocs.io/en/stable/).


It's based on [MongoDb from Docker Hub](https://hub.docker.com/_/mongo?tab=description#-via-docker-stack-deploy-or-docker-compose), [DDEV custom compose files](https://ddev.readthedocs.io/en/stable/users/extend/custom-compose-files/) and [API Platform tutorial](https://api-platform.com/docs/core/mongodb/#enabling-mongodb-support).

## Installation

For DDEV v1.23.5 or above run

```bash
ddev add-on get ddev/ddev-mongo
```

For earlier versions of DDEV run

```bash
ddev get ddev/ddev-mongo
```

Then restart your project

```bash
ddev restart
```

## Configuration

1. Your project will likely require the [Doctrine MongoDB ODM bundle](https://github.com/doctrine/DoctrineMongoDBBundle)
`ddev composer require doctrine/mongodb-odm-bundle:^4.0.0@beta doctrine/mongodb-odm:^2.0.0@beta`

2. In your application `.env` or other client, set the connection string:

```
MONGODB_URL=mongodb://db:db@mongo:27017
MONGODB_DB=api
```

Mongo Express will now be accessible from `http://<project>.ddev.site:9091`
```
MONGODB_URL=mongodb://db:db@mongo:27017
```

Mongo Express will now be accessible by running `ddev mongo-express` command.

## Features

### `ddev mongosh` command

This command will run the `mongosh` (mongoDB Shell) command in the `mongo` container. Please [read the documentation](https://www.mongodb.com/docs/mongodb-shell/) for more information.

### `ddev mongo-express` command

## Caveats:
This command opens your browser to the Mongo Express page.

* The php extension (phpX.X-mongodb) is set up in `.ddev/config.mongo.yaml` using `webimage_extra_packages`. If you have an earlier `webimage_extra_packages` in your config.yaml, this will override it. You may want to edit your config.yaml to do what you want and remove the config.mongo.yaml.
* You can't define custom MongoDB configuration with this current setup.
* You can't use `ddev import-db` to import to mongo.
## Caveats:

- The php extension (phpX.X-mongodb) is set up in `.ddev/config.mongo.yaml` using `webimage_extra_packages`. If you have an earlier `webimage_extra_packages` in your config.yaml, this will override it. You may want to edit your config.yaml to do what you want and remove the config.mongo.yaml.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have an earlier webimage_extra_packages in your config.yaml, this will override it.

This is not true, webimage_extra_packages are merged from all configs, this should be removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will remove this.

- You can't define custom MongoDB configuration with this current setup.
- You can't use `ddev import-db` to import to mongo.

**Based on the original [ddev-contrib recipe](https://github.com/ddev/ddev-contrib/tree/master/docker-compose-services/mongodb)**

Expand Down
23 changes: 23 additions & 0 deletions commands/host/mongo-express
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

## #ddev-generated: If you want to edit and own this file, remove this line.
## Description: Launch a browser with Mongo Express
## Usage: mongo-express
## Example: "ddev mongo-express"

# Determine whether to use HTTP or HTTPS based on environment
PROTOCOL="HTTP"
if [ "${DDEV_PRIMARY_URL%://*}" != "http" ] && [ -z "${GITPOD_WORKSPACE_ID:-}" ] && [ "${CODESPACES:-}" != "true" ]; then
PROTOCOL="HTTPS"
fi

# Fetch the appropriate port within the mongo-express container
DDEV_MONGO_EXPRESS_PORT=$(ddev exec -s mongo-express sh -c "printenv | grep -w ${PROTOCOL}_EXPOSE | cut -d '=' -f 2 | cut -d ':' -f 1")

if [ -z "$DDEV_MONGO_EXPRESS_PORT" ]; then
echo "Error: Could not retrieve Mongo Express port."
exit 1
fi

ddev launch :"$DDEV_MONGO_EXPRESS_PORT"

4 changes: 4 additions & 0 deletions docker-compose.mongo_norouter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ddev-generated
# If omit_containers[ddev-router] then this file will be replaced
# with another with a `ports` statement to directly expose port 9091 to 8081
services: {}
27 changes: 22 additions & 5 deletions install.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
name: mongo

pre_install_actions:
- |
#ddev-description:Create config.mongo.yaml with webimage_extra_packages in it
printf '#ddev-generated\nwebimage_extra_packages: ["php${DDEV_PHP_VERSION}-mongodb"]\n' >.ddev/config.mongo.yaml
- |
#ddev-description:Create config.mongo.yaml with webimage_extra_packages in it
printf '#ddev-generated\nwebimage_extra_packages: ["php${DDEV_PHP_VERSION}-mongodb"]\n' >.ddev/config.mongo.yaml
# Ensure we're on DDEV 1.23+. It's required for the `redis-commander` command (launch by port).
- |
#ddev-nodisplay
#ddev-description:Checking DDEV version
(ddev debug capabilities | grep corepack >/dev/null) || (echo "Please upgrade DDEV to v1.23+ to enable launching." && false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than this approach, it's easier now to use ddev_version_constraint in the config.mongo.yaml if there is one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have said ddev_version_constraint in the install.yaml., https://github.com/ddev/ddev-addon-template/blob/main/install.yaml#L85


post_install_actions:
- |
#ddev-description:If router disabled, directly expose port
#
if ( {{ contains "ddev-router" (list .DdevGlobalConfig.omit_containers | toString) }} ); then
printf "#ddev-generated\nservices:\n mongo-express:\n ports:\n - 9091:8081\n" > docker-compose.mongo_norouter.yaml
fi
- |
echo "You can now use 'ddev mongo-express' to launch Mongo Express UI"

# list of files and directories listed that are copied into project .ddev directory
project_files:
- commands/mongo/mongosh
- docker-compose.mongo.yaml
- commands/mongo/mongosh
- commands/host/mongo-express
- docker-compose.mongo.yaml
- docker-compose.mongo_norouter.yaml
47 changes: 24 additions & 23 deletions tests/test.bats
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
setup() {
set -eu -o pipefail
brew_prefix=$(brew --prefix)
load "${brew_prefix}/lib/bats-support/load.bash"
load "${brew_prefix}/lib/bats-assert/load.bash"
export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.."
export TESTDIR=~/tmp/testmongo
mkdir -p $TESTDIR
Expand All @@ -19,12 +22,11 @@ teardown() {
[ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR}
}

@test "install from directory" {
set -eu -o pipefail
cd ${TESTDIR}
echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ${DIR} >/dev/null
ddev restart >/dev/null
health_checks() {
set +u # bats-assert has unset variables so turn off unset check
# ddev restart is required because we have done `ddev add-on get` on a new service
run ddev restart
assert_success
curl -sI ${PROJNAME}.ddev.site:9091/db/admin/expArr/system.users | grep "HTTP/1.1 200 OK"
out=$(curl -s ${PROJNAME}.ddev.site:9091/db/admin/expArr/system.users | jq -r ".[0].user")
[ "${out}" = "db" ]
Expand All @@ -39,26 +41,25 @@ teardown() {
echo "$result"
exit 1
fi
# Make sure `ddev mongo-express` works
DDEV_DEBUG=true run ddev mongo-express
assert_success
assert_output --partial "FULLURL https://${PROJNAME}.ddev.site:9092"
}

@test "install from directory" {
set -eu -o pipefail
cd ${TESTDIR}
echo "# ddev add-on get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev add-on get ${DIR} >/dev/null
health_checks
}

# bats test_tags=release
@test "install from release" {
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
echo "# ddev get ddev/ddev-mongo with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ddev/ddev-mongo >/dev/null
ddev restart >/dev/null
curl -sI ${PROJNAME}.ddev.site:9091/db/admin/expArr/system.users | grep "HTTP/1.1 200 OK"
out=$(curl -s ${PROJNAME}.ddev.site:9091/db/admin/expArr/system.users | jq -r ".[0].user")
[ "${out}" = "db" ]
(ddev exec php -i | grep mongodb.debug) || (echo "# php mongodb extension not found" >&3 || exit 1)
# Test mongosh
result=$(ddev mongosh "mongodb://db:db@mongo:27017/test?authSource=admin" --quiet --eval '"printjson(db.getUsers())"' --json | grep "ok" | sed 's/: 1/ /g' | sed 's/ //g' | tr ' \n' '#')
if [[ $result == "ok#" ]]
then
echo "# Mongosh OK" >&3
else
echo "# Mongosh KO"
echo "$result"
exit 1
fi
echo "# ddev add-on get ddev/ddev-mongo with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev add-on get ddev/ddev-mongo >/dev/null
health_checks
}