From 3a174d34eafed9b83d514309b1d7c8137883bddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Date: Fri, 12 Jul 2024 16:21:54 +0200 Subject: [PATCH] Add stdin parameter to RunCmdStdMapping() In some cases we will need to pass a string to stdin. --- mgradm/cmd/install/podman/utils.go | 2 +- mgradm/shared/podman/podman.go | 2 +- mgrpxy/cmd/logs/kubernetes.go | 2 +- mgrpxy/cmd/logs/podman.go | 2 +- shared/connection.go | 6 +++--- shared/kubernetes/helm.go | 2 +- shared/kubernetes/utils.go | 2 +- shared/podman/images.go | 2 +- shared/podman/utils.go | 2 +- shared/utils/exec.go | 10 +++++++--- shared/utils/utils.go | 2 +- 11 files changed, 19 insertions(+), 15 deletions(-) diff --git a/mgradm/cmd/install/podman/utils.go b/mgradm/cmd/install/podman/utils.go index 3a9011637..eb266d972 100644 --- a/mgradm/cmd/install/podman/utils.go +++ b/mgradm/cmd/install/podman/utils.go @@ -130,7 +130,7 @@ func installForPodman( if path, err := exec.LookPath("uyuni-payg-extract-data"); err == nil { // the binary is installed - err = utils.RunCmdStdMapping(zerolog.DebugLevel, path) + err = utils.RunCmdStdMapping(zerolog.DebugLevel, "", path) if err != nil { return utils.Errorf(err, L("failed to extract payg data")) } diff --git a/mgradm/shared/podman/podman.go b/mgradm/shared/podman/podman.go index 2a3397f42..5c29f022d 100644 --- a/mgradm/shared/podman/podman.go +++ b/mgradm/shared/podman/podman.go @@ -441,7 +441,7 @@ func CallCloudGuestRegistryAuth() error { path, err := exec.LookPath(cloudguestregistryauth) if err == nil { // the binary is installed - return utils.RunCmdStdMapping(zerolog.DebugLevel, path) + return utils.RunCmdStdMapping(zerolog.DebugLevel, "", path) } // silently ignore error if it is missing return nil diff --git a/mgrpxy/cmd/logs/kubernetes.go b/mgrpxy/cmd/logs/kubernetes.go index a552493e6..6192bbdf3 100644 --- a/mgrpxy/cmd/logs/kubernetes.go +++ b/mgrpxy/cmd/logs/kubernetes.go @@ -57,7 +57,7 @@ func kubernetesLogs( commandArgs = append(commandArgs, args...) } - return utils.RunCmdStdMapping(zerolog.DebugLevel, "kubectl", commandArgs...) + return utils.RunCmdStdMapping(zerolog.DebugLevel, "", "kubectl", commandArgs...) } func isRFC3339(timestamp string) bool { diff --git a/mgrpxy/cmd/logs/podman.go b/mgrpxy/cmd/logs/podman.go index dc8f42ee5..4c46fcae5 100644 --- a/mgrpxy/cmd/logs/podman.go +++ b/mgrpxy/cmd/logs/podman.go @@ -43,5 +43,5 @@ func podmanLogs( commandArgs = append(commandArgs, args...) } - return utils.RunCmdStdMapping(zerolog.DebugLevel, "podman", commandArgs...) + return utils.RunCmdStdMapping(zerolog.DebugLevel, "", "podman", commandArgs...) } diff --git a/shared/connection.go b/shared/connection.go index 4954c9d99..57ca07264 100644 --- a/shared/connection.go +++ b/shared/connection.go @@ -229,7 +229,7 @@ func (c *Connection) Copy(src string, dst string, user string, group string) err return fmt.Errorf(L("unknown container kind: %s"), command) } - if err := utils.RunCmdStdMapping(zerolog.DebugLevel, command, commandArgs...); err != nil { + if err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", command, commandArgs...); err != nil { return err } @@ -241,7 +241,7 @@ func (c *Connection) Copy(src string, dst string, user string, group string) err owner = user + ":" + group } execArgs = append(execArgs, "chown", owner, strings.Replace(dst, "server:", "", 1)) - return utils.RunCmdStdMapping(zerolog.DebugLevel, command, execArgs...) + return utils.RunCmdStdMapping(zerolog.DebugLevel, "", command, execArgs...) } return nil } @@ -293,7 +293,7 @@ func (c *Connection) CopyCaCertificate(fqdn string) error { } log.Info().Msg(L("Updating host trusted certificates")) - return utils.RunCmdStdMapping(zerolog.DebugLevel, "update-ca-certificates") + return utils.RunCmdStdMapping(zerolog.DebugLevel, "", "update-ca-certificates") } // ChoosePodmanOrKubernetes selects either the podman or the kubernetes function based on the backend. diff --git a/shared/kubernetes/helm.go b/shared/kubernetes/helm.go index e07d5f6d0..a9ec21c0c 100644 --- a/shared/kubernetes/helm.go +++ b/shared/kubernetes/helm.go @@ -52,7 +52,7 @@ func HelmUpgrade(kubeconfig string, namespace string, install bool, if install { command = "install" } - if err := utils.RunCmdStdMapping(zerolog.DebugLevel, "helm", helmArgs...); err != nil { + if err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", "helm", helmArgs...); err != nil { // TODO We cannot use the command variable in the message as that would break localization if command == "upgrade" { return utils.Errorf(err, L("failed to upgrade helm chart %[1]s in namespace %[2]s"), chart, namespace) diff --git a/shared/kubernetes/utils.go b/shared/kubernetes/utils.go index 3713206b3..b9237216b 100644 --- a/shared/kubernetes/utils.go +++ b/shared/kubernetes/utils.go @@ -282,7 +282,7 @@ func RunPod(podname string, filter string, image string, pullPolicy string, comm } arguments = append(arguments, "--command", "--", command) - err := utils.RunCmdStdMapping(zerolog.DebugLevel, "kubectl", arguments...) + err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", "kubectl", arguments...) if err != nil { return utils.Errorf(err, PL("The first placeholder is a command", "cannot run %[1]s using image %[2]s"), command, image) diff --git a/shared/podman/images.go b/shared/podman/images.go index 310cc8606..8508f5d15 100644 --- a/shared/podman/images.go +++ b/shared/podman/images.go @@ -217,7 +217,7 @@ func pullImage(image string, args ...string) error { log.Debug().Msg("Additional arguments for pull command will not be shown.") } - return utils.RunCmdStdMapping(loglevel, "podman", podmanArgs...) + return utils.RunCmdStdMapping(loglevel, "", "podman", podmanArgs...) } // ShowAvailableTag returns the list of available tag for a given image. diff --git a/shared/podman/utils.go b/shared/podman/utils.go index d25254e62..31b2c7c9c 100644 --- a/shared/podman/utils.go +++ b/shared/podman/utils.go @@ -68,7 +68,7 @@ func RunContainer(name string, image string, volumes []types.VolumeMount, extraA podmanArgs = append(podmanArgs, image) podmanArgs = append(podmanArgs, cmd...) - err := utils.RunCmdStdMapping(zerolog.DebugLevel, "podman", podmanArgs...) + err := utils.RunCmdStdMapping(zerolog.DebugLevel, "", "podman", podmanArgs...) if err != nil { return utils.Errorf(err, L("failed to run %s container"), name) } diff --git a/shared/utils/exec.go b/shared/utils/exec.go index 5f86d3df9..8e69ff1f6 100644 --- a/shared/utils/exec.go +++ b/shared/utils/exec.go @@ -5,6 +5,7 @@ package utils import ( + "bytes" "fmt" "os" "os/exec" @@ -45,15 +46,18 @@ func RunCmd(command string, args ...string) error { } // RunCmdStdMapping execute a shell command mapping the stdout and stderr. -func RunCmdStdMapping(logLevel zerolog.Level, command string, args ...string) error { +func RunCmdStdMapping(logLevel zerolog.Level, stdin string, command string, args ...string) error { localLogger := log.Level(logLevel) localLogger.Debug().Msgf("Running: %s %s", command, strings.Join(args, " ")) runCmd := exec.Command(command, args...) + if stdin != "" { + runCmd.Stdin = bytes.NewBuffer([]byte(stdin)) + } + runCmd.Stdout = os.Stdout runCmd.Stderr = os.Stderr - err := runCmd.Run() - return err + return runCmd.Run() } // RunCmdOutput execute a shell command and collects output. diff --git a/shared/utils/utils.go b/shared/utils/utils.go index 3d731f8f4..e9e83252e 100644 --- a/shared/utils/utils.go +++ b/shared/utils/utils.go @@ -349,7 +349,7 @@ func InspectHost(serverHost bool) (*HostInspectData, error) { return nil, err } - if err := RunCmdStdMapping(zerolog.DebugLevel, inspector.GetScriptPath()); err != nil { + if err := RunCmdStdMapping(zerolog.DebugLevel, "", inspector.GetScriptPath()); err != nil { return nil, Errorf(err, L("failed to run inspect script in host system")) }