From 46ca32a29da38539fe4bf32393e1ffb217f63629 Mon Sep 17 00:00:00 2001 From: Viet Anh Nguyen Date: Fri, 3 Nov 2023 17:41:42 +0700 Subject: [PATCH] Only scan port if running in local mode --- Dockerfile | 2 +- README.md | 1 - extension-ui/index.html | 32 ++++++++++++++++------------ frontend/components/service-card.tsx | 12 +++++++++++ p8hub/app.py | 7 ++++++ p8hub/core/service_manager.py | 17 +++++++++------ p8hub/globals.py | 1 + 7 files changed, 50 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index d4dc51c..061c66f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,4 +40,4 @@ COPY MANIFEST.in /workspace COPY --from=client-builder /ui/out /workspace/p8hub/frontend-dist RUN cd /workspace && pip3 install -e . -CMD python3 -m p8hub.app +CMD python3 -m p8hub.app --environment extension diff --git a/README.md b/README.md index 65a1fd7..684e82f 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,6 @@ npm run dev - Next Template: [https://github.com/shadcn-ui/next-template](https://github.com/shadcn-ui/next-template). - PAutoBot: [https://github.com/nrl-ai/pautobot](https://github.com/nrl-ai/pautobot). - ![DockerML Hackathon 2023](images/dockerml2023.jpeg) This project was created for the [Docker AI/ML Hackathon 2023](https://docker.devpost.com/). diff --git a/extension-ui/index.html b/extension-ui/index.html index 98266bb..d0daff0 100644 --- a/extension-ui/index.html +++ b/extension-ui/index.html @@ -1,18 +1,22 @@ - + - - - + + + P8Hub Extension - - - - - \ No newline at end of file + + + + + diff --git a/frontend/components/service-card.tsx b/frontend/components/service-card.tsx index fb0669e..f722ca1 100644 --- a/frontend/components/service-card.tsx +++ b/frontend/components/service-card.tsx @@ -89,6 +89,18 @@ export function ServiceCard({ {description} + {service_port && ( +
+ Address:{" "} + + http://localhost:{service_port} + +
+ )}
diff --git a/p8hub/app.py b/p8hub/app.py index 14cc01d..d479634 100644 --- a/p8hub/app.py +++ b/p8hub/app.py @@ -38,6 +38,10 @@ def main(): os.path.join(os.path.expanduser("~"), "p8hub-data") ), ) + parser.add_argument( + "--environment", + default="local", + ) args = parser.parse_args() if args.version: @@ -47,6 +51,9 @@ def main(): logging.info(f"Starting {__appname__}...") logging.info(f"Version: {__version__}") + logging.info(f"Environment: {args.environment}") + global_data.environment = args.environment + logging.info(f"Data root: {args.data_root}") global_data.data_root = args.data_root pathlib.Path(global_data.data_root).mkdir(parents=True, exist_ok=True) diff --git a/p8hub/core/service_manager.py b/p8hub/core/service_manager.py index 5974fc4..12e48a5 100644 --- a/p8hub/core/service_manager.py +++ b/p8hub/core/service_manager.py @@ -10,6 +10,7 @@ from p8hub.core.app_manager import AppManager from p8hub.database import session, Service, ServiceStatus +from p8hub import globals as global_data class ServiceManager: """Manage services (app instances)""" @@ -95,7 +96,9 @@ def run_docker_service(self, app: dict, service: Service): docker.compose.pull() # Find a usable port and update .env file - usable_port = self.find_usable_port(app["default_service_port"], logger=logger) + usable_port = app["default_service_port"] + if global_data.environment == "local": + usable_port = self.find_usable_port(app["default_service_port"], logger=logger) logger.info(f"Using port {usable_port}") with open(env_file, "a") as f: f.write(f"P8HUB_SERVICE_PORT={usable_port}\n") @@ -104,11 +107,13 @@ def run_docker_service(self, app: dict, service: Service): docker.compose.up(detach=True) # Wait for service port to be available - service.status = ServiceStatus.waiting_to_online.value - session.commit() - while self.is_free_port(usable_port): - time.sleep(2) - logger.debug(f"Waiting for service port {usable_port}") + if global_data.environment == "local": + service.status = ServiceStatus.waiting_to_online.value + session.commit() + while self.is_free_port(usable_port): + time.sleep(2) + logger.debug(f"Waiting for service port {usable_port}") + except DockerException as e: logger.error(e) service.status = ServiceStatus.error.value diff --git a/p8hub/globals.py b/p8hub/globals.py index 41185d1..690d50c 100644 --- a/p8hub/globals.py +++ b/p8hub/globals.py @@ -1,3 +1,4 @@ +environment = "local" # local or extension data_root = None app_manager = None service_manager = None