Skip to content

Commit

Permalink
Added the ability to pass service name to invoke cli. (#188)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Kiely <[email protected]>
  • Loading branch information
gsnider2195 and smk4664 authored Oct 18, 2024
1 parent c37b5d5 commit 235a38e
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions nautobot-app/{{ cookiecutter.project_slug }}/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def docker_compose(context, command, **kwargs):
return context.run(compose_command, env=build_env, **kwargs)


def run_command(context, command, **kwargs):
def run_command(context, command, service="nautobot", **kwargs):
"""Wrapper to run a command locally or inside the nautobot container."""
if is_truthy(context.{{ cookiecutter.app_name }}.local):
if "command_env" in kwargs:
Expand All @@ -159,7 +159,7 @@ def run_command(context, command, **kwargs):
}
return context.run(command, **kwargs)
else:
# Check if nautobot is running, no need to start another nautobot container to run a command
# Check if service is running, no need to start another container to run a command
docker_compose_status = "ps --services --filter status=running"
results = docker_compose(context, docker_compose_status, hide="out")

Expand All @@ -169,10 +169,10 @@ def run_command(context, command, **kwargs):
for key, value in command_env.items():
command_env_args += f' --env="{key}={value}"'

if "nautobot" in results.stdout:
compose_command = f"exec{command_env_args} nautobot {command}"
if service in results.stdout:
compose_command = f"exec{command_env_args} {service} {command}"
else:
compose_command = f"run{command_env_args} --rm --entrypoint='{command}' nautobot"
compose_command = f"run{command_env_args} --rm --entrypoint='{command}' {service}"

pty = kwargs.pop("pty", True)

Expand Down Expand Up @@ -412,10 +412,14 @@ def shell_plus(context):
run_command(context, command)


@task
def cli(context):
"""Launch a bash shell inside the Nautobot container."""
run_command(context, "bash")
@task(
help={
"service": "Docker compose service name to launch cli in (default: nautobot).",
}
)
def cli(context, service="nautobot"):
"""Launch a bash shell inside the container."""
run_command(context, "bash", service=service)


@task(
Expand Down

0 comments on commit 235a38e

Please sign in to comment.