mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Merge pull request #1769 from Salamandar/fix_port_reuse
Fix port reuse during provisionning.
This commit is contained in:
commit
3574a7792d
1 changed files with 12 additions and 4 deletions
|
@ -1216,17 +1216,24 @@ class PortsResource(AppResource):
|
||||||
if properties[port]["default"] is None:
|
if properties[port]["default"] is None:
|
||||||
properties[port]["default"] = random.randint(10000, 60000)
|
properties[port]["default"] = random.randint(10000, 60000)
|
||||||
|
|
||||||
|
# This is to prevent using twice the same port during provisionning.
|
||||||
|
self.ports_used_by_self: list[int] = []
|
||||||
|
|
||||||
super().__init__({"ports": properties}, *args, **kwargs)
|
super().__init__({"ports": properties}, *args, **kwargs)
|
||||||
|
|
||||||
def _port_is_used(self, port):
|
def _port_is_used(self, port):
|
||||||
# FIXME : this could be less brutal than two os.system...
|
# FIXME : this could be less brutal than two os.system...
|
||||||
cmd1 = (
|
used_by_process = os.system(
|
||||||
"ss --numeric --listening --tcp --udp | awk '{print$5}' | grep --quiet --extended-regexp ':%s$'"
|
"ss --numeric --listening --tcp --udp | awk '{print$5}' | grep --quiet --extended-regexp ':%s$'"
|
||||||
% port
|
% port
|
||||||
)
|
) == 0
|
||||||
# This second command is mean to cover (most) case where an app is using a port yet ain't currently using it for some reason (typically service ain't up)
|
# This second command is mean to cover (most) case where an app is using a port yet ain't currently using it for some reason (typically service ain't up)
|
||||||
cmd2 = f"grep --quiet --extended-regexp \"port: '?{port}'?\" /etc/yunohost/apps/*/settings.yml"
|
used_by_app = os.system(
|
||||||
return os.system(cmd1) == 0 or os.system(cmd2) == 0
|
f"grep --quiet --extended-regexp \"port: '?{port}'?\" /etc/yunohost/apps/*/settings.yml"
|
||||||
|
) == 0
|
||||||
|
used_by_self_provisioning = port in self.ports_used_by_self
|
||||||
|
|
||||||
|
return used_by_process or used_by_app or used_by_self_provisioning
|
||||||
|
|
||||||
def provision_or_update(self, context: Dict = {}):
|
def provision_or_update(self, context: Dict = {}):
|
||||||
from yunohost.firewall import firewall_allow, firewall_disallow
|
from yunohost.firewall import firewall_allow, firewall_disallow
|
||||||
|
@ -1256,6 +1263,7 @@ class PortsResource(AppResource):
|
||||||
while self._port_is_used(port_value):
|
while self._port_is_used(port_value):
|
||||||
port_value += 1
|
port_value += 1
|
||||||
|
|
||||||
|
self.ports_used_by_self.append(port_value)
|
||||||
self.set_setting(setting_name, port_value)
|
self.set_setting(setting_name, port_value)
|
||||||
|
|
||||||
if infos["exposed"]:
|
if infos["exposed"]:
|
||||||
|
|
Loading…
Add table
Reference in a new issue