Adding new state which will be used for db cleanup.
Prod-Related: PROD-35183
Change-Id: I8ee6b33eae01cadcd7e5b72fa0051d1f4775f577
diff --git a/README.rst b/README.rst
index f51f533..8e7385e 100644
--- a/README.rst
+++ b/README.rst
@@ -1120,3 +1120,37 @@
cinder:
upgrade:
manage_service_maintenance: false
+
+
+Execute database maintenance tasks
+----------------------------------
+Cleanup stale records from cinder 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
+
+ cinder:
+ controller:
+ db_purge:
+ enabled: True
+
+Execute state cinder.db.db_cleanup to purge stale records:
+
+.. code-block:: bash
+
+ salt -C 'I@cinder:controller:role:primary' state.apply cinder.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
+
+ cinder:
+ controller:
+ db_purge:
+ enabled: True
+ days: 45
+
diff --git a/cinder/db/db_cleanup.sls b/cinder/db/db_cleanup.sls
new file mode 100644
index 0000000..cc0fae7
--- /dev/null
+++ b/cinder/db/db_cleanup.sls
@@ -0,0 +1,18 @@
+{%- from "cinder/map.jinja" import controller with context %}
+
+{%- if controller.get('db_purge', {}).get('enabled', False) %}
+
+{%- if controller.db_purge.days is defined %}
+{%- set cmd_args = controller.db_purge.days|string %}
+{%- else %}
+{%- set cmd_args = '0' %}
+{%- endif %}
+
+cinder_db_clean:
+ cmd.run:
+ - name: cinder-manage db purge {{ cmd_args }}
+ - runas: 'cinder'
+ {%- if grains.get('noservices') or controller.get('role', 'primary') == 'secondary' %}
+ - onlyif: /bin/false
+ {%- endif %}
+{%- endif %}