029b9cee2e
- startall now uses systemctl start (not enable --now) - stopall now uses systemctl stop (not disable --now) - add installall command (bulk install with --dry-run support) - add uninstallall command (bulk disable+remove in reverse order) - fix docker ExecStart to use -a flag so the process stays attached - prefix ExecStop with - so a stop-on-already-dead container does not fail the unit
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 all installed units",
|
|
Long: `Stopall runs 'systemctl stop' for all installed units in reverse startup order.`,
|
|
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.Stop(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
|
|
}
|