From 8679edb160e55690d945a14bc71ebf8501f36668 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Wed, 28 Nov 2018 19:56:56 +0100
Subject: [PATCH] Fix #626: Load env file in config/.env automatically to avoid
 sourcing it by hand

---
 api/config/settings/common.py       | 31 +++++++++++++++++++++++++----
 changes/changelog.d/626.enhancement | 10 ++++++++++
 docs/installation/debian.rst        | 17 ++++------------
 docs/upgrading/index.rst            |  2 --
 4 files changed, 41 insertions(+), 19 deletions(-)
 create mode 100644 changes/changelog.d/626.enhancement

diff --git a/api/config/settings/common.py b/api/config/settings/common.py
index cd1b3208..6a9501c0 100644
--- a/api/config/settings/common.py
+++ b/api/config/settings/common.py
@@ -11,6 +11,8 @@ https://docs.djangoproject.com/en/dev/ref/settings/
 from __future__ import absolute_import, unicode_literals
 
 import datetime
+import logging
+
 from urllib.parse import urlparse, urlsplit
 
 import environ
@@ -18,14 +20,35 @@ from celery.schedules import crontab
 
 from funkwhale_api import __version__
 
+logger = logging.getLogger(__name__)
 ROOT_DIR = environ.Path(__file__) - 3  # (/a/b/myfile.py - 3 = /)
 APPS_DIR = ROOT_DIR.path("funkwhale_api")
 
 env = environ.Env()
-try:
-    env.read_env(ROOT_DIR.file(".env"))
-except FileNotFoundError:
-    pass
+env_file = env("ENV_FILE", default=None)
+if env_file:
+    # we have an explicitely specified env file
+    # so we try to load and it fail loudly if it does not exist
+    print("ENV_FILE", env_file)
+    env.read_env(env_file)
+else:
+    # we try to load from .env and config/.env
+    # but do not crash if those files don't exist
+    paths = [
+        # /srv/funwhale/api/.env
+        ROOT_DIR,
+        # /srv/funwhale/config/.env
+        ((ROOT_DIR - 1) + "config"),
+    ]
+    for path in paths:
+        try:
+            env_path = path.file(".env")
+        except FileNotFoundError:
+            logger.debug("No env file found at %s/.env", path)
+            continue
+        env.read_env(env_path)
+        logger.info("Loaded env file at %s/.env", path)
+        break
 
 FUNKWHALE_HOSTNAME = None
 FUNKWHALE_HOSTNAME_SUFFIX = env("FUNKWHALE_HOSTNAME_SUFFIX", default=None)
diff --git a/changes/changelog.d/626.enhancement b/changes/changelog.d/626.enhancement
new file mode 100644
index 00000000..8f2f2010
--- /dev/null
+++ b/changes/changelog.d/626.enhancement
@@ -0,0 +1,10 @@
+Load env file in config/.env automatically to avoid sourcing it by hand (#626)
+
+Automatically load .env file
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+On non-docker deployments, earlier versions required you to source
+the config/.env file before launching any Funkwhale command, with ``export $(cat config/.env | grep -v ^# | xargs)``
+This led to more complex and error prode deployment / setup.
+
+This is not the case anymore, and Funkwhale will automatically load this file if it's available.
diff --git a/docs/installation/debian.rst b/docs/installation/debian.rst
index 7614dc68..61cb18b9 100644
--- a/docs/installation/debian.rst
+++ b/docs/installation/debian.rst
@@ -112,11 +112,11 @@ Then we'll download the frontend files:
     case, run
 
         cd /srv
-        
+
         rm -r funkwhale
-        
+
         git clone -b master https://code.eliotberriot.com/funkwhale/funkwhale funkwhale
-        
+
         cd funkwhale
 
     The above clone command uses the master branch instead of the default develop branch, as master is stable and more suited for production setups.
@@ -228,18 +228,9 @@ Especially, populate the ``DATABASE_URL`` and ``CACHE_URL`` values based on
 how you configured your PostgreSQL and Redis servers in
 :doc:`external dependencies <./external_dependencies>`.
 
-
-When you want to run command on the API server, such as to create the
-database or compile static files, you have to ensure you source
-the environment variables in that file.
-
-This can be done like this::
-
-    export $(cat config/.env | grep -v ^# | xargs)
-
 .. note::
 
-    Remember to reload these variables whenever you edit your .env file.
+    The environment file at config/.env is loaded automatically by Funkwhale processes.
 
 Database setup
 ---------------
diff --git a/docs/upgrading/index.rst b/docs/upgrading/index.rst
index 08c8a568..339274f9 100644
--- a/docs/upgrading/index.rst
+++ b/docs/upgrading/index.rst
@@ -105,8 +105,6 @@ match what is described in :doc:`/installation/debian`:
 
     # update os dependencies
     sudo api/install_os_dependencies.sh install
-    # update python dependencies
-    source /srv/funkwhale/load_env
     sudo -u funkwhale -E /srv/funkwhale/virtualenv/bin/pip install -r api/requirements.txt
 
     # collect static files
-- 
GitLab