[Patchew-devel] [PATCH 6/6] server: Use postgres in Docker deploy

Fam Zheng posted 6 patches 5 years, 10 months ago
[Patchew-devel] [PATCH 6/6] server: Use postgres in Docker deploy
Posted by Fam Zheng 5 years, 10 months ago
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 patchew/settings.py                           | 39 +++++++++++--------
 requirements.txt                              |  1 +
 scripts/playbooks/deploy-servers.yml          |  1 +
 scripts/playbooks/templates/docker.service.j2 |  1 +
 4 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/patchew/settings.py b/patchew/settings.py
index 16f60a5..b11da54 100644
--- a/patchew/settings.py
+++ b/patchew/settings.py
@@ -98,21 +98,35 @@ WSGI_APPLICATION = 'patchew.wsgi.application'
 # https://docs.djangoproject.com/en/1.9/ref/settings/#databases
 
 def env_detect():
-    if "PATCHEW_DATA_DIR" in os.environ:
+    if "PATCHEW_DB_PORT_5432_TCP_ADDR" in os.environ:
         # Docker deployment
-        return False, os.environ.get("PATCHEW_DATA_DIR")
+        return (False, os.environ.get("PATCHEW_DATA_DIR"),
+            {
+                'default': {
+                    'ENGINE': 'django.db.backends.postgresql',
+                    'NAME': 'patchew',
+                    'USER': 'patchew',
+                    'PASSWORD': 'patchew',
+                    'HOST': os.environ.get('PATCHEW_DB_PORT_5432_TCP_ADDR'),
+                    'PORT': '5432',
+                    }
+            })
     elif "VIRTUAL_ENV" in os.environ or os.environ.get("PATCHEW_DEBUG", False):
         # Development setup
-        return True, os.path.join(os.environ.get("VIRTUAL_ENV",
-                                                 "/var/tmp/patchew-dev"),
-                                  "data")
-    elif os.environ.get("PATCHEW_TEST"):
-        # Test environment
-        return True, os.environ.get("PATCHEW_TEST_DATA_DIR")
+        data_dir = os.path.join(os.environ.get("VIRTUAL_ENV",
+                                               "/var/tmp/patchew-dev"),
+                                "data")
+        return (True, data_dir,
+            {
+                'default': {
+                    'ENGINE': 'django.db.backends.sqlite3',
+                    'NAME': os.path.join(data_dir, "patchew-db.sqlite3"),
+                    }
+            })
     else:
         raise Exception("Unknown running environment")
 
-DEBUG, DATA_DIR = env_detect()
+DEBUG, DATA_DIR, DATABASES = env_detect()
 
 # In production environments, we run in a container, behind nginx, which should
 # filter the allowed host names. So be a little flexible here
@@ -135,13 +149,6 @@ MEDIA_URL = "/media/"
 if not os.path.isdir(MEDIA_ROOT):
     os.makedirs(MEDIA_ROOT)
 
-DATABASES = {
-    'default': {
-        'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(DATA_DIR, "patchew-db.sqlite3"),
-    }
-}
-
 # If the PATCHEW_ADMIN_EMAIL env var is set, let Django send error reporting to
 # the address.
 admin_email = os.environ.get("PATCHEW_ADMIN_EMAIL")
diff --git a/requirements.txt b/requirements.txt
index aa89c4c..59c9480 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -8,3 +8,4 @@ jsonfield2 >= 3.0, < 4.0
 drf-nested-routers
 coreapi-cli
 pyyaml
+psycopg2
diff --git a/scripts/playbooks/deploy-servers.yml b/scripts/playbooks/deploy-servers.yml
index 933d065..7c6281e 100644
--- a/scripts/playbooks/deploy-servers.yml
+++ b/scripts/playbooks/deploy-servers.yml
@@ -16,6 +16,7 @@
     base_dir: "/data/{{ instance_name }}"
     src_dir: "{{ base_dir }}/src"
     data_dir: "{{ base_dir }}/data"
+    docker_run_args: "--link {{ instance_name }}-db"
   tasks:
     - name: Create data dir
       file:
diff --git a/scripts/playbooks/templates/docker.service.j2 b/scripts/playbooks/templates/docker.service.j2
index afd5df1..85dd9d2 100644
--- a/scripts/playbooks/templates/docker.service.j2
+++ b/scripts/playbooks/templates/docker.service.j2
@@ -10,6 +10,7 @@ ExecStartPre=-/usr/bin/docker stop {{ instance_name }} ; -/usr/bin/docker rm {{
 ExecStart=/usr/bin/docker run --privileged --name {{ instance_name }} \
     -v {{ data_dir }}:/data/patchew:rw \
     -e PATCHEW_DATA_DIR=/data/patchew \
+    {{ docker_run_args | default() }} \
     patchew:{{ instance_name }}
 ExecStop=/usr/bin/docker stop -t 10 {{ instance_name }}
 RestartSec=60
-- 
2.17.2

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel
Re: [Patchew-devel] [PATCH 6/6] server: Use postgres in Docker deploy
Posted by Paolo Bonzini 5 years, 10 months ago
On 31/10/2018 02:28, Fam Zheng wrote:
> -    elif os.environ.get("PATCHEW_TEST"):
> -        # Test environment
> -        return True, os.environ.get("PATCHEW_TEST_DATA_DIR")
> +        data_dir = os.path.join(os.environ.get("VIRTUAL_ENV",
> +                                               "/var/tmp/patchew-dev"),
> +                                "data")
> +        return (True, data_dir,
> +            {
> +                'default': {
> +                    'ENGINE': 'django.db.backends.sqlite3',
> +                    'NAME': os.path.join(data_dir, "patchew-db.sqlite3"),
> +                    }

Why drop the PATCHEW_TEST case?

Paolo

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel
Re: [Patchew-devel] [PATCH 6/6] server: Use postgres in Docker deploy
Posted by Fam Zheng 5 years, 10 months ago
On Wed, 10/31 14:32, Paolo Bonzini wrote:
> On 31/10/2018 02:28, Fam Zheng wrote:
> > -    elif os.environ.get("PATCHEW_TEST"):
> > -        # Test environment
> > -        return True, os.environ.get("PATCHEW_TEST_DATA_DIR")
> > +        data_dir = os.path.join(os.environ.get("VIRTUAL_ENV",
> > +                                               "/var/tmp/patchew-dev"),
> > +                                "data")
> > +        return (True, data_dir,
> > +            {
> > +                'default': {
> > +                    'ENGINE': 'django.db.backends.sqlite3',
> > +                    'NAME': os.path.join(data_dir, "patchew-db.sqlite3"),
> > +                    }
> 
> Why drop the PATCHEW_TEST case?

Because it's unreachable when venv is used, and it seems the idea is reversed in
the first place and is therefore wrong. The env var is set by
tests/patchewtest.py, but this piece of code can run before that, by
./manage.py.

Fam

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel
Re: [Patchew-devel] [PATCH 6/6] server: Use postgres in Docker deploy
Posted by Paolo Bonzini 5 years, 10 months ago
On 01/11/2018 07:34, Fam Zheng wrote:
> On Wed, 10/31 14:32, Paolo Bonzini wrote:
>> On 31/10/2018 02:28, Fam Zheng wrote:
>>> -    elif os.environ.get("PATCHEW_TEST"):
>>> -        # Test environment
>>> -        return True, os.environ.get("PATCHEW_TEST_DATA_DIR")
>>> +        data_dir = os.path.join(os.environ.get("VIRTUAL_ENV",
>>> +                                               "/var/tmp/patchew-dev"),
>>> +                                "data")
>>> +        return (True, data_dir,
>>> +            {
>>> +                'default': {
>>> +                    'ENGINE': 'django.db.backends.sqlite3',
>>> +                    'NAME': os.path.join(data_dir, "patchew-db.sqlite3"),
>>> +                    }
>>
>> Why drop the PATCHEW_TEST case?
> 
> Because it's unreachable when venv is used, and it seems the idea is reversed in
> the first place and is therefore wrong. The env var is set by
> tests/patchewtest.py, but this piece of code can run before that, by
> ./manage.py.

Oh, I see.  Thanks!

Paolo

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel