fix: couldn't return stdout or stderr during script execution if errors occur on deployment to local/ssh

This commit is contained in:
Fu Diwei
2025-01-16 16:47:33 +08:00
parent d712f07b96
commit dea4106569
4 changed files with 41 additions and 29 deletions

View File

@@ -103,7 +103,7 @@ func (d *SshDeployer) Deploy(ctx context.Context, certPem string, privkeyPem str
if d.config.PreCommand != "" {
stdout, stderr, err := execSshCommand(client, d.config.PreCommand)
if err != nil {
return nil, xerrors.Wrapf(err, "failed to run pre-command: stdout: %s, stderr: %s", stdout, stderr)
return nil, xerrors.Wrapf(err, "failed to execute pre-command: stdout: %s, stderr: %s", stdout, stderr)
}
d.logger.Logt("SSH pre-command executed", stdout)
@@ -160,7 +160,7 @@ func (d *SshDeployer) Deploy(ctx context.Context, certPem string, privkeyPem str
if d.config.PostCommand != "" {
stdout, stderr, err := execSshCommand(client, d.config.PostCommand)
if err != nil {
return nil, xerrors.Wrapf(err, "failed to run command, stdout: %s, stderr: %s", stdout, stderr)
return nil, xerrors.Wrapf(err, "failed to execute post-command, stdout: %s, stderr: %s", stdout, stderr)
}
d.logger.Logt("SSH post-command executed", stdout)
@@ -211,13 +211,13 @@ func execSshCommand(sshCli *ssh.Client, command string) (string, string, error)
}
defer session.Close()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
var stderrBuf bytes.Buffer
session.Stderr = &stderrBuf
stdoutBuf := bytes.NewBuffer(nil)
session.Stdout = stdoutBuf
stderrBuf := bytes.NewBuffer(nil)
session.Stderr = stderrBuf
err = session.Run(command)
if err != nil {
return "", "", err
return stdoutBuf.String(), stderrBuf.String(), xerrors.Wrap(err, "failed to execute ssh command")
}
return stdoutBuf.String(), stderrBuf.String(), nil

View File

@@ -69,6 +69,7 @@ func TestDeploy(t *testing.T) {
SshPort: int32(fSshPort),
SshUsername: fSshUsername,
SshPassword: fSshPassword,
OutputFormat: provider.OUTPUT_FORMAT_PEM,
OutputCertPath: fOutputCertPath,
OutputKeyPath: fOutputKeyPath,
})