2018-04-26 23:20:26 +04:00
|
|
|
#!/bin/bash
|
|
|
|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
2023-01-25 17:14:03 +04:00
|
|
|
source "$(dirname -- "$0")/lib/ulib/auto" || exit 1
|
2018-04-26 23:20:26 +04:00
|
|
|
urequire debian service conf
|
|
|
|
|
|
|
|
function display_help() {
|
|
|
|
uecho "$scriptname: installer une webpyapp et le service associé
|
|
|
|
|
|
|
|
USAGE
|
|
|
|
$scriptname [options] [APPDIR]
|
|
|
|
|
|
|
|
OPTIONS
|
|
|
|
-c, --config CONFIG
|
|
|
|
Spécifier un fichier de configuration à utiliser pour installer le
|
|
|
|
service. Par défaut, prendre APPDIR/config/server.conf
|
|
|
|
-n, --name NAME
|
|
|
|
Spécifier le nom du service
|
|
|
|
--port PORT
|
|
|
|
Spécifier le port sur lequel doit écouter le serveur
|
|
|
|
-p, --profile PROFILE
|
|
|
|
Spécifier le profil avec lequel l'application doit tourner
|
|
|
|
-P, --prod-profile
|
|
|
|
-T, --test-profile
|
|
|
|
Raccourcis pour -pprod et -ptest respectivement
|
|
|
|
-u, --owner USER[:GROUP]
|
|
|
|
Spécifier l'utilisateur qui doit faire tourner le serveur. La valeur par
|
|
|
|
défaut est $DEFAULT_OWNER
|
|
|
|
-d, --destdir DESTDIR
|
|
|
|
Spécifier le répertoire d'installation. Par défaut, l'installation se
|
|
|
|
fait dans le répertoire $DEFAULT_DESTDIR
|
|
|
|
-s, --start
|
|
|
|
Activer et démarrer le service après installation
|
|
|
|
-k, --no-restart
|
|
|
|
Ne pas redémarrer le service s'il était déjà en train de tourner"
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_DESTDIR=/opt
|
|
|
|
DEFAULT_OWNER=root
|
|
|
|
DEFAULT_PYTHON=python2.7
|
|
|
|
|
|
|
|
serverconf=
|
|
|
|
name=
|
|
|
|
port=
|
|
|
|
profile=
|
|
|
|
owner=
|
|
|
|
mode=u=rwX,go=rX
|
|
|
|
destdir=
|
|
|
|
overwrite_config=
|
|
|
|
enable=
|
|
|
|
start=
|
|
|
|
norestart=
|
|
|
|
args=(
|
|
|
|
--help '$exit_with display_help'
|
|
|
|
-c:,--config: serverconf=
|
|
|
|
-n:,--name: name=
|
|
|
|
--port: port=
|
|
|
|
-p:,--profile: profile=
|
|
|
|
-P,--prod-profile profile=prod
|
|
|
|
-T,--test-profile profile=test
|
|
|
|
-u:,--owner: owner=
|
|
|
|
-d:,--destdir: destdir=
|
|
|
|
--overwrite-config overwrite_config=1
|
|
|
|
-s,--start '$enable=1; start=1'
|
|
|
|
-k,--no-restart norestart=1
|
|
|
|
)
|
|
|
|
parse_args "$@"; set -- "${args[@]}"
|
|
|
|
|
|
|
|
appdir="${1:-.}"; shift
|
|
|
|
[ -d "$appdir" ] || die "$appdir: répertoire inexistant"
|
|
|
|
[ -d "$appdir/config" -a -d "$appdir/python" ] || die "$appdir: ne semble pas être une webpyapp"
|
|
|
|
setx appdir=abspath "$appdir"
|
|
|
|
|
|
|
|
[ -n "$serverconf" ] || serverconf="$appdir/config/server.conf"
|
|
|
|
[ -f "$serverconf" ] || die "$(ppath "$serverconf"): impossible de trouver le fichier de configuration"
|
|
|
|
NAME=
|
|
|
|
PORT=
|
|
|
|
PROFILE=
|
|
|
|
OWNER=
|
|
|
|
VIRTUAL_ENV=
|
|
|
|
PYTHON=
|
|
|
|
source "$serverconf"
|
|
|
|
|
|
|
|
[ -n "$name" ] || name="$NAME"
|
|
|
|
[ -n "$name" ] || setx name=basename "$appdir"
|
|
|
|
[ -n "$port" ] || port="$PORT"
|
|
|
|
[ -n "$profile" ] || profile="$PROFILE"
|
|
|
|
[ -n "$owner" ] || owner="$OWNER"
|
|
|
|
[ -n "$destdir" ] || destdir="$DEFAULT_DESTDIR"
|
|
|
|
run_as_root -c "$serverconf" -n "$name" ${port:+--port "$port"} \
|
|
|
|
${profile:+-p "$profile"} \
|
|
|
|
${owner:+-u "$owner"} \
|
|
|
|
-d "$destdir" ${overwrite_config:+--overwrite-config} \
|
|
|
|
${start:+-s} ${norestart:+-k} \
|
|
|
|
"$appdir" "$@"
|
|
|
|
|
|
|
|
: "${PYTHON:=$DEFAULT_PYTHON}"
|
|
|
|
if [ -n "$VIRTUAL_ENV" ]; then
|
|
|
|
etitle "Environnement virtuel"
|
|
|
|
packages=(python-virtualenv)
|
|
|
|
if ! pkg_check "${packages[@]}"; then
|
|
|
|
estep "Installation ${packages[*]}"
|
|
|
|
pkg_install "${packages[@]}" || die
|
|
|
|
fi
|
|
|
|
if [ ! -d "$VIRTUAL_ENV" ]; then
|
|
|
|
estep "Installation dans $VIRTUAL_ENV"
|
|
|
|
virtualenv -p "$PYTHON" "$VIRTUAL_ENV" || die
|
|
|
|
fi
|
|
|
|
eend
|
|
|
|
fi
|
|
|
|
[ -n "$VIRTUAL_ENV" ] && PYTHON="$VIRTUAL_ENV/bin/python"
|
|
|
|
|
|
|
|
restart=
|
|
|
|
if [ -z "$norestart" ]; then
|
|
|
|
if service "$name" check; then
|
|
|
|
estep "Arrêt du service"
|
|
|
|
service "$name" stop
|
|
|
|
start=1
|
|
|
|
restart=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
etitle "Copie des fichiers"
|
|
|
|
destdir="$destdir/$name"
|
|
|
|
mkdir -p "$destdir" || die
|
|
|
|
rsync -a --delete-after \
|
|
|
|
--exclude /config/ --exclude /nulib --exclude /.devel \
|
|
|
|
-f "P /var/**" \
|
|
|
|
"$appdir/" "$destdir"
|
|
|
|
eend
|
|
|
|
|
|
|
|
etitle "Vérification de la configuration"
|
|
|
|
estep "Dans $destdir/config:"
|
|
|
|
array_from_lines configs "$(list_files "$appdir/config")"
|
|
|
|
for config in "${configs[@]}"; do
|
|
|
|
if [ -n "$overwrite_config" -o ! -f "$destdir/config/$config" ]; then
|
|
|
|
estepi "$config: copie initiale"
|
|
|
|
mkdir -p "$destdir/config"
|
|
|
|
if [ "$config" == server.conf ]; then
|
|
|
|
# pour ce cas particulier, prendre le fichier $serverconf
|
|
|
|
cp "$serverconf" "$destdir/config/$config"
|
|
|
|
estep "Mise à jour de la configuration"
|
|
|
|
conf_enable "$destdir/config/$config" \
|
|
|
|
NAME="$name" ${port:+PORT="$port"} \
|
|
|
|
${profile:+PROFILE="$profile"} \
|
|
|
|
${owner:+OWNER="$owner"}
|
|
|
|
else
|
|
|
|
cp "$appdir/config/$config" "$destdir/config/$config"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
estepw "$config: refus d'écraser la configuration existante"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
eend
|
|
|
|
|
|
|
|
[ -n "$owner" ] || owner="$DEFAULT_OWNER"
|
|
|
|
splitpair "$owner" user group
|
|
|
|
sdowner="User=$user"
|
|
|
|
[ -n "$group" ] && sdowner="$sdowner\\
|
|
|
|
Group=$group"
|
|
|
|
|
|
|
|
etitle "Compilation des classes"
|
|
|
|
"$PYTHON" -m compileall "$destdir/config"
|
|
|
|
"$PYTHON" -m compileall "$destdir/python"
|
|
|
|
eend
|
|
|
|
|
|
|
|
etitle "Correction des permissions"
|
|
|
|
chown -R "$user:$group" "$destdir"
|
|
|
|
chmod -R "$mode" "$destdir"
|
|
|
|
eend
|
|
|
|
|
|
|
|
etitle "Configuration du service"
|
|
|
|
estep "Vérification du fichier unit"
|
|
|
|
if [ -d /etc/systemd/system ]; then
|
|
|
|
service="/etc/systemd/system/$name.service"
|
|
|
|
ac_set_tmpfile tmpfile
|
|
|
|
sed "\
|
|
|
|
s|@@destdir@@|$destdir|g
|
|
|
|
/^User=@@owner@@\$/c\\
|
|
|
|
$sdowner
|
|
|
|
s|@@name@@|$name|g" "$destdir/support/server.service" >"$tmpfile"
|
|
|
|
if testupdated "$tmpfile" "$service"; then
|
|
|
|
estep "$service"
|
|
|
|
cat "$tmpfile" >"$service"
|
|
|
|
systemctl daemon-reload
|
|
|
|
fi
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
fi
|
|
|
|
if [ -n "$enable" ]; then
|
|
|
|
estep "Activation du service"
|
|
|
|
service_enable "$name"
|
|
|
|
fi
|
|
|
|
if [ -n "$start" ]; then
|
|
|
|
if [ -n "$restart" ]; then estep "Redémarrage du service"
|
|
|
|
else estep "Démarrage du service"
|
|
|
|
fi
|
|
|
|
service "$name" start
|
|
|
|
fi
|
|
|
|
eend
|