Skip to content

Commit

Permalink
Add CLI for build and pull
Browse files Browse the repository at this point in the history
Add CLI options to build and pull relevant containers separate from running the compose services. Allows for easier control of runtime environment on production.
  • Loading branch information
webb-ben committed Feb 6, 2025
1 parent 4fb73de commit 047fd62
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
"""


def check_dotenv():
if not os.path.exists(".env"):
if not sys.stdin.isatty():
shutil.copy(".env.example", ".env")
else:
answer = input(
"Missing .env file. Do you want to copy .env.example to .env ? (y/n)"
).lower()
if answer == "y" or answer == "yes":
print("Copying .env.example to .env")
shutil.copy(".env.example", ".env")
else:
raise RuntimeError("Missing .env file. Exiting")


def run_subprocess(command: str, returnStdoutAsValue: bool = False):
"""Run a shell command and stream the output in realtime"""
process = subprocess.Popen(
Expand Down Expand Up @@ -45,21 +60,23 @@ def login():
run_subprocess(f"docker exec -it {containerName} /bin/bash")


def pull_and_build(profiles: list[str], actions: list[str]):
"""Pull and Build imgaes the Docker Compose services"""
check_dotenv()

profileCommand = " ".join(f"--profile {profile}" for profile in profiles)

for action in actions:
# Run subprocess without waiting if detach is True
command = (
f"docker compose {profileCommand} -f Docker/Docker-compose.yaml {action}"
)
run_subprocess(command)


def up(profiles: list[str], build: bool = False, detach: bool = False):
"""Run the Docker Compose services"""
if not os.path.exists(".env"):
if not sys.stdin.isatty():
shutil.copy(".env.example", ".env")
else:
answer = input(
"Missing .env file. Do you want to copy .env.example to .env ? (y/n)"
).lower()
if answer == "y" or answer == "yes":
print("Copying .env.example to .env")
shutil.copy(".env.example", ".env")
else:
print("Missing .env file. Exiting")
return
check_dotenv()

profileCommand = " ".join(f"--profile {profile}" for profile in profiles)
command = f"docker compose {profileCommand} -f Docker/Docker-compose.yaml up"
Expand Down Expand Up @@ -88,6 +105,24 @@ def main():
"dagster-dev", help="Run dagster dev; will point to local dev infrastructure"
)

pull = subparsers.add_parser("pull", help="Pull the Docker Compose services")
pull.add_argument(
"--profiles",
nargs="+",
choices=["localInfra", "production"],
required=True,
help="Specify the profiles to pull",
)

build = subparsers.add_parser("build", help="Build the Docker Compose services")
build.add_argument(
"--profiles",
nargs="+",
choices=["localInfra", "production"],
required=True,
help="Specify the profiles to build",
)

dev = subparsers.add_parser(
"dev", help="Run local infrastructure needed for dagster dev"
)
Expand Down Expand Up @@ -136,6 +171,10 @@ def main():
)
elif args.command == "dagster-dev":
run_subprocess("DAGSTER_POSTGRES_HOST=0.0.0.0 dagster dev")
elif args.command == "build":
pull_and_build(profiles=args.profiles, actions=["build"])
elif args.command == "pull":
pull_and_build(profiles=args.profiles, actions=["pull"])
elif args.command == "dev":
up(profiles=["localInfra"], build=args.build, detach=args.detach)
elif args.command == "prod":
Expand Down

0 comments on commit 047fd62

Please sign in to comment.