I'm developing a juju charm in python using a reactive pattern. All my hooks are called using decorators provided by 'basic' base layer. When trying to deploy the charm I encountered an error. In order to solve the error I used 'juju debug-hooks' and 'juju dhx -r' (dhx debugging plugin) but I can't find a way to run charm code using python following reactive pattern... As I understand, (when using dhx for debugging for example) the charm execution is returned to the state exactly before the hook that caused an error and the developer is logged in into a remote debugging session. It is on the developer to run hooks. Normally these hooks would be under /hooks directory and would be run by just executing them. This isn't possible using reactive pattern, since there is no entry point for hooks but everything is just a cascade of reactions to states...
How do I run a juju charm code written in python following reactive pattern during a debugging session?
2 Answers
I had the same problem because in some way my set_state('myservice.installed') was triggered even when I got the error that my installation hook had failed. I did not found a specific solution to run a certain piece of code but a good workaround can be to change the event that would normally trigger your code and rerun hooks/install. I only tried it with juju debug-hooks and not with juju dhx, but that did the job for me.
In my case I saw that my current state was set to 'Installed'. By changing my event to trigger the code I was able to rerun my Install-hook. In my debug-hooks session I could then see where my hook failed.
from charms.reactive import when, set_state
import charms.apt
@when('myservice.installed')
def install(): charms.apt.queue_install(['some-package']) // do some extra stuff set_state('myservice.installed') How do I run a reactive charm during a debugging session?
You run a reactive charm by running a hook in the hooks/ directory. That hook will start the reactive framework and initiate the "cascade of states".
The hook files in the hooks/ directory are created by layer-basic and by charm build. Make sure to include layer-basic in your layer.yaml file if the hook files aren't present in the hooks/ directory.
You can find more information about debugging reactive charms in the Juju docs.