- 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
131 lines
4.0 KiB
Go
131 lines
4.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/warkanum/unitdore/config"
|
|
"github.com/warkanum/unitdore/initsys"
|
|
)
|
|
|
|
// The *all commands are kept as shortcuts for '<command> --all'.
|
|
var (
|
|
startallCmd = &cobra.Command{
|
|
Use: "startall",
|
|
Short: "Start all installed, enabled units (same as start --all)",
|
|
RunE: bulkRun(func(cfg *config.Config, ini initsys.Init) {
|
|
bulkStart(cfg, ini, os.Stdout, initsys.Init.Start, "started")
|
|
}),
|
|
}
|
|
enableallCmd = &cobra.Command{
|
|
Use: "enableall",
|
|
Short: "Enable at boot and start all installed, enabled units (same as enable --all)",
|
|
RunE: bulkRun(func(cfg *config.Config, ini initsys.Init) {
|
|
bulkStart(cfg, ini, os.Stdout, initsys.Init.Enable, "enabled")
|
|
}),
|
|
}
|
|
stopallCmd = &cobra.Command{
|
|
Use: "stopall",
|
|
Short: "Stop all installed units (same as stop --all)",
|
|
RunE: bulkRun(func(cfg *config.Config, ini initsys.Init) { stopAll(cfg, ini, os.Stdout) }),
|
|
}
|
|
uninstallallCmd = &cobra.Command{
|
|
Use: "uninstallall",
|
|
Short: "Stop, disable and remove all installed units (same as uninstall --all)",
|
|
RunE: bulkRun(func(cfg *config.Config, ini initsys.Init) { uninstallAll(cfg, ini, os.Stdout) }),
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(startallCmd, enableallCmd, stopallCmd, uninstallallCmd)
|
|
}
|
|
|
|
func bulkRun(fn func(*config.Config, initsys.Init)) func(*cobra.Command, []string) error {
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
cfg, ini, err := loadInit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fn(cfg, ini)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
type unitAction func(ini initsys.Init, u config.Unit, prefix, suffix string) error
|
|
|
|
// bulkStart applies act to all enabled, installed units in startup order.
|
|
func bulkStart(cfg *config.Config, ini initsys.Init, w io.Writer, act unitAction, past string) {
|
|
prefix, suffix := cfg.Prefix, cfg.Suffix
|
|
var todo []config.Unit
|
|
for _, u := range sortUnits(cfg.Units, false) {
|
|
if !u.Enabled {
|
|
continue
|
|
}
|
|
if !ini.IsInstalled(u, prefix, suffix) {
|
|
fmt.Fprintf(w, " ! %s: not installed — run 'unitdore install %s' first\n", u.Name, u.Name)
|
|
continue
|
|
}
|
|
todo = append(todo, u)
|
|
}
|
|
|
|
ok, failed := runOrdered(todo, w, func(u config.Unit) (string, error) {
|
|
if err := act(ini, u, prefix, suffix); err != nil {
|
|
return "", err
|
|
}
|
|
return past + ": " + u.Name, nil
|
|
})
|
|
fmt.Fprintf(w, "\nDone. %s: %d Failed: %d\n", strings.ToUpper(past[:1])+past[1:], ok, failed)
|
|
}
|
|
|
|
// stopAll stops every installed unit in reverse startup order.
|
|
func stopAll(cfg *config.Config, ini initsys.Init, w io.Writer) {
|
|
prefix, suffix := cfg.Prefix, cfg.Suffix
|
|
stopped, failed := 0, 0
|
|
for _, u := range sortUnits(cfg.Units, true) {
|
|
if !ini.IsInstalled(u, prefix, suffix) {
|
|
continue
|
|
}
|
|
fmt.Fprintf(w, " ■ stopping: %s...\n", ini.ServiceName(u, prefix, suffix))
|
|
if err := ini.Stop(u, prefix, suffix); err != nil {
|
|
fmt.Fprintf(w, " ✗ failed: %s: %v\n", u.Name, err)
|
|
failed++
|
|
continue
|
|
}
|
|
fmt.Fprintf(w, " ✓ stopped: %s\n", u.Name)
|
|
stopped++
|
|
}
|
|
fmt.Fprintf(w, "\nDone. Stopped: %d Failed: %d\n", stopped, failed)
|
|
}
|
|
|
|
// uninstallAll disables and removes every installed unit in reverse startup order.
|
|
func uninstallAll(cfg *config.Config, ini initsys.Init, w io.Writer) {
|
|
prefix, suffix := cfg.Prefix, cfg.Suffix
|
|
removed, failed := 0, 0
|
|
|
|
for _, u := range sortUnits(cfg.Units, true) {
|
|
if !ini.IsInstalled(u, prefix, suffix) {
|
|
continue
|
|
}
|
|
fmt.Fprintf(w, " ■ disabling %s...\n", ini.ServiceName(u, prefix, suffix))
|
|
if err := ini.Disable(u, prefix, suffix); err != nil {
|
|
fmt.Fprintf(w, " ✗ failed to disable %s: %v\n", u.Name, err)
|
|
failed++
|
|
continue
|
|
}
|
|
path, _ := ini.UnitPath(u, prefix, suffix)
|
|
fmt.Fprintf(w, " ✗ removing %s...\n", path)
|
|
if err := ini.Uninstall(u, prefix, suffix); err != nil {
|
|
fmt.Fprintf(w, " ✗ failed to remove %s: %v\n", u.Name, err)
|
|
failed++
|
|
continue
|
|
}
|
|
fmt.Fprintf(w, " ✓ uninstalled: %s\n", u.Name)
|
|
removed++
|
|
}
|
|
|
|
fmt.Fprintf(w, "\nDone. Uninstalled: %d Failed: %d\n", removed, failed)
|
|
}
|