Skip to content

Commit

Permalink
build: configure mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
medeirosinacio committed Jul 6, 2024
1 parent a46b18f commit cf987b1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
59 changes: 59 additions & 0 deletions .docker/mongo/reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

set -eu

# Set variables
SDDB_USER=user
SDDB_PASS=password
SDDB_DATABASE=default

# replicate set initiate
echo "Checking mongo container..."
until mongosh --host mongo --eval "print(\"waited for connection\")"
do
sleep 1
done

echo "Initializing replicaset..."
mongosh --host mongo <<EOF
rs.initiate(
{
_id: "rs0",
version: 1,
members: [
{ _id: 0, host: "mongo:27017"}
]
}
)
rs.status()
EOF


echo "Creating admin user: root@root/admin"
mongosh --host mongo <<EOF
db.getSiblingDB('admin').createUser(
{
user: "root",
pwd: "root",
roles: [ { role: "root", db: "admin" } ]
}
)
rs.status()
EOF

echo "Creating normal user: ${SDDB_USER}:${SDDB_PASS}/${SDDB_DATABASE}"
mongosh --host mongo <<EOF
use ${SDDB_DATABASE}
db.createUser(
{
user: "${SDDB_USER}",
pwd: "${SDDB_PASS}",
roles: [ { role: "dbOwner", db: "${SDDB_DATABASE}" } ]
}
)
EOF

echo "Confirm normal user account"
echo "---------------------------------------"
mongosh --eval 'rs.status()' "mongodb://${SDDB_USER}:${SDDB_PASS}@mongo:27017/${SDDB_DATABASE}"
echo "---------------------------------------"
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- melhorar container do mongodb
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.7"

services:

php:
Expand All @@ -12,7 +10,7 @@ services:
image: mongo:6.0.13
command: --replSet rs0
volumes:
- ./migration/mongo:/migration
- ./migrations/mongo:/migrations
ports:
- '27017:27017'
restart: on-failure
Expand Down
31 changes: 29 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
#!/usr/bin/make

.SILENT: clean
.PHONY: all
.DEFAULT_GOAL := help

##@ Development resources

setup: ## Setup the project
@chmod +x ./.docker/mongo/init.sh
@chmod +x ./migrations/mongo/records_collection.sh
docker-compose down -v
docker-compose pull
docker-compose up -d --build --remove-orphans --force-recreate
sleep 5
make setup-database

setup-database: ## Setup the database
make database-cleanup
make database-migrate

database-cleanup: ## Cleanup the database
docker-compose exec mongo bash -c "mongosh \"mongodb://mongo/default?replicaSet=rs0&readPreference=primary\" --quiet --eval 'db.getCollectionNames().forEach(c => db.getCollection(c).drop());'"

database-migrate: ## Migrate the database
docker-compose exec mongo bash -c "find /migrations/ -name \"*.sh\" -exec {} \;"

playground: ## Start a PHP playground dockerized environment
@make check-docker
@docker-compose exec php bash

check-docker: ## Check if Docker is installed
@docker --version > /dev/null 2>&1 || (echo "Docker is not installed. Please install Docker and try again." && exit 1)

help: ## Show this help message
@echo "Usage: make [command]"
@echo ""
@echo "Commands available:"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort
5 changes: 2 additions & 3 deletions migrations/mongo/records_collection.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env bash

mongosh --host mongo <<EOF
use default
## Need to connect to the mongo primary node
mongosh "mongodb://mongo/default?replicaSet=rs0&readPreference=primary" <<EOF
db.createCollection("records")
db.records.createIndex(
{ "expires_at": 1 },
Expand Down

0 comments on commit cf987b1

Please sign in to comment.