Phase 3+4: systemd generator, install, active, status

- systemd/generator.go: build .service file content from Unit
- systemd/manager.go: install/uninstall/enable/disable/status via systemctl
- cmd/install.go: write unit files, --dry-run flag, remove disabled units
- cmd/active.go: enable + start units in order
- cmd/status.go: summary table with name/runtime/user/enabled/installed/state
This commit is contained in:
2026-04-03 14:46:25 +02:00
parent aa7d85822c
commit 203e8e3f04
5 changed files with 522 additions and 0 deletions

105
systemd/generator.go Normal file
View File

@@ -0,0 +1,105 @@
package systemd
import (
"fmt"
"strings"
"text/template"
"github.com/warkanum/unitdore/config"
)
const systemUnitTemplate = `# /etc/systemd/system/{{.ServiceName}}
# Generated by unitdore — do not edit manually
[Unit]
Description=Unitdore: {{.Unit.Name}} ({{.Unit.Runtime}})
After=network.target
[Service]
Type=simple
ExecStart={{.ExecStart}}
ExecStop={{.ExecStop}}
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
`
const userUnitTemplate = `# ~/.config/systemd/user/{{.ServiceName}}
# Generated by unitdore — do not edit manually
[Unit]
Description=Unitdore: {{.Unit.Name}} ({{.Unit.Runtime}})
After=default.target
[Service]
Type=simple
ExecStart={{.ExecStart}}
ExecStop={{.ExecStop}}
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target
`
type templateData struct {
ServiceName string
Unit config.Unit
ExecStart string
ExecStop string
}
// ServiceName returns the systemd service name for a unit.
func ServiceName(u config.Unit) string {
return fmt.Sprintf("unitdore-%s.service", u.Name)
}
// Generate produces the .service file content for a unit.
func Generate(u config.Unit) (string, error) {
execStart, execStop := buildExecCommands(u)
data := templateData{
ServiceName: ServiceName(u),
Unit: u,
ExecStart: execStart,
ExecStop: execStop,
}
tmplStr := systemUnitTemplate
if u.User != "" {
tmplStr = userUnitTemplate
}
tmpl, err := template.New("unit").Parse(tmplStr)
if err != nil {
return "", fmt.Errorf("parsing template: %w", err)
}
var buf strings.Builder
if err := tmpl.Execute(&buf, data); err != nil {
return "", fmt.Errorf("rendering template: %w", err)
}
return buf.String(), nil
}
func buildExecCommands(u config.Unit) (start, stop string) {
// If a custom command is provided, use it directly
if u.Command != "" {
return u.Command, ""
}
switch u.Runtime {
case "podman":
start = fmt.Sprintf("/usr/bin/podman start -a %s", u.Name)
stop = fmt.Sprintf("/usr/bin/podman stop %s", u.Name)
case "docker":
start = fmt.Sprintf("/usr/bin/docker start %s", u.Name)
stop = fmt.Sprintf("/usr/bin/docker stop %s", u.Name)
default:
start = fmt.Sprintf("/usr/bin/%s start %s", u.Runtime, u.Name)
stop = fmt.Sprintf("/usr/bin/%s stop %s", u.Runtime, u.Name)
}
return start, stop
}

145
systemd/manager.go Normal file
View File

@@ -0,0 +1,145 @@
package systemd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/warkanum/unitdore/config"
)
const systemUnitDir = "/etc/systemd/system"
// UnitPath returns the filesystem path where the .service file should be written.
func UnitPath(u config.Unit) (string, error) {
if u.User == "" {
return filepath.Join(systemUnitDir, ServiceName(u)), nil
}
// Rootless: write to the user's systemd config dir
home, err := userHomeDir(u.User)
if err != nil {
return "", err
}
return filepath.Join(home, ".config", "systemd", "user", ServiceName(u)), nil
}
// Install writes the .service file for a unit and reloads systemd.
func Install(u config.Unit) error {
content, err := Generate(u)
if err != nil {
return err
}
path, err := UnitPath(u)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return fmt.Errorf("creating unit dir: %w", err)
}
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
return fmt.Errorf("writing unit file: %w", err)
}
return daemonReload(u.User)
}
// Uninstall removes the .service file for a unit and reloads systemd.
func Uninstall(u config.Unit) error {
path, err := UnitPath(u)
if err != nil {
return err
}
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("removing unit file: %w", err)
}
return daemonReload(u.User)
}
// Enable enables and starts a unit.
func Enable(u config.Unit) error {
return systemctl(u.User, "enable", "--now", ServiceName(u))
}
// Disable disables and stops a unit.
func Disable(u config.Unit) error {
return systemctl(u.User, "disable", "--now", ServiceName(u))
}
// Status returns the ActiveState of a unit ("active", "inactive", "failed", "unknown").
func Status(u config.Unit) string {
args := []string{"show", "-p", "ActiveState", "--value", ServiceName(u)}
var out []byte
var err error
if u.User == "" {
out, err = exec.Command("systemctl", args...).Output()
} else {
out, err = runAsUser(u.User, "systemctl", append([]string{"--user"}, args...)...)
}
if err != nil {
return "unknown"
}
return strings.TrimSpace(string(out))
}
// IsInstalled returns true if the unit file exists on disk.
func IsInstalled(u config.Unit) bool {
path, err := UnitPath(u)
if err != nil {
return false
}
_, err = os.Stat(path)
return err == nil
}
// --- helpers ---
func daemonReload(user string) error {
if user == "" {
return systemctl("", "daemon-reload")
}
return systemctl(user, "daemon-reload")
}
func systemctl(user string, args ...string) error {
if user == "" {
cmd := exec.Command("systemctl", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
return runAsUserCmd(user, "systemctl", append([]string{"--user"}, args...)...)
}
func runAsUser(user string, bin string, args ...string) ([]byte, error) {
full := append([]string{"-u", user, "--", bin}, args...)
return exec.Command("runuser", full...).Output()
}
func runAsUserCmd(user string, bin string, args ...string) error {
full := append([]string{"-u", user, "--", bin}, args...)
cmd := exec.Command("runuser", full...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func userHomeDir(user string) (string, error) {
out, err := exec.Command("getent", "passwd", user).Output()
if err != nil {
return "", fmt.Errorf("looking up user %s: %w", user, err)
}
// passwd format: name:pass:uid:gid:gecos:home:shell
parts := strings.Split(strings.TrimSpace(string(out)), ":")
if len(parts) < 6 {
return "", fmt.Errorf("unexpected passwd entry for %s", user)
}
return parts[5], nil
}