canonical / charmcraft

Collaborate, build and publish charmed operators for Kubernetes, Linux and Windows.
Apache License 2.0
65 stars 69 forks source link

Event charm.py not defined #21

Closed knkski closed 4 years ago

knkski commented 4 years ago

I have written a charm using charmcraft, found here:

https://github.com/juju-solutions/bundle-kubeflow/tree/admission-webhook-charm/charms/admission-webhook

I am deploying it like this:

charmcraft build
juju deploy ./admission-webhook.charm --resource oci-image=gcr.io/kubeflow-images-public/admission-webhook:vmaster-gaf96e4e3

This is with charmcraft 0.1.0 installed via pypi.org and juju 2.7.6. After deployment, the charm stays in unknown status, and I get this message from juju debug-log:

application-admission-webhook: 11:04:06 DEBUG unit.admission-webhook/6.juju-log Event charm.py not defined for <__main__.AdmissionWebhookCharm object at 0x7f5a3790ec18>.

I'm not sure how I'm getting an event of that name.

jameinel commented 4 years ago

This looks like the code to interact with 2.7 isn't quite working correctly. It seems to be trying to invoke src/charm.py directly, rather than via a symlink, so sys.argv[0] is charm.py not 'hooks/install'. And then

    def _set_name_from_path(self, path: Path):
        """Sets the name attribute to that which can be inferred from the
given path."""
        name = path.name.replace('-', '_')
        if path.parent.name == 'actions':
            name = '{}_action'.format(name)
        self.event_name = name

Is then determining the name of the event is 'charm.py'

On Mon, Jun 1, 2020 at 8:44 PM Kenneth Koski notifications@github.com wrote:

I have written a charm using charmcraft, found here:

https://github.com/juju-solutions/bundle-kubeflow/tree/admission-webhook-charm/charms/admission-webhook

I am deploying it like this:

charmcraft build juju deploy ./admission-webhook.charm --resource oci-image=gcr.io/kubeflow-images-public/admission-webhook:vmaster-gaf96e4e3

This is with charmcraft 0.1.0 installed via pypi.org and juju 2.7.6. After deployment, the charm stays in unknown status, and I get this message from juju debug-log:

application-admission-webhook: 11:04:06 DEBUG unit.admission-webhook/6.juju-log Event charm.py not defined for <main.AdmissionWebhookCharm object at 0x7f5a3790ec18>.

I'm not sure how I'm getting an event of that name.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/canonical/charmcraft/issues/21, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABRQ7PXWQGIIC5HA6HLO6LRUPLFPANCNFSM4NP653JQ .

jameinel commented 4 years ago

We talked through what is happening. The issue is that 'hooks/install' is a copy of 'dispatch', which is doing an 'exec src/charm.py' to trigger the charm main. However, that sets sys.argv[0] to 'src/charm.py' rather than being 'hooks/install'. We can use bin/bash to pass 'exec -a' in order to set argv[0], or we can just set JUJU_DISPATCH_PATH in the script. eg:

#!/bin/sh
set -eu

dispatch=${JUJU_DISPATCH_PATH:-}
if [ -z "$dispatch" ]; then
  export JUJU_DISPATCH_PATH="$0"
fi
exec src/charm.py

That way if Juju is 2.8+ and is setting JUJU_DISPATCH_PATH then we just use it. If it is 2.7 and we are invoking 'hooks/install' as a script, that script then tells the operator framework what hook is being run via JUJU_DISPATCH_PATH.

chipaca commented 4 years ago

this is part of 0.1.2 (which is the same as 0.1.1 for silly reasons), already in pypi.