|
2 | 2 |
|
3 | 3 | import click
|
4 | 4 | import dotenv
|
| 5 | +import errno |
5 | 6 | import importlib
|
6 | 7 | import pathlib
|
7 | 8 | import re
|
8 | 9 | import requests
|
9 | 10 | import shutil
|
| 11 | +import socket |
10 | 12 | import subprocess
|
11 | 13 | import typing
|
12 | 14 |
|
@@ -100,6 +102,36 @@ def ping_service(url: str, service_name: str, headers: typing.Dict[str, any] = d
|
100 | 102 | return True
|
101 | 103 |
|
102 | 104 |
|
| 105 | +def check_port(port: int) -> bool: |
| 106 | + """ |
| 107 | + Check if a given port on localhost is available for use. |
| 108 | +
|
| 109 | + This function attempts to bind to the specified port on the local machine to determine its availability. |
| 110 | + If the port is already in use or an error occurs during the check, it logs an appropriate error message. |
| 111 | +
|
| 112 | + Args: |
| 113 | + port (int): The port number to check. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + bool: `True` if the port is available, `False` otherwise. |
| 117 | +
|
| 118 | + Raises: |
| 119 | + socket.error: Captures errors during socket binding. |
| 120 | + """ |
| 121 | + is_available = True |
| 122 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 123 | + try: |
| 124 | + s.bind(("127.0.0.1", port)) |
| 125 | + except socket.error as e: |
| 126 | + if e.errno == errno.EADDRINUSE: |
| 127 | + click.echo(click.style(f"ERROR: Port {port} is already in use", fg="red"), err=True) |
| 128 | + else: |
| 129 | + click.echo(click.style(f"ERROR: An error ocurred when checking port ({port}) availability: {e}", fg="red"), err=True) |
| 130 | + is_available = False |
| 131 | + s.close() |
| 132 | + return is_available |
| 133 | + |
| 134 | + |
103 | 135 | def check_docker(debug: bool = False) -> bool:
|
104 | 136 | """
|
105 | 137 | Verify that Docker is installed and its daemon is running.
|
@@ -331,11 +363,20 @@ def setup(
|
331 | 363 |
|
332 | 364 | click.echo(click.style(f"(Step {step}/{total_steps}) Setting Vector DB settings...", fg="yellow"))
|
333 | 365 |
|
334 |
| - external_qdrant = click.confirm( |
335 |
| - f"Do you have a running QdrantDB instance?", |
336 |
| - default=False, |
337 |
| - show_default=True |
338 |
| - ) |
| 366 | + while True: |
| 367 | + external_qdrant = click.confirm( |
| 368 | + f"Do you have a running QdrantDB instance?", |
| 369 | + default=False, |
| 370 | + show_default=True |
| 371 | + ) |
| 372 | + if not external_qdrant: |
| 373 | + # TODO: check port is not in use |
| 374 | + port_available = check_port(6333) |
| 375 | + if not port_available: |
| 376 | + click.echo(click.style(f"Cannot deploy a QdrantDB instance", fg="red"), err=True) |
| 377 | + continue |
| 378 | + break |
| 379 | + |
339 | 380 | qdrantdb_url = "http://127.0.0.1:6333"
|
340 | 381 | if external_qdrant:
|
341 | 382 | while True:
|
|
0 commit comments