|
| 1 | +(how-to-manage-charms)= |
| 2 | +# How to manage charms |
| 3 | + |
| 4 | +> See first: {external+juju:ref}`Juju | How to manage charms or bundles <manage-charms>` |
| 5 | +
|
| 6 | +The primary programming language charms are written in is Python, and the |
| 7 | +primary framework for developing charms is the Python Operator Framework, or |
| 8 | +`ops`. |
| 9 | + |
| 10 | +`charmcraft init` provides you with a `src/charm.py` file that demonstrates the |
| 11 | +basis of using the library. |
| 12 | + |
| 13 | +```python |
| 14 | +# Import ops and use names from there. |
| 15 | +import ops |
| 16 | + |
| 17 | +# Charms always have a class that inherits from ops.CharmBase that define the |
| 18 | +# charm's functionality. |
| 19 | +class MyCharm(ops.CharmBase): |
| 20 | + # There's always an __init__ method that sets up observers. |
| 21 | + def __init__(self, framework: ops.Framework): |
| 22 | + super().__init__(framework) |
| 23 | + framework.observe(...) |
| 24 | + |
| 25 | + # There will always be one or more event handlers that will be called in |
| 26 | + # response to a Juju, ops, or custom event. |
| 27 | + # Note that this has a leading underscore. These methods should only be |
| 28 | + # called by the ops framework, so should not be public. |
| 29 | + def _my_event_handler(self, event: ops.EventBase): # or a more specific type |
| 30 | + ... |
| 31 | + self.unit.status = ops.ActiveStatus() |
| 32 | + |
| 33 | +# Finally, the charm.py file always ends with a call to ops.main, passing in the |
| 34 | +# charm class. |
| 35 | +if __name__ == "__main__": |
| 36 | + ops.main(MyCharm) |
| 37 | +``` |
| 38 | + |
| 39 | +## Interacting with the workload |
| 40 | + |
| 41 | +For simple interactions with an application or service or when a high quality |
| 42 | +Python binding is not available, subprocess or API calls should be used to |
| 43 | +perform the required operations on the application or service. |
| 44 | + |
| 45 | +TODO: Create a `src/{workload}.py` file for each workload or service that your charm |
| 46 | +is managing, and ... For example: |
| 47 | + |
| 48 | +```python |
| 49 | +import subprocess |
| 50 | + |
| 51 | +try: |
| 52 | + # Comment to explain why subprocess is used. |
| 53 | + result = subprocess.run( |
| 54 | + # Array based execution. |
| 55 | + ["/usr/bin/echo", "hello world"], |
| 56 | + capture_output=True, |
| 57 | + check=True, |
| 58 | + ) |
| 59 | + logger.debug("Command output: %s", result.stdout) |
| 60 | +except subprocess.CalledProcessError as err: |
| 61 | + logger.error("Command failed with code %i: %s", err.returncode, err.stderr) |
| 62 | + raise |
| 63 | +``` |
| 64 | + |
| 65 | +```{admonition} Best Practice |
| 66 | +:class: hint |
| 67 | +Limit the use of shell scripts and commands as much as possible in favour of |
| 68 | +writing Python for charm code. Examples where it could be reasonable to use a |
| 69 | +script include: extracting data from a machine or container which can't be |
| 70 | +obtained through Python; or issuing commands to applications that do not have |
| 71 | +Python bindings (such as starting a process on a machine). |
| 72 | +``` |
| 73 | + |
| 74 | +## Base / Python |
| 75 | + |
| 76 | +Charms have access to the default Python version in the Ubuntu release defined |
| 77 | +as the base. |
| 78 | + |
| 79 | +Your code should be compatible with the operating system and Juju versions it |
| 80 | +will be executed on. For example, if your charm is to be deployed with Juju 3.6, |
| 81 | +its Python code should be compatible with Python 3.8. |
| 82 | + |
| 83 | +TODO: put in the relevant bits in pyproject.toml to have tools recognise this. |
| 84 | + |
| 85 | +TODO: can we be more detailed, or link to something here that says which Ubuntu |
| 86 | +has which Python, and which base you should support? |
| 87 | + |
| 88 | +## Dependencies |
| 89 | + |
| 90 | +TODO: put in instructions for dependencies from the spec (pyproject, generate requirements.txt) |
| 91 | + |
| 92 | +TODO: instructions on putting libs in charmcraft.yaml and running fetch-libs and then keeping that updated. |
| 93 | + |
| 94 | +> See more: charmcraft.yaml, charmcraft fetch-libs |
| 95 | +
|
| 96 | +```{admonition} Best Practice |
| 97 | +:class: hint |
| 98 | +Including an external dependency in a charm is a significant choice. It can help |
| 99 | +with reducing the complexity and development cost. However, every additional |
| 100 | +dependency increases the risk for supply chain security issues, the maintenance |
| 101 | +burden of having to manage the dependencies, and makes it more difficult to |
| 102 | +completely understand all of the code involved in the charm. |
| 103 | +``` |
| 104 | + |
| 105 | +> See more: [The software dependency issue](https://research.swtch.com/deps) |
| 106 | +
|
| 107 | +TODO: is that still the article we want to link to? There are a lot on this problem. |
| 108 | + |
| 109 | +## Events |
| 110 | + |
| 111 | +TODO: There are Juju events, ops events, and custom events. A bit more about how to |
| 112 | +respond to each of these, probably mostly linking out. |
| 113 | + |
| 114 | +> See also: Juju events, something about the ops events. |
| 115 | +
|
| 116 | +```{admonition} Best Practice |
| 117 | +:class: hint |
| 118 | +Custom events are defined and emitted by charm libraries. Charms should never |
| 119 | +define or emit custom events themselves. |
| 120 | +``` |
| 121 | + |
| 122 | +> See more: manage libraries |
| 123 | +
|
0 commit comments