102 lines
2.4 KiB
Python
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)
|