Implement alarms history cleanup via aodh-expirer

Added ability to create cron for aodh-expirer launch.
By default expirer will be launched on hourly basis.

Change-Id: I0a68cc4038e92932ca3fa97c6b6a137b9e0d168c
Related-Prod: https://mirantis.jira.com/browse/PROD-21307
diff --git a/README.rst b/README.rst
index c6204e1..3a69669 100644
--- a/README.rst
+++ b/README.rst
@@ -53,6 +53,23 @@
         - host: 10.10.10.12
             port: 11211
 
+Setting alarm history cleanup
+
+In order to allow alarm cleanup from one node of the cluster,
+server:role field should be set to primary and all others to
+secondaey to avoid race conditions. On the example below
+expirer is set to run every day at 2:00 AM. By default
+it will be run every hour.
+
+.. code-block:: yaml
+
+    aodh:
+      server:
+        role: primary
+        expirer:
+          cron:
+            minute: 0
+            hour: 2
 
 Enhanced logging with logging.conf
 ----------------------------------
diff --git a/aodh/map.jinja b/aodh/map.jinja
index 08a214f..b1df315 100644
--- a/aodh/map.jinja
+++ b/aodh/map.jinja
@@ -1,5 +1,12 @@
 
 {% set server = salt['grains.filter_by']({
+    'default': {
+        'expirer': {
+          'cron': {
+            'minute': 0
+          },
+        },
+    },
     'Debian': {
         'pkgs': ['aodh-api', 'aodh-evaluator', 'aodh-notifier', 'aodh-listener', 'python-aodhclient'],
         'services': ['aodh-evaluator', 'aodh-notifier', 'aodh-listener'],
@@ -40,7 +47,7 @@
           }
         },
     },
-}, merge=salt['pillar.get']('aodh:server')) %}
+}, merge=salt['pillar.get']('aodh:server'), base='default') %}
 
 {%- if salt['pillar.get']('aodh:server:version', 'mitaka') in ['mitaka'] %}
     {%- do server.update({'services': server.services + server.webserved}) %}
diff --git a/aodh/server.sls b/aodh/server.sls
index 3d6a024..fdf0186 100644
--- a/aodh/server.sls
+++ b/aodh/server.sls
@@ -101,6 +101,29 @@
     - file: /etc/aodh/aodh.conf
     - pkg: aodh_server_packages
 
+{%- if server.get('role', 'secondary') == 'primary' %}
+{%- set cron = server.expirer.cron %}
+aodh_expirer_cron:
+  cron.present:
+    - name: /usr/bin/aodh-expirer
+    - user: aodh
+    - minute: '{{ cron.minute }}'
+    {%- if cron.hour is defined %}
+    - hour: '{{ cron.hour }}'
+    {%- endif %}
+    {%- if cron.daymonth is defined %}
+    - daymonth: '{{ cron.daymonth }}'
+    {%- endif %}
+    {%- if cron.month is defined %}
+    - month: '{{ cron.month }}'
+    {%- endif %}
+    {%- if cron.dayweek is defined %}
+    - dayweek: '{{ cron.dayweek }}'
+    {%- endif %}
+    - require:
+      - file: /etc/aodh/aodh.conf
+{%- endif %}
+
 # for Newton and newer
 {%- if server.version not in ['mitaka'] %}