This repository has been archived on 2024-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
spadinastan/tasks.py
Marc Cataford 427ee18714
refactor: hoist start/stop/restart to top-level (#21)
* refactor: hoist start/stop/restart to top-level

* docs: README updates
2023-05-28 14:52:01 -04:00

102 lines
2.4 KiB
Python

import invoke
import pathlib
import typing
ns = invoke.Collection()
PYINFRA_COMMON_PREFIX = "pyinfra -vvv pyinfra/inventory.py"
@invoke.task()
def system_updates(ctx):
ctx.run(f"{PYINFRA_COMMON_PREFIX} pyinfra/system_updates.py")
@invoke.task()
def system_reboot(ctx):
ctx.run(f"{PYINFRA_COMMON_PREFIX} pyinfra/reboot.py")
@invoke.task()
def start(context: invoke.context.Context, service: typing.Optional[str]):
"""
Starts a service.
Services are assumed to be docker-compose-friendly and start as
docker-compose up -d.
The supplied service name is used to pathing, with the expected
file structure being
/
/services
/service1
docker-compose.yml
"""
if service is None:
raise ValueError("Service name must be provided.")
service_path = pathlib.Path("services", service)
if not service_path.exists():
raise ValueError(f"Service path does not exist: {service_path}")
with context.cd(service_path):
context.run("docker-compose up --build --force-recreate -d")
@invoke.task()
def stop(context: invoke.context.Context, service: typing.Optional[str]):
"""
Stops a service.
The same assumptions about file and service structure as made as with
`start <service>`.
"""
if service is None:
raise ValueError("Service name must be provided.")
service_path = pathlib.Path("services", service)
if not service_path.exists():
raise ValueError(f"Service path does not exist: {service_path}")
with context.cd(service_path):
context.run("docker-compose down")
@invoke.task()
def restart(context: invoke.context.Context, service: typing.Optional[str]):
"""
Restarts a service.
The same assumptions about file and service structure as made as with
`start <service>`.
"""
if service is None:
raise ValueError("Service name must be provided.")
service_path = pathlib.Path("services", service)
if not service_path.exists():
raise ValueError(f"Service path does not exist: {service_path}")
with context.cd(service_path):
context.run("docker-compose restart")
services = invoke.Collection("services")
services.add_task(start)
services.add_task(stop)
services.add_task(restart)
server = invoke.Collection("server")
server.add_task(system_updates, name="update")
server.add_task(system_reboot, name="reboot")
ns.add_collection(server)
ns.add_collection(services)