2022-12-22 09:41:07 +00:00
#!/bin/bash
2022-12-28 06:40:44 +00:00
WORKING_DIR=/usr/share/fastapi-dls
2022-12-28 06:03:41 +00:00
CONFIG_DIR=/etc/fastapi-dls
echo "> Create config directory ..."
mkdir -p $CONFIG_DIR
2022-12-22 09:41:07 +00:00
echo "> Install service ..."
2022-12-28 06:03:41 +00:00
cat <<EOF >/etc/systemd/system/fastapi-dls.service
2022-12-22 09:41:07 +00:00
[Unit]
Description=Service for fastapi-dls
After=network.target
[Service]
User=www-data
Group=www-data
2022-12-28 05:47:06 +00:00
AmbientCapabilities=CAP_NET_BIND_SERVICE
2022-12-28 07:57:35 +00:00
WorkingDirectory=$WORKING_DIR/app
2022-12-28 06:03:41 +00:00
EnvironmentFile=$CONFIG_DIR/env
2022-12-28 05:47:06 +00:00
ExecStart=uvicorn main:app \\
--env-file /etc/fastapi-dls/env \\
--host \$DLS_URL --port \$DLS_PORT \\
2022-12-28 06:40:44 +00:00
--app-dir $WORKING_DIR/app \\
2022-12-28 05:47:06 +00:00
--ssl-keyfile /etc/fastapi-dls/webserver.key \\
2022-12-28 06:14:24 +00:00
--ssl-certfile /etc/fastapi-dls/webserver.crt \\
2022-12-22 09:41:07 +00:00
--proxy-headers
Restart=always
KillSignal=SIGQUIT
2022-12-28 05:47:06 +00:00
Type=simple
2022-12-22 09:41:07 +00:00
NotifyAccess=all
[Install]
WantedBy=multi-user.target
2022-12-27 21:18:02 +00:00
2022-12-22 09:41:07 +00:00
EOF
2022-12-28 07:21:04 +00:00
systemctl daemon-reload
2022-12-28 08:04:35 +00:00
if [[ ! -f $CONFIG_DIR/env ]]; then
echo "> Writing initial config ..."
touch $CONFIG_DIR/env
cat <<EOF >$CONFIG_DIR/env
2022-12-29 11:19:49 +00:00
# Toggle debug mode
#DEBUG=false
# Where the client can find the DLS server
2022-12-22 09:41:07 +00:00
DLS_URL=127.0.0.1
DLS_PORT=443
2022-12-29 11:19:49 +00:00
# CORS configuration
## comma separated list without spaces
#CORS_ORIGINS="https://$DLS_URL:$DLS_PORT"
# Lease expiration in days
2022-12-22 09:41:07 +00:00
LEASE_EXPIRE_DAYS=90
2022-12-29 11:19:49 +00:00
# Database location
## https://docs.sqlalchemy.org/en/14/core/engines.html
2022-12-28 06:40:44 +00:00
DATABASE=sqlite:///$CONFIG_DIR/db.sqlite
2022-12-29 11:19:49 +00:00
# UUIDs for identifying the instance
#SITE_KEY_XID="00000000-0000-0000-0000-000000000000"
#INSTANCE_REF="00000000-0000-0000-0000-000000000000"
# Site-wide signing keys
2022-12-28 06:31:23 +00:00
INSTANCE_KEY_RSA=$CONFIG_DIR/instance.private.pem
INSTANCE_KEY_PUB=$CONFIG_DIR/instance.public.pem
2022-12-27 21:18:02 +00:00
2022-12-22 09:41:07 +00:00
EOF
2022-12-28 08:04:35 +00:00
fi
2022-12-22 09:41:07 +00:00
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 09:38:26 +00: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.
2022-12-22 09:41:07 +00:00
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 ..."
2022-12-28 08:04:35 +00:00
source $CONFIG_DIR/env
2022-12-29 09:31:25 +00:00
curl --insecure -X GET https://$DLS_URL:$DLS_PORT/-/health
2022-12-22 09:41:07 +00:00
else
echo "> Testing API failed, curl not available. Please test manually!"
fi
fi
2022-12-28 07:35:59 +00:00
chown -R www-data:www-data $CONFIG_DIR
2022-12-28 06:40:44 +00:00
chown -R www-data:www-data $WORKING_DIR
2022-12-28 06:14:24 +00:00
2022-12-22 09:41:07 +00:00
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-27 21:18:02 +00:00
2022-12-22 09:41:07 +00:00
EOF