2022-08-26 18:15:21 +00:00
|
|
|
import invoke
|
2023-09-04 18:22:53 +00:00
|
|
|
import jinja2
|
|
|
|
import yaml
|
2022-08-26 18:15:21 +00:00
|
|
|
|
2023-09-04 18:22:53 +00:00
|
|
|
import os
|
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
|
|
|
|
2023-09-04 18:22:53 +00:00
|
|
|
def _get_tag(ctx: invoke.context.Context) -> str:
|
|
|
|
"""
|
|
|
|
Gets the current tag, if it exists, for image versioning purposes.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
out = ctx.run("git describe --tags --exact-match", hide=True)
|
|
|
|
return out.stdout.strip()
|
|
|
|
except:
|
|
|
|
return "dev"
|
|
|
|
|
|
|
|
|
|
|
|
def _ensure_networks(ctx: invoke.context.Context):
|
|
|
|
"""
|
|
|
|
Ensures that the required networks exist.
|
|
|
|
"""
|
|
|
|
print("Ensuring networks exist...")
|
|
|
|
output = ctx.run("docker network ls -f name=spad-internal -q", hide=True)
|
|
|
|
if not bool(output.stdout):
|
|
|
|
ctx.run("docker network create --scope=swarm spad-internal")
|
|
|
|
print('\t✅ Created network "internal".')
|
|
|
|
else:
|
|
|
|
print('\t✅ Network "internal" already exists.')
|
|
|
|
|
|
|
|
|
|
|
|
def _ensure_swarm(ctx: invoke.context.Context):
|
|
|
|
"""
|
|
|
|
Ensures that swarm mode is on.
|
|
|
|
"""
|
|
|
|
print("Ensuring swarm mode is on...")
|
|
|
|
try:
|
|
|
|
ctx.run("docker swarm init --advertise-addr 192.168.1.17", hide=True)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
print("\t✅ Swarm in on.")
|
|
|
|
|
|
|
|
|
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()
|
2023-09-04 18:22:53 +00:00
|
|
|
def generate_configuration(ctx):
|
2023-05-28 18:39:27 +00:00
|
|
|
"""
|
2023-09-04 18:22:53 +00:00
|
|
|
Generates the service configuration file `services.yml`
|
|
|
|
based on environment variables.
|
2023-05-28 18:39:27 +00:00
|
|
|
|
2023-09-04 18:22:53 +00:00
|
|
|
To avoid polluting the environment, this can be called as
|
|
|
|
`env $(cat .env | xargs) inv generate-configuration` where
|
|
|
|
.env contains the variables.
|
2023-05-28 18:39:27 +00:00
|
|
|
"""
|
2023-09-04 18:22:53 +00:00
|
|
|
template_loader = jinja2.FileSystemLoader(searchpath="./")
|
|
|
|
template_env = jinja2.Environment(
|
|
|
|
loader=template_loader, undefined=jinja2.StrictUndefined
|
|
|
|
)
|
|
|
|
template = template_env.get_template("services.yml.j2")
|
2023-05-28 18:39:27 +00:00
|
|
|
|
2023-09-04 18:22:53 +00:00
|
|
|
with open("services.yml", "w") as outfile:
|
|
|
|
outfile.write(template.render(**os.environ))
|
2023-05-28 18:39:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@invoke.task()
|
2023-09-04 18:22:53 +00:00
|
|
|
def start(ctx, services: str):
|
2023-05-28 18:39:27 +00:00
|
|
|
"""
|
2023-09-04 18:22:53 +00:00
|
|
|
Starts one of more services, defined by a comma-separated list of labels.
|
2023-05-28 18:39:27 +00:00
|
|
|
|
2023-09-04 18:22:53 +00:00
|
|
|
Services should correspond to an entry in `services.yml`.
|
2023-05-28 18:39:27 +00:00
|
|
|
"""
|
2023-09-04 18:22:53 +00:00
|
|
|
_ensure_swarm(ctx)
|
|
|
|
_ensure_networks(ctx)
|
|
|
|
|
|
|
|
current_version = _get_tag(ctx)
|
|
|
|
|
|
|
|
print("Starting services...")
|
|
|
|
|
|
|
|
for service in services.split(","):
|
|
|
|
with open("services.yml", "r") as config:
|
|
|
|
service_config = yaml.load(config, Loader=yaml.Loader)["services"][service]
|
|
|
|
|
|
|
|
ctx.run(
|
|
|
|
f"docker build -t spadinaistan-{service}:{current_version} -f services/Dockerfile-{service} .",
|
|
|
|
hide=True,
|
|
|
|
)
|
|
|
|
ports_args = (
|
|
|
|
f"--publish {ports}\\\n" for ports in service_config.get("ports", [])
|
|
|
|
)
|
|
|
|
|
|
|
|
volume_args = []
|
|
|
|
|
|
|
|
for volume in service_config.get("volumes", []):
|
|
|
|
source, target = volume.split(":")
|
|
|
|
volume_args.append(f"--mount type=bind,source={source},target={target}\\\n")
|
|
|
|
|
|
|
|
env_files = (
|
|
|
|
f"--env-file {env_file}\\\n"
|
|
|
|
for env_file in service_config.get("env_files", [])
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
out = ctx.run(
|
|
|
|
f"""docker service create \
|
|
|
|
--name spad-{service}\
|
|
|
|
--network spad-internal\
|
|
|
|
{" ".join(env_files)}\
|
|
|
|
--hostname=\"{service}\"\
|
|
|
|
{" ".join(volume_args)}\
|
|
|
|
{" ".join(ports_args)}\
|
|
|
|
spadinaistan-{service}:{current_version}""",
|
|
|
|
hide=True,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
print(out.stdout)
|
|
|
|
print(out.stderr)
|
|
|
|
continue
|
|
|
|
|
|
|
|
print(f"\t✅ {service} is running.")
|
2023-05-28 18:39:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@invoke.task()
|
2023-09-04 18:22:53 +00:00
|
|
|
def stop(ctx: invoke.context.Context, services: str):
|
2023-05-28 18:39:27 +00:00
|
|
|
"""
|
2023-09-04 18:22:53 +00:00
|
|
|
Stops the provided list of services, as a comma-separated list
|
|
|
|
of labels.
|
2023-05-28 18:39:27 +00:00
|
|
|
"""
|
2023-09-04 18:22:53 +00:00
|
|
|
print("Stopping services...")
|
|
|
|
for service in services.split(","):
|
|
|
|
ctx.run(f"docker service rm spad-{service}", hide=True)
|
|
|
|
print(f"\t✅ {service} is stopped.")
|
2023-05-28 18:39:27 +00:00
|
|
|
|
|
|
|
|
2023-09-04 18:22:53 +00:00
|
|
|
services = invoke.Collection("service")
|
2023-05-28 18:39:27 +00:00
|
|
|
services.add_task(start)
|
|
|
|
services.add_task(stop)
|
|
|
|
|
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)
|
2023-09-04 18:22:53 +00:00
|
|
|
ns.add_task(generate_configuration)
|