fastapi-dls/DEBIAN/postinst

92 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/bin/bash
echo "> Install service ..."
cat <<EOF > /etc/systemd/system/fastapi-dls.service
[Unit]
Description=Service for fastapi-dls
After=network.target
[Service]
User=www-data
Group=www-data
2022-12-28 08:47:06 +03:00
AmbientCapabilities=CAP_NET_BIND_SERVICE
WorkingDirectory=/usr/share/fastapi-dls
2022-12-28 08:56:31 +03:00
EnvironmentFile=/etc/fastapi-dls/env
2022-12-28 08:47:06 +03:00
ExecStart=uvicorn main:app \\
--env-file /etc/fastapi-dls/env \\
--host \$DLS_URL --port \$DLS_PORT \\
--app-dir /usr/share/fastapi-dls/app \\
--ssl-keyfile /etc/fastapi-dls/webserver.key \\
--ssl-certfile /opt/fastapi-dls/webserver.crt \\
--proxy-headers
Restart=always
KillSignal=SIGQUIT
2022-12-28 08:47:06 +03:00
Type=simple
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
2022-12-28 00:18:02 +03:00
EOF
CONFIG_DIR=/etc/fastapi-dls
echo "> Create config directory ..."
mkdir -p $CONFIG_DIR
echo "> Writing default config parameters ..."
2022-12-28 08:56:31 +03:00
touch $CONFIG_DIR/fastapi-dls/env
cat <<EOF > $CONFIG_DIR/fastapi-dls/env
DLS_URL=127.0.0.1
DLS_PORT=443
LEASE_EXPIRE_DAYS=90
DATABASE=sqlite:////usr/share/fastapi-dls/db.sqlite
2022-12-28 00:18:02 +03:00
EOF
echo "> Create dls-instance keypair ..."
openssl genrsa -out $CONFIG_DIR/instance.private.pem 2048
openssl rsa -in $CONFIG_DIR/instance.private.pem -outform PEM -pubout -out $CONFIG_DIR/instance.public.pem
while true; do
2022-12-27 12:38:26 +03:00
read -p "> Do you wish to create self-signed webserver certificate? [Y/n]" yn
yn=${yn:-y} # ${parameter:-word} If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
case $yn in
[Yy]*)
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout $CONFIG_DIR/webserver.key -out $CONFIG_DIR/webserver.crt
break
;;
[Nn]*) break ;;
*) echo "Please answer [y] or [n]." ;;
esac
done
if [[ -f $CONFIG_DIR/webserver.key ]]; then
echo "> Starting service ..."
systemctl start fastapi-dls.service
if [ -x "$(command -v curl)" ]; then
echo "> Testing API ..."
curl --insecure -X GET https://127.0.0.1/status
else
echo "> Testing API failed, curl not available. Please test manually!"
fi
fi
cat <<EOF
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# fastapi-dls is now installed. #
# #
# Service should be up and running. #
# Webservice is listen to https://localhost #
# #
# Configuration is stored in ${CONFIG_DIR}/env #
# #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
2022-12-28 00:18:02 +03:00
EOF