Extract init-neutral unit resolution into service/, add initsys.Init with systemd, openrc and runit backends, selected by 'init:' config or autodetect. OpenRC and runit are root only; rootless units run as system services as that user. Add docs and generated examples for all three.
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/warkanum/unitdore/config"
|
|
"github.com/warkanum/unitdore/initsys"
|
|
)
|
|
|
|
var uninstallallCmd = &cobra.Command{
|
|
Use: "uninstallall",
|
|
Short: "Stop, disable, and remove service files for all installed units",
|
|
Long: `Uninstallall runs 'systemctl disable --now' and removes the .service file for every installed unit, in reverse startup order.`,
|
|
RunE: runUninstallall,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(uninstallallCmd)
|
|
}
|
|
|
|
func runUninstallall(cmd *cobra.Command, args []string) error {
|
|
cfg, err := config.Load(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
prefix, suffix := cfg.Prefix, cfg.Suffix
|
|
ini, err := initsys.New(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
units := make([]config.Unit, len(cfg.Units))
|
|
copy(units, cfg.Units)
|
|
sort.Slice(units, func(i, j int) bool {
|
|
if units[i].Order != units[j].Order {
|
|
return units[i].Order > units[j].Order
|
|
}
|
|
return units[i].Name > units[j].Name
|
|
})
|
|
|
|
removed := 0
|
|
failed := 0
|
|
|
|
for _, u := range units {
|
|
if !ini.IsInstalled(u, prefix, suffix) {
|
|
continue
|
|
}
|
|
svcName := ini.ServiceName(u, prefix, suffix)
|
|
fmt.Printf(" ■ disabling %s...\n", svcName)
|
|
if err := ini.Disable(u, prefix, suffix); err != nil {
|
|
fmt.Printf(" ✗ failed to disable %s: %v\n", u.Name, err)
|
|
failed++
|
|
continue
|
|
}
|
|
path, _ := ini.UnitPath(u, prefix, suffix)
|
|
fmt.Printf(" ✗ removing %s...\n", path)
|
|
if err := ini.Uninstall(u, prefix, suffix); err != nil {
|
|
fmt.Printf(" ✗ failed to remove %s: %v\n", u.Name, err)
|
|
failed++
|
|
continue
|
|
}
|
|
fmt.Printf(" ✓ uninstalled: %s\n", u.Name)
|
|
removed++
|
|
}
|
|
|
|
fmt.Printf("\nDone. Uninstalled: %d Failed: %d\n", removed, failed)
|
|
return nil
|
|
}
|