[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:
Alexandre Aubin 2017-04-16 16:46:57 +02:00 committed by GitHub
parent 43155ebc64
commit 674d639530
3 changed files with 37 additions and 7 deletions

View file

@ -550,6 +550,7 @@ app:
checkport:
action_help: Check availability of a local port
api: GET /tools/checkport
deprecated: true
arguments:
port:
help: Port to check
@ -1345,6 +1346,16 @@ tools:
help: Show private data (domain, IP)
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 #

View file

@ -29,7 +29,6 @@ import shutil
import yaml
import time
import re
import socket
import urlparse
import errno
import subprocess
@ -955,12 +954,12 @@ def app_checkport(port):
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:
# This import cannot be moved on top of file because it create a recursive
# import...
from yunohost.tools import tools_portavailable
availability = tools_portavailable(port)
if availability["available"]:
logger.success(m18n.n('port_available', port=int(port)))
else:
raise MoulinetteError(errno.EINVAL,

View file

@ -31,6 +31,7 @@ import errno
import logging
import subprocess
import pwd
import socket
from collections import OrderedDict
import apt
@ -574,3 +575,22 @@ def tools_diagnosis(auth, private=False):
diagnosis['private']['domains'] = domain_list(auth)['domains']
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