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:
+63
-32
@@ -3,6 +3,8 @@ package cmd
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
@@ -38,44 +40,73 @@ func runUpdate(cmd *cobra.Command, args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
u := cfg.FindUnit(name)
|
||||
if u == nil {
|
||||
return fmt.Errorf("unit %q not found", name)
|
||||
}
|
||||
|
||||
if cmd.Flags().Changed("order") {
|
||||
u.Order = updateOrder
|
||||
fmt.Printf(" order → %d\n", u.Order)
|
||||
}
|
||||
if updateEnable {
|
||||
u.Enabled = true
|
||||
u.DisabledReason = ""
|
||||
fmt.Printf(" enabled\n")
|
||||
}
|
||||
if updateDisable {
|
||||
u.Enabled = false
|
||||
fmt.Printf(" disabled\n")
|
||||
}
|
||||
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
ini, err := initsys.New(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ini.IsInstalled(*u, prefix, suffix) {
|
||||
if err := ini.Install(*u, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("recreating service file: %w", err)
|
||||
}
|
||||
path, _ := ini.UnitPath(*u, prefix, suffix)
|
||||
fmt.Printf(" ✓ service file recreated: %s\n", path)
|
||||
var order *int
|
||||
if cmd.Flags().Changed("order") {
|
||||
order = &updateOrder
|
||||
}
|
||||
return applyUpdate(cfg, ini, args[0], order, updateEnable, updateDisable, configPath, os.Stdout)
|
||||
}
|
||||
|
||||
// applyUpdate changes a unit's config, saves it and brings the installed
|
||||
// service file in line: re-created if enabled, stopped and removed if disabled.
|
||||
func applyUpdate(cfg *config.Config, ini initsys.Init, name string, order *int, enable, disable bool, path string, w io.Writer) error {
|
||||
u := cfg.FindUnit(name)
|
||||
if u == nil {
|
||||
return fmt.Errorf("unit %q not found", name)
|
||||
}
|
||||
|
||||
if order != nil {
|
||||
u.Order = *order
|
||||
fmt.Fprintf(w, " order → %d\n", u.Order)
|
||||
}
|
||||
if enable {
|
||||
u.Enabled = true
|
||||
u.DisabledReason = ""
|
||||
fmt.Fprintf(w, " enabled\n")
|
||||
}
|
||||
if disable {
|
||||
u.Enabled = false
|
||||
fmt.Fprintf(w, " disabled\n")
|
||||
}
|
||||
|
||||
if err := cfg.ResolveOrder(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cfg.Save(path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
// The target is stopped and removed if now disabled
|
||||
if ini.IsInstalled(*u, prefix, suffix) && !u.Enabled {
|
||||
svcPath, _ := ini.UnitPath(*u, prefix, suffix)
|
||||
if err := ini.Disable(*u, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("disabling unit: %w", err)
|
||||
}
|
||||
if err := ini.Uninstall(*u, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("removing service file: %w", err)
|
||||
}
|
||||
fmt.Fprintf(w, " ✓ service stopped and removed: %s\n", svcPath)
|
||||
}
|
||||
|
||||
// Order and enabled state change the After=/delay of other units too, so
|
||||
// every installed, enabled unit is regenerated
|
||||
for i := range cfg.Units {
|
||||
o := cfg.Units[i]
|
||||
if !o.Enabled || !ini.IsInstalled(o, prefix, suffix) {
|
||||
continue
|
||||
}
|
||||
if err := ini.Install(o, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("recreating service file for %s: %w", o.Name, err)
|
||||
}
|
||||
svcPath, _ := ini.UnitPath(o, prefix, suffix)
|
||||
fmt.Fprintf(w, " ✓ service file recreated: %s\n", svcPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user