From 4b9097564bf52dbba5cefc7648cd0188c11a1926 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 8 Oct 2019 10:30:03 +0200 Subject: [PATCH] new: [mongo] new charm --- mongo/build/Dockerfile | 17 ++++++++ mongo/hooks/init | 51 ++++++++++++++++++++++ mongo/hooks/mongo_database-relation-joined | 29 ++++++++++++ mongo/lib/common | 30 +++++++++++++ mongo/metadata.yml | 8 ++++ 5 files changed, 135 insertions(+) create mode 100644 mongo/build/Dockerfile create mode 100755 mongo/hooks/init create mode 100755 mongo/hooks/mongo_database-relation-joined create mode 100644 mongo/lib/common create mode 100644 mongo/metadata.yml diff --git a/mongo/build/Dockerfile b/mongo/build/Dockerfile new file mode 100644 index 00000000..e1081576 --- /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 00000000..8cc5bb27 --- /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 00000000..99520b35 --- /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 00000000..ca426a01 --- /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 00000000..83a12bfd --- /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: