mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Adding new port availability checker (#266)
* Adding new port availability checker in tools * [fix] move import at file's beginning. * Moving back import inside function * Using boolean instead of Yes/No * Using built-in depreciation mechanism in Moulinette * We're now using boolean instead of yes/no * Renaming to port-available * Returning directly a boolean
This commit is contained in:
parent
43155ebc64
commit
674d639530
3 changed files with 37 additions and 7 deletions
|
@ -550,6 +550,7 @@ app:
|
||||||
checkport:
|
checkport:
|
||||||
action_help: Check availability of a local port
|
action_help: Check availability of a local port
|
||||||
api: GET /tools/checkport
|
api: GET /tools/checkport
|
||||||
|
deprecated: true
|
||||||
arguments:
|
arguments:
|
||||||
port:
|
port:
|
||||||
help: Port to check
|
help: Port to check
|
||||||
|
@ -1345,6 +1346,16 @@ tools:
|
||||||
help: Show private data (domain, IP)
|
help: Show private data (domain, IP)
|
||||||
action: store_true
|
action: store_true
|
||||||
|
|
||||||
|
### tools_port_available()
|
||||||
|
port-available:
|
||||||
|
action_help: Check availability of a local port
|
||||||
|
api: GET /tools/portavailable
|
||||||
|
arguments:
|
||||||
|
port:
|
||||||
|
help: Port to check
|
||||||
|
extra:
|
||||||
|
pattern: *pattern_port
|
||||||
|
|
||||||
|
|
||||||
#############################
|
#############################
|
||||||
# Hook #
|
# Hook #
|
||||||
|
|
|
@ -29,7 +29,6 @@ import shutil
|
||||||
import yaml
|
import yaml
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
import socket
|
|
||||||
import urlparse
|
import urlparse
|
||||||
import errno
|
import errno
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -955,12 +954,12 @@ def app_checkport(port):
|
||||||
port -- Port to check
|
port -- Port to check
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
# This import cannot be moved on top of file because it create a recursive
|
||||||
s.settimeout(1)
|
# import...
|
||||||
s.connect(("localhost", int(port)))
|
from yunohost.tools import tools_portavailable
|
||||||
s.close()
|
availability = tools_portavailable(port)
|
||||||
except socket.error:
|
if availability["available"]:
|
||||||
logger.success(m18n.n('port_available', port=int(port)))
|
logger.success(m18n.n('port_available', port=int(port)))
|
||||||
else:
|
else:
|
||||||
raise MoulinetteError(errno.EINVAL,
|
raise MoulinetteError(errno.EINVAL,
|
||||||
|
|
|
@ -31,6 +31,7 @@ import errno
|
||||||
import logging
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
import pwd
|
import pwd
|
||||||
|
import socket
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
import apt
|
import apt
|
||||||
|
@ -574,3 +575,22 @@ def tools_diagnosis(auth, private=False):
|
||||||
diagnosis['private']['domains'] = domain_list(auth)['domains']
|
diagnosis['private']['domains'] = domain_list(auth)['domains']
|
||||||
|
|
||||||
return diagnosis
|
return diagnosis
|
||||||
|
|
||||||
|
|
||||||
|
def tools_port_available(port):
|
||||||
|
"""
|
||||||
|
Check availability of a local port
|
||||||
|
|
||||||
|
Keyword argument:
|
||||||
|
port -- Port to check
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
s.settimeout(1)
|
||||||
|
s.connect(("localhost", int(port)))
|
||||||
|
s.close()
|
||||||
|
except socket.error:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
Loading…
Add table
Reference in a new issue