diff --git a/snap/hooks/install b/snap/hooks/install index c0f43d2..e65f13e 100755 --- a/snap/hooks/install +++ b/snap/hooks/install @@ -1,15 +1,35 @@ -#!/bin/bash +#!/usr/bin/env bash +# Installation hook for charmed-mongodb snap set -eux +export CONF="${SNAP_DATA}"/etc/mongod +export PBM_CONF="${SNAP_DATA}"/etc/pbm +export DATA="${SNAP_COMMON}"/var/lib/mongodb +export LOGS="${SNAP_COMMON}"/var/log/mongodb +export MONGO_CONFIG_FILE="${CONF}"/mongod.conf + # Create the necessary parent directories -mkdir -p "${SNAP_DATA}/etc/pbm/" -mkdir -p "${SNAP_DATA}/etc/mongod/" -mkdir -p "${SNAP_COMMON}/var/lib/mongodb/" -mkdir -p "${SNAP_COMMON}/var/log/mongodb/" +mkdir -p $DATA +mkdir -p $LOGS +mkdir -p $PBM_CONF +mkdir -p $CONF + +# If we just created the directory, we set up permissions +if [ stat -c '%u' "$DATA" == 0 ]; then + chmod -R 770 "${SNAP_COMMON}" + chmod 750 "${DATA}" +fi + +# If we just created the directory, we set up permissions +if [ stat -c '%u' "$LOGS" == 0 ]; then + chmod -R 770 "${SNAP_COMMON}" + chmod g+s "$LOGS"/* +fi + +chown -R 584788:root "${SNAP_COMMON}"/* # Copy over the mongod.conf and create needed directories/files -MONGO_CONFIG_FILE="${SNAP_DATA}/etc/mongod/mongod.conf" echo "configuration file does not exist." echo "copying default config to ${MONGO_CONFIG_FILE}" cp -r ${SNAP}/etc/mongod.conf ${MONGO_CONFIG_FILE} @@ -17,12 +37,9 @@ cp -r ${SNAP}/etc/mongod.conf ${MONGO_CONFIG_FILE} # mongod.conf default values are not consistent with the snap directory system. sed -i "s/fork: true/fork: false/g" $MONGO_CONFIG_FILE -sed -i "s:/var/log/mongodb:$SNAP_COMMON/var/log/mongodb:g" $MONGO_CONFIG_FILE -sed -i "s:/var/lib/mongodb:$SNAP_COMMON/var/lib/mongodb:g" $MONGO_CONFIG_FILE +sed -i "s:/var/log/mongodb:$LOGS:g" $MONGO_CONFIG_FILE +sed -i "s:/var/lib/mongodb:$DATA:g" $MONGO_CONFIG_FILE sed -i "s:/var/run:/tmp:g" $MONGO_CONFIG_FILE # Change ownership of snap directories to allow snap_daemon to read/write -chmod g+s "${SNAP_COMMON}/var/log/"* chown -R 584788:root "${SNAP_DATA}"/* -chown -R 584788:root "${SNAP_COMMON}"/* -chgrp root "${SNAP_COMMON}/var/log/"* \ No newline at end of file diff --git a/snap/local/drop_priv.sh b/snap/local/drop_priv.sh index 37294bc..a258b23 100755 --- a/snap/local/drop_priv.sh +++ b/snap/local/drop_priv.sh @@ -4,13 +4,14 @@ export PBM_MONGODB_URI="$(snapctl get pbm-uri)" if [[ $(id -u) == "0" ]]; then - -exec "${SNAP}"/usr/bin/setpriv \ + exec bash -c "cd ${SNAP} && \ + ${SNAP}/usr/bin/setpriv \ --clear-groups \ --reuid snap_daemon \ - --regid snap_daemon -- \ - "$SNAP/usr/bin/$@" + --regid snap_daemon \ + -- \ + ${SNAP}/usr/bin/$*" else - -exec "$SNAP/usr/bin/$@" + exec bash -c "cd ${SNAP} && \ + ${SNAP}/usr/bin/$*" fi