Create systemd unit file generator from manifest service definitions.
Omni/Deploy/Systemd.py - Unit file generationdef generate_unit(service: Service) -> str:
"""Generate systemd unit file content for a service."""
# Determine ExecStart
binary = service.exec.command or service.name
exec_start = f"{service.artifact.storePath}/bin/{binary}"
# Build environment lines
env_lines = [f'Environment="{k}={v}"' for k, v in service.env.items()]
unit = f'''[Unit]
Description={service.name}
After={' '.join(service.systemd.after)}
{f"Requires={' '.join(service.systemd.requires)}" if service.systemd.requires else ""}
[Service]
Type=simple
ExecStart={exec_start}
User={service.exec.user}
Group={service.exec.group}
Restart={service.systemd.restart}
RestartSec={service.systemd.restartSec}
{chr(10).join(env_lines)}
{f"EnvironmentFile={service.envFile}" if service.envFile else ""}
# Hardening
PrivateTmp={"yes" if service.hardening.privateTmp else "no"}
ProtectSystem={service.hardening.protectSystem}
ProtectHome={"yes" if service.hardening.protectHome else "no"}
NoNewPrivileges=yes
[Install]
WantedBy=multi-user.target
'''
return unit.strip()
def write_unit(service: Service, base_dir: str = "/var/lib/biz-deployer/services"):
"""Write unit file to disk."""
path = f"{base_dir}/{service.name}.service"
content = generate_unit(service)
with open(path, 'w') as f:
f.write(content)
return path
def reload_and_restart(service_name: str):
"""Reload systemd and restart service."""
subprocess.run(["systemctl", "daemon-reload"], check=True)
subprocess.run(["systemctl", "enable", "--now", f"{service_name}.service"], check=True)
/var/lib/biz-deployer/services/<name>.servicebild --test Omni/Deploy/Systemd.py