<div dir="ltr">Hi Junien,<div><br></div><div>Using hooks in the reactive world is perfectly file - just use them carefully.</div><div><br></div><div>Think of a distinction between the "Juju world" and the "charm world". Juju's unit agent is an integration point with your charm - the "charm world" starts when a unit agent runs a charm code and ends when you run a hook tool (for a moment until returns the result to the charm) or when the charm process exits. A unit agent (literally) executes a file at <charmdir>/hooks/<event-name> when an event happens. In most classic (non-reactive) charms <event-name> is a symlink to <something>_hooks.py - argument 0 of any executed Linux process will contain that symlink name instead of a target python file name (e.g. "stop") - this is done so that charm-helpers can route that to a decorated function @hooks.hook("stop") via a library shim. This is just routing to the right code within a file. The same happens with reactive, only the execution path will be unit agent -> <charmdir>/hooks/<event-name> -> reactive library code -> @hook or @when* execution.</div><div><br></div><div>If you look at any built reactive charm, it will have a generated "hooks" directory with files that have identical contents for every event name - they all get you to the reactive library "routing engine" first.</div><div><br></div><div><a href="https://git.launchpad.net/telegraf-charm/tree/hooks/hook.template?h=built">https://git.launchpad.net/telegraf-charm/tree/hooks/hook.template?h=built</a><br></div><div><a href="https://git.launchpad.net/telegraf-charm/tree/hooks/stop?h=built">https://git.launchpad.net/telegraf-charm/tree/hooks/stop?h=built</a><br></div><div><br></div><div>This engine then decides what to execute.</div><div><br></div><div>Hook template files are provided by layer-basic:</div><div><br></div><div><a href="https://charmsreactive.readthedocs.io/en/latest/layer-basic.html#hooks">https://charmsreactive.readthedocs.io/en/latest/layer-basic.html#hooks</a><br></div><div><br></div><div>The "only implements those hooks" restriction comes from the fact that only certain symlinks are provided and this makes sense to provide those in layer-basic only as they do not have a relation context.</div><div><br></div><div><a href="https://git.io/vFd16">https://git.io/vFd16</a><br></div><div><br></div><div>Other hooks are provided as well (again, with just a library entry point) in subsequent layers (generated via charm-tools when you do `charm build` based on metadata.yaml):</div><div><br></div><div><a href="https://git.launchpad.net/telegraf-charm/tree/hooks/haproxy-relation-joined?h=built">https://git.launchpad.net/telegraf-charm/tree/hooks/haproxy-relation-joined?h=built</a><br></div><div><br></div><div>A natural question would be to ask why there are no "install" hook implementations. This is a trick that most reactive charms use:</div><div><br></div><div>1. install event happens;</div><div>2. reactive library gets invoked;</div><div>3. when_not("installed") gets fired as this state is not set.</div><div><br></div><div>You could use any state instead of "installed", like when_not("foobar") and install packages there - it's just not intuitive. Removal is different as it is quite explicit (you don't really need a state for it).</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr">Best Regards,<div>Dmitrii Shcherbakov</div><div><br></div><div><div style="color:rgb(136,136,136);font-size:12.8px"><span style="color:rgb(68,68,68);font-size:12.8px">Field Software Engineer</span><br style="color:rgb(68,68,68);font-size:12.8px"><span style="font-size:12.8px">IRC (freenode): Dmitrii-Sh</span><br></div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Tue, Nov 21, 2017 at 5:58 PM, Junien Fridrick <span dir="ltr"><<a href="mailto:junien.fridrick@canonical.com" target="_blank">junien.fridrick@canonical.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'm developping a charm, and would like to know the best practive to<br>
do some uninstallation steps when a subordinate gets removed from a<br>
unit (these steps being : uninstall a snap)<br>
<br>
The only way I can think of is to use @hook('stop'), but as far as I<br>
remember, using hooks is not something one should do in the reactive<br>
world.<br>
<br>
Does anyone have any advice for doing that properly ?<br>
<br>
I was also thinking that maybe the snap layer could save which<br>
application installed which snap, and if said application is removed,<br>
then remove said snap. Would that be a good idea ?<br>
<br>
Thanks !<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Juju mailing list<br>
<a href="mailto:Juju@lists.ubuntu.com">Juju@lists.ubuntu.com</a><br>
Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/juju" rel="noreferrer" target="_blank">https://lists.ubuntu.com/<wbr>mailman/listinfo/juju</a><br>
</font></span></blockquote></div><br></div>