From 552c45df06287817108bcccdf2146fa5e71e7349 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Tue, 26 Nov 2019 17:51:35 +0100
Subject: [PATCH 2/2] run chrono and combo in python3 (#36894)

---
 clean.yml                                     |  7 ++-
 group_vars/all                                |  1 +
 install.yml                                   |  3 +-
 install_source.yml                            |  4 +-
 roles/app-setup/tasks/main.yml                |  4 +-
 .../app-setup/templates/app-settings-base.j2  |  6 +-
 roles/app-setup/templates/combo-settings.j2   |  1 -
 roles/authentic/vars/main.yml                 |  1 +
 roles/base/tasks/main.yml                     | 60 +++++++++++++++----
 roles/base/templates/getlasso.j2              |  4 +-
 roles/bijoe/vars/main.yml                     |  1 +
 roles/chrono/vars/main.yml                    |  3 +-
 roles/combo/vars/main.yml                     |  3 +-
 roles/fargo/vars/main.yml                     |  1 +
 .../templates/hobo-agent-settings.j2          | 18 ++++--
 roles/hobo/tasks/main.yml                     |  6 +-
 roles/hobo/vars/main.yml                      |  1 +
 roles/passerelle/vars/main.yml                |  1 +
 roles/wcs/vars/main.yml                       |  1 +
 roles/welco/vars/main.yml                     |  1 +
 20 files changed, 95 insertions(+), 32 deletions(-)

diff --git a/clean.yml b/clean.yml
index 97f121d..ffaedc1 100644
--- a/clean.yml
+++ b/clean.yml
@@ -1,10 +1,13 @@
 ---
 - hosts: local
   tasks:
-    - name: delete venv
+    - name: delete python venvs
       file:
-        path: "{{venv}}"
+        path: "{{item}}"
         state: absent
+      loop:
+        - "{{venv}}"
+        - "{{venv_py3}}"
 
     - name: delete app /var/lib directory
       file:
diff --git a/group_vars/all b/group_vars/all
index 12c1df1..6354478 100644
--- a/group_vars/all
+++ b/group_vars/all
@@ -8,6 +8,7 @@ src_dir: "/home/{{user}}/src"
 ssl_certificate: "{{venv_certs}}/dev.publik.love-fullchain.pem"
 ssl_certificate_key: "{{venv_certs}}/dev.publik.love-privkey.pem"
 venv: "/home/{{user}}/envs/publik-env"
+venv_py3: "/home/{{user}}/envs/publik-env-py3"
 user: your_user_name
 apps:
   authentic:
diff --git a/install.yml b/install.yml
index 85789bd..f00e640 100644
--- a/install.yml
+++ b/install.yml
@@ -18,7 +18,8 @@
   vars:
     themes_dir: "{{venv}}/themes"
     venv_bin: "{{venv}}/bin"
+    venv_py3_bin: "{{venv_py3}}/bin"
     venv_certs: "{{venv}}/certs"
-    venv_pip: "{{venv_bin}}/pip"
     venv_python: "{{venv_bin}}/python"
+    venv_py3_python: "{{venv_py3_bin}}/python"
     venv_share: "{{venv}}/share"
diff --git a/install_source.yml b/install_source.yml
index efa60b8..a152450 100644
--- a/install_source.yml
+++ b/install_source.yml
@@ -20,9 +20,9 @@
     name: django
     version: "{{ django_version}}"
     extra_args: "-e {{ repo_dir }}"
-    virtualenv: "{{ venv }}"
+    virtualenv: "{{ virtualenv }}"
 
 - name: "build {{ repo_dir }}"
-  command: "{{ venv_python }} setup.py build"
+  command: "{{ virtualenv }}/bin/python setup.py build"
   args:
     chdir: "{{ repo_dir }}"
diff --git a/roles/app-setup/tasks/main.yml b/roles/app-setup/tasks/main.yml
index bf2d93a..eae2313 100644
--- a/roles/app-setup/tasks/main.yml
+++ b/roles/app-setup/tasks/main.yml
@@ -2,7 +2,7 @@
   set_fact:
     app_settings: "{{publik_settings}}/{{project_name}}/settings.py"
     app_settings_dir: "{{publik_settings}}/{{project_name}}"
-    manage_app_bin: "{{venv_bin}}/{{project_name}}-manage"
+    manage_app_bin: "{{virtualenv}}/bin/{{project_name}}-manage"
     server_app_name: "{{project_name}}"
 
 - name: "{{app_name}} - create db"
@@ -90,7 +90,7 @@
 - name: "{{app_name}} - server script"
   template:
     src: app-server.j2
-    dest: "{{venv_bin}}/{{server_app_name}}-server"
+    dest: "{{virtualenv}}/bin/{{server_app_name}}-server"
     mode: "u=rwx,g=rx,o=rx"
   
 - name: "{{app_name}} - server log directory"
diff --git a/roles/app-setup/templates/app-settings-base.j2 b/roles/app-setup/templates/app-settings-base.j2
index e678fbd..48ec1f8 100644
--- a/roles/app-setup/templates/app-settings-base.j2
+++ b/roles/app-setup/templates/app-settings-base.j2
@@ -6,11 +6,13 @@
 import glob
 import os
 
+from django.utils import six
+
 {% block app_custom_before %}{% endblock %}
 
 PROJECT_NAME = "{{project_name}}"
 
-execfile('{{src_dir}}/hobo/debian/debian_config_common.py')
+exec(open('{{src_dir}}/hobo/debian/debian_config_common.py').read())
 
 THEMES_DIRECTORY = '{{themes_dir}}'
 
@@ -49,4 +51,4 @@ DEBUG = True
 
 for filename in sorted(
     glob.glob(os.path.join('{{app_settings_dir}}', 'settings.d', '*.py'))):
-    execfile(filename)
+    exec(open(filename).read())
diff --git a/roles/app-setup/templates/combo-settings.j2 b/roles/app-setup/templates/combo-settings.j2
index 23f9a24..f7a8db0 100644
--- a/roles/app-setup/templates/combo-settings.j2
+++ b/roles/app-setup/templates/combo-settings.j2
@@ -5,5 +5,4 @@
 INSTALLED_APPS = ('hobo.agent.combo', ) + INSTALLED_APPS
 # add statics_hash context processor
 TEMPLATES[0]['OPTIONS']['context_processors'].append('hobo.context_processors.statics_hash')
-{% include 'middleware_class_debug_toolbar.j2' %}
 {% endblock %}
diff --git a/roles/authentic/vars/main.yml b/roles/authentic/vars/main.yml
index 537badf..b00749d 100644
--- a/roles/authentic/vars/main.yml
+++ b/roles/authentic/vars/main.yml
@@ -8,3 +8,4 @@ nginx_template_name: "std-nginx-server.j2"
 project_name: "{{apps['authentic']['project_name']}}"
 server_port: "{{apps['authentic']['server_port']}}"
 settings_env_var: AUTHENTIC2_SETTINGS_FILE
+virtualenv: "{{venv}}"
diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml
index b9f90f6..9e0500b 100644
--- a/roles/base/tasks/main.yml
+++ b/roles/base/tasks/main.yml
@@ -41,16 +41,23 @@
   become: yes
   become_user: postgres
 
-- name: create the virtualenv and install django
+- name: create the virtualenvs and install django
   pip:
     name: django
     version: "{{django_version}}"
-    virtualenv: "{{venv}}"
+    virtualenv: "{{item.venv}}"
+    virtualenv_python: "{{item.venv_python}}"
+  loop:
+    - { venv: '{{venv}}', venv_python: 'python2' }
+    - { venv: '{{venv_py3}}', venv_python: 'python3' }
 
 - name: install django-debug-toolbar
   pip:
     name: django-debug-toolbar
-    virtualenv: "{{venv}}"
+    virtualenv: "{{item}}"
+  loop:
+    - "{{venv}}"
+    - "{{venv_py3}}"
 
 - name: "create {{src_dir}} directory"
   file:
@@ -79,49 +86,78 @@
     path: "{{publik_settings}}"
     state: directory
 
-- name: create getlasso script
+- name: create getlasso scripts
   template:
     src: getlasso.j2
-    dest: "{{venv_bin}}/getlasso.sh"
+    dest: "{{item.dest}}/getlasso.sh"
     mode: "u=rwx,g=rx,o=rx"
+  vars:
+    virtualenv_python: "{{item.vp}}"
+    system_python: "{{item.sp}}"
+  loop:
+    - { vp: '{{venv_python}}', sp: '/usr/bin/python', dest: '{{venv_bin}}' }
+    - { vp: '{{venv_py3_python}}', sp: '/usr/bin/python3', dest: '{{venv_py3_bin}}' }
 
 - name: execute getlasso
-  command: "{{venv_bin}}/getlasso.sh"
+  command: "{{item}}/getlasso.sh"
+  loop:
+    - "{{venv_bin}}"
+    - "{{venv_py3_bin}}"
 
 - name: source install eopayment
-  import_tasks: install_source.yml
+  include_tasks: install_source.yml
   vars:
     repo: "eopayment"
     repo_dir: "{{ src_dir }}/eopayment"
+    virtualenv: "{{venv_py3}}"
 
 - name: source install django-tenant-schemas
-  import_tasks: install_source.yml
+  include_tasks: install_source.yml
   vars:
     repo: "debian/django-tenant-schemas"
     repo_dir: "{{ src_dir }}/django-tenant-schemas"
+    virtualenv: "{{item}}"
+  loop:
+    - "{{venv}}"
+    - "{{venv_py3}}"
 
 - name: source install django-mellon
-  import_tasks: install_source.yml
+  include_tasks: install_source.yml
   vars:
     repo: "django-mellon"
     repo_dir: "{{ src_dir }}/django-mellon"
+    virtualenv: "{{item}}"
+  loop:
+    - "{{venv}}"
+    - "{{venv_py3}}"
 
 - name: source install gadjo
-  import_tasks: install_source.yml
+  include_tasks: install_source.yml
   vars:
     repo: "gadjo"
     repo_dir: "{{ src_dir }}/gadjo"
+    virtualenv: "{{item}}"
+  loop:
+    - "{{venv}}"
+    - "{{venv_py3}}"
 
 - name: install python-memcached
   pip:
     name: python-memcached
-    virtualenv: "{{venv}}"
+    virtualenv: "{{item}}"
+  loop:
+    - "{{venv}}"
+    - "{{venv_py3}}"
 
 - name: source install django-ckeditor
-  import_tasks: install_source.yml
+  include_tasks: install_source.yml
   vars:
     repo: "debian/django-ckeditor"
     repo_dir: "{{ src_dir }}/django-ckeditor"
+    virtualenv: "{{item}}"
+  loop:
+    - "{{venv}}"
+    - "{{venv_py3}}"
 
 - name: install theme
   import_tasks: tasks/install_theme.yml
diff --git a/roles/base/templates/getlasso.j2 b/roles/base/templates/getlasso.j2
index 6040733..c2246b2 100644
--- a/roles/base/templates/getlasso.j2
+++ b/roles/base/templates/getlasso.j2
@@ -1,9 +1,9 @@
 #!/bin/sh
 
 # Get venv site-packages path
-DSTDIR=`{{venv_python}} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'`
+DSTDIR=`{{virtualenv_python}} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'`
 
-SRCDIR=`/usr/bin/python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'`
+SRCDIR=`{{system_python}} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'`
 
 # Clean up
 rm -f $DSTDIR/lasso.*
diff --git a/roles/bijoe/vars/main.yml b/roles/bijoe/vars/main.yml
index 3197c38..fc592dc 100644
--- a/roles/bijoe/vars/main.yml
+++ b/roles/bijoe/vars/main.yml
@@ -8,3 +8,4 @@ nginx_template_name: "std-nginx-server.j2"
 project_name: "{{apps['bijoe']['project_name']}}"
 server_port: "{{apps['bijoe']['server_port']}}"
 settings_env_var: BIJOE_SETTINGS_FILE
+virtualenv: "{{venv}}"
diff --git a/roles/chrono/vars/main.yml b/roles/chrono/vars/main.yml
index 466870e..cb2eba1 100644
--- a/roles/chrono/vars/main.yml
+++ b/roles/chrono/vars/main.yml
@@ -2,9 +2,10 @@ app_name: chrono
 compile_scss: false
 compile_translations: true
 db_name: "{{apps['chrono']['db_name']}}"
-manage_cmd: "{{venv_python}} {{src_dir}}/chrono/manage.py"
+manage_cmd: "{{venv_py3_python}} {{src_dir}}/chrono/manage.py"
 nginx_host_pattern: "{{apps['chrono']['nginx_host_pattern']}}"
 nginx_template_name: "chrono-nginx-server.j2"
 project_name: "{{apps['chrono']['project_name']}}"
 server_port: "{{apps['chrono']['server_port']}}"
 settings_env_var: CHRONO_SETTINGS_FILE
+virtualenv: "{{venv_py3}}"
diff --git a/roles/combo/vars/main.yml b/roles/combo/vars/main.yml
index 3d64ad2..3a3636c 100644
--- a/roles/combo/vars/main.yml
+++ b/roles/combo/vars/main.yml
@@ -2,9 +2,10 @@ app_name: combo
 compile_scss: false
 compile_translations: true
 db_name: "{{apps['combo']['db_name']}}"
-manage_cmd: "{{venv_python}} {{src_dir}}/combo/manage.py"
+manage_cmd: "{{venv_py3_python}} {{src_dir}}/combo/manage.py"
 nginx_host_pattern: "{{apps['combo']['nginx_host_pattern']}}"
 nginx_template_name: "std-nginx-server.j2"
 project_name: "{{apps['combo']['project_name']}}"
 server_port: "{{apps['combo']['server_port']}}"
 settings_env_var: COMBO_SETTINGS_FILE
+virtualenv: "{{venv_py3}}"
diff --git a/roles/fargo/vars/main.yml b/roles/fargo/vars/main.yml
index 8177c66..552a390 100644
--- a/roles/fargo/vars/main.yml
+++ b/roles/fargo/vars/main.yml
@@ -8,3 +8,4 @@ nginx_template_name: "fargo-nginx-server.j2"
 project_name: "{{apps['fargo']['project_name']}}"
 server_port: "{{apps['fargo']['server_port']}}"
 settings_env_var: FARGO_SETTINGS_FILE
+virtualenv: "{{venv}}"
diff --git a/roles/hobo-agent/templates/hobo-agent-settings.j2 b/roles/hobo-agent/templates/hobo-agent-settings.j2
index f53a510..76ac295 100644
--- a/roles/hobo-agent/templates/hobo-agent-settings.j2
+++ b/roles/hobo-agent/templates/hobo-agent-settings.j2
@@ -1,3 +1,4 @@
+#jinja2: lstrip_blocks: True, trim_blocks: True
 BROKER_URL = 'amqp://'
 
 
@@ -8,11 +9,18 @@ AGENT_HOST_PATTERNS = {
 }
 
 {% for appkey, app in apps.items() %}
-{% if appkey != 'wcs' %}
-{% filter upper %}{{appkey}}{% endfilter %}_MANAGE_COMMAND = "{{venv_bin}}/{{app['project_name']}}-manage"
-{% filter upper %}{{appkey}}{% endfilter %}_MANAGE_TRY_COMMAND = {% filter upper %}{{appkey}}{% endfilter %}_MANAGE_COMMAND
-{% else %}
+  {% if appkey == 'wcs' %}
 WCS_MANAGE_COMMAND = "{{venv_bin}}/wcsctl.py -f {{publik_settings}}/wcs/wcs.cfg"
 WCS_MANAGE_TRY_COMMAND = "{{venv_bin}}/wcsctl.py"
-{% endif %}
+  {% else %}
+    {% if appkey in ('combo', 'chrono') %}
+    {# applications en python3 #}
+{% filter upper %}{{appkey}}{% endfilter %}_MANAGE_COMMAND = "{{venv_py3_bin}}/{{app['project_name']}}-manage"
+    {% else %}
+    {# cas général, applications Django toutjours en python2, devra disparaître #}
+{% filter upper %}{{appkey}}{% endfilter %}_MANAGE_COMMAND = "{{venv_bin}}/{{app['project_name']}}-manage"
+    {% endif %}
+{% filter upper %}{{appkey}}{% endfilter %}_MANAGE_TRY_COMMAND = {% filter upper %}{{appkey}}{% endfilter %}_MANAGE_COMMAND
+   {% endif %}
+
 {% endfor %}
diff --git a/roles/hobo/tasks/main.yml b/roles/hobo/tasks/main.yml
index f53fe87..e995a67 100644
--- a/roles/hobo/tasks/main.yml
+++ b/roles/hobo/tasks/main.yml
@@ -1,8 +1,12 @@
 - name: source install hobo
-  import_tasks: install_source.yml
+  include_tasks: install_source.yml
   vars:
     repo: "hobo"
     repo_dir: "{{ src_dir }}/hobo"
+    virtualenv: "{{item}}"
+  loop:
+    - "{{venv}}"
+    - "{{venv_py3}}"
 
 - name: hobo app setup
   import_role:
diff --git a/roles/hobo/vars/main.yml b/roles/hobo/vars/main.yml
index 95bc168..98ec1ac 100644
--- a/roles/hobo/vars/main.yml
+++ b/roles/hobo/vars/main.yml
@@ -8,3 +8,4 @@ nginx_template_name: "std-nginx-server.j2"
 project_name: "{{apps['hobo']['project_name']}}"
 server_port: "{{apps['hobo']['server_port']}}"
 settings_env_var: HOBO_SETTINGS_FILE
+virtualenv: "{{venv}}"
diff --git a/roles/passerelle/vars/main.yml b/roles/passerelle/vars/main.yml
index 1657b79..8818b4f 100644
--- a/roles/passerelle/vars/main.yml
+++ b/roles/passerelle/vars/main.yml
@@ -8,3 +8,4 @@ nginx_template_name: "std-nginx-server.j2"
 project_name: "{{apps['passerelle']['project_name']}}"
 server_port: "{{apps['passerelle']['server_port']}}"
 settings_env_var: PASSERELLE_SETTINGS_FILE
+virtualenv: "{{venv}}"
diff --git a/roles/wcs/vars/main.yml b/roles/wcs/vars/main.yml
index f811a4c..cc9ddbc 100644
--- a/roles/wcs/vars/main.yml
+++ b/roles/wcs/vars/main.yml
@@ -9,3 +9,4 @@ nginx_template_name: "wcs-nginx-server.j2"
 project_name: "{{apps['wcs']['project_name']}}"
 server_port: "{{apps['wcs']['server_port']}}"
 settings_env_var: WCS_SETTINGS_FILE
+virtualenv: "{{venv}}"
diff --git a/roles/welco/vars/main.yml b/roles/welco/vars/main.yml
index 1092bd7..e8dfa5a 100644
--- a/roles/welco/vars/main.yml
+++ b/roles/welco/vars/main.yml
@@ -8,3 +8,4 @@ nginx_template_name: "welco-nginx-server.j2"
 project_name: "{{apps['welco']['project_name']}}"
 server_port: "{{apps['welco']['server_port']}}"
 settings_env_var: WELCO_SETTINGS_FILE
+virtualenv: "{{venv}}"
-- 
2.20.1

