charm-helpers icon indicating copy to clipboard operation
charm-helpers copied to clipboard

Suggestion to check if port is already open before the port is opened?

Open sebastienpattyn93 opened this issue 8 years ago • 4 comments

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?

sebastienpattyn93 avatar Dec 05 '17 14:12 sebastienpattyn93

A couple of thoughts here (which may be off base):

  1. 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.

  2. 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.)

  3. If it's the subordinate trying to open the port twice, then possibly catch the error from open_port()

  4. 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.!

ajkavanagh avatar Jan 02 '18 18:01 ajkavanagh

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.

sebastienpattyn93 avatar Jan 03 '18 09:01 sebastienpattyn93

Okay, let's go for '4' then: I've opened a bug on juju: https://bugs.launchpad.net/juju/+bug/1741028

ajkavanagh avatar Jan 03 '18 10:01 ajkavanagh

@ajkavanagh Great thanks for the help

sebastienpattyn93 avatar Jan 03 '18 11:01 sebastienpattyn93