Ejemplo n.º 1
0
    def _on_config_changed(self, event: HookEvent) -> None:
        """Handle the pebble_ready event for the influxdb2 container."""

        container = self.unit.get_container(WORKLOAD_CONTAINER)
        try:
            plan = container.get_plan().to_dict()
        except (APIError, ConnectionError) as error:
            logger.debug(
                f"The Pebble API is not ready yet. Error message: {error}")
            event.defer()
            return

        logger.debug(f"[*] container plan => {plan}")
        pebble_config = Layer(raw=self._influxdb2_layer())
        # If there's no new config, do nothing
        if plan.get("services", {}) == pebble_config.to_dict()["services"]:
            logger.debug(
                "Pebble plan has already been loaded. No need to update the config."
            )
            return

        try:
            # Add out initial config layer
            container.add_layer("influxdb2", pebble_config, combine=True)
        except (APIError, ConnectionError) as error:
            logger.debug(
                f"The Pebble API is not ready yet. Error message: {error}")
            event.defer()
            return

        # If the service is INACTIVE, then skip this step
        if self._is_running(container, WORKLOAD_CONTAINER):  # pragma: no cover
            container.stop(WORKLOAD_CONTAINER)
        container.start(WORKLOAD_CONTAINER)
        self.unit.status = ActiveStatus("Pod is ready")
Ejemplo n.º 2
0
    def _build_layer(self) -> Layer:
        """Construct the pebble layer information"""
        # Placeholder for when we add "proper" mysql support for HA
        dbinfo = {"GF_DATABASE_TYPE": "sqlite3"}

        layer = Layer({
            "summary": "grafana-k8s layer",
            "description": "grafana-k8s layer",
            "services": {
                self.name: {
                    "override": "replace",
                    "summary": "grafana-k8s service",
                    "command": "grafana-server -config {}".format(CONFIG_PATH),
                    "startup": "enabled",
                    "environment": {
                        "GF_SERVER_HTTP_PORT": self.model.config["port"],
                        "GF_LOG_LEVEL": self.model.config["log_level"],
                        "GF_PATHS_PROVISIONING": DATASOURCE_PATH,
                        "GF_SECURITY_ADMIN_USER":
                        self.model.config["admin_user"],
                        **dbinfo,
                    },
                }
            },
        })

        return layer
Ejemplo n.º 3
0
    def _on_kafka_pebble_ready(self, event: PebbleReadyEvent) -> None:
        container = self.unit.get_container(SERVICE)
        logger.info("_on_kafka_pebble_ready")

        logger.info("_start_kafka")
        layer = Layer(raw=self._kafka_layer())
        container.add_layer(SERVICE, layer, combine=True)
        container.autostart()
        self.unit.status = ActiveStatus("kafka started")
Ejemplo n.º 4
0
    def _build_layer(self) -> Layer:
        """Construct the pebble layer information."""
        # Placeholder for when we add "proper" mysql support for HA
        extra_info = {
            "GF_DATABASE_TYPE": "sqlite3",
        }

        grafana_path = self.model.config.get("web_external_url", "")

        # We have to do this dance because urlparse() doesn't have any good
        # truthiness, and parsing an empty string is still 'true'
        if grafana_path:
            parts = self._parse_grafana_path(urlparse(grafana_path))

            # It doesn't matter unless there's a subpath, since the
            # redirect to login is fine with a bare hostname
            if parts and parts["path"]:
                extra_info.update(
                    {
                        "GF_SERVER_SERVE_FROM_SUB_PATH": "True",
                        "GF_SERVER_ROOT_URL": "{}://{}:{}{}".format(
                            parts["scheme"], parts["host"], parts["port"], parts["path"]
                        ),
                    }
                )

        layer = Layer(
            {
                "summary": "grafana-k8s layer",
                "description": "grafana-k8s layer",
                "services": {
                    self.name: {
                        "override": "replace",
                        "summary": "grafana-k8s service",
                        "command": "grafana-server -config {}".format(CONFIG_PATH),
                        "startup": "enabled",
                        "environment": {
                            "GF_SERVER_HTTP_PORT": PORT,
                            "GF_LOG_LEVEL": self.model.config["log_level"],
                            "GF_PATHS_PROVISIONING": PROVISIONING_PATH,
                            "GF_SECURITY_ADMIN_USER": self.model.config["admin_user"],
                            "GF_SECURITY_ADMIN_PASSWORD": self._get_admin_password(),
                            **extra_info,
                        },
                    }
                },
            }
        )

        return layer
Ejemplo n.º 5
0
 def _tester_pebble_layer(self):
     """Generate Prometheus tester pebble layer."""
     layer_spec = {
         "summary": "prometheus tester",
         "description": "a test data generator for Prometheus",
         "services": {
             self._name: {
                 "override": "replace",
                 "summary": "metrics exporter service",
                 "command": "python /metrics.py",
                 "startup": "enabled",
             }
         },
     }
     return Layer(layer_spec)
Ejemplo n.º 6
0
    def _discourse_layer(self):
        layer = Layer(
            raw = {
            "summary": "discourse layer",
            "description": "discourse layer",
            "services": {
                "discourse": {
                    "override": "replace",
                    "summary": "discourse service",
                    "command": "nami start --foreground discourse",
                    "startup": "enabled",
                    "environment": []
                }
            },
        })

        return layer
Ejemplo n.º 7
0
    def _cassandra_layer(self):
        layer = Layer(
            raw={
                "summary": "cassandra layer",
                "description": "cassandra layer",
                "services": {
                    "cassandra": {
                        "override": "replace",
                        "summary": "cassandra service",
                        "command": "sh -c docker-entrypoint.sh",
                        "startup": "enabled",
                        "environment": []
                    }
                },
            })

        return layer
Ejemplo n.º 8
0
    def _prometheus_layer(self) -> Layer:
        """Construct the pebble layer.

        Returns:
            a Pebble layer specification for the Prometheus workload container.
        """
        logger.debug("Building pebble layer")
        layer_config = {
            "summary": "Prometheus layer",
            "description": "Pebble layer configuration for Prometheus",
            "services": {
                self._name: {
                    "override": "replace",
                    "summary": "prometheus daemon",
                    "command": self._command(),
                    "startup": "enabled",
                }
            },
        }

        return Layer(layer_config)