mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-21 03:39:56 +00:00
chore: create a func for running ssh command
This commit is contained in:
parent
9acdd15c1e
commit
e7e2e4786d
@ -71,20 +71,9 @@ func (s *ssh) Deploy(ctx context.Context) error {
|
|||||||
|
|
||||||
// 执行前置命令
|
// 执行前置命令
|
||||||
if access.PreCommand != "" {
|
if access.PreCommand != "" {
|
||||||
session, err := client.NewSession()
|
err, stdout, stderr := s.sshExecCommand(client, access.Command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create session: %w", err)
|
return fmt.Errorf("failed to run pre-command: %w, stdout: %s, stderr: %s", err, stdout, stderr)
|
||||||
}
|
|
||||||
defer session.Close()
|
|
||||||
var stdoutBuf bytes.Buffer
|
|
||||||
session.Stdout = &stdoutBuf
|
|
||||||
var stderrBuf bytes.Buffer
|
|
||||||
session.Stderr = &stderrBuf
|
|
||||||
|
|
||||||
s.infos = append(s.infos, toStr("ssh为前置命令创建session成功", nil))
|
|
||||||
|
|
||||||
if err := session.Run(access.PreCommand); err != nil {
|
|
||||||
return fmt.Errorf("failed to run pre-command: %w, stdout: %s, stderr: %s", err, stdoutBuf.String(), stderrBuf.String())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,23 +92,28 @@ func (s *ssh) Deploy(ctx context.Context) error {
|
|||||||
s.infos = append(s.infos, toStr("ssh上传私钥成功", nil))
|
s.infos = append(s.infos, toStr("ssh上传私钥成功", nil))
|
||||||
|
|
||||||
// 执行命令
|
// 执行命令
|
||||||
|
err, stdout, stderr := s.sshExecCommand(client, access.Command)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to run command: %w, stdout: %s, stderr: %s", err, stdout, stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.infos = append(s.infos, toStr("ssh执行命令成功", stdout))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ssh) sshExecCommand(client *sshPkg.Client, command string) (error, string, string) {
|
||||||
session, err := client.NewSession()
|
session, err := client.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create session: %w", err)
|
return fmt.Errorf("failed to create ssh session: %w", err), "", ""
|
||||||
}
|
}
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
var stdoutBuf bytes.Buffer
|
var stdoutBuf bytes.Buffer
|
||||||
session.Stdout = &stdoutBuf
|
session.Stdout = &stdoutBuf
|
||||||
var stderrBuf bytes.Buffer
|
var stderrBuf bytes.Buffer
|
||||||
session.Stderr = &stderrBuf
|
session.Stderr = &stderrBuf
|
||||||
s.infos = append(s.infos, toStr("ssh创建session成功", nil))
|
err = session.Run(command)
|
||||||
if err := session.Run(access.Command); err != nil {
|
return err, stdoutBuf.String(), stderrBuf.String()
|
||||||
return fmt.Errorf("failed to run command: %w, stdout: %s, stderr: %s", err, stdoutBuf.String(), stderrBuf.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
s.infos = append(s.infos, toStr("ssh执行命令成功", []string{stdoutBuf.String()}))
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ssh) upload(client *sshPkg.Client, content, path string) error {
|
func (s *ssh) upload(client *sshPkg.Client, content, path string) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user