2022-08-26 18:15:21 +00:00
|
|
|
import invoke
|
|
|
|
|
2023-05-28 18:39:27 +00:00
|
|
|
import pathlib
|
|
|
|
import typing
|
2022-08-26 18:15:21 +00:00
|
|
|
|
|
|
|
ns = invoke.Collection()
|
|
|
|
|
2022-11-12 18:36:19 +00:00
|
|
|
PYINFRA_COMMON_PREFIX = "pyinfra -vvv pyinfra/inventory.py"
|
|
|
|
|
2023-05-28 18:39:27 +00:00
|
|
|
|
2022-11-12 18:36:19 +00:00
|
|
|
@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")
|
|
|
|
|
|
|
|
|
2023-05-28 18:39:27 +00:00
|
|
|
@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)
|
|
|
|
|
2022-11-12 18:36:19 +00:00
|
|
|
server = invoke.Collection("server")
|
|
|
|
|
|
|
|
server.add_task(system_updates, name="update")
|
|
|
|
server.add_task(system_reboot, name="reboot")
|
|
|
|
|
|
|
|
ns.add_collection(server)
|
2023-05-28 18:39:27 +00:00
|
|
|
ns.add_collection(services)
|