From 5dc19fd9448bc53ada269434002b254ca0b2070e Mon Sep 17 00:00:00 2001
From: Roberto di Lallo <roberto.dilallo@garr.it>
Date: Mon, 10 Dec 2018 16:47:02 +0100
Subject: [PATCH] 2018-12-10:  FG;  Allow file-based incoming storage, create
 relevant filesystem in RAM.

---
 config.yaml                    |  5 +++++
 lib/charm/openstack/gnocchi.py | 32 ++++++++++++++++++++++++++++++++
 templates/gnocchi.conf         |  6 ++++++
 templates/queens/gnocchi.conf  |  6 ++++++
 4 files changed, 49 insertions(+)

diff --git a/config.yaml b/config.yaml
index f749eeec..903d418c 100644
--- a/config.yaml
+++ b/config.yaml
@@ -12,6 +12,11 @@
     "default": !!bool "false"
     "description": |
       Setting this to True will allow supporting services to log to syslog.
+  "incomingtofile":
+    "type": "boolean"
+    "default": !!bool "false"
+    "description": |
+      Use file driver for incoming measures.
   "use-internal-endpoints":
     "type": "boolean"
     "default": !!bool "false"
diff --git a/lib/charm/openstack/gnocchi.py b/lib/charm/openstack/gnocchi.py
index 034c2c5a..3e32802e 100644
--- a/lib/charm/openstack/gnocchi.py
+++ b/lib/charm/openstack/gnocchi.py
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import errno
 import os
 import pwd
 import grp
@@ -84,6 +85,37 @@ def ceph_config(config):
         return CEPH_CONF
 
 
+@charms_openstack.adapters.config_property
+def file_pathbase(config):
+    file_pathbasedir = '/var/lib/gnocchi/measures'
+    # create path if it does not exist
+    try:
+        os.makedirs(file_pathbasedir)
+    except OSError as exc:
+        if exc.errno == errno.EEXIST and os.path.isdir(file_pathbasedir):
+            pass
+        else:
+            raise
+    usr = GnocchiCharm.user
+    gru = GnocchiCharm.group
+    uid = pwd.getpwnam(usr).pw_uid
+    gid = grp.getgrnam(gru).gr_gid
+    os.chown(file_pathbasedir, uid, gid)
+    # mount tmpfs unless already mounted
+    do_mount = 1
+    cmd_out = subprocess.check_output(['cat', '/proc/mounts'])
+    lines = cmd_out.split('\n')
+    for line in lines:
+        if file_pathbasedir in line:
+            do_mount = 0
+            break
+    tmpfs_size = 'size=128m'
+    if do_mount == 1:
+        subprocess.check_call(['mount', '-t', 'tmpfs', '-o', tmpfs_size,
+                               'tmpfs', file_pathbasedir])
+    return file_pathbasedir
+
+
 # TODO(jamespage): charms.openstack
 class StorageCephRelationAdapter(adapters.OpenStackRelationAdapter):
 
diff --git a/templates/gnocchi.conf b/templates/gnocchi.conf
index 2e5f4097..66f94fd8 100644
--- a/templates/gnocchi.conf
+++ b/templates/gnocchi.conf
@@ -21,6 +21,12 @@ url = {{ shared_db.uri }}
 [metricd]
 workers = {{ options.workers }}
 
+{% if options.incomingtofile -%}
+[incoming]
+driver = file
+file_basepath = {{ options.file_pathbase }}
+{%- endif %}
+
 [storage]
 {% if coordinator_memcached.url -%}
 coordination_url = {{ coordinator_memcached.url }}
diff --git a/templates/queens/gnocchi.conf b/templates/queens/gnocchi.conf
index bf609148..9369c4cc 100644
--- a/templates/queens/gnocchi.conf
+++ b/templates/queens/gnocchi.conf
@@ -25,6 +25,12 @@ url = {{ shared_db.uri }}
 [metricd]
 workers = {{ options.workers }}
 
+{% if options.incomingtofile -%}
+[incoming]
+driver = file
+file_basepath = /var/lib/gnocchi/measures/
+{%- endif %}
+
 [storage]
 {% if storage_ceph.key -%}
 driver = ceph
-- 
GitLab