From 98ed47284a089dea8bf73673b641919419e0f191 Mon Sep 17 00:00:00 2001
From: Andrea Dell'Amico <adellam@isti.cnr.it>
Date: Tue, 20 Nov 2018 19:41:53 +0100
Subject: [PATCH] Configure CKAN to use memcached for its sessions. Must set a
 variable to enable the feature. See
 https://support.d4science.org/issues/12827

---
 ckan/ckan/tasks/ckan-memcache.yml | 44 +++++++++++++++++++++++++++++++
 ckan/ckan/tasks/ckan-plugins.yml  |  6 +++--
 ckan/ckan/tasks/main.yml          |  1 +
 3 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 ckan/ckan/tasks/ckan-memcache.yml

diff --git a/ckan/ckan/tasks/ckan-memcache.yml b/ckan/ckan/tasks/ckan-memcache.yml
new file mode 100644
index 00000000..1ee93b16
--- /dev/null
+++ b/ckan/ckan/tasks/ckan-memcache.yml
@@ -0,0 +1,44 @@
+---
+- block:
+  - name: Install the memcache library
+    pip: name=pylibmc virtualenv={{ ckan_virtenv }} state=present
+
+  become: True
+  become_user: '{{ ckan_shell_user }}'
+  when: ckan_memcache_sessions is defined and ckan_memcache_sessions
+  tags: [ 'ckan', 'ckan_sessions', 'ckan_memcache' ]
+
+- block:
+  - name: Install the memcache library
+    apt: pkg=libmemcached10 state=present update_cache=yes cache_valid_time=1800
+
+  - name: Configure CKAN so that it uses memcache for its sessions
+    ini_file: dest={{ ckan_config_file }} section={{ item.section }} option={{ item.option }} value={{ item.value }} state={{ item.state }}
+    with_items: '{{ ckan_memcache_ini_opts }}'
+    notify:
+      - Restart CKAN
+      - Restart fetch and gather consumers
+    tags: [ 'ckan', 'ckan_ini', 'ckan_plugins', 'ckan_sessions', 'ckan_memcache' ]
+
+  - name: Remove the CKAN session files
+    file: dest=/tmp/{{ ckan_site_id }} state=absent
+
+  become: False
+  when: ckan_memcache_sessions is defined and ckan_memcache_sessions
+  tags: [ 'ckan', 'ckan_sessions', 'ckan_memcache' ]
+
+- block:
+  - name: Configure CKAN to not use memcache for its sessions
+    ini_file: dest={{ ckan_config_file }} section={{ item.section }} option={{ item.option }} value={{ item.value }} state=absent
+    with_items: '{{ ckan_memcache_ini_opts }}'
+    notify:
+      - Restart CKAN
+      - Restart fetch and gather consumers
+    tags: [ 'ckan', 'ckan_ini', 'ckan_plugins', 'ckan_sessions', 'ckan_memcache' ]
+
+  become: False
+  when:
+    - ckan_memcache_sessions is defined
+    - not ckan_memcache_sessions
+  tags: [ 'ckan', 'ckan_sessions', 'ckan_memcache' ]
+
diff --git a/ckan/ckan/tasks/ckan-plugins.yml b/ckan/ckan/tasks/ckan-plugins.yml
index 3563a033..001c6a2a 100644
--- a/ckan/ckan/tasks/ckan-plugins.yml
+++ b/ckan/ckan/tasks/ckan-plugins.yml
@@ -1,11 +1,13 @@
 ---
 - block:
     - name: Install some packages dependencies
-      become: False
       apt: name={{ item }} state=latest update_cache=yes cache_valid_time=3600
       with_items: '{{ ckan_additional_packages }}'
-      tags: [ 'ckan', 'geonetwork', 'ckan_plugins', 'ckan_pip_deps' ]
 
+  become: False
+  tags: [ 'ckan', 'geonetwork', 'ckan_plugins', 'ckan_pip_deps' ]
+
+- block:
     - name: Install some python versioned plugins dependencies inside the CKAN virtualenv
       pip: name={{ item.name }} virtualenv={{ ckan_virtenv }} version={{ item.version }} state={{ item.state }}
       with_items: '{{ ckan_pip_versioned_dependencies }}'
diff --git a/ckan/ckan/tasks/main.yml b/ckan/ckan/tasks/main.yml
index 97c5acd1..cb5f8a92 100644
--- a/ckan/ckan/tasks/main.yml
+++ b/ckan/ckan/tasks/main.yml
@@ -1,6 +1,7 @@
 ---
 - import_tasks: ckan.yml
 - import_tasks: ckan-plugins.yml
+- import_tasks: ckan-memcache.yml
 - import_tasks: ckan-config.yml
 - import_tasks: enable-ckan.yml
 - import_tasks: supervisor.yml