* Implement startall to enable and start all installed units * Implement stopall to stop and disable all running units
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/warkanum/unitdore/config"
|
|
"github.com/warkanum/unitdore/systemd"
|
|
)
|
|
|
|
var stopallCmd = &cobra.Command{
|
|
Use: "stopall",
|
|
Short: "Stop and disable all running managed units",
|
|
Long: `Stopall runs 'systemctl disable --now' for all enabled, installed units.`,
|
|
RunE: runStopall,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(stopallCmd)
|
|
}
|
|
|
|
func runStopall(cmd *cobra.Command, args []string) error {
|
|
cfg, err := config.Load(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
prefix, suffix := cfg.Prefix, cfg.Suffix
|
|
|
|
// Reverse order for shutdown
|
|
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
|
|
})
|
|
|
|
stopped := 0
|
|
failed := 0
|
|
|
|
for _, u := range units {
|
|
if !systemd.IsInstalled(u, prefix, suffix) {
|
|
continue
|
|
}
|
|
fmt.Printf(" ■ stopping: %s...\n", systemd.ServiceName(u, prefix, suffix))
|
|
if err := systemd.Disable(u, prefix, suffix); err != nil {
|
|
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
|
|
failed++
|
|
} else {
|
|
fmt.Printf(" ✓ stopped: %s\n", u.Name)
|
|
stopped++
|
|
}
|
|
}
|
|
|
|
fmt.Printf("\nDone. Stopped: %d Failed: %d\n", stopped, failed)
|
|
return nil
|
|
}
|