Skip to content

Commit

Permalink
- [CLI] Minor update of the PortUsedError error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
peterschmidt85 committed Jul 24, 2023
1 parent 0bf40df commit 669781f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cli/dstack/_internal/cli/commands/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _command(self, args: argparse.Namespace):
ports_locks=ports_locks,
)
except PortUsedError as e:
exit(f"{type(e).__name__}: {e}")
exit(f"\n{e.message}")

def __init__(self, parser):
super().__init__(parser)
Expand Down
2 changes: 1 addition & 1 deletion cli/dstack/_internal/cli/commands/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _command(self, args: Namespace):
ports_locks=ports_locks,
)
except PortUsedError as e:
exit(f"{type(e).__name__}: {e}")
exit(f"\n{e.message}")
finally:
if watcher.is_alive():
watcher.stop()
Expand Down
4 changes: 2 additions & 2 deletions cli/dstack/_internal/cli/commands/run/ssh_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def acquire(self) -> "PortsLock":
if not local_port: # None or 0
continue
if local_port in assigned_ports:
raise PortUsedError(f"Mapped port {app_port}:{local_port} is already in use")
raise PortUsedError(f"Port {local_port} is already in use")
sock = self._listen(local_port)
if sock is None:
raise PortUsedError(f"Mapped port {app_port}:{local_port} is already in use")
raise PortUsedError(f"Port {local_port} is already in use")
self.sockets[app_port] = sock
assigned_ports.add(local_port)

Expand Down
18 changes: 7 additions & 11 deletions cli/dstack/_internal/configurators/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@ class PortUsedError(DstackError):


def merge_ports(schema: List[PortMapping], args: List[PortMapping]) -> Dict[int, PortMapping]:
unique_ports_constraint(
[pm.container_port for pm in schema], error="Schema port {} is already in use"
)
unique_ports_constraint(
[pm.container_port for pm in args], error="Args port {} is already in use"
)
unique_ports_constraint([pm.container_port for pm in schema])
unique_ports_constraint([pm.container_port for pm in args])

ports = {pm.container_port: pm for pm in schema}
for pm in args: # override schema
ports[pm.container_port] = pm

unique_ports_constraint(
[pm.local_port for pm in ports.values() if pm.local_port is not None],
error="Mapped port {} is already in use",
)
unique_ports_constraint([pm.local_port for pm in ports.values() if pm.local_port is not None])
return ports


def unique_ports_constraint(ports: List[int], error: str = "Port {} is already in use"):
def unique_ports_constraint(
ports: List[int],
error: str = "Port {} is already in use",
):
used_ports = set()
for i in ports:
if i in used_ports:
Expand Down

0 comments on commit 669781f

Please sign in to comment.