2
0
docker-skeleton/setpermissions.sh

80 lines
2.8 KiB
Bash

#!/bin/bash
#
# Helper script to clean up, restore symlinks and set permissions
# in a just downloaded and uncompressed (zipball) instance.
#
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
# 2023-09-11 v1.0
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
# 2022-11-04 v0.2
# fix: Content regexp now matches well to the special chars allowed.
# 2022-09-22 v0.1 Initial release.
# Where I'm?
# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
[[ -z "$(which dirname)" ]] && exit 1
[[ -z "$(which readlink)" ]] && exit 1
#
SOURCE="$0"
while [ -h "$SOURCE" ]; do
# resolve $SOURCE until the file is no longer a symlink
SCRPATH="$( cd -P "$(dirname "$SOURCE" )" && echo "$PWD" )" #"
SOURCE="$(readlink "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE"
done; SCRPATH="$( cd -P "$(dirname "$SOURCE" )" && echo "$PWD" )" #"
# We've the absolute path of the script.
# Removing some unnecessary files.
#
if [ -n "$(which find)" ]; then
find "$SCRPATH" -name .gitignore -delete
fi
if [ -d "$SCRPATH/.git" ]; then
rm -Rf "$SCRPATH/.git" 2>/dev/null
fi
# Restoring the symlinks.
# All files considered as a symlink which is containing a single line
# with a valid pathname inside.
#
if [ -n "$(which awk)" -a -n "$(which cut)" -a -n "$(which find)" -a -n "$(which ln)" ]; then
# Files containing maximum 1 line.
IFS=$'\n' read -r -d '' -a SUSPECTS < <( (find "$SCRPATH" -type f -exec awk 'END { if (NR < 2) print FILENAME }' {} \;) && printf '\0' )
# Enumerates these files.
for suspect in "${SUSPECTS[@]}"
do
if [ -n "$suspect" -a -s "$suspect" ]; then
# Checks the content: it seems to be a valid pathname?
# For this tricky read see https://stackoverflow.com/questions/46163678/
IFS= read -r -n 1024 -d '' content <"$suspect" || [[ $content ]]
if [[ "$content" =~ ^([[:alnum:]]|[-_/. ])+$ ]]; then
# Replaces the suspect with a symlink pointing to it's content.
rm "$suspect" 2>/dev/null ; ln -s "$content" "$suspect" 2>/dev/null
fi
fi
done
fi
# Restoring the permissions.
#
# First a base setup - sets permissions to 770/660 [umask 007]
chmod -R g+rw,o-rw "$SCRPATH"
find "$SCRPATH" -type d -exec chmod 2771 {} \;
[[ -n "$(which setfacl)" ]] && setfacl -R -d -m u:$USER:rwX,g::rwX "$SCRPATH" 2>/dev/null
#
# Then we'll use the metastore DB to set the permissions individually.
#
if [ -n "$(which metastore)" -a -x "$(which metastore)" ]; then
( cd "$SCRPATH"
if [ -e ".metadata" ]; then
"$(which metastore)" -amqq 2>/dev/null
fi
)
fi
# That's all, Folks! :)