From 48817d4c082f5bb3c484591cd0a18cef2e37c792 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Mon, 20 Jan 2025 20:44:18 +0000 Subject: [PATCH] new file: .gitlab-ci.yml modified: app.py --- .gitlab-ci.yml | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ app.py | 4 +- 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..d9f2fef --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,123 @@ +stages: + - deploy + +variables: + APP_NAME: "upload.unturf.com" + APP_DIR: "/opt/${APP_NAME}" + VENV_DIR: "${APP_DIR}/venv" + CADDYFILE_DIR: "/etc/caddy" + CADDY_SNIPPETS_DIR: "/etc/caddy/caddy_snippets" + +deploy: + stage: deploy + tags: + - master.unturf.com + only: + - main # Adjust if you want to deploy from other branches + script: + # Step 1: Define variables with substitutions + - | + # Variables with substitutions must be defined in the script section + SERVICE_NAME="uwsgi-${APP_NAME//./-}.service" # Replace dots with dashes + UWSGI_SOCKET="${APP_DIR}/uwsgi.sock" # Place socket in app directory + + # Step 2: Synchronize the code to the application directory + - | + if [ ! -d "${APP_DIR}" ]; then + mkdir -p "${APP_DIR}" + fi + rsync -a --delete "${CI_PROJECT_DIR}/" "${APP_DIR}/" + + # Step 3: Set up Python virtual environment and install dependencies + - | + if [ ! -d "${VENV_DIR}" ]; then + python3 -m venv "${VENV_DIR}" + fi + source "${VENV_DIR}/bin/activate" + pip install --upgrade pip + pip install -r "${APP_DIR}/requirements.txt" + + # Step 4: Install uWSGI in virtual environment + - | + source "${VENV_DIR}/bin/activate" + pip install uwsgi + + # Step 5: Create or update the systemd user service file for uWSGI + - | + mkdir -p ~/.config/systemd/user/ + + # Determine the correct module and callable for your application + # Adjust 'app:app' based on your application's entry point + # For example, if your main application file is 'app.py' and the Flask app is named 'app', use 'app:app' + + # If your application entry point is different, adjust accordingly + # For example, if your main file is 'wsgi.py' and the callable is 'application', use 'wsgi:application' + + # Set the MODULE variable based on your application's entry point + MODULE="app:uwsgi_app" # Adjust this based on your application + + cat > ~/.config/systemd/user/${SERVICE_NAME} < "${CADDY_SNIPPETS_DIR}/${APP_NAME}.caddy" < "${CADDYFILE_DIR}/Caddyfile" + cat "${CADDY_SNIPPETS_DIR}"/*.caddy >> "${CADDYFILE_DIR}/Caddyfile" + + # Validate the Caddyfile + caddy validate --config "${CADDYFILE_DIR}/Caddyfile" + + # Step 8: Reload Caddy service + - touch ~/reload_caddy # Assuming a trigger file is used to reload Caddy + + # Deployment completed + - echo "Deployment completed successfully." diff --git a/app.py b/app.py index 980b64b..b0881bd 100644 --- a/app.py +++ b/app.py @@ -1406,7 +1406,7 @@ def view_media_view(request): ################################################################################ -def main(global_config=None, **settings): +def main(*config, **settings): # Configure logging logging.basicConfig(level=logging.INFO) @@ -1520,6 +1520,8 @@ def main(global_config=None, **settings): config.scan() return config.make_wsgi_app() +# Expose the WSGI application callable for uwsgi +uwsgi_app = main({}) if __name__ == "__main__": app = main()