From 3f033ad95e2b0bf3fe728d502b49c6d0da8754a8 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 24 Mar 2021 23:20:34 +0100 Subject: [PATCH] [constellation] Install constellation from Python module in production Signed-off-by: Yohann D'ANELLO --- group_vars/constellation_front.yml | 8 +-- roles/constellation-front/handlers/main.yml | 4 +- roles/constellation-front/tasks/main.yml | 47 +++++++++++++----- .../uwsgi/apps-available/constellation.ini.j2 | 6 +-- roles/constellation/tasks/main.yml | 49 +++++++++++++++---- .../constellation/settings_local.py.j2 | 8 +++ .../templates/update-motd.d/05-service.j2 | 2 +- 7 files changed, 91 insertions(+), 33 deletions(-) diff --git a/group_vars/constellation_front.yml b/group_vars/constellation_front.yml index 8ebc68ec..fc6c9aac 100644 --- a/group_vars/constellation_front.yml +++ b/group_vars/constellation_front.yml @@ -11,15 +11,11 @@ service_nginx: locations: - filter: "/static" params: - - "alias /var/local/constellation/static/" - - - filter: "/javascript" - params: - - "alias /usr/share/javascript/" + - "alias {% if constellation.version == 'master' %}/var/lib/constellation/static/{% else %}/var/local/constellation/static/{% endif %}" - filter: "/media" params: - - "alias /var/local/constellation/media/" + - "alias {% if constellation.version == 'master' %}/var/lib/constellation/media/{% else %}/var/local/constellation/media/{% endif %}" - filter: "/" params: diff --git a/roles/constellation-front/handlers/main.yml b/roles/constellation-front/handlers/main.yml index 2b15eaea..73c9606a 100644 --- a/roles/constellation-front/handlers/main.yml +++ b/roles/constellation-front/handlers/main.yml @@ -1,5 +1,5 @@ --- -- name: Reload uWSGI +- name: Restart uWSGI systemd: name: uwsgi - state: reloaded + state: restarted diff --git a/roles/constellation-front/tasks/main.yml b/roles/constellation-front/tasks/main.yml index 96d0b5d4..727ba0be 100644 --- a/roles/constellation-front/tasks/main.yml +++ b/roles/constellation-front/tasks/main.yml @@ -18,6 +18,28 @@ name: - git+https://gitlab.adm.crans.org/nounous/crispy-bootstrap5.git +- name: Set data directories in development mode + when: constellation.version != "master" + set_fact: + project_path: "/var/local/constellation" + module_path: "/var/local/constellation/constellation" + static_dir: "/var/local/constellation/static" + media_dir: "/var/local/constellation/media" + +- name: Set data directories in production mode + when: constellation.version == "master" + set_fact: + project_path: "/usr/local/lib/python3.9/dist-packages/constellation" + module_path: "/usr/local/lib/python3.9/dist-packages/constellation" + static_dir: "/var/lib/constellation/static" + media_dir: "/var/lib/constellation/media" + +- name: Check front dependencies (production) + when: constellation.version == "master" + pip: + name: + - git+https://gitlab.adm.crans.org/nounous/constellation.git[front] + - name: Install uWSGI apt: install_recommends: false @@ -36,7 +58,7 @@ owner: root group: root mode: 0644 - notify: Reload uWSGI + notify: Restart uWSGI - name: Activate constellation uWSGI app file: @@ -46,38 +68,41 @@ group: root state: link ignore_errors: "{{ ansible_check_mode }}" - notify: Reload uWSGI + notify: Restart uWSGI # In the future, migrations will be included in the repository. - name: Make Django migrations django_manage: command: makemigrations - project_path: "/var/local/constellation" - notify: Reload uWSGI + project_path: "{{ project_path }}" + notify: Restart uWSGI - name: Migrate database django_manage: command: migrate - project_path: "/var/local/constellation" - notify: Reload uWSGI + project_path: "{{ project_path }}" + notify: Restart uWSGI - name: Load initial data django_manage: command: loaddata initial - project_path: "/var/local/constellation" - notify: Reload uWSGI + project_path: "{{ project_path }}" + notify: Restart uWSGI - name: Create static files directory file: - path: /var/local/constellation/static + path: "{{ item }}" state: directory mode: '2775' owner: "www-data" group: "{{ constellation.group }}" recurse: true + loop: + - "{{ static_dir }}" + - "{{ media_dir }}" - name: Collect static files django_manage: command: collectstatic - project_path: "/var/local/constellation" - notify: Reload uWSGI + project_path: "{{ project_path }}" + notify: Restart uWSGI diff --git a/roles/constellation-front/templates/uwsgi/apps-available/constellation.ini.j2 b/roles/constellation-front/templates/uwsgi/apps-available/constellation.ini.j2 index 89906f3a..bf2bbeda 100644 --- a/roles/constellation-front/templates/uwsgi/apps-available/constellation.ini.j2 +++ b/roles/constellation-front/templates/uwsgi/apps-available/constellation.ini.j2 @@ -5,8 +5,8 @@ uid = www-data gid = www-data # Django-related settings # the base directory (full path) -chdir = /var/local/constellation -wsgi-file = /var/local/constellation/constellation/wsgi.py +chdir = {{ project_path }} +wsgi-file = {{ module_path }}/wsgi.py plugin = python3 # process-related settings # master @@ -20,4 +20,4 @@ chmod-socket = 664 # clear environment on exit vacuum = true # Touch reload -touch-reload = /var/local/constellation/constellation/settings.py +touch-reload = {{ module_path }}/settings.py diff --git a/roles/constellation/tasks/main.yml b/roles/constellation/tasks/main.yml index ec39f4a8..48aefa99 100644 --- a/roles/constellation/tasks/main.yml +++ b/roles/constellation/tasks/main.yml @@ -28,9 +28,23 @@ name: - git+https://gitlab.adm.crans.org/nounous/django-dnsmanager.git +- name: Set configuration directories in development mode + when: constellation.version != "master" + set_fact: + config_file: "/var/local/constellation/constellation/settings_local.py" + config_dir: "/var/local/constellation" + project_path: "/var/local/constellation" + +- name: Set configuration directories in production mode + when: constellation.version == "master" + set_fact: + config_file: "/etc/constellation/settings_local.py" + config_dir: "/etc/constellation" + project_path: "/usr/local/lib/python3.9/dist-packages/constellation" + - name: Create constellation directory file: - path: /var/local/constellation + path: "{{ config_dir }}" state: directory mode: '2775' owner: "{{ constellation.owner }}" @@ -38,7 +52,7 @@ - name: Set ACL for constellation directory acl: - path: /var/local/constellation + path: "{{ config_dir }}" default: true entity: nounou etype: group @@ -46,17 +60,25 @@ state: query ignore_errors: "{{ ansible_check_mode }}" -- name: Clone constellation repository +- name: Clone constellation repository (development) + when: constellation.version != "master" git: repo: 'https://gitlab.adm.crans.org/nounous/constellation.git' - dest: /var/local/constellation + dest: "{{ project_path }}" umask: '002' version: "{{ constellation.version }}" recursive: true +- name: Install constellation (production) + when: constellation.version == "master" + pip: + name: + - git+https://gitlab.adm.crans.org/nounous/constellation.git + - name: Set owner of cloned project + when: constellation.version != "master" file: - path: /var/local/constellation + path: "{{ project_path }}" owner: "{{ constellation.owner }}" group: "{{ constellation.group }}" recurse: true @@ -64,35 +86,42 @@ - name: Deploy Constellation settings_local.py template: src: constellation/settings_local.py.j2 - dest: /var/local/constellation/constellation/settings_local.py + dest: "{{ config_file }}" mode: 0660 owner: "{{ constellation.settings_local_owner }}" group: "{{ constellation.settings_local_group }}" +- name: Symlink configuration file (production) + when: constellation.version == "master" + file: + src: "{{ config_file }}" + dest: "{{ project_path }}/settings_local.py" + state: link + - name: Compile messages when: not constellation.front django_manage: command: compilemessages - project_path: "/var/local/constellation" + project_path: "{{ project_path }}" # In the future, migrations will be included in the repository. - name: Make Django migrations (non-front app) when: not constellation.front django_manage: command: makemigrations - project_path: "/var/local/constellation" + project_path: "{{ project_path }}" - name: Migrate database (non-front app) when: not constellation.front django_manage: command: migrate - project_path: "/var/local/constellation" + project_path: "{{ project_path }}" - name: Load initial data (non-front app) when: not constellation.front django_manage: command: loaddata initial - project_path: "/var/local/constellation" + project_path: "{{ project_path }}" - name: Indicate constellation in motd template: diff --git a/roles/constellation/templates/constellation/settings_local.py.j2 b/roles/constellation/templates/constellation/settings_local.py.j2 index d4c6a79c..a396676f 100644 --- a/roles/constellation/templates/constellation/settings_local.py.j2 +++ b/roles/constellation/templates/constellation/settings_local.py.j2 @@ -38,6 +38,14 @@ DATABASES = { }, } +{% if constellation.version == "master" %} +{% if constellation.front %} +STATIC_ROOT = "/var/lib/constellation/static/" + +{% endif %} +MEDIA_ROOT = "/var/lib/constellation/media/" +{% endif %} + # The mail configuration for Constellation to send mails EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_USE_SSL = {{ constellation.email.ssl }} diff --git a/roles/constellation/templates/update-motd.d/05-service.j2 b/roles/constellation/templates/update-motd.d/05-service.j2 index a13717c8..dee40fbe 100755 --- a/roles/constellation/templates/update-motd.d/05-service.j2 +++ b/roles/constellation/templates/update-motd.d/05-service.j2 @@ -1,3 +1,3 @@ #!/usr/bin/tail +14 {{ ansible_header | comment }} -> Constellation a été déployé sur cette machine. Voir /var/www/constellation/. +> Constellation a été déployé sur cette machine. Voir {{ project_path }}/.