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:
2026-04-03 14:46:25 +02:00
parent aa7d85822c
commit 203e8e3f04
5 changed files with 522 additions and 0 deletions

63
cmd/active.go Normal file
View 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
}

83
cmd/install.go Normal file
View File

@@ -0,0 +1,83 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var dryRun bool
var installCmd = &cobra.Command{
Use: "install",
Short: "Generate and install systemd unit files for all enabled units",
Long: `Install generates .service files for all enabled units and writes them
to the appropriate systemd directory. It does not enable or start them.
Use --dry-run to preview the generated unit files without writing anything.`,
RunE: runInstall,
}
func init() {
installCmd.Flags().BoolVar(&dryRun, "dry-run", false, "preview unit files without writing")
rootCmd.AddCommand(installCmd)
}
func runInstall(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath)
if err != nil {
return err
}
installed := 0
skipped := 0
removed := 0
for _, u := range cfg.Units {
if !u.Enabled {
// Remove unit file if it exists for disabled units
if systemd.IsInstalled(u) {
if dryRun {
fmt.Printf(" ~ would remove: %s\n", systemd.ServiceName(u))
} else {
if err := systemd.Uninstall(u); err != nil {
fmt.Printf(" ✗ failed to remove %s: %v\n", u.Name, err)
} else {
fmt.Printf(" - removed: %s (disabled)\n", systemd.ServiceName(u))
removed++
}
}
} else {
skipped++
}
continue
}
if dryRun {
content, err := systemd.Generate(u)
if err != nil {
fmt.Printf(" ✗ %s: %v\n", u.Name, err)
continue
}
path, _ := systemd.UnitPath(u)
fmt.Printf("\n--- %s ---\n%s\n", path, content)
installed++
continue
}
if err := systemd.Install(u); err != nil {
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
} else {
path, _ := systemd.UnitPath(u)
fmt.Printf(" ✓ installed: %s\n", path)
installed++
}
}
if !dryRun {
fmt.Printf("\nDone. Installed: %d Removed: %d Skipped: %d\n", installed, removed, skipped)
}
return nil
}

126
cmd/status.go Normal file
View File

@@ -0,0 +1,126 @@
package cmd
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/warkanum/unitdore/config"
"github.com/warkanum/unitdore/systemd"
)
var statusCmd = &cobra.Command{
Use: "status",
Short: "Show status summary of all managed units",
RunE: runStatus,
}
func init() {
rootCmd.AddCommand(statusCmd)
}
type statusRow struct {
Name string
Runtime string
User string
Enabled string
Installed string
State string
Reason string
}
func runStatus(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configPath)
if err != nil {
return err
}
if len(cfg.Units) == 0 {
fmt.Println("No units configured. Run 'unitdore syncup' to discover containers.")
return nil
}
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
})
rows := make([]statusRow, 0, len(units))
for _, u := range units {
user := u.User
if user == "" {
user = "root"
}
enabled := "yes"
if !u.Enabled {
enabled = "no"
}
installed := "yes"
if !systemd.IsInstalled(u) {
installed = "no"
}
state := "—"
if systemd.IsInstalled(u) {
state = systemd.Status(u)
}
rows = append(rows, statusRow{
Name: u.Name,
Runtime: u.Runtime,
User: user,
Enabled: enabled,
Installed: installed,
State: state,
Reason: u.DisabledReason,
})
}
// Calculate column widths
cols := []string{"NAME", "RUNTIME", "USER", "ENABLED", "INSTALLED", "STATE", "REASON"}
widths := []int{4, 7, 4, 7, 9, 5, 6}
for _, r := range rows {
vals := []string{r.Name, r.Runtime, r.User, r.Enabled, r.Installed, r.State, r.Reason}
for i, v := range vals {
if len(v) > widths[i] {
widths[i] = len(v)
}
}
}
// Print header
fmt.Println()
printRow(cols, widths)
fmt.Println(strings.Repeat("─", totalWidth(widths)))
// Print rows
for _, r := range rows {
vals := []string{r.Name, r.Runtime, r.User, r.Enabled, r.Installed, r.State, r.Reason}
printRow(vals, widths)
}
fmt.Println()
return nil
}
func printRow(vals []string, widths []int) {
for i, v := range vals {
fmt.Printf("%-*s", widths[i]+2, v)
}
fmt.Println()
}
func totalWidth(widths []int) int {
total := 0
for _, w := range widths {
total += w + 2
}
return total
}