ScoDocMM/tools/utils.sh

113 lines
3.2 KiB
Bash
Raw Permalink Normal View History

2020-12-19 19:22:22 +01:00
#!/bin/bash
2020-09-26 16:19:37 +02:00
# Misc utilities for ScoDoc install shell scripts
to_lower() {
2020-12-19 19:22:22 +01:00
echo "$1" | tr "[:upper:]" "[:lower:]"
2020-09-26 16:19:37 +02:00
}
to_upper() {
2020-12-19 19:22:22 +01:00
echo "$1" | tr "[:lower:]" "[:upper:]"
2020-09-26 16:19:37 +02:00
}
norm_ans() {
2020-12-19 19:22:22 +01:00
x=$(to_upper "$1" | tr O Y)
echo "${x:0:1}"
2020-09-26 16:19:37 +02:00
}
check_uid_root() {
if [ "$UID" != "0" ]
then
echo "Erreur: le script $1 doit etre lance par root"
exit 1
fi
}
terminate() {
2020-12-21 18:15:01 +01:00
status=${2:-1} # default: exit 1
2020-09-26 16:19:37 +02:00
echo
echo "Erreur: $1"
echo
2020-12-21 18:15:01 +01:00
exit $status
2020-09-26 16:19:37 +02:00
}
2020-12-05 17:29:26 +01:00
# Start/stop scodoc, using sysv or systemd
scodocctl() {
if [ "$1" = "start" ]; then
echo "Starting ScoDoc..."
elif [ "$1" = "stop" ]; then
echo "Stopping ScoDoc"
else
echo "Error: invalid argument for scodocctl"
exit 1
fi
if [ -e /etc/systemd/system/scodoc.service ]
then
systemctl $1 scodoc
else
echo "(using legacy SystemV)"
2020-12-19 19:22:22 +01:00
/etc/init.d/scodoc "$1"
2020-12-05 17:29:26 +01:00
fi
}
# --- Ensure postgres user scodoc exists
2020-09-26 16:19:37 +02:00
init_postgres_user() { # run as root
2021-08-17 14:15:15 +02:00
if [ -z $(echo "select usename from pg_user;" | su -c "(cd; $PSQL -d template1 -p $POSTGRES_PORT)" "$POSTGRES_SUPERUSER" | grep "$POSTGRES_USER") ]
2020-09-26 16:19:37 +02:00
then
# add database user
echo "Creating postgresql user $POSTGRES_USER"
2021-08-17 14:15:15 +02:00
su -c "(cd; createuser -p $POSTGRES_PORT --createdb --no-superuser --no-createrole ${POSTGRES_USER})" "$POSTGRES_SUPERUSER"
2020-09-26 16:19:37 +02:00
fi
}
2021-07-23 09:32:20 +02:00
# --- Ensure Unix user "scodoc" exists
check_create_scodoc_user() {
if ! id -u "${SCODOC_USER}" &> /dev/null
2021-07-23 09:32:20 +02:00
then
echo "Creating unix user ${SCODOC_USER}"
adduser --shell /bin/bash --disabled-password --gecos "ScoDoc service" "${SCODOC_USER}" || die "failed to create user"
2021-07-23 09:32:20 +02:00
else
echo "Unix user ${SCODOC_USER} exists"
fi
2021-08-27 10:41:22 +02:00
# Check / set FLASK_APP
scodoc_home=$(getent passwd "${SCODOC_USER}" | cut -d: -f 6)
if [ -e "$scodoc_home/.profile" ] && [ $(grep -c FLASK_APP "$scodoc_home/.profile") == 0 ]
then
echo "export FLASK_APP=scodoc.py" >> "$scodoc_home/.profile"
fi
2021-07-23 09:32:20 +02:00
}
2021-08-19 20:23:48 +02:00
# --- Give all ScoDoc files (/opt/scodoc) to user "scodoc":
2021-07-23 09:32:20 +02:00
change_scodoc_file_ownership() {
echo "Changing owner of ${SCODOC_DIR} to ${SCODOC_USER}"
chown -R "${SCODOC_USER}:${SCODOC_GROUP}" "${SCODOC_DIR}" || die "change_scodoc_file_ownership failed on ${SCODOC_DIR}"
2021-07-23 09:32:20 +02:00
}
2020-09-26 16:19:37 +02:00
# Création du répertoire local (scodoc-data) et vérification du propriétaire
set_scodoc_var_dir() {
echo "Checking $SCODOC_VAR_DIR..."
2021-08-27 10:41:22 +02:00
[ -z ${SCODOC_VAR_DIR+x} ] && die "Error: env var SCODOC_VAR_DIR not set"
2021-07-27 13:27:21 +02:00
[ -d "$SCODOC_VAR_DIR" ] || mkdir "$SCODOC_VAR_DIR" || die "can't create $SCODOC_VAR_DIR directory"
2021-08-24 16:18:00 +02:00
for d in archives photos tmp log config certs config/version config/depts config/logos
do
[ -d "$SCODOC_VAR_DIR/$d" ] || mkdir "$SCODOC_VAR_DIR/$d" || die "can't create $SCODOC_VAR_DIR/$d subdirectory"
done
chown -R "${SCODOC_USER}:${SCODOC_GROUP}" "${SCODOC_VAR_DIR}" || die "change_scodoc_file_ownership failed on ${SCODOC_VAR_DIR}"
}
2020-09-26 16:19:37 +02:00
# XXX inutilise
gen_passwd() {
PASSWORD_LENGTH="8"
2020-12-05 17:29:26 +01:00
ALLOWABLE_ASCII="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%^&*()_+=-?><"
2020-09-26 16:19:37 +02:00
SEED=$(head -c4 /dev/urandom | od -t u4 | awk '{ print $2 }')
RANDOM=$SEED
n=1
password=""
while [ "$n" -le "$PASSWORD_LENGTH" ]
do
2020-12-19 19:22:22 +01:00
password="$password${ALLOWABLE_ASCII:$((RANDOM%${#ALLOWABLE_ASCII})):1}"
n=$((n+1))
2020-09-26 16:19:37 +02:00
done
2020-12-19 19:22:22 +01:00
echo "$password"
2020-09-26 16:19:37 +02:00
}