2022-10-09 15:35:24 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-11-08 07:42:31 +00:00
|
|
|
"bytes"
|
2023-04-12 06:16:29 +00:00
|
|
|
"context"
|
2023-02-14 06:19:26 +00:00
|
|
|
"fmt"
|
2022-10-09 15:35:24 +00:00
|
|
|
"os/exec"
|
2023-06-25 10:31:21 +00:00
|
|
|
"strings"
|
2023-04-12 06:16:29 +00:00
|
|
|
"time"
|
2023-04-20 10:44:17 +00:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-09 15:35:24 +00:00
|
|
|
)
|
|
|
|
|
2022-11-08 07:42:31 +00:00
|
|
|
func Exec(cmdStr string) (string, error) {
|
2023-04-12 14:46:30 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.Command("bash", "-c", cmdStr)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return "", buserr.New(constant.ErrCmdTimeout)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errMsg := ""
|
|
|
|
if len(stderr.String()) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
|
|
|
|
}
|
|
|
|
if len(stdout.String()) != 0 {
|
|
|
|
if len(errMsg) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
|
|
|
|
} else {
|
|
|
|
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errMsg, err
|
|
|
|
}
|
|
|
|
return stdout.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
2023-04-12 06:16:29 +00:00
|
|
|
defer cancel()
|
2022-11-08 07:42:31 +00:00
|
|
|
cmd := exec.Command("bash", "-c", cmdStr)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
2023-04-12 06:16:29 +00:00
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return "", buserr.New(constant.ErrCmdTimeout)
|
|
|
|
}
|
2022-10-09 15:35:24 +00:00
|
|
|
if err != nil {
|
2023-03-13 02:04:53 +00:00
|
|
|
errMsg := ""
|
|
|
|
if len(stderr.String()) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
|
|
|
|
}
|
|
|
|
if len(stdout.String()) != 0 {
|
|
|
|
if len(errMsg) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
|
|
|
|
} else {
|
|
|
|
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errMsg, err
|
2022-10-09 15:35:24 +00:00
|
|
|
}
|
2022-11-18 10:02:14 +00:00
|
|
|
return stdout.String(), nil
|
2022-10-09 15:35:24 +00:00
|
|
|
}
|
2023-02-14 06:19:26 +00:00
|
|
|
|
2023-06-06 10:29:22 +00:00
|
|
|
func ExecCronjobWithTimeOut(cmdStr string, workdir string, timeout time.Duration) (string, error) {
|
2023-05-06 09:23:34 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.Command("bash", "-c", cmdStr)
|
2023-06-06 10:29:22 +00:00
|
|
|
cmd.Dir = workdir
|
2023-05-06 09:23:34 +00:00
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return "", buserr.New(constant.ErrCmdTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
errMsg := ""
|
|
|
|
if len(stderr.String()) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("stderr:\n %s", stderr.String())
|
|
|
|
}
|
|
|
|
if len(stdout.String()) != 0 {
|
|
|
|
if len(errMsg) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("%s \n\n; stdout:\n %s", errMsg, stdout.String())
|
|
|
|
} else {
|
2023-05-25 07:12:16 +00:00
|
|
|
errMsg = fmt.Sprintf("stdout:\n %s", stdout.String())
|
2023-05-06 09:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errMsg, err
|
|
|
|
}
|
|
|
|
|
2023-02-14 06:19:26 +00:00
|
|
|
func Execf(cmdStr string, a ...interface{}) (string, error) {
|
|
|
|
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2023-03-13 02:04:53 +00:00
|
|
|
errMsg := ""
|
|
|
|
if len(stderr.String()) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
|
|
|
|
}
|
|
|
|
if len(stdout.String()) != 0 {
|
|
|
|
if len(errMsg) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
|
|
|
|
} else {
|
|
|
|
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errMsg, err
|
2023-02-14 06:19:26 +00:00
|
|
|
}
|
|
|
|
return stdout.String(), nil
|
|
|
|
}
|
2023-04-19 03:17:00 +00:00
|
|
|
|
2023-06-25 10:31:21 +00:00
|
|
|
func ExecWithCheck(name string, a ...string) (string, error) {
|
|
|
|
cmd := exec.Command(name, a...)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
errMsg := ""
|
|
|
|
if len(stderr.String()) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
|
|
|
|
}
|
|
|
|
if len(stdout.String()) != 0 {
|
|
|
|
if len(errMsg) != 0 {
|
|
|
|
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
|
|
|
|
} else {
|
|
|
|
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errMsg, err
|
|
|
|
}
|
|
|
|
return stdout.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckIllegal(args ...string) bool {
|
|
|
|
if args == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
if strings.Contains(arg, "&") || strings.Contains(arg, "|") || strings.Contains(arg, ";") ||
|
|
|
|
strings.Contains(arg, "$") || strings.Contains(arg, "'") || strings.Contains(arg, "`") ||
|
|
|
|
strings.Contains(arg, "(") || strings.Contains(arg, ")") || strings.Contains(arg, "\"") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-19 03:17:00 +00:00
|
|
|
func HasNoPasswordSudo() bool {
|
|
|
|
cmd2 := exec.Command("sudo", "-n", "ls")
|
|
|
|
err2 := cmd2.Run()
|
|
|
|
return err2 == nil
|
|
|
|
}
|
2023-05-22 09:45:39 +00:00
|
|
|
|
|
|
|
func SudoHandleCmd() string {
|
|
|
|
cmd := exec.Command("sudo", "-n", "ls")
|
|
|
|
if err := cmd.Run(); err == nil {
|
|
|
|
return "sudo "
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2023-08-08 09:00:12 +00:00
|
|
|
|
|
|
|
func Which(name string) bool {
|
|
|
|
_, err := exec.LookPath(name)
|
|
|
|
return err == nil
|
|
|
|
}
|