feat: add host actions, lifecycle hooks and operational tools
This commit is contained in:
1 parent
8d4915543e
commit
e5bde9c61f
32 files changed
+2083
-64
No files matched your search
+31
-8
@@ -3,13 +3,13 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
||||
"github.com/charmbracelet/x/term"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -25,7 +25,7 @@ func expandHostCommand(command config.HostCommand, host resolvedHost, extra []st
|
||||
}
|
||||
|
||||
func runCmd() *cobra.Command {
|
||||
var dryRun bool
|
||||
var dryRun, yes, status bool
|
||||
cmd := &cobra.Command{
|
||||
Use: "run [--dry-run] COMMAND HOST [-- EXTRA_ARGS...]",
|
||||
Short: "Run a configured local command for a NetBox or static host",
|
||||
@@ -39,9 +39,18 @@ func runCmd() *cobra.Command {
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown configured command %q", args[0])
|
||||
}
|
||||
host := resolveTarget(cfg, args[1])
|
||||
host, err := resolveTargetE(cfg, args[1], !dryRun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !command.Allowed(commandHost(host)) {
|
||||
return fmt.Errorf("command %q is not allowed for %s", args[0], host.Name)
|
||||
}
|
||||
argv := expandHostCommand(command, host, args[2:])
|
||||
if dryRun {
|
||||
if post := config.HookCommand(command.PostRun); post != "" {
|
||||
fmt.Fprintf(cmd.ErrOrStderr(), "# post_run (skipped in --dry-run): %s\n", post)
|
||||
}
|
||||
for i, arg := range argv {
|
||||
if i > 0 {
|
||||
fmt.Fprint(cmd.OutOrStdout(), " ")
|
||||
@@ -51,11 +60,19 @@ func runCmd() *cobra.Command {
|
||||
fmt.Fprintln(cmd.OutOrStdout())
|
||||
return nil
|
||||
}
|
||||
path, err := exec.LookPath(argv[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s not found in PATH: %w", argv[0], err)
|
||||
if command.Confirm {
|
||||
if err := confirmAction("Run "+args[0]+" on "+host.Name, yes); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return syscall.Exec(path, argv, os.Environ())
|
||||
timeout, _ := time.ParseDuration(command.Timeout)
|
||||
code := supervise(cfg, host, args[0], argv, command.PostRun, timeout, status || term.IsTerminal(os.Stderr.Fd()), os.Stdin, cmd.OutOrStdout(), cmd.ErrOrStderr())
|
||||
if code != 0 {
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SilenceUsage = true
|
||||
return &commandExit{code}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, prefix string) ([]string, cobra.ShellCompDirective) {
|
||||
if len(args) == 0 {
|
||||
@@ -78,6 +95,12 @@ func runCmd() *cobra.Command {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
},
|
||||
}
|
||||
cmd.Flags().BoolVar(&yes, "yes", false, "Approve an action requiring confirmation")
|
||||
cmd.Flags().BoolVar(&status, "status", false, "Show elapsed runtime")
|
||||
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the command without running it")
|
||||
return cmd
|
||||
}
|
||||
|
||||
type commandExit struct{ code int }
|
||||
|
||||
func (e *commandExit) Error() string { return fmt.Sprintf("command exited with status %d", e.code) }
|
||||
Reference in new issue
Block a user