Suggestion to check if port is already open before the port is opened?
https://github.com/juju/charm-helpers/blob/6d72df2925096239be70b5daa70c1fc18229a8c6/charmhelpers/core/hookenv.py#L665
I was testing one of my subordinate charms and When I wanted to open a port that was already opened with charmhelpers.core.hookenv.open_port I got an error
subprocess.CalledProcessError: Command '['open-port', '8080/TCP']' returned non-zero exit status 1
Now I noticed that the method opened_ports is not in the API documentation and not yet available.
AttributeError: module 'charmhelpers.core.hookenv' has no attribute 'opened_ports'
but this method is not able to see the opened ports on the machine so this would not completely fix my issue. Is there any other possibility to check which other ports have been opened on the machine using charmhelpers?
A couple of thoughts here (which may be off base):
-
We could change
def open_port(..)to fail silently if the port is already open, assuming there's an error message we can parse which says something like 'port is already open'; but that might be brittle. -
Is this a port already opened by the principal charm? If so, then the subordinate probably shouldn't be trying to open it as that might break the principal charm at some point (without 1.)
-
If it's the subordinate trying to open the port twice, then possibly catch the error from
open_port() -
Perhaps this is really an error in Juju; I reckon that opening/closing ports should be idempotent, and it should silently ignore multiple requests if the port is already open.
Obviously, my favourite fix is 4.!
Thanks for the response on this. My favourite fix is 4 as well... the problem was that we had a subordinate service to monitor an application. Sometimes we have 2 applications that need to be monitored at the same time. That was were we encountered the problem for the first time since our subordinate was opening the same port twice. I noticed that close_port never gives an error, even if the port is already closed, So that was a workaround for now. And in that case I think 4 is the best solution since it would behave the same as close_port then.
Okay, let's go for '4' then: I've opened a bug on juju: https://bugs.launchpad.net/juju/+bug/1741028
@ajkavanagh Great thanks for the help