Browse Source

new: [mongo] new charm

framadate
Valentin Lab 5 years ago
parent
commit
4b9097564b
  1. 17
      mongo/build/Dockerfile
  2. 51
      mongo/hooks/init
  3. 29
      mongo/hooks/mongo_database-relation-joined
  4. 30
      mongo/lib/common
  5. 8
      mongo/metadata.yml

17
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" ]

51
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 <<EOF > "$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
"

29
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

30
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 "$@"; }

8
mongo/metadata.yml

@ -0,0 +1,8 @@
summary: "MongoDB server"
maintainer: "Valentin Lab <valentin.lab@kalysto.org>"
data-resources:
- /var/lib/mongodb
config-resources:
- /etc/mongod.conf
provides:
mongo-database:
Loading…
Cancel
Save