Files
charm-ceph-nfs/src/charm.py
Chris MacNaughton 3807908724 Initial commit
2021-11-26 13:49:23 +01:00

141 lines
4.6 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2021 OpenStack Charmers
# See LICENSE file for licensing details.
#
# Learn more at: https://juju.is/docs/sdk
"""Charm the service.
Refer to the following post for a quick-start guide that will help you
develop a new k8s charm using the Operator Framework:
https://discourse.charmhub.io/t/4208
"""
import logging
from ops.charm import CharmBase
from ops.framework import StoredState
from ops.main import main
from ops.model import ActiveStatus
import ops_openstack.adapters
import ops_openstack.core
import ops_openstack.plugins.classes
logger = logging.getLogger(__name__)
class CephClientAdapter(ops_openstack.adapters.OpenStackOperRelationAdapter):
"""Adapter for ceph client interface."""
@property
def mon_hosts(self):
"""Sorted list of ceph mon addresses.
:returns: Ceph MON addresses.
:rtype: str
"""
hosts = self.relation.get_relation_data()['mon_hosts']
return ' '.join(sorted(hosts))
@property
def auth_supported(self):
"""Authentication type.
:returns: Authentication type
:rtype: str
"""
return self.relation.get_relation_data()['auth']
@property
def key(self):
"""Key client should use when communicating with Ceph cluster.
:returns: Key
:rtype: str
"""
return self.relation.get_relation_data()['key']
class CephNfsCharm(CharmBase):
"""Charm the service."""
_stored = StoredState()
def __init__(self, *args):
super().__init__(*args)
self.framework.observe(self.on.httpbin_pebble_ready, self._on_httpbin_pebble_ready)
self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(self.on.fortune_action, self._on_fortune_action)
self._stored.set_default(things=[])
def _on_httpbin_pebble_ready(self, event):
"""Define and start a workload using the Pebble API.
TEMPLATE-TODO: change this example to suit your needs.
You'll need to specify the right entrypoint and environment
configuration for your specific workload. Tip: you can see the
standard entrypoint of an existing container using docker inspect
Learn more about Pebble layers at https://github.com/canonical/pebble
"""
# Get a reference the container attribute on the PebbleReadyEvent
container = event.workload
# Define an initial Pebble layer configuration
pebble_layer = {
"summary": "httpbin layer",
"description": "pebble config layer for httpbin",
"services": {
"httpbin": {
"override": "replace",
"summary": "httpbin",
"command": "gunicorn -b 0.0.0.0:80 httpbin:app -k gevent",
"startup": "enabled",
"environment": {"thing": self.model.config["thing"]},
}
},
}
# Add intial Pebble config layer using the Pebble API
container.add_layer("httpbin", pebble_layer, combine=True)
# Autostart any services that were defined with startup: enabled
container.autostart()
# Learn more about statuses in the SDK docs:
# https://juju.is/docs/sdk/constructs#heading--statuses
self.unit.status = ActiveStatus()
def _on_config_changed(self, _):
"""Just an example to show how to deal with changed configuration.
TEMPLATE-TODO: change this example to suit your needs.
If you don't need to handle config, you can remove this method,
the hook created in __init__.py for it, the corresponding test,
and the config.py file.
Learn more about config at https://juju.is/docs/sdk/config
"""
current = self.config["thing"]
if current not in self._stored.things:
logger.debug("found a new thing: %r", current)
self._stored.things.append(current)
def _on_fortune_action(self, event):
"""Just an example to show how to receive actions.
TEMPLATE-TODO: change this example to suit your needs.
If you don't need to handle actions, you can remove this method,
the hook created in __init__.py for it, the corresponding test,
and the actions.py file.
Learn more about actions at https://juju.is/docs/sdk/actions
"""
fail = event.params["fail"]
if fail:
event.fail(fail)
else:
event.set_results({"fortune": "A bug in the code is worth two in the documentation."})
if __name__ == "__main__":
main(CephNfsCharm)