automated mongodb + mongo-express deployment with user creation

This commit is contained in:
voje 2019-03-15 09:00:05 +01:00
parent 49e45900c4
commit d8fd3ad30b
8 changed files with 43 additions and 20 deletions

View File

@ -15,8 +15,10 @@ OUTPUT = "file"
OUTDIR = "/home/voje/workdir/test_out"
DBADDR = ""
DB_ADM_USER = testuser
DB_ADM_PASS = testpass
DB_ADM_USER = testadmin
DB_ADM_PASS = testadminpass
DB_USR_USER = testuser
DB_USR_PASS = testuserpass
export
.PHONY: dev-env preflight

View File

@ -0,0 +1,5 @@
FROM mongo:latest
WORKDIR /
COPY init_inside_container.sh /.
COPY create.js /.

View File

@ -4,20 +4,31 @@
# mongo admin -u root -p password --eval "db.getSiblingDB('vlDB').addUser('vluser', 'password')"
.PHONY: start_db
STACKNAME = dbstack
all: run_stack create_users
.PHONY: start_db FORCE
all: build_mongo run_stack create_users
info:
echo "[*] RUN THIS A FEW TIMES (THE STACK NEEDS TIME TO CLEAN UP AND TO SPIN UP THE NODES)"
create.js: FORCE
FORCE:
echo 'db.auth("$(DB_ADM_USER)", "$(DB_ADM_PASS)")' > create.js
echo 'use valdb' >> create.js
echo 'db.createUser({user: "$(DB_USR_USER)", pwd: "$(DB_USR_PASS)", roles: ["readWrite"]})' >> create.js
build_mongo: create.js
docker build . -t my-mongo --no-cache
clean_stack:
docker stack rm $(STACKNAME)
run_stack:
docker stack deploy --compose-file mongodb-stack.yml $(STACKNAME)
create.js:
echo -e 'db.auth("$(DB_ADM_USER)", "$(DB_ADM_PASS)")\n \
use valdb\n \
db.createUser({user: "kristjan", pwd: "password", roles: ["readWrite"]})' > create.js
create_users: create.js
docker exec $(shell ./get_container_name.sh) mongo admin < create.js
docker exec $(shell ./get_container_name.sh) /init_inside_container.sh
# rm create.js

View File

@ -0,0 +1,3 @@
# Some caveats
* Access mongo-express using `0.0.0.0:8087` instead of `localhost:...`.
* Run make from the root Makefile `make database`. You might need to run it several times (wait for conatiners to come up, etc.).

View File

@ -1,5 +1,3 @@
db.auth("testuser", "testpass")
\
db.auth("testadmin", "testadminpass")
use valdb
\
db.createUser({user: "kristjan", pwd: "password", roles: ["readWrite"]})
db.createUser({user: "testuser", pwd: "testuserpass", roles: ["readWrite"]})

View File

@ -1,2 +1,2 @@
#!/bin/bash
docker ps | grep mongo:latest | awk '{print $1}'
docker ps | grep my-mongo | awk '{print $1}'

View File

@ -0,0 +1,3 @@
#!/bin/bash
mongo admin < /create.js

View File

@ -2,8 +2,8 @@ version: '3.1'
services:
mongo:
image: mongo
my-mongo:
image: my-mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${DB_ADM_USER}
@ -13,7 +13,8 @@ services:
image: mongo-express
restart: always
ports:
- 8081:8081
- 8087:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: ${DB_ADM_USER}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${DB_ADM_PASS}
ME_CONFIG_MONGODB_SERVER: my-mongo