Possibly this isn't a bug but me not understanding how this works. I'm doing a proof of concept conversion of the mattermost k8s charm to the sidecar approach, and am testing the `grant-admin-role` action.
This action takes a string parameter of "user" and runs the following:
```
/mattermost/bin/mattermost roles system_admin $user
```
However, when executing it I get the following output:
```
UnitId: mattermost/0
id: "2"
message: exit status 1
results:
ReturnCode: 1
status: failed
timing:
completed: 2021-04-14 10:07:42 +0000 UTC
enqueued: 2021-04-14 10:07:37 +0000 UTC
started: 2021-04-14 10:07:41 +0000 UTC
```
Checking the logs for the "charm" container I see:
```
microk8s.kubectl logs -n mattermost -c charm mattermost-0
starting containeragent unit command
containeragent unit "unit-mattermost-0" start (2.9-rc9 [gc])
ERROR Uncaught exception while in charm code:
Traceback (most recent call last):
File "./src/charm.py", line 623, in <module>
main(MattermostK8sCharm)
File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/main.py", line 406, in main
_emit_charm_event(charm, dispatcher.event_name)
File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/main.py", line 140, in _emit_charm_event
event_to_emit.emit(*args, **kwargs)
File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/framework.py", line 278, in emit
framework._emit(event)
File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/framework.py", line 722, in _emit
self._reemit(event_path)
File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/framework.py", line 767, in _reemit
custom_handler(event)
File "./src/charm.py", line 611, in _on_grant_admin_role_action
granted = subprocess.run(cmd, capture_output=True)
File "/usr/lib/python3.8/subprocess.py", line 489, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/mattermost/bin/mattermost'
```
This is because the binary in question is in the workload container, not the charm container. Is there a way I'm missing to run the action on the workload container? The function that executes this is as follows:
```
def _on_grant_admin_role_action(self, event):
"""Handle the grant-admin-role action."""
user = event.params["user"]
cmd = ["/mattermost/bin/mattermost", "roles", "system_admin", user]
granted = subprocess.run(cmd, capture_output=True)
if granted.returncode != 0:
event.fail("Failed to run '{}'. Output was:\n{}".format(" ".join(cmd), granted.stderr.decode("utf-8")))
else:
msg = (
"Ran grant-admin-role for user '{}'. They will need to log out and log back in "
"to Mattermost to fully receive their permissions upgrade.".format(user)
)
event.set_results({"info": msg})
```
I think this raises the question of whether we want to run an action in the workload container, or have the ability to execute a command in the workload container.
At least this way, the action itself will run in the charm container, so there is no need for Python + deps in the workload container.
A workaround could include adding a Pebble layer that you start/stop with the command specified, but I wonder if the Pebble API/Operator Framework might be extended later to enable running arbitrary commands in a one-shot fashion in the workload container