feat: order/delay in service files, enable cmd, --all on unit commands

- install/uninstall/start/stop/enable take <name> or --all; *all kept as shortcuts
- add enable (enable at boot + start); startall no longer enables
- order/delay resolved by config.ResolveOrder and written into systemd,
  OpenRC and runit service files (After=/sleep), so they apply at boot
- install --all stops and disables units before removing their files
- update regenerates all installed, enabled units and removes disabled ones
- edit creates the config dir; docs and man page updated
- tests: fake Init for cmd, ResolveOrder, backend ordering output
This commit is contained in:
2026-10-12 21:31:41 +02:00
parent d1d0889eb3
commit 91811b4fa1
28 changed files with 1394 additions and 503 deletions
+25 -4
View File
@@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/service"
@@ -194,13 +195,28 @@ func (b Backend) Generate(u config.Unit, prefix, suffix string) (string, error)
if u.Runtime == "docker" {
w.WriteString("\tneed docker\n")
}
if len(u.After) > 0 {
names := make([]string, len(u.After))
for i, n := range u.After {
names[i] = service.BaseName(config.Unit{Name: n}, prefix, suffix)
}
fmt.Fprintf(&w, "\tafter %s\n", strings.Join(names, " "))
}
w.WriteString("}\n")
var pre []string
if spec.Type != service.TypeOneshot && user != nil {
pre = append(pre, fmt.Sprintf("checkpath -d -m 0700 -o %s %s",
service.ShellQuote(user.Name), service.ShellQuote(user.RuntimeDir)))
}
if secs := delaySeconds(u); secs > 0 {
pre = append(pre, fmt.Sprintf("sleep %d", secs))
}
if len(pre) > 0 {
fmt.Fprintf(&w, "\nstart_pre() {\n\t%s\n}\n", strings.Join(pre, "\n\t"))
}
if spec.Type != service.TypeOneshot {
if user != nil {
fmt.Fprintf(&w, "\nstart_pre() {\n\tcheckpath -d -m 0700 -o %s %s\n}\n",
service.ShellQuote(user.Name), service.ShellQuote(user.RuntimeDir))
}
if stop != "" {
fmt.Fprintf(&w, "\nstop_pre() {\n\t%s || true\n}\n", asUser(user, stop))
}
@@ -277,3 +293,8 @@ func dq(s string) string {
r := strings.NewReplacer(`\`, `\\`, `"`, `\"`, "$", `\$`, "`", "\\`")
return `"` + r.Replace(s) + `"`
}
// delaySeconds rounds the unit's start delay up to whole seconds.
func delaySeconds(u config.Unit) int {
return int((u.StartDelay + time.Second - 1) / time.Second)
}