mirror of
https://github.com/woodchen-ink/certimate.git
synced 2025-07-18 17:31:55 +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 != "" {
|
if d.config.PreCommand != "" {
|
||||||
stdout, stderr, err := execCommand(d.config.ShellEnv, d.config.PreCommand)
|
stdout, stderr, err := execCommand(d.config.ShellEnv, d.config.PreCommand)
|
||||||
if err != nil {
|
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)
|
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 != "" {
|
if d.config.PostCommand != "" {
|
||||||
stdout, stderr, err := execCommand(d.config.ShellEnv, d.config.PostCommand)
|
stdout, stderr, err := execCommand(d.config.ShellEnv, d.config.PostCommand)
|
||||||
if err != nil {
|
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)
|
d.logger.Logt("post-command executed", stdout)
|
||||||
@ -154,7 +154,7 @@ func execCommand(shellEnv ShellEnvType, command string) (string, string, error)
|
|||||||
case SHELL_ENV_POWERSHELL:
|
case SHELL_ENV_POWERSHELL:
|
||||||
cmd = exec.Command("powershell", "-Command", command)
|
cmd = exec.Command("powershell", "-Command", command)
|
||||||
|
|
||||||
case "":
|
case ShellEnvType(""):
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
cmd = exec.Command("cmd", "/C", command)
|
cmd = exec.Command("cmd", "/C", command)
|
||||||
} else {
|
} else {
|
||||||
@ -165,14 +165,13 @@ func execCommand(shellEnv ShellEnvType, command string) (string, string, error)
|
|||||||
return "", "", fmt.Errorf("unsupported shell env: %s", shellEnv)
|
return "", "", fmt.Errorf("unsupported shell env: %s", shellEnv)
|
||||||
}
|
}
|
||||||
|
|
||||||
var stdoutBuf bytes.Buffer
|
stdoutBuf := bytes.NewBuffer(nil)
|
||||||
cmd.Stdout = &stdoutBuf
|
cmd.Stdout = stdoutBuf
|
||||||
var stderrBuf bytes.Buffer
|
stderrBuf := bytes.NewBuffer(nil)
|
||||||
cmd.Stderr = &stderrBuf
|
cmd.Stderr = stderrBuf
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
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
|
return stdoutBuf.String(), stderrBuf.String(), nil
|
||||||
|
@ -20,6 +20,9 @@ var (
|
|||||||
fJksAlias string
|
fJksAlias string
|
||||||
fJksKeypass string
|
fJksKeypass string
|
||||||
fJksStorepass string
|
fJksStorepass string
|
||||||
|
fShellEnv string
|
||||||
|
fPreCommand string
|
||||||
|
fPostCommand string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -33,6 +36,9 @@ func init() {
|
|||||||
flag.StringVar(&fJksAlias, argsPrefix+"JKSALIAS", "", "")
|
flag.StringVar(&fJksAlias, argsPrefix+"JKSALIAS", "", "")
|
||||||
flag.StringVar(&fJksKeypass, argsPrefix+"JKSKEYPASS", "", "")
|
flag.StringVar(&fJksKeypass, argsPrefix+"JKSKEYPASS", "", "")
|
||||||
flag.StringVar(&fJksStorepass, argsPrefix+"JKSSTOREPASS", "", "")
|
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_PFXPASSWORD="your-pfx-password" \
|
||||||
--CERTIMATE_DEPLOYER_LOCAL_JKSALIAS="your-jks-alias" \
|
--CERTIMATE_DEPLOYER_LOCAL_JKSALIAS="your-jks-alias" \
|
||||||
--CERTIMATE_DEPLOYER_LOCAL_JKSKEYPASS="your-jks-keypass" \
|
--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) {
|
func TestDeploy(t *testing.T) {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@ -58,11 +67,18 @@ func TestDeploy(t *testing.T) {
|
|||||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||||
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
||||||
fmt.Sprintf("OUTPUTKEYPATH: %v", fOutputKeyPath),
|
fmt.Sprintf("OUTPUTKEYPATH: %v", fOutputKeyPath),
|
||||||
|
fmt.Sprintf("SHELLENV: %v", fShellEnv),
|
||||||
|
fmt.Sprintf("PRECOMMAND: %v", fPreCommand),
|
||||||
|
fmt.Sprintf("POSTCOMMAND: %v", fPostCommand),
|
||||||
}, "\n"))
|
}, "\n"))
|
||||||
|
|
||||||
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
||||||
OutputCertPath: fOutputCertPath,
|
OutputFormat: provider.OUTPUT_FORMAT_PEM,
|
||||||
OutputKeyPath: fOutputKeyPath,
|
OutputCertPath: fOutputCertPath + ".pem",
|
||||||
|
OutputKeyPath: fOutputKeyPath + ".pem",
|
||||||
|
ShellEnv: provider.ShellEnvType(fShellEnv),
|
||||||
|
PreCommand: fPreCommand,
|
||||||
|
PostCommand: fPostCommand,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err: %+v", err)
|
t.Errorf("err: %+v", err)
|
||||||
@ -77,7 +93,7 @@ func TestDeploy(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fstat1, err := os.Stat(fOutputCertPath)
|
fstat1, err := os.Stat(fOutputCertPath + ".pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err: %+v", err)
|
t.Errorf("err: %+v", err)
|
||||||
return
|
return
|
||||||
@ -86,7 +102,7 @@ func TestDeploy(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fstat2, err := os.Stat(fOutputKeyPath)
|
fstat2, err := os.Stat(fOutputKeyPath + ".pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err: %+v", err)
|
t.Errorf("err: %+v", err)
|
||||||
return
|
return
|
||||||
@ -104,14 +120,12 @@ func TestDeploy(t *testing.T) {
|
|||||||
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
||||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||||
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
||||||
fmt.Sprintf("OUTPUTKEYPATH: %v", fOutputKeyPath),
|
|
||||||
fmt.Sprintf("PFXPASSWORD: %v", fPfxPassword),
|
fmt.Sprintf("PFXPASSWORD: %v", fPfxPassword),
|
||||||
}, "\n"))
|
}, "\n"))
|
||||||
|
|
||||||
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
||||||
OutputFormat: provider.OUTPUT_FORMAT_PFX,
|
OutputFormat: provider.OUTPUT_FORMAT_PFX,
|
||||||
OutputCertPath: fOutputCertPath,
|
OutputCertPath: fOutputCertPath + ".pfx",
|
||||||
OutputKeyPath: fOutputKeyPath,
|
|
||||||
PfxPassword: fPfxPassword,
|
PfxPassword: fPfxPassword,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -127,7 +141,7 @@ func TestDeploy(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fstat, err := os.Stat(fOutputCertPath)
|
fstat, err := os.Stat(fOutputCertPath + ".pfx")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err: %+v", err)
|
t.Errorf("err: %+v", err)
|
||||||
return
|
return
|
||||||
@ -145,7 +159,6 @@ func TestDeploy(t *testing.T) {
|
|||||||
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
|
||||||
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
|
||||||
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
fmt.Sprintf("OUTPUTCERTPATH: %v", fOutputCertPath),
|
||||||
fmt.Sprintf("OUTPUTKEYPATH: %v", fOutputKeyPath),
|
|
||||||
fmt.Sprintf("JKSALIAS: %v", fJksAlias),
|
fmt.Sprintf("JKSALIAS: %v", fJksAlias),
|
||||||
fmt.Sprintf("JKSKEYPASS: %v", fJksKeypass),
|
fmt.Sprintf("JKSKEYPASS: %v", fJksKeypass),
|
||||||
fmt.Sprintf("JKSSTOREPASS: %v", fJksStorepass),
|
fmt.Sprintf("JKSSTOREPASS: %v", fJksStorepass),
|
||||||
@ -153,8 +166,7 @@ func TestDeploy(t *testing.T) {
|
|||||||
|
|
||||||
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
deployer, err := provider.New(&provider.LocalDeployerConfig{
|
||||||
OutputFormat: provider.OUTPUT_FORMAT_JKS,
|
OutputFormat: provider.OUTPUT_FORMAT_JKS,
|
||||||
OutputCertPath: fOutputCertPath,
|
OutputCertPath: fOutputCertPath + ".jks",
|
||||||
OutputKeyPath: fOutputKeyPath,
|
|
||||||
JksAlias: fJksAlias,
|
JksAlias: fJksAlias,
|
||||||
JksKeypass: fJksKeypass,
|
JksKeypass: fJksKeypass,
|
||||||
JksStorepass: fJksStorepass,
|
JksStorepass: fJksStorepass,
|
||||||
@ -172,7 +184,7 @@ func TestDeploy(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fstat, err := os.Stat(fOutputCertPath)
|
fstat, err := os.Stat(fOutputCertPath + ".jks")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err: %+v", err)
|
t.Errorf("err: %+v", err)
|
||||||
return
|
return
|
||||||
|
@ -103,7 +103,7 @@ func (d *SshDeployer) Deploy(ctx context.Context, certPem string, privkeyPem str
|
|||||||
if d.config.PreCommand != "" {
|
if d.config.PreCommand != "" {
|
||||||
stdout, stderr, err := execSshCommand(client, d.config.PreCommand)
|
stdout, stderr, err := execSshCommand(client, d.config.PreCommand)
|
||||||
if err != nil {
|
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)
|
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 != "" {
|
if d.config.PostCommand != "" {
|
||||||
stdout, stderr, err := execSshCommand(client, d.config.PostCommand)
|
stdout, stderr, err := execSshCommand(client, d.config.PostCommand)
|
||||||
if err != nil {
|
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)
|
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()
|
defer session.Close()
|
||||||
|
|
||||||
var stdoutBuf bytes.Buffer
|
stdoutBuf := bytes.NewBuffer(nil)
|
||||||
session.Stdout = &stdoutBuf
|
session.Stdout = stdoutBuf
|
||||||
var stderrBuf bytes.Buffer
|
stderrBuf := bytes.NewBuffer(nil)
|
||||||
session.Stderr = &stderrBuf
|
session.Stderr = stderrBuf
|
||||||
err = session.Run(command)
|
err = session.Run(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return stdoutBuf.String(), stderrBuf.String(), xerrors.Wrap(err, "failed to execute ssh command")
|
||||||
}
|
}
|
||||||
|
|
||||||
return stdoutBuf.String(), stderrBuf.String(), nil
|
return stdoutBuf.String(), stderrBuf.String(), nil
|
||||||
|
@ -69,6 +69,7 @@ func TestDeploy(t *testing.T) {
|
|||||||
SshPort: int32(fSshPort),
|
SshPort: int32(fSshPort),
|
||||||
SshUsername: fSshUsername,
|
SshUsername: fSshUsername,
|
||||||
SshPassword: fSshPassword,
|
SshPassword: fSshPassword,
|
||||||
|
OutputFormat: provider.OUTPUT_FORMAT_PEM,
|
||||||
OutputCertPath: fOutputCertPath,
|
OutputCertPath: fOutputCertPath,
|
||||||
OutputKeyPath: fOutputKeyPath,
|
OutputKeyPath: fOutputKeyPath,
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user