ansible/roles/nfs-server/files/mknfsroledir.sh

54 lines
1.7 KiB
Bash
Executable file

#!/bin/bash
set -e
_basedn="$(awk '{ if ($1 == "BASE") print $2 }' /etc/openldap/ldap.conf)"
if [ -z "$_basedn" ]; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Failed to get LDAP basedn" 1>&2
exit 1
fi
if [ $# -eq 1 ]; then
_filter="(&(automountKey=$1)(objectClass=automount))"
elif [ $# -eq 0 ]; then
_filter="(objectClass=automount)"
else
echo "Usage: $(basename "$0") [role]" 1>&2
exit 1
fi
ldapsearch -Q -LLL -b "ou=Groups,${_basedn}" "$_filter" automountInformation | \
awk -v_hostname="$(hostname -f)" '{
if ($1 == "automountInformation:") {
split($2, _, ":");
if (_[1] == _hostname) {
print _[2];
}
}
}' | while read -r _target ; do
_role="$(basename "$_target")"
_basedir="$(dirname "$_target")"
[ -d "$_target" ] && continue
if ! getent group "$_role" > /dev/null 2>&1 ; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Cannot find group '${_role}'" 1>&2
continue
fi
if [ ! -d "$_basedir" ]; then
logger -i -t "$(basename "$0")" -p user.error -s \
"ERROR: Cannot find base direcory '${_basedir}'"
continue
fi
logger -i -t "$(basename "$0")" -p user.info \
"Creating role directory '${_target}' for role '${_role}'"
install -d -o root -g "$_role" -m 2751 "$_target"
install -d -o root -g "$_role" -m 2770 "${_target}/development"
install -d -o root -g "$_role" -m 2770 "${_target}/external"
install -d -o root -g "$_role" -m 2770 "${_target}/library"
install -d -o root -g "$_role" -m 2775 "${_target}/public"
done