Skip to content

Commit

Permalink
Only scan port if running in local mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vietanhdev committed Nov 3, 2023
1 parent 0027da3 commit 46ca32a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
32 changes: 18 additions & 14 deletions extension-ui/index.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>P8Hub Extension</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
</style>
</head>
<body>
<iframe src="http://localhost:18180" frameborder="0" style="width: 100%; height: 100vh;"></iframe>
</body>
</html>
</head>
<body>
<iframe
src="http://localhost:18180"
frameborder="0"
style="width: 100%; height: 100vh"
></iframe>
</body>
</html>
12 changes: 12 additions & 0 deletions frontend/components/service-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ export function ServiceCard({
</CardTitle>
<CardDescription className="h-[50px] overflow-auto">
{description}
{service_port && (
<div>
Address:{" "}
<a
href={`http://localhost:${service_port}`}
target="_blank"
rel="noreferrer"
>
http://localhost:{service_port}
</a>
</div>
)}
</CardDescription>
</div>
<div className="flex items-center space-x-1 rounded-md bg-secondary text-secondary-foreground">
Expand Down
7 changes: 7 additions & 0 deletions p8hub/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions p8hub/core/service_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"""
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions p8hub/globals.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
environment = "local" # local or extension
data_root = None
app_manager = None
service_manager = None

0 comments on commit 46ca32a

Please sign in to comment.