From f0fdfafaede34ac2c6cd7bb9068c341f62e3e7ae Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 22 Dec 2022 10:41:07 +0100 Subject: [PATCH 01/47] added basic debian package setup and pipeline --- .gitlab-ci.yml | 44 +++++++++++++++++++++++-- DEBIAN/conffiles | 8 +++++ DEBIAN/control | 9 +++++ DEBIAN/postinst | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ DEBIAN/postrm | 8 +++++ DEBIAN/prerm | 5 +++ 6 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 DEBIAN/conffiles create mode 100644 DEBIAN/control create mode 100644 DEBIAN/postinst create mode 100755 DEBIAN/postrm create mode 100755 DEBIAN/prerm diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c24c913..9d529ae 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,28 @@ cache: key: one-key-to-rule-them-all -build: +build:debian: + # debian:bullseye-slim + image: debian:bookworm-slim # just to get "python3-jose" working + stage: build + before_script: + - apt-get update -qq && apt-get install -qq -y build-essential + - chmod 0755 -R . + # create build directory for .deb sources + - mkdir build + # copy install instructions + - cp -r DEBIAN build/ + # copy app + - mkdir -p build/usr/share/ + - cp -r app build/usr/share/fastapi-dls + script: + - dpkg -b . build.deb + artifacts: + expire_in: 1 week + paths: + - build.deb + +build:docker: image: docker:dind interruptible: true stage: build @@ -15,10 +36,27 @@ build: - docker build . --tag ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${CI_BUILD_REF} - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${CI_BUILD_REF} -test: +test:debian: + image: debian:bookworm-slim stage: test + needs: + - job: build:debian + artifacts: true + before_script: + - apt-get update -qq && apt-get install -qq -y jq # systemd script: - - echo "Nothing to do ..." + # test installation + - apt-get install -q -y ./build.deb --fix-missing + # copy example config from GitLab-CI-Variables + #- cat ${EXAMPLE_CONFIG} > /etc/fastapi-dls/env + #- systemctl daemon-reload + #- systemctl enable fastapi-dls.service + #- systemctl start fastapi-dls.service + #- if [ "`curl --insecure -s https://localhost:8000/status | jq .status`" != "up" ]; then exit 2; fi + #- systemctl stop fastapi-dls.service + #- systemctl disable fastapi-dls.service + - apt-get purge -qq -y fastapi-dls + - apt-get autoremove -qq -y && apt-get clean -qq deploy: stage: deploy diff --git a/DEBIAN/conffiles b/DEBIAN/conffiles new file mode 100644 index 0000000..02e3534 --- /dev/null +++ b/DEBIAN/conffiles @@ -0,0 +1,8 @@ +/etc/systemd/system/fastapi-dls.service +/etc/fastapi-dls/env +/etc/fastapi-dls/instance.private.pem +/etc/fastapi-dls/instance.public.pem +/etc/fastapi-dls/webserver.key +/etc/fastapi-dls/webserver.crt + +# todo diff --git a/DEBIAN/control b/DEBIAN/control new file mode 100644 index 0000000..b7495d8 --- /dev/null +++ b/DEBIAN/control @@ -0,0 +1,9 @@ +Package: fastapi-dls +Version: 0.5 +Architecture: all +Maintainer: Oscar Krause oscar.krause@collinwebdesigns.de +Depends: python3, python3-fastapi, python3-uvicorn, python3-dotenv, python3-dateutil, python3-jose, uvicorn, openssl +Recommends: curl +Installed-Size: 10240 +Homepage: https://git.collinwebdesigns.de/oscar.krause/fastapi-dls +Description: Minimal Delegated License Service (DLS). diff --git a/DEBIAN/postinst b/DEBIAN/postinst new file mode 100644 index 0000000..dc5ee05 --- /dev/null +++ b/DEBIAN/postinst @@ -0,0 +1,85 @@ +#!/bin/bash + +echo "> Install service ..." +echo </etc/systemd/system/fastapi-dls.service +[Unit] +Description=Service for fastapi-dls +After=network.target + +[Service] +User=www-data +Group=www-data +WorkingDirectory=/usr/share/fastapi-dls +ExecStart=uvicorn \ + --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 +EnvironmentFile=/etc/fastapi-dls.env +Restart=always +KillSignal=SIGQUIT +Type=notify +StandardError=syslog +NotifyAccess=all + +[Install] +WantedBy=multi-user.target +EOF + +CONFIG_DIR=/etc/fastapi-dls + +echo "> Create config directory ..." +mkdir -p $CONFIG_DIR + +echo "> Writing default config parameters ..." +touch $CONFIG_DIR/fastapi-dls.env +echo <$CONFIG_DIR +DLS_URL=127.0.0.1 +DLS_PORT=443 +LEASE_EXPIRE_DAYS=90 +DATABASE=sqlite:////usr/share/fastapi-dls/db.sqlite +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 + read -p "> Do you wish to create self-signed webserver certificate? [y/n]" yn + 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 < Removing config directory." + rm -r /etc/fastapi-dls +fi + +# todo diff --git a/DEBIAN/prerm b/DEBIAN/prerm new file mode 100755 index 0000000..296c995 --- /dev/null +++ b/DEBIAN/prerm @@ -0,0 +1,5 @@ +#!/bin/bash + +echo -e "> Starting uninstallation of 'fastapi-dls'!" + +# todo From 81608fe497ab5c25efd81f0830ff2d93b536f326 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 13:48:48 +0100 Subject: [PATCH 02/47] merged dev into debian --- .gitlab-ci.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9d529ae..330345a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,6 +36,19 @@ build:docker: - docker build . --tag ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${CI_BUILD_REF} - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${CI_BUILD_REF} +test: + image: python:3.10-slim-bullseye + stage: test + before_script: + - pip install -r requirements.txt + - pip install pytest httpx + - mkdir -p app/cert + - openssl genrsa -out app/cert/instance.private.pem 2048 + - openssl rsa -in app/cert/instance.private.pem -outform PEM -pubout -out app/cert/instance.public.pem + - cd test + script: + - pytest main.py + test:debian: image: debian:bookworm-slim stage: test From 843d918e59462f397b0313a26ffed9f424ddbbe3 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 14:08:56 +0100 Subject: [PATCH 03/47] added dependencies --- DEBIAN/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEBIAN/control b/DEBIAN/control index b7495d8..aea841a 100644 --- a/DEBIAN/control +++ b/DEBIAN/control @@ -2,7 +2,7 @@ Package: fastapi-dls Version: 0.5 Architecture: all Maintainer: Oscar Krause oscar.krause@collinwebdesigns.de -Depends: python3, python3-fastapi, python3-uvicorn, python3-dotenv, python3-dateutil, python3-jose, uvicorn, openssl +Depends: python3, python3-fastapi, python3-uvicorn, python3-dotenv, python3-dateutil, python3-jose, python3-sqlalchemy, python3-pycryptodome, uvicorn, openssl Recommends: curl Installed-Size: 10240 Homepage: https://git.collinwebdesigns.de/oscar.krause/fastapi-dls From 4e17e6da82acfec816493de5cce01469da6759f9 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 14:09:13 +0100 Subject: [PATCH 04/47] main.py fixed pycryptodome import --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 4ae3c6b..c5970d3 100644 --- a/app/main.py +++ b/app/main.py @@ -19,8 +19,8 @@ from starlette.middleware.cors import CORSMiddleware from starlette.responses import StreamingResponse, JSONResponse, HTMLResponse from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from Crypto.PublicKey import RSA -from Crypto.PublicKey.RSA import RsaKey +from Cryptodome.PublicKey import RSA # Crypto | Cryptodome on Debian +from Cryptodome.PublicKey.RSA import RsaKey # Crypto | Cryptodome on Debian from orm import Origin, Lease, init as db_init From df0816832eb93e6bc6781f0014917ff8579e884b Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:04:26 +0100 Subject: [PATCH 05/47] fixed conffiles --- .gitlab-ci.yml | 2 ++ DEBIAN/conffiles | 7 ------- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 650262a..a9310fe 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,6 +15,8 @@ build:debian: # copy app - mkdir -p build/usr/share/ - cp -r app build/usr/share/fastapi-dls + # create conf file + - touch build/etc/fastapi-dls/env script: - dpkg -b . build.deb artifacts: diff --git a/DEBIAN/conffiles b/DEBIAN/conffiles index 02e3534..008d731 100644 --- a/DEBIAN/conffiles +++ b/DEBIAN/conffiles @@ -1,8 +1 @@ -/etc/systemd/system/fastapi-dls.service /etc/fastapi-dls/env -/etc/fastapi-dls/instance.private.pem -/etc/fastapi-dls/instance.public.pem -/etc/fastapi-dls/webserver.key -/etc/fastapi-dls/webserver.crt - -# todo From f1eddaa99a45d50060df8e2688f0bfac575a1d39 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:05:52 +0100 Subject: [PATCH 06/47] fixed missing directory --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a9310fe..daaa4fa 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,6 +16,7 @@ build:debian: - mkdir -p build/usr/share/ - cp -r app build/usr/share/fastapi-dls # create conf file + - mkdir -p build/etc/fastapi-dls - touch build/etc/fastapi-dls/env script: - dpkg -b . build.deb From 98e98ccd8446a1f4bc8b183c988a9ea0a208db7f Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:10:00 +0100 Subject: [PATCH 07/47] chroot into "build" dir --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index daaa4fa..b1f9175 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,7 +19,7 @@ build:debian: - mkdir -p build/etc/fastapi-dls - touch build/etc/fastapi-dls/env script: - - dpkg -b . build.deb + - chroot build/ dpkg -b . build.deb artifacts: expire_in: 1 week paths: From 1e84e141df5e126cb970d298d4f384ca36662f34 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:16:04 +0100 Subject: [PATCH 08/47] fixes --- .gitlab-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b1f9175..1e7e0d0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,8 +18,10 @@ build:debian: # create conf file - mkdir -p build/etc/fastapi-dls - touch build/etc/fastapi-dls/env + # cd into "build/" + - cd build/ script: - - chroot build/ dpkg -b . build.deb + - dpkg -b . build.deb artifacts: expire_in: 1 week paths: From 5d48f6b7d53380b152ca4ab187e4ee5927e6d26c Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:19:35 +0100 Subject: [PATCH 09/47] .gitlab-ci.yml - fixed artifact path --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1e7e0d0..72fc8e8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,7 +25,7 @@ build:debian: artifacts: expire_in: 1 week paths: - - build.deb + - build/build.deb build:docker: image: docker:dind @@ -66,7 +66,7 @@ test:debian: - apt-get update -qq && apt-get install -qq -y jq # systemd script: # test installation - - apt-get install -q -y ./build.deb --fix-missing + - apt-get install -q -y build/build.deb --fix-missing # copy example config from GitLab-CI-Variables #- cat ${EXAMPLE_CONFIG} > /etc/fastapi-dls/env #- systemctl daemon-reload From e2cea7136560b0330084c0fef7a1c54db41ad9d1 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:22:03 +0100 Subject: [PATCH 10/47] .gitlab-ci.yml - added some debugging --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 72fc8e8..5fdbcb9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -65,6 +65,7 @@ test:debian: before_script: - apt-get update -qq && apt-get install -qq -y jq # systemd script: + - ls -alh # test installation - apt-get install -q -y build/build.deb --fix-missing # copy example config from GitLab-CI-Variables From ab30ad2117e6985b6821d6b82e57dcb2c9e82d1c Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:23:51 +0100 Subject: [PATCH 11/47] .gitlab-ci.yml - debugging --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5fdbcb9..b2a928b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -65,9 +65,9 @@ test:debian: before_script: - apt-get update -qq && apt-get install -qq -y jq # systemd script: - - ls -alh + - ls -alh build/ # test installation - - apt-get install -q -y build/build.deb --fix-missing + - apt-get install -q -y ./build/build.deb --fix-missing # copy example config from GitLab-CI-Variables #- cat ${EXAMPLE_CONFIG} > /etc/fastapi-dls/env #- systemctl daemon-reload From 60ec2821e2fb6fa4851023644eb1e94fa2bc3fdb Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:38:26 +0100 Subject: [PATCH 12/47] postinst - add default value --- DEBIAN/postinst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index dc5ee05..99e3e41 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -46,7 +46,8 @@ 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 - read -p "> Do you wish to create self-signed webserver certificate? [y/n]" yn + 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 From 646cca42f4368900309f689147fde4a649555ff9 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 10:38:49 +0100 Subject: [PATCH 13/47] .gitlab-ci.yml - removed some debugging --- .gitlab-ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b2a928b..aaad6f5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -65,7 +65,6 @@ test:debian: before_script: - apt-get update -qq && apt-get install -qq -y jq # systemd script: - - ls -alh build/ # test installation - apt-get install -q -y ./build/build.deb --fix-missing # copy example config from GitLab-CI-Variables From 7c8a113fbdb991187e9a698d085f223f6c9ab9db Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 11:05:11 +0100 Subject: [PATCH 14/47] .gitlab-ci.yml - added "DEBIAN_FRONTEND=noninteractive" for debian test --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index aaad6f5..29435c4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -59,6 +59,8 @@ test: test:debian: image: debian:bookworm-slim stage: test + variables: + DEBIAN_FRONTEND: noninteractive needs: - job: build:debian artifacts: true From 52fb18dea0cc1089b863765591579347565467e4 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 12:21:52 +0100 Subject: [PATCH 15/47] main.py - fixed imports for "Crypto" and "Cryptodome" (on debian) --- app/main.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index c5970d3..75ec633 100644 --- a/app/main.py +++ b/app/main.py @@ -19,8 +19,14 @@ from starlette.middleware.cors import CORSMiddleware from starlette.responses import StreamingResponse, JSONResponse, HTMLResponse from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from Cryptodome.PublicKey import RSA # Crypto | Cryptodome on Debian -from Cryptodome.PublicKey.RSA import RsaKey # Crypto | Cryptodome on Debian + +try: + # Crypto | Cryptodome on Debian + from Crypto.PublicKey import RSA + from Crypto.PublicKey.RSA import RsaKey +except ModuleNotFoundError: + from Cryptodome.PublicKey import RSA + from Cryptodome.PublicKey.RSA import RsaKey from orm import Origin, Lease, init as db_init From 507ce93718d393bedb9fdab7acc1cd447722fdc7 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 12:32:40 +0100 Subject: [PATCH 16/47] .gitlab-ci.yml - test starting service --- .gitlab-ci.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 29435c4..f74c967 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -69,6 +69,14 @@ test:debian: script: # test installation - apt-get install -q -y ./build/build.deb --fix-missing + - uvicorn --host 127.0.0.1 --port 443 \ + --app-dir /usr/share/fastapi-dls/app \ + --ssl-keyfile /etc/fastapi-dls/webserver.key \ + --ssl-certfile /opt/fastapi-dls/webserver.crt \ + --proxy-headers & + - FASTAPI_DLS_PID=$! + - echo "> Started service with pid: $FASTAPI_DLS_PID" + - kill $FASTAPI_DLS_PID # copy example config from GitLab-CI-Variables #- cat ${EXAMPLE_CONFIG} > /etc/fastapi-dls/env #- systemctl daemon-reload From 701453b18a8e95e70c6231a91366c0f0a0e115bf Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 12:35:07 +0100 Subject: [PATCH 17/47] .gitlab-ci.yml - fixes --- .gitlab-ci.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f74c967..634f427 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -69,13 +69,13 @@ test:debian: script: # test installation - apt-get install -q -y ./build/build.deb --fix-missing - - uvicorn --host 127.0.0.1 --port 443 \ - --app-dir /usr/share/fastapi-dls/app \ - --ssl-keyfile /etc/fastapi-dls/webserver.key \ - --ssl-certfile /opt/fastapi-dls/webserver.crt \ + - uvicorn --host 127.0.0.1 --port 443 + --app-dir /usr/share/fastapi-dls/app + --ssl-keyfile /etc/fastapi-dls/webserver.key + --ssl-certfile /opt/fastapi-dls/webserver.crt --proxy-headers & - FASTAPI_DLS_PID=$! - - echo "> Started service with pid: $FASTAPI_DLS_PID" + - echo "Started service with pid $FASTAPI_DLS_PID" - kill $FASTAPI_DLS_PID # copy example config from GitLab-CI-Variables #- cat ${EXAMPLE_CONFIG} > /etc/fastapi-dls/env From 4df5f18b67c748253f443d0943813efc093f90ef Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 12:40:33 +0100 Subject: [PATCH 18/47] .gitlab-ci.yml - improved testing --- .gitlab-ci.yml | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 634f427..eae130a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -65,10 +65,13 @@ test:debian: - job: build:debian artifacts: true before_script: - - apt-get update -qq && apt-get install -qq -y jq # systemd + - apt-get update -qq && apt-get install -qq -y jq script: # test installation - apt-get install -q -y ./build/build.deb --fix-missing + # copy example config from GitLab-CI-Variables + #- cat ${EXAMPLE_CONFIG} > /etc/fastapi-dls/env + # start service in background - uvicorn --host 127.0.0.1 --port 443 --app-dir /usr/share/fastapi-dls/app --ssl-keyfile /etc/fastapi-dls/webserver.key @@ -76,15 +79,10 @@ test:debian: --proxy-headers & - FASTAPI_DLS_PID=$! - echo "Started service with pid $FASTAPI_DLS_PID" + # testing service + - if [ "`curl --insecure -s https://127.0.0.1/status | jq .status`" != "up" ]; then echo "Success"; else "Error"; fi + # cleanup - kill $FASTAPI_DLS_PID - # copy example config from GitLab-CI-Variables - #- cat ${EXAMPLE_CONFIG} > /etc/fastapi-dls/env - #- systemctl daemon-reload - #- systemctl enable fastapi-dls.service - #- systemctl start fastapi-dls.service - #- if [ "`curl --insecure -s https://localhost:8000/status | jq .status`" != "up" ]; then exit 2; fi - #- systemctl stop fastapi-dls.service - #- systemctl disable fastapi-dls.service - apt-get purge -qq -y fastapi-dls - apt-get autoremove -qq -y && apt-get clean -qq From 4c643b18dd3bf276a039a556a2b34a7e71f77cea Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 12:49:12 +0100 Subject: [PATCH 19/47] .gitlab-ci.yml - implemented deploy stage for debian package --- .gitlab-ci.yml | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index eae130a..69ce698 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -86,7 +86,7 @@ test:debian: - apt-get purge -qq -y fastapi-dls - apt-get autoremove -qq -y && apt-get clean -qq -deploy: +deploy:docker: stage: deploy rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH @@ -107,3 +107,35 @@ deploy: - docker build . --tag $PUBLIC_REGISTRY_USER/${CI_PROJECT_NAME}:latest - docker push $PUBLIC_REGISTRY_USER/${CI_PROJECT_NAME}:${VERSION} - docker push $PUBLIC_REGISTRY_USER/${CI_PROJECT_NAME}:latest + +deploy:debian: + image: debian:bookworm-slim + stage: deploy + rules: + - if: $CI_COMMIT_BRANCH == "debian" # $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + needs: + - job: build:debian + artifacts: true + before_script: + - apt-get update -qq && apt-get install -qq -y curl + script: + # Naming format: _-_.deb + # Version is the version number of the app being packaged + # Release number is the version number of the *packaging* itself. + # The release number might increment if the package maintainer + # updated the packaging, while the version number of the application + # being packaged did not change. + - BUILD_NAME=build.deb # inherited by build-stage + - PACKAGE_NAME=`dpkg -I build/build.deb | grep "Package:" | awk '{ print $2 }'` + - PACKAGE_VERSION=`dpkg -I build/build.deb | grep "Version:" | awk '{ print $2 }'` + - PACKAGE_ARCH=amd64 + - EXPORT_NAME="${PACKAGE_NAME}_${PACKAGE_VERSION}-0_${PACKAGE_ARCH}.deb" + - mv ${BUILD_NAME} ${EXPORT_NAME} + - 'echo "PACKAGE_NAME: ${PACKAGE_NAME}"' + - 'echo "PACKAGE_VERSION: ${PACKAGE_VERSION}"' + - 'echo "PACKAGE_ARCH: ${PACKAGE_ARCH}"' + - 'echo "EXPORT_NAME: ${EXPORT_NAME}"' + # https://docs.gitlab.com/14.3/ee/user/packages/debian_repository/index.html + - URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/debian/${EXPORT_NAME}" + - 'echo "URL: ${URL}"' + - 'curl --request PUT --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ${EXPORT_NAME} ${URL}' From 751546995d5f791d6941a6c35460e08a9d7dd785 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 12:56:46 +0100 Subject: [PATCH 20/47] .gitlab-ci.yml - fixed artifact upload --- .gitlab-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 69ce698..26d97f9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -125,9 +125,9 @@ deploy:debian: # The release number might increment if the package maintainer # updated the packaging, while the version number of the application # being packaged did not change. - - BUILD_NAME=build.deb # inherited by build-stage - - PACKAGE_NAME=`dpkg -I build/build.deb | grep "Package:" | awk '{ print $2 }'` - - PACKAGE_VERSION=`dpkg -I build/build.deb | grep "Version:" | awk '{ print $2 }'` + - BUILD_NAME=build/build.deb # inherited by build-stage + - PACKAGE_NAME=`dpkg -I ${BUILD_NAME} | grep "Package:" | awk '{ print $2 }'` + - PACKAGE_VERSION=`dpkg -I ${BUILD_NAME} | grep "Version:" | awk '{ print $2 }'` - PACKAGE_ARCH=amd64 - EXPORT_NAME="${PACKAGE_NAME}_${PACKAGE_VERSION}-0_${PACKAGE_ARCH}.deb" - mv ${BUILD_NAME} ${EXPORT_NAME} From 9d900c4f5c923f1e3eb6bcda002c8be18b7b2a04 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 13:27:27 +0100 Subject: [PATCH 21/47] .gitlab-ci.yml - create initial debian repo --- .gitlab-ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 26d97f9..5b9c2b1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -109,6 +109,7 @@ deploy:docker: - docker push $PUBLIC_REGISTRY_USER/${CI_PROJECT_NAME}:latest deploy:debian: + # doc: https://git.collinwebdesigns.de/help/user/packages/debian_repository/index.md#install-a-package image: debian:bookworm-slim stage: deploy rules: @@ -117,7 +118,10 @@ deploy:debian: - job: build:debian artifacts: true before_script: - - apt-get update -qq && apt-get install -qq -y curl + - apt-get update -qq && apt-get install -qq -y curl lsb-release + # create distribution initial + - CODENAME=`lsb_release -cs` + - 'curl --request POST --header "JOB-TOKEN: $CI_JOB_TOKEN" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/debian_distributions?codename=${CODENAME}"' script: # Naming format: _-_.deb # Version is the version number of the app being packaged From 8f5ff50aaf8fe21b59e33d5069d617e8e5107327 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 13:34:21 +0100 Subject: [PATCH 22/47] .gitlab-ci.yml - dynamically create repo for codename if not exist --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5b9c2b1..8efda48 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -121,7 +121,8 @@ deploy:debian: - apt-get update -qq && apt-get install -qq -y curl lsb-release # create distribution initial - CODENAME=`lsb_release -cs` - - 'curl --request POST --header "JOB-TOKEN: $CI_JOB_TOKEN" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/debian_distributions?codename=${CODENAME}"' + # create repo if not exists + - 'if [ "`curl -s -o /dev/null -w "%{http_code}" --header "JOB-TOKEN: $CI_JOB_TOKEN" -s ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/debian_distributions/${CODENAME}/key.asc`" != "200" ]; then curl --request POST --header "JOB-TOKEN: $CI_JOB_TOKEN" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/debian_distributions?codename=${CODENAME}"; fi' script: # Naming format: _-_.deb # Version is the version number of the app being packaged From 6947d928ec70c7deacf660632b37fcea0c436b7d Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 13:45:31 +0100 Subject: [PATCH 23/47] .gitlab-ci.yml - fixed artifact upload with access token --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8efda48..1734ea1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -143,4 +143,5 @@ deploy:debian: # https://docs.gitlab.com/14.3/ee/user/packages/debian_repository/index.html - URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/debian/${EXPORT_NAME}" - 'echo "URL: ${URL}"' - - 'curl --request PUT --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ${EXPORT_NAME} ${URL}' + #- 'curl --request PUT --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ${EXPORT_NAME} ${URL}' + - 'curl --request PUT --upload-file ${EXPORT_NAME} https://${PRIVATE_WRITE_PACKAGE_REGISTRY_USER}:${PRIVATE_WRITE_PACKAGE_REGISTRY_TOKEN}@git.collinwebdesigns.de/api/v4/projects/${CI_PROJECT_ID}/packages/debian/${EXPORT_NAME}' From c2e04552f7c9af9ec16f20c4c7700292a3048cf1 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 14:45:03 +0100 Subject: [PATCH 24/47] debian - bump version to 0.6.0 --- DEBIAN/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEBIAN/control b/DEBIAN/control index aea841a..aa81b51 100644 --- a/DEBIAN/control +++ b/DEBIAN/control @@ -1,5 +1,5 @@ Package: fastapi-dls -Version: 0.5 +Version: 0.6.0 Architecture: all Maintainer: Oscar Krause oscar.krause@collinwebdesigns.de Depends: python3, python3-fastapi, python3-uvicorn, python3-dotenv, python3-dateutil, python3-jose, python3-sqlalchemy, python3-pycryptodome, uvicorn, openssl From 6f143f219950a2d70c72002edf8b6c44597b64ce Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 14:52:17 +0100 Subject: [PATCH 25/47] .gitlab-ci.yml - fixed filename --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1734ea1..baa1479 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -134,7 +134,8 @@ deploy:debian: - PACKAGE_NAME=`dpkg -I ${BUILD_NAME} | grep "Package:" | awk '{ print $2 }'` - PACKAGE_VERSION=`dpkg -I ${BUILD_NAME} | grep "Version:" | awk '{ print $2 }'` - PACKAGE_ARCH=amd64 - - EXPORT_NAME="${PACKAGE_NAME}_${PACKAGE_VERSION}-0_${PACKAGE_ARCH}.deb" + #- EXPORT_NAME="${PACKAGE_NAME}_${PACKAGE_VERSION}-0_${PACKAGE_ARCH}.deb" + - EXPORT_NAME="${PACKAGE_NAME}_${PACKAGE_VERSION}_${PACKAGE_ARCH}.deb" - mv ${BUILD_NAME} ${EXPORT_NAME} - 'echo "PACKAGE_NAME: ${PACKAGE_NAME}"' - 'echo "PACKAGE_VERSION: ${PACKAGE_VERSION}"' From 7898052207f4274037202c9e436f48283a7a7754 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 17:00:33 +0100 Subject: [PATCH 26/47] fixed service --- DEBIAN/postinst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 99e3e41..1e8d83e 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -10,13 +10,14 @@ After=network.target User=www-data Group=www-data WorkingDirectory=/usr/share/fastapi-dls -ExecStart=uvicorn \ +EnvironmentFile=/etc/fastapi-dls.env +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 -EnvironmentFile=/etc/fastapi-dls.env Restart=always KillSignal=SIGQUIT Type=notify From 9a0db3c18f0b4fd4ff1295f621a159d46e058017 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 21:58:08 +0100 Subject: [PATCH 27/47] .gitlab-ci.yml - using generic package registry temporary --- .gitlab-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index baa1479..e60da24 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -145,4 +145,6 @@ deploy:debian: - URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/debian/${EXPORT_NAME}" - 'echo "URL: ${URL}"' #- 'curl --request PUT --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ${EXPORT_NAME} ${URL}' - - 'curl --request PUT --upload-file ${EXPORT_NAME} https://${PRIVATE_WRITE_PACKAGE_REGISTRY_USER}:${PRIVATE_WRITE_PACKAGE_REGISTRY_TOKEN}@git.collinwebdesigns.de/api/v4/projects/${CI_PROJECT_ID}/packages/debian/${EXPORT_NAME}' + # using generic-package-registry until debian-registry is GA + # https://docs.gitlab.com/ee/user/packages/generic_packages/index.html#publish-a-generic-package-by-using-cicd + - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file ${EXPORT_NAME} "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${PACKAGE_NAME}/${PACKAGE_VERSION}/${EXPORT_NAME}"' From 18d6da8ebff9c0e6c2e0cb271de4d0ea55c8c35e Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Dec 2022 22:18:02 +0100 Subject: [PATCH 28/47] fixes --- DEBIAN/postinst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 1e8d83e..c7d374e 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -26,6 +26,7 @@ NotifyAccess=all [Install] WantedBy=multi-user.target + EOF CONFIG_DIR=/etc/fastapi-dls @@ -35,11 +36,12 @@ mkdir -p $CONFIG_DIR echo "> Writing default config parameters ..." touch $CONFIG_DIR/fastapi-dls.env -echo <$CONFIG_DIR +echo < $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 + EOF echo "> Create dls-instance keypair ..." @@ -84,4 +86,5 @@ cat < Date: Wed, 28 Dec 2022 06:46:42 +0100 Subject: [PATCH 29/47] postinst - fixed "cat" instead of "echo" --- DEBIAN/postinst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index c7d374e..7562e31 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -1,7 +1,7 @@ #!/bin/bash echo "> Install service ..." -echo </etc/systemd/system/fastapi-dls.service +cat < /etc/systemd/system/fastapi-dls.service [Unit] Description=Service for fastapi-dls After=network.target @@ -36,7 +36,7 @@ mkdir -p $CONFIG_DIR echo "> Writing default config parameters ..." touch $CONFIG_DIR/fastapi-dls.env -echo < $CONFIG_DIR/fastapi-dls.env +cat < $CONFIG_DIR/fastapi-dls.env DLS_URL=127.0.0.1 DLS_PORT=443 LEASE_EXPIRE_DAYS=90 From 548e1c94926df108385850142b7d1b40191db30f Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 06:47:06 +0100 Subject: [PATCH 30/47] postinst - fixed service file --- DEBIAN/postinst | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 7562e31..ea81cfa 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -9,18 +9,19 @@ After=network.target [Service] User=www-data Group=www-data +AmbientCapabilities=CAP_NET_BIND_SERVICE +EnvironmentFile=/etc/fastapi-dls/env WorkingDirectory=/usr/share/fastapi-dls -EnvironmentFile=/etc/fastapi-dls.env -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 \ +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 -Type=notify +Type=simple StandardError=syslog NotifyAccess=all From 8c1c51897ff1de9c75a71b21f8831ae86085a578 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 06:53:31 +0100 Subject: [PATCH 31/47] README.md - added install instructions --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d5da347..3845c0d 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,27 @@ EOF ``` Now you have to run `systemctl daemon-reload`. After that you can start service -with `systemctl start fastapi-dls.service` (and enable autostart with `systemctl enable fastapi-dls.service`). +with `systemctl start fastapi-dls.service`. + +## Debian/Ubuntu (using `dpkg`) + +Packages are available here: + +- [GitLab-Registry](https://git.collinwebdesigns.de/oscar.krause/fastapi-dls/-/packages/63) + +Successful tested with: +- Debian 12 (Bookworm) +- Ubuntu 22.10 (Kinetic Kudu) + +**Run this on your server instance** + +```shell +apt-get update +FILENAME=/opt/fastapi-dls.deb +wget -O $FILENAME https://git.collinwebdesigns.de/oscar.krause/fastapi-dls/-/package_files/148/download +dpkg -i $FILENAME +apt-get install -f --fix-missing +``` # Configuration From 2af4b456b6bfa1395a6713f0c3ad4bde2f1a4600 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 06:56:31 +0100 Subject: [PATCH 32/47] fixes --- DEBIAN/postinst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index ea81cfa..83e6ccd 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -10,8 +10,8 @@ After=network.target User=www-data Group=www-data AmbientCapabilities=CAP_NET_BIND_SERVICE -EnvironmentFile=/etc/fastapi-dls/env WorkingDirectory=/usr/share/fastapi-dls +EnvironmentFile=/etc/fastapi-dls/env ExecStart=uvicorn main:app \\ --env-file /etc/fastapi-dls/env \\ --host \$DLS_URL --port \$DLS_PORT \\ @@ -36,8 +36,8 @@ echo "> Create config directory ..." mkdir -p $CONFIG_DIR echo "> Writing default config parameters ..." -touch $CONFIG_DIR/fastapi-dls.env -cat < $CONFIG_DIR/fastapi-dls.env +touch $CONFIG_DIR/fastapi-dls/env +cat < $CONFIG_DIR/fastapi-dls/env DLS_URL=127.0.0.1 DLS_PORT=443 LEASE_EXPIRE_DAYS=90 From 63670f52e89b8573620e25418ddf62bc8ab3674d Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 07:03:41 +0100 Subject: [PATCH 33/47] postinst fixes --- DEBIAN/postinst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 83e6ccd..d1c43e4 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -1,7 +1,12 @@ #!/bin/bash +CONFIG_DIR=/etc/fastapi-dls + +echo "> Create config directory ..." +mkdir -p $CONFIG_DIR + echo "> Install service ..." -cat < /etc/systemd/system/fastapi-dls.service +cat </etc/systemd/system/fastapi-dls.service [Unit] Description=Service for fastapi-dls After=network.target @@ -11,7 +16,7 @@ User=www-data Group=www-data AmbientCapabilities=CAP_NET_BIND_SERVICE WorkingDirectory=/usr/share/fastapi-dls -EnvironmentFile=/etc/fastapi-dls/env +EnvironmentFile=$CONFIG_DIR/env ExecStart=uvicorn main:app \\ --env-file /etc/fastapi-dls/env \\ --host \$DLS_URL --port \$DLS_PORT \\ @@ -30,14 +35,9 @@ WantedBy=multi-user.target EOF -CONFIG_DIR=/etc/fastapi-dls - -echo "> Create config directory ..." -mkdir -p $CONFIG_DIR - echo "> Writing default config parameters ..." -touch $CONFIG_DIR/fastapi-dls/env -cat < $CONFIG_DIR/fastapi-dls/env +touch $CONFIG_DIR/env +cat <$CONFIG_DIR/env DLS_URL=127.0.0.1 DLS_PORT=443 LEASE_EXPIRE_DAYS=90 From a08261f7cd8ecb428b465cf5f500bef29f5d0322 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 07:14:24 +0100 Subject: [PATCH 34/47] postinst - fixed paths and permissions --- DEBIAN/postinst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index d1c43e4..14700be 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -20,9 +20,9 @@ EnvironmentFile=$CONFIG_DIR/env ExecStart=uvicorn main:app \\ --env-file /etc/fastapi-dls/env \\ --host \$DLS_URL --port \$DLS_PORT \\ - --app-dir /usr/share/fastapi-dls/app \\ + --app-dir /usr/share/fastapi-dls \\ --ssl-keyfile /etc/fastapi-dls/webserver.key \\ - --ssl-certfile /opt/fastapi-dls/webserver.crt \\ + --ssl-certfile /etc/fastapi-dls/webserver.crt \\ --proxy-headers Restart=always KillSignal=SIGQUIT @@ -74,6 +74,8 @@ if [[ -f $CONFIG_DIR/webserver.key ]]; then fi fi +chown -R www-data:www-data $CONFIG_DIR/* + cat < Date: Wed, 28 Dec 2022 07:16:34 +0100 Subject: [PATCH 35/47] postrm - remove service --- DEBIAN/postrm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DEBIAN/postrm b/DEBIAN/postrm index a3d8b25..f786c67 100755 --- a/DEBIAN/postrm +++ b/DEBIAN/postrm @@ -5,4 +5,9 @@ if [[ -d /etc/fastapi-dls ]]; then rm -r /etc/fastapi-dls fi +if [[ -f /etc/systemd/system/fastapi-dls.service ]]; then + echo "> Removing service file." + rm /etc/systemd/system/fastapi-dls.service +fi + # todo From cca24f0ad56f3bc2e11d54ca67add64135cde6ec Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 07:31:23 +0100 Subject: [PATCH 36/47] fixed instance keypair path --- DEBIAN/postinst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 14700be..e4e8873 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -42,6 +42,8 @@ DLS_URL=127.0.0.1 DLS_PORT=443 LEASE_EXPIRE_DAYS=90 DATABASE=sqlite:////usr/share/fastapi-dls/db.sqlite +INSTANCE_KEY_RSA=$CONFIG_DIR/instance.private.pem +INSTANCE_KEY_PUB=$CONFIG_DIR/instance.public.pem EOF From 6b3f536681c86296526786fc80aa4737e1d42b20 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 07:40:44 +0100 Subject: [PATCH 37/47] fixes - fixed app dir - fixed missing readme and version file - keep config on update/remove --- .gitlab-ci.yml | 5 +++-- DEBIAN/postinst | 8 +++++--- DEBIAN/postrm | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e60da24..0c34ce4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,9 +12,10 @@ build:debian: - mkdir build # copy install instructions - cp -r DEBIAN build/ - # copy app + # copy app into "/usr/share/fastapi-dls" as "/usr/share/fastapi-dls/app" & copy README.md and version.env - mkdir -p build/usr/share/ - - cp -r app build/usr/share/fastapi-dls + - cp -r app build/usr/share/fastapi-dls/ + - cp README.md version.env build/usr/share/fastapi-dls # create conf file - mkdir -p build/etc/fastapi-dls - touch build/etc/fastapi-dls/env diff --git a/DEBIAN/postinst b/DEBIAN/postinst index e4e8873..77bf587 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -1,5 +1,6 @@ #!/bin/bash +WORKING_DIR=/usr/share/fastapi-dls CONFIG_DIR=/etc/fastapi-dls echo "> Create config directory ..." @@ -15,12 +16,12 @@ After=network.target User=www-data Group=www-data AmbientCapabilities=CAP_NET_BIND_SERVICE -WorkingDirectory=/usr/share/fastapi-dls +WorkingDirectory=$WORKING_DIR EnvironmentFile=$CONFIG_DIR/env ExecStart=uvicorn main:app \\ --env-file /etc/fastapi-dls/env \\ --host \$DLS_URL --port \$DLS_PORT \\ - --app-dir /usr/share/fastapi-dls \\ + --app-dir $WORKING_DIR/app \\ --ssl-keyfile /etc/fastapi-dls/webserver.key \\ --ssl-certfile /etc/fastapi-dls/webserver.crt \\ --proxy-headers @@ -41,7 +42,7 @@ cat <$CONFIG_DIR/env DLS_URL=127.0.0.1 DLS_PORT=443 LEASE_EXPIRE_DAYS=90 -DATABASE=sqlite:////usr/share/fastapi-dls/db.sqlite +DATABASE=sqlite:///$CONFIG_DIR/db.sqlite INSTANCE_KEY_RSA=$CONFIG_DIR/instance.private.pem INSTANCE_KEY_PUB=$CONFIG_DIR/instance.public.pem @@ -77,6 +78,7 @@ if [[ -f $CONFIG_DIR/webserver.key ]]; then fi chown -R www-data:www-data $CONFIG_DIR/* +chown -R www-data:www-data $WORKING_DIR cat < Removing config directory." - rm -r /etc/fastapi-dls + echo "> Config directory not empty, please remove manually." + # rm -r /etc/fastapi-dls fi if [[ -f /etc/systemd/system/fastapi-dls.service ]]; then From cf21bec3b0f26e34e46ac9c165b41f9830aae20d Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 08:05:35 +0100 Subject: [PATCH 38/47] postrm fixed removing app dir --- DEBIAN/postrm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DEBIAN/postrm b/DEBIAN/postrm index 15aba78..07cb178 100755 --- a/DEBIAN/postrm +++ b/DEBIAN/postrm @@ -5,6 +5,11 @@ if [[ -d /etc/fastapi-dls ]]; then # rm -r /etc/fastapi-dls fi +if [[ -d /usr/share/fastapi-dls/ ]]; then + echo "> Removing app dir." + rm -r /usr/share/fastapi-dls +fi + if [[ -f /etc/systemd/system/fastapi-dls.service ]]; then echo "> Removing service file." rm /etc/systemd/system/fastapi-dls.service From 45af6c11c0872c745a056639ca9eec648bab2ad8 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 08:21:04 +0100 Subject: [PATCH 39/47] fixed missing systemctl daemon-reload --- DEBIAN/postinst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 77bf587..080dc6c 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -36,6 +36,8 @@ WantedBy=multi-user.target EOF +systemctl daemon-reload + echo "> Writing default config parameters ..." touch $CONFIG_DIR/env cat <$CONFIG_DIR/env From 6844604a0b59bdb473158b34a394f14cfdeb6858 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 08:25:31 +0100 Subject: [PATCH 40/47] fixed deb package paths --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0c34ce4..fc06da2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -13,8 +13,8 @@ build:debian: # copy install instructions - cp -r DEBIAN build/ # copy app into "/usr/share/fastapi-dls" as "/usr/share/fastapi-dls/app" & copy README.md and version.env - - mkdir -p build/usr/share/ - - cp -r app build/usr/share/fastapi-dls/ + - mkdir -p build/usr/share/fastapi-dls + - cp -r app build/usr/share/fastapi-dls - cp README.md version.env build/usr/share/fastapi-dls # create conf file - mkdir -p build/etc/fastapi-dls From da21ef3cdc5d96eeb5e4be2f12e89ceeeba9bdae Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 08:35:59 +0100 Subject: [PATCH 41/47] fixed some permissions --- DEBIAN/postinst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 080dc6c..6376347 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -79,7 +79,7 @@ if [[ -f $CONFIG_DIR/webserver.key ]]; then fi fi -chown -R www-data:www-data $CONFIG_DIR/* +chown -R www-data:www-data $CONFIG_DIR chown -R www-data:www-data $WORKING_DIR cat < Date: Wed, 28 Dec 2022 08:48:36 +0100 Subject: [PATCH 42/47] postrm fixed --- DEBIAN/postrm | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/DEBIAN/postrm b/DEBIAN/postrm index 07cb178..b99d0fa 100755 --- a/DEBIAN/postrm +++ b/DEBIAN/postrm @@ -1,15 +1,5 @@ #!/bin/bash -if [[ -d /etc/fastapi-dls ]]; then - echo "> Config directory not empty, please remove manually." - # rm -r /etc/fastapi-dls -fi - -if [[ -d /usr/share/fastapi-dls/ ]]; then - echo "> Removing app dir." - rm -r /usr/share/fastapi-dls -fi - if [[ -f /etc/systemd/system/fastapi-dls.service ]]; then echo "> Removing service file." rm /etc/systemd/system/fastapi-dls.service From 4e5559bb85248a23a95d856b6310f34295994d7c Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 08:51:55 +0100 Subject: [PATCH 43/47] fixed service Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether. --- DEBIAN/postinst | 1 - 1 file changed, 1 deletion(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 6376347..307ee2f 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -28,7 +28,6 @@ ExecStart=uvicorn main:app \\ Restart=always KillSignal=SIGQUIT Type=simple -StandardError=syslog NotifyAccess=all [Install] From 437b62376fc0984c46b731ca72692583fa4040ee Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 08:56:11 +0100 Subject: [PATCH 44/47] fixed missing debian dependency --- DEBIAN/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEBIAN/control b/DEBIAN/control index aa81b51..1eeb2b2 100644 --- a/DEBIAN/control +++ b/DEBIAN/control @@ -2,7 +2,7 @@ Package: fastapi-dls Version: 0.6.0 Architecture: all Maintainer: Oscar Krause oscar.krause@collinwebdesigns.de -Depends: python3, python3-fastapi, python3-uvicorn, python3-dotenv, python3-dateutil, python3-jose, python3-sqlalchemy, python3-pycryptodome, uvicorn, openssl +Depends: python3, python3-fastapi, python3-uvicorn, python3-dotenv, python3-dateutil, python3-jose, python3-sqlalchemy, python3-pycryptodome, python3-markdown, uvicorn, openssl Recommends: curl Installed-Size: 10240 Homepage: https://git.collinwebdesigns.de/oscar.krause/fastapi-dls From 2340931a6066340b47dd6f76613c86e3b496f3a3 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 08:57:35 +0100 Subject: [PATCH 45/47] fixes --- DEBIAN/postinst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 307ee2f..a0e7334 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -16,7 +16,7 @@ After=network.target User=www-data Group=www-data AmbientCapabilities=CAP_NET_BIND_SERVICE -WorkingDirectory=$WORKING_DIR +WorkingDirectory=$WORKING_DIR/app EnvironmentFile=$CONFIG_DIR/env ExecStart=uvicorn main:app \\ --env-file /etc/fastapi-dls/env \\ From b22613c3379408336efe7af6108b1ad098f2a46c Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 09:04:35 +0100 Subject: [PATCH 46/47] postinst improvements --- DEBIAN/postinst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/DEBIAN/postinst b/DEBIAN/postinst index a0e7334..2311b35 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -37,9 +37,10 @@ EOF systemctl daemon-reload -echo "> Writing default config parameters ..." -touch $CONFIG_DIR/env -cat <$CONFIG_DIR/env +if [[ ! -f $CONFIG_DIR/env ]]; then + echo "> Writing initial config ..." + touch $CONFIG_DIR/env + cat <$CONFIG_DIR/env DLS_URL=127.0.0.1 DLS_PORT=443 LEASE_EXPIRE_DAYS=90 @@ -48,6 +49,7 @@ INSTANCE_KEY_RSA=$CONFIG_DIR/instance.private.pem INSTANCE_KEY_PUB=$CONFIG_DIR/instance.public.pem EOF +fi echo "> Create dls-instance keypair ..." openssl genrsa -out $CONFIG_DIR/instance.private.pem 2048 @@ -72,7 +74,8 @@ if [[ -f $CONFIG_DIR/webserver.key ]]; then if [ -x "$(command -v curl)" ]; then echo "> Testing API ..." - curl --insecure -X GET https://127.0.0.1/status + source $CONFIG_DIR/env + curl --insecure -X GET https://$DLS_URL:$DLS_PORT/status else echo "> Testing API failed, curl not available. Please test manually!" fi From 3dc9c8bcb19a7166edbedccc27c412cec723d878 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Wed, 28 Dec 2022 09:10:57 +0100 Subject: [PATCH 47/47] README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1dfabe7..358ceae 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ EOF ``` Now you have to run `systemctl daemon-reload`. After that you can start service -with `systemctl start fastapi-dls.service`. +with `systemctl start fastapi-dls.service` and enable autostart with `systemctl enable fastapi-dls.service`. ## Debian/Ubuntu (using `dpkg`) @@ -214,7 +214,8 @@ wget -O $FILENAME https://git.collinwebdesigns.de/oscar.krause/fastapi-dls/-/pac dpkg -i $FILENAME apt-get install -f --fix-missing ``` -with `systemctl start fastapi-dls.service`. + +Start with `systemctl start fastapi-dls.service` and enable autostart with `systemctl enable fastapi-dls.service`. ## Let's Encrypt Certificate