feat: order/delay in service files, enable cmd, --all on unit commands
- 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
This commit is contained in:
+121
-39
@@ -2,27 +2,48 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/initsys"
|
||||
)
|
||||
|
||||
var dryRun bool
|
||||
var dryRun, installAllFlag 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: "install <name> | --all",
|
||||
Short: "Generate and install the service file for one unit (or all with --all)",
|
||||
Long: `Install generates the service file for the named unit using the configured
|
||||
init system. It does not enable or start it. The unit must be enabled in the
|
||||
config.
|
||||
|
||||
Use --dry-run to preview the generated unit files without writing anything.`,
|
||||
With --all it installs every enabled unit (same as 'installall'); disabled
|
||||
units that are installed are stopped, disabled and removed.
|
||||
|
||||
Use --dry-run to preview the generated files without writing anything.`,
|
||||
Args: nameOrAll(&installAllFlag),
|
||||
RunE: runInstall,
|
||||
}
|
||||
|
||||
var installallCmd = &cobra.Command{
|
||||
Use: "installall",
|
||||
Short: "Generate and install service files for all enabled units",
|
||||
Long: `Installall generates service files for all enabled units using the configured
|
||||
init system. It does not enable or start them. Units that are disabled in the
|
||||
config are stopped, disabled and their service files removed.
|
||||
|
||||
Use --dry-run to preview the generated files without writing anything.`,
|
||||
RunE: runInstallall,
|
||||
}
|
||||
|
||||
func init() {
|
||||
installCmd.Flags().BoolVar(&dryRun, "dry-run", false, "preview unit files without writing")
|
||||
rootCmd.AddCommand(installCmd)
|
||||
installCmd.Flags().BoolVar(&installAllFlag, "all", false, "install all enabled units")
|
||||
for _, c := range []*cobra.Command{installCmd, installallCmd} {
|
||||
c.Flags().BoolVar(&dryRun, "dry-run", false, "preview service files without writing")
|
||||
rootCmd.AddCommand(c)
|
||||
}
|
||||
}
|
||||
|
||||
func runInstall(cmd *cobra.Command, args []string) error {
|
||||
@@ -30,61 +51,122 @@ func runInstall(cmd *cobra.Command, args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
ini, err := initsys.New(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
installed := 0
|
||||
skipped := 0
|
||||
removed := 0
|
||||
if installAllFlag {
|
||||
installAll(cfg, ini, dryRun, os.Stdout)
|
||||
return nil
|
||||
}
|
||||
return installOne(cfg, ini, args[0], dryRun, os.Stdout)
|
||||
}
|
||||
|
||||
func runInstallall(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ini, err := initsys.New(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
installAll(cfg, ini, dryRun, os.Stdout)
|
||||
return nil
|
||||
}
|
||||
|
||||
// installOne installs a single enabled unit. Disabled units are an error:
|
||||
// removing files is installall's and update's job.
|
||||
func installOne(cfg *config.Config, ini initsys.Init, name string, dry bool, w io.Writer) error {
|
||||
u := cfg.FindUnit(name)
|
||||
if u == nil {
|
||||
return fmt.Errorf("unit %q not found", name)
|
||||
}
|
||||
if !u.Enabled {
|
||||
return fmt.Errorf("unit %q is disabled — run 'unitdore update %s --enable' first", name, name)
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
path, err := ini.UnitPath(*u, prefix, suffix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dry {
|
||||
content, err := ini.Generate(*u, prefix, suffix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(w, "\n--- %s ---\n%s\n", path, content)
|
||||
return nil
|
||||
}
|
||||
if err := ini.Install(*u, prefix, suffix); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(w, " ✓ installed: %s\n", path)
|
||||
lingerHint(w, ini, map[string]bool{}, *u)
|
||||
return nil
|
||||
}
|
||||
|
||||
func installAll(cfg *config.Config, ini initsys.Init, dry bool, w io.Writer) {
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
installed, skipped, removed := 0, 0, 0
|
||||
lingerSeen := map[string]bool{}
|
||||
|
||||
for _, u := range cfg.Units {
|
||||
name := ini.ServiceName(u, prefix, suffix)
|
||||
|
||||
if !u.Enabled {
|
||||
// Remove unit file if it exists for disabled units
|
||||
if ini.IsInstalled(u, prefix, suffix) {
|
||||
if dryRun {
|
||||
fmt.Printf(" ~ would remove: %s\n", ini.ServiceName(u, prefix, suffix))
|
||||
} else {
|
||||
if err := ini.Uninstall(u, prefix, suffix); err != nil {
|
||||
fmt.Printf(" ✗ failed to remove %s: %v\n", u.Name, err)
|
||||
} else {
|
||||
fmt.Printf(" - removed: %s (disabled)\n", ini.ServiceName(u, prefix, suffix))
|
||||
removed++
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !ini.IsInstalled(u, prefix, suffix) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
if dry {
|
||||
fmt.Fprintf(w, " ~ would remove: %s\n", name)
|
||||
continue
|
||||
}
|
||||
// Stop and disable first so the service doesn't outlive its file
|
||||
if err := ini.Disable(u, prefix, suffix); err != nil {
|
||||
fmt.Fprintf(w, " ✗ failed to disable %s: %v\n", u.Name, err)
|
||||
continue
|
||||
}
|
||||
if err := ini.Uninstall(u, prefix, suffix); err != nil {
|
||||
fmt.Fprintf(w, " ✗ failed to remove %s: %v\n", u.Name, err)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(w, " - removed: %s (disabled)\n", name)
|
||||
removed++
|
||||
continue
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
if dry {
|
||||
content, err := ini.Generate(u, prefix, suffix)
|
||||
if err != nil {
|
||||
fmt.Printf(" ✗ %s: %v\n", u.Name, err)
|
||||
fmt.Fprintf(w, " ✗ %s: %v\n", u.Name, err)
|
||||
continue
|
||||
}
|
||||
path, _ := ini.UnitPath(u, prefix, suffix)
|
||||
fmt.Printf("\n--- %s ---\n%s\n", path, content)
|
||||
path, err := ini.UnitPath(u, prefix, suffix)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, " ✗ %s: %v\n", u.Name, err)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(w, "\n--- %s ---\n%s\n", path, content)
|
||||
installed++
|
||||
continue
|
||||
}
|
||||
|
||||
if err := ini.Install(u, prefix, suffix); err != nil {
|
||||
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
|
||||
} else {
|
||||
path, _ := ini.UnitPath(u, prefix, suffix)
|
||||
fmt.Printf(" ✓ installed: %s\n", path)
|
||||
lingerHint(ini, lingerSeen, u)
|
||||
installed++
|
||||
fmt.Fprintf(w, " ✗ failed: %s: %v\n", u.Name, err)
|
||||
continue
|
||||
}
|
||||
path, _ := ini.UnitPath(u, prefix, suffix)
|
||||
fmt.Fprintf(w, " ✓ installed: %s\n", path)
|
||||
lingerHint(w, ini, lingerSeen, u)
|
||||
installed++
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
fmt.Printf("\nDone. Installed: %d Removed: %d Skipped: %d\n", installed, removed, skipped)
|
||||
verb := "Installed"
|
||||
if dry {
|
||||
verb = "Would install"
|
||||
}
|
||||
return nil
|
||||
fmt.Fprintf(w, "\nDone. %s: %d Removed: %d Skipped: %d\n", verb, installed, removed, skipped)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user