Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cave_cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
RunDashboard,
print_section,
step_done,
step_fail,
step_start,
)
from cave_cli.utils.docker import (
build_image,
create_network,
remove_containers,
run_and_log,
run_detached,
run_detached_logged,
run_interactive,
Expand Down Expand Up @@ -137,6 +139,50 @@ def run_cave(

django_volumes = [f"{app_dir}:/app"]

# Check if database needs initialization
if is_server_run and "postgres" in db_image.lower():
from cave_cli.utils.docker import is_container_ready, is_db_initialized
from cave_cli.utils.validate import confirm_action

db_container = f"{app_name}_db_host"
db_user = f"{app_name}_user"
db_name = f"{app_name}_name"

step_start("Checking database")
import time

ready = False
for _ in range(30):
if is_container_ready(db_container, db_user):
ready = True
break
time.sleep(1)

if ready:
if not is_db_initialized(db_container, db_user, db_name):
step_done("Checking database")
confirm_action(
"The database for this app is not set up. "
"Would you like to set it up now?"
)
step_start("Initializing database")
run_interactive(
name=f"{app_name}_django_setup",
image=f"cave-app:{app_name}",
network=network,
volumes=django_volumes,
env_vars=django_env,
command=["./utils/reset_db.sh"],
)
run_and_log(["docker", "rm", f"{app_name}_django_setup"])
step_done("Initializing database")
else:
step_done("Checking database")
else:
step_fail("Checking database", "Database container failed to become ready.")
remove_containers(app_name)
sys.exit(1)

parsed = parse_ip_port(ip_port_arg) if ip_port_arg else None
django_container = f"{app_name}_django"

Expand Down
73 changes: 73 additions & 0 deletions cave_cli/utils/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,79 @@ def stream_container_logs(
log_queue.put(None)


def is_container_ready(name: str, user: str) -> bool:
"""
Usage:

- Checks if a Postgres container is ready to accept connections

Requires:

- ``name``:
- Type: str
- What: The container name

- ``user``:
- Type: str
- What: The database user to check with

Returns:

- ``ready``:
- Type: bool
- What: True if ready, False otherwise
"""
result = run(
["docker", "exec", name, "pg_isready", "-U", user],
capture=True,
)
return result.returncode == 0


def is_db_initialized(name: str, user: str, db: str) -> bool:
"""
Usage:

- Checks if the Django migrations table exists in the database

Requires:

- ``name``:
- Type: str
- What: The container name

- ``user``:
- Type: str
- What: The database user

- ``db``:
- Type: str
- What: The database name

Returns:

- ``initialized``:
- Type: bool
- What: True if initialized, False otherwise
"""
result = run(
[
"docker",
"exec",
name,
"psql",
"-U",
user,
"-d",
db,
"-c",
"SELECT 1 FROM django_migrations LIMIT 1;",
],
capture=True,
)
return result.returncode == 0


def save_redis(app_name: str) -> None:
"""
Usage:
Expand Down
Binary file added dist/cave_cli-3.5.5-py3-none-any.whl
Binary file not shown.
Binary file added dist/cave_cli-3.5.5.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "cave_cli"
version = "3.5.4"
version = "3.5.5"
description = "CLI for creating and managing Docker-based CAVE web applications."
authors = [
{name = "MIT-CAVE", email = "cave-contact@mit.edu"}
Expand Down