mirror of
https://github.com/woodchen-ink/certimate.git
synced 2025-07-18 09:21:56 +08:00
fix: couldn't return stdout or stderr during script execution if errors occur on deployment to local/ssh
This commit is contained in:
parent
d712f07b96
commit
dea4106569
@ -75,7 +75,7 @@ func (d *LocalDeployer) Deploy(ctx context.Context, certPem string, privkeyPem s
|
||||
if d.config.PreCommand != "" {
|
||||
stdout, stderr, err := execCommand(d.config.ShellEnv, 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("pre-command executed", stdout)
|
||||
@ -132,7 +132,7 @@ func (d *LocalDeployer) Deploy(ctx context.Context, certPem string, privkeyPem s
|
||||
if d.config.PostCommand != "" {
|
||||
stdout, stderr, err := execCommand(d.config.ShellEnv, 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("post-command executed", stdout)
|
||||
@ -154,7 +154,7 @@ func execCommand(shellEnv ShellEnvType, command string) (string, string, error)
|
||||
case SHELL_ENV_POWERSHELL:
|
||||
cmd = exec.Command("powershell", "-Command", command)
|
||||
|
||||
case "":
|
||||
case ShellEnvType(""):
|
||||
if runtime.GOOS == "windows" {
|
||||
cmd = exec.Command("cmd", "/C", command)
|
||||
} else {
|
||||
@ -165,14 +165,13 @@ func execCommand(shellEnv ShellEnvType, command string) (string, string, error)
|
||||
return "", "", fmt.Errorf("unsupported shell env: %s", shellEnv)
|
||||
}
|
||||
|
||||
var stdoutBuf bytes.Buffer
|
||||
cmd.Stdout = &stdoutBuf
|
||||
var stderrBuf bytes.Buffer
|
||||
cmd.Stderr = &stderrBuf
|
||||
|
||||
stdoutBuf := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = stdoutBuf
|
||||
stderrBuf := bytes.NewBuffer(nil)
|
||||
cmd.Stderr = stderrBuf
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return "", "", xerrors.Wrap(err, "failed to execute shell command")
|
||||
return stdoutBuf.String(), stderrBuf.String(), xerrors.Wrap(err, "failed to execute command")
|
||||
}
|
||||
|
||||
return stdoutBuf.String(), stderrBuf.String(), nil
|
||||
|
@ -20,6 +20,9 @@ var (
|
||||
fJksAlias string
|
||||
fJksKeypass string
|
||||
fJksStorepass string
|
||||
fShellEnv string
|
||||
fPreCommand string
|
||||
fPostCommand string
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -33,6 +36,9 @@ func init() {
|
||||
flag.StringVar(&fJksAlias, argsPrefix+"JKSALIAS", "", "")
|
||||
flag.StringVar(&fJksKeypass, argsPrefix+"JKSKEYPASS", "", "")
|
||||
flag.StringVar(&fJksStorepass, argsPrefix+"JKSSTOREPASS", "", "")
|
||||
flag.StringVar(&fShellEnv, argsPrefix+"SHELLENV", "", "")
|
||||
flag.StringVar(&fPreCommand, argsPrefix+"PRECOMMAND", "", "")
|
||||
flag.StringVar(&fPostCommand, argsPrefix+"POSTCOMMAND", "", "")
|
||||
}
|
||||
|
||||
/*
|
||||
@ -46,7 +52,10 @@ Shell command to run this test:
|
||||
--CERTIMATE_DEPLOYER_LOCAL_PFXPASSWORD="your-pfx-password" \
|
||||
--CERTIMATE_DEPLOYER_LOCAL_JKSALIAS="your-jks-alias" \
|
||||
--CERTIMATE_DEPLOYER_LOCAL_JKSKEYPASS="your-jks-keypass" \
|
||||
--CERTIMATE_DEPLOYER_LOCAL_JKSSTOREPASS="your-jks-storepass"
|
||||
--CERTIMATE_DEPLOYER_LOCAL_JKSSTOREPASS="your-jks-storepass" \
|
||||
--CERTIMATE_DEPLOYER_LOCAL_SHELLENV="sh" \
|
||||
--CERTIMATE_DEPLOYER_LOCAL_PRECOMMAND="echo 'hello world'" \
|
||||
--CERTIMATE_DEPLOYER_LOCAL_POSTCOMMAND="echo 'bye-bye world'"
|
||||
*/
|
||||
func TestDeploy(t *testing.T) {
|
||||
flag.Parse()
|
||||
@ -58,11 +67,18 @@ func TestDeploy(t *testing.T) {
|
||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
||||
fmt.Sprintf("OUTPUTKEYPATH: %v", fOutputKeyPath),
|
||||
fmt.Sprintf("SHELLENV: %v", fShellEnv),
|
||||
fmt.Sprintf("PRECOMMAND: %v", fPreCommand),
|
||||
fmt.Sprintf("POSTCOMMAND: %v", fPostCommand),
|
||||
}, "\n"))
|
||||
|
||||
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
||||
OutputCertPath: fOutputCertPath,
|
||||
OutputKeyPath: fOutputKeyPath,
|
||||
OutputFormat: provider.OUTPUT_FORMAT_PEM,
|
||||
OutputCertPath: fOutputCertPath + ".pem",
|
||||
OutputKeyPath: fOutputKeyPath + ".pem",
|
||||
ShellEnv: provider.ShellEnvType(fShellEnv),
|
||||
PreCommand: fPreCommand,
|
||||
PostCommand: fPostCommand,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
@ -77,7 +93,7 @@ func TestDeploy(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
fstat1, err := os.Stat(fOutputCertPath)
|
||||
fstat1, err := os.Stat(fOutputCertPath + ".pem")
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
@ -86,7 +102,7 @@ func TestDeploy(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
fstat2, err := os.Stat(fOutputKeyPath)
|
||||
fstat2, err := os.Stat(fOutputKeyPath + ".pem")
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
@ -104,14 +120,12 @@ func TestDeploy(t *testing.T) {
|
||||
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
||||
fmt.Sprintf("OUTPUTKEYPATH: %v", fOutputKeyPath),
|
||||
fmt.Sprintf("PFXPASSWORD: %v", fPfxPassword),
|
||||
}, "\n"))
|
||||
|
||||
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
||||
OutputFormat: provider.OUTPUT_FORMAT_PFX,
|
||||
OutputCertPath: fOutputCertPath,
|
||||
OutputKeyPath: fOutputKeyPath,
|
||||
OutputCertPath: fOutputCertPath + ".pfx",
|
||||
PfxPassword: fPfxPassword,
|
||||
})
|
||||
if err != nil {
|
||||
@ -127,7 +141,7 @@ func TestDeploy(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
fstat, err := os.Stat(fOutputCertPath)
|
||||
fstat, err := os.Stat(fOutputCertPath + ".pfx")
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
@ -145,7 +159,6 @@ func TestDeploy(t *testing.T) {
|
||||
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
||||
fmt.Sprintf("OUTPUTKEYPATH: %v", fOutputKeyPath),
|
||||
fmt.Sprintf("JKSALIAS: %v", fJksAlias),
|
||||
fmt.Sprintf("JKSKEYPASS: %v", fJksKeypass),
|
||||
fmt.Sprintf("JKSSTOREPASS: %v", fJksStorepass),
|
||||
@ -153,8 +166,7 @@ func TestDeploy(t *testing.T) {
|
||||
|
||||
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
||||
OutputFormat: provider.OUTPUT_FORMAT_JKS,
|
||||
OutputCertPath: fOutputCertPath,
|
||||
OutputKeyPath: fOutputKeyPath,
|
||||
OutputCertPath: fOutputCertPath + ".jks",
|
||||
JksAlias: fJksAlias,
|
||||
JksKeypass: fJksKeypass,
|
||||
JksStorepass: fJksStorepass,
|
||||
@ -172,7 +184,7 @@ func TestDeploy(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
fstat, err := os.Stat(fOutputCertPath)
|
||||
fstat, err := os.Stat(fOutputCertPath + ".jks")
|
||||
if err != nil {
|
||||
t.Errorf("err: %+v", err)
|
||||
return
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user