Implement systemd unit file generator

t-266.6·WorkTask·
·
·
·Omni/Deploy.hs
Parent:t-266·Created2 months ago·Updated2 months ago

Dependencies

Description

Edit

Create systemd unit file generator from manifest service definitions.

Files to Create

  • Omni/Deploy/Systemd.py - Unit file generation

Implementation

def 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)

Unit File Location

  • Write to: /var/lib/biz-deployer/services/<name>.service
  • systemd can load units from absolute paths

Testing

bild --test Omni/Deploy/Systemd.py

Timeline (2)

🔄[human]Open → InProgress2 months ago
🔄[human]InProgress → Done2 months ago