Adding an ability to cleanup database from stale records.
Prod-Related: PROD-35183
Change-Id: I5676b1ee5a181b18753ae195f0994702f50a3579
diff --git a/README.rst b/README.rst
index 0b41d13..daae157 100644
--- a/README.rst
+++ b/README.rst
@@ -396,3 +396,36 @@
| | operations, verify do not have dead network |
| | agents/compute services) |
+-------------------------------+------------------------------------------------------+
+
+Execute database maintenance tasks
+----------------------------------
+Cleanup stale records from heat database to make it smaller.
+This is helpful before any upgrade activity.
+It is safe to execute it generally without maintenance window same as online db_sync.
+
+Enable this pillar:
+
+.. code-block:: yaml
+
+ heat:
+ server:
+ db_purge:
+ enabled: True
+
+Execute state heat.db.db_cleanup to purge stale records:
+
+.. code-block:: bash
+
+ salt -C 'I@heat:server:role:primary' state.apply heat.db.db_cleanup -l debug
+
+It is possible to pass days parameter.
+If you skip setting it, all records would be archived/purged:
+
+.. code-block:: yaml
+
+ heat:
+ server:
+ db_purge:
+ enabled: True
+ days: 45
+
diff --git a/heat/db/db_cleanup.sls b/heat/db/db_cleanup.sls
new file mode 100644
index 0000000..f227de1
--- /dev/null
+++ b/heat/db/db_cleanup.sls
@@ -0,0 +1,27 @@
+{%- from "heat/map.jinja" import server with context %}
+
+{%- if server.get('db_purge', {}).get('enabled', False) %}
+
+{%- set cmd_args = [] %}
+{%- if server.db_purge.days is defined %}
+{%- do cmd_args.append('-g') %}
+{%- do cmd_args.append('days') %}
+{%- endif %}
+
+{%- if server.db_purge.project_id is defined %}
+{%- do cmd_args.append('-p') %}
+{%- do cmd_args.append(server.db_purge.project_id|string) %}
+{%- endif %}
+
+{%- if server.db_purge.days is defined %}
+{%- do cmd_args.append(server.db_purge.days|string) %}
+{%- endif %}
+heat_db_clean:
+ cmd.run:
+ - name: "heat-manage purge_deleted {{ cmd_args|join(' ') }}"
+ - runas: 'heat'
+ {%- if grains.get('noservices') or server.get('role', 'primary') == 'secondary' %}
+ - onlyif: /bin/false
+ {%- endif %}
+
+{%- endif %}