diff --git a/mongo/build/Dockerfile b/mongo/build/Dockerfile new file mode 100644 index 0000000..e108157 --- /dev/null +++ b/mongo/build/Dockerfile @@ -0,0 +1,17 @@ +## Note: alpine version >= 3.9 is required to get mongodb version 4 +## is a simple copy of alpine:3.9 +FROM docker.0k.io/alpine:3.9 as common + +RUN apk add --no-cache mongodb + +RUN mkdir -p /var/lib/mongod +RUN chown -R mongodb:mongodb /var/lib/mongod + +VOLUME ["/var/lib/mongodb"] +EXPOSE 27017 28017 + +USER mongodb + +ENTRYPOINT [ "mongod", "--config", "/etc/mongod.conf" ] + + diff --git a/mongo/hooks/init b/mongo/hooks/init new file mode 100755 index 0000000..8cc5bb2 --- /dev/null +++ b/mongo/hooks/init @@ -0,0 +1,51 @@ +#!/bin/bash + +## Init is run on host +## For now it is run every time the script is launched, but +## it should be launched only once after build. + +## Accessible variables are: +## - SERVICE_NAME Name of current service +## - DOCKER_BASE_IMAGE Base image from which this service might be built if any +## - SERVICE_DATASTORE Location on host of the DATASTORE of this service +## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service + +. lib/common + +set -e + +uid=$(docker_get_uid "$SERVICE_NAME" "mongodb") + +CONFIG=$SERVICE_CONFIGSTORE/etc/mongod.conf + +mkdir -p "$(dirname "$CONFIG")" +## XXXvlab: replication here is hardwired and not handled at all +cat < "$CONFIG" +storage: + dbPath: /var/lib/mongodb +net: + bindIp: 0.0.0.0 +replication: + oplogSizeMB: 128 + replSetName: rs01 +EOF +chown -v "$uid" "$CONFIG" + + +dirs=(/var/{log,lib}/mongodb ) +host_dirs=() +for dir in "${dirs[@]}"; do + host_dirs+=("$SERVICE_DATASTORE$dir") +done + +mkdir -p "${host_dirs[@]}" +find "${host_dirs[@]}" \! -user "$uid" \ + -exec chown -v "$uid" {} + || true + + +config_hash=$(cat "$CONFIG" | md5_compat) || exit 1 +init-config-add " +$MASTER_BASE_SERVICE_NAME: + labels: + - compose.config_hash=$config_hash +" diff --git a/mongo/hooks/mongo_database-relation-joined b/mongo/hooks/mongo_database-relation-joined new file mode 100755 index 0000000..99520b3 --- /dev/null +++ b/mongo/hooks/mongo_database-relation-joined @@ -0,0 +1,29 @@ +#!/bin/bash + +. lib/common + +set -e + +DBNAME=$(relation-get dbname) || { + DBNAME="${BASE_SERVICE_NAME//\./_}" + relation-set dbname "$DBNAME" +} + + +cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$TARGET_SERVICE_NAME:27017' } ]})" +debug "${WHITE}running:$NORMAL $cmd" + +out=$(ddb < <(echo "use $DBNAME"; + echo "$cmd")) + +if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]]; then + exit 0 +fi + +if [[ "$out" == *"\"ok\" : 1"* ]]; then + exit 0 +fi + +err Replicate Set initialisation failed +echo "$out" >&2 + diff --git a/mongo/lib/common b/mongo/lib/common new file mode 100644 index 0000000..ca426a0 --- /dev/null +++ b/mongo/lib/common @@ -0,0 +1,30 @@ +# -*- mode: shell-script -*- + +include pretty + +export DB_NAME="$SERVICE_NAME" ## general type of database (ie: postgres/mysql...) +export DB_DATADIR=/var/lib/mongodb + +export DATA_DIR=$SERVICE_DATASTORE$DB_DATADIR + + +is_db_locked() { + is_volume_used "$DATASTORE/${SERVICE_NAME}" +} + + +_set_server_db_params() { + server_docker_opts+=("-v" "${SERVICE_CONFIGSTORE}/etc/mongod.conf:/etc/mongod.conf" + "--add-host" "${SERVICE_NAME}:127.0.0.1") +} + +_set_db_params() { + local docker_ip="$1" docker_network="$2" + + db_docker_opts+=("--network" "$docker_network") + + db_cmd_opts+=("--host" "$docker_ip") + check_command="db.serverStatus().ok" +} + +ddb () { dcmd mongo "$@"; } diff --git a/mongo/metadata.yml b/mongo/metadata.yml new file mode 100644 index 0000000..83a12bf --- /dev/null +++ b/mongo/metadata.yml @@ -0,0 +1,8 @@ +summary: "MongoDB server" +maintainer: "Valentin Lab " +data-resources: + - /var/lib/mongodb +config-resources: + - /etc/mongod.conf +provides: + mongo-database: