Adding backup action to prometheus charm 37/10437/6
authorsousaedu <eduardo.sousa@canonical.com>
Wed, 3 Mar 2021 00:12:49 +0000 (01:12 +0100)
committersousaedu <eduardo.sousa@canonical.com>
Mon, 5 Apr 2021 07:36:33 +0000 (09:36 +0200)
Change-Id: Icfc97e308c7d49eb1e7c5c18b6f21648822cba87
Signed-off-by: sousaedu <eduardo.sousa@canonical.com>
installers/charm/prometheus/actions.yaml [new file with mode: 0644]
installers/charm/prometheus/config.yaml
installers/charm/prometheus/metadata.yaml
installers/charm/prometheus/requirements.txt
installers/charm/prometheus/src/charm.py

diff --git a/installers/charm/prometheus/actions.yaml b/installers/charm/prometheus/actions.yaml
new file mode 100644 (file)
index 0000000..e41f3df
--- /dev/null
@@ -0,0 +1,23 @@
+# Copyright 2021 Canonical Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: legal@canonical.com
+#
+# To get in touch with the maintainers, please contact:
+# osm-charmers@lists.launchpad.net
+##
+
+backup:
+  description: "Do a mongodb backup"
index d953de3..9f35e51 100644 (file)
@@ -55,4 +55,4 @@ options:
   enable_web_admin_api:
     type: boolean
     description: Boolean to enable the web admin api
-    default: False
+    default: false
index f021418..0e09c3b 100644 (file)
@@ -34,6 +34,10 @@ deployment:
   type: stateful
   service: cluster
 resources:
+  backup-image:
+    type: oci-image
+    description: Container image to run backup actions
+    upstream-source: "ed1000/prometheus-backup:latest"
   image:
     type: oci-image
     description: OSM docker image for Prometheus
index 1a8928c..e1150c5 100644 (file)
@@ -19,4 +19,8 @@
 # osm-charmers@lists.launchpad.net
 ##
 
-git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
\ No newline at end of file
+git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master
+git+https://github.com/juju-solutions/resource-oci-image/@c5778285d332edf3d9a538f9d0c06154b7ec1b0b#egg=oci-image
+ops
+requests
+urllib3>1.25.9
index 47533dd..5cd163d 100755 (executable)
@@ -27,6 +27,7 @@ import logging
 from typing import NoReturn, Optional
 from urllib.parse import urlparse
 
+from oci_image import OCIImageResource
 from ops.framework import EventBase
 from ops.main import main
 from opslib.osm.charm import CharmedOsmBase
@@ -41,6 +42,7 @@ from opslib.osm.validator import (
     ModelValidator,
     validator,
 )
+import requests
 
 
 logger = logging.getLogger(__name__)
@@ -99,9 +101,24 @@ class PrometheusCharm(CharmedOsmBase):
             self._publish_prometheus_info,
         )
 
+        # Registering actions
+        self.framework.observe(
+            self.on.backup_action,  # pylint: disable=E1101
+            self._on_backup_action,
+        )
+
     def _publish_prometheus_info(self, event: EventBase) -> NoReturn:
         self.prometheus.publish_info(self.app.name, PORT)
 
+    def _on_backup_action(self, event: EventBase) -> NoReturn:
+        url = f"http://{self.model.app.name}:{PORT}/api/v2/admin/tsdb/snapshot"
+        result = requests.post(url)
+
+        if result.status_code == 200:
+            event.set_results({"backup-name": result.json()["name"]})
+        else:
+            event.fail(f"status-code: {result.status_code}, result: {result.json()}")
+
     def _build_files(self, config: ConfigModel):
         files_builder = FilesV3Builder()
         files_builder.add_file(
@@ -128,6 +145,15 @@ class PrometheusCharm(CharmedOsmBase):
         config = ConfigModel(**dict(self.config))
         # Create Builder for the PodSpec
         pod_spec_builder = PodSpecV3Builder()
+
+        # Build Backup Container
+        backup_image = OCIImageResource(self, "backup-image")
+        backup_image_info = backup_image.fetch()
+        backup_container_builder = ContainerV3Builder("prom-backup", backup_image_info)
+        backup_container = backup_container_builder.build()
+        # Add backup container to pod spec
+        pod_spec_builder.add_container(backup_container)
+
         # Build Container
         container_builder = ContainerV3Builder(self.app.name, image_info)
         container_builder.add_port(name=self.app.name, port=PORT)