Phase 3+4: systemd generator, install, active, status
- systemd/generator.go: build .service file content from Unit - systemd/manager.go: install/uninstall/enable/disable/status via systemctl - cmd/install.go: write unit files, --dry-run flag, remove disabled units - cmd/active.go: enable + start units in order - cmd/status.go: summary table with name/runtime/user/enabled/installed/state
This commit is contained in:
63
cmd/active.go
Normal file
63
cmd/active.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var activeCmd = &cobra.Command{
|
||||
Use: "active",
|
||||
Short: "Enable and start all installed, enabled units",
|
||||
Long: `Active runs 'systemctl enable --now' for all enabled units that have
|
||||
been installed. Units must be installed first via 'unitdore install'.`,
|
||||
RunE: runActive,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(activeCmd)
|
||||
}
|
||||
|
||||
func runActive(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Sort by order then name for deterministic startup sequence
|
||||
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
|
||||
})
|
||||
|
||||
started := 0
|
||||
failed := 0
|
||||
|
||||
for _, u := range units {
|
||||
if !u.Enabled {
|
||||
continue
|
||||
}
|
||||
if !systemd.IsInstalled(u) {
|
||||
fmt.Printf(" ! %s: not installed — run 'unitdore install' first\n", u.Name)
|
||||
continue
|
||||
}
|
||||
fmt.Printf(" ▶ enabling: %s...\n", systemd.ServiceName(u))
|
||||
if err := systemd.Enable(u); err != nil {
|
||||
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
|
||||
failed++
|
||||
} else {
|
||||
fmt.Printf(" ✓ started: %s\n", u.Name)
|
||||
started++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("\nDone. Started: %d Failed: %d\n", started, failed)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user