2022-10-09 15:35:24 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-11-08 07:42:31 +00:00
|
|
|
"bytes"
|
2023-02-14 06:19:26 +00:00
|
|
|
"fmt"
|
2024-01-24 09:41:56 +00:00
|
|
|
"os"
|
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) {
|
2024-03-05 10:06:08 +00:00
|
|
|
return ExecWithTimeOut(cmdStr, 20*time.Second)
|
2023-04-12 14:46:30 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 03:08:15 +00:00
|
|
|
func handleErr(stdout, stderr bytes.Buffer, err error) (string, error) {
|
|
|
|
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-04-12 14:46:30 +00:00
|
|
|
func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
|
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
|
2024-03-05 10:06:08 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return "", err
|
2023-04-12 06:16:29 +00:00
|
|
|
}
|
2024-03-05 10:06:08 +00:00
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
done <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
after := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
_ = cmd.Process.Kill()
|
|
|
|
return "", buserr.New(constant.ErrCmdTimeout)
|
|
|
|
case err := <-done:
|
|
|
|
if err != nil {
|
|
|
|
return handleErr(stdout, stderr, err)
|
|
|
|
}
|
2022-10-09 15:35:24 +00:00
|
|
|
}
|
2024-03-05 10:06:08 +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-10-10 09:34:28 +00:00
|
|
|
func ExecContainerScript(containerName, cmdStr string, timeout time.Duration) error {
|
|
|
|
cmdStr = fmt.Sprintf("docker exec -i %s bash -c '%s'", containerName, cmdStr)
|
|
|
|
out, err := ExecWithTimeOut(cmdStr, timeout)
|
|
|
|
if err != nil {
|
|
|
|
if out != "" {
|
|
|
|
return fmt.Errorf("%s; err: %v", out, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-24 09:41:56 +00:00
|
|
|
func ExecCronjobWithTimeOut(cmdStr, workdir, outPath string, timeout time.Duration) error {
|
|
|
|
file, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2023-05-06 09:23:34 +00:00
|
|
|
cmd := exec.Command("bash", "-c", cmdStr)
|
2023-06-06 10:29:22 +00:00
|
|
|
cmd.Dir = workdir
|
2024-01-24 09:41:56 +00:00
|
|
|
cmd.Stdout = file
|
|
|
|
cmd.Stderr = file
|
2024-03-05 10:06:08 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
done <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
after := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
_ = cmd.Process.Kill()
|
2024-01-24 09:41:56 +00:00
|
|
|
return buserr.New(constant.ErrCmdTimeout)
|
2024-03-05 10:06:08 +00:00
|
|
|
case err := <-done:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-06 09:23:34 +00:00
|
|
|
}
|
2024-03-05 10:06:08 +00:00
|
|
|
return nil
|
2023-05-06 09:23:34 +00:00
|
|
|
}
|
|
|
|
|
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-10-16 03:08:15 +00:00
|
|
|
return handleErr(stdout, stderr, 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()
|
2023-08-23 14:48:14 +00:00
|
|
|
if err != nil {
|
2023-10-16 03:08:15 +00:00
|
|
|
return handleErr(stdout, stderr, err)
|
2023-08-23 14:48:14 +00:00
|
|
|
}
|
|
|
|
return stdout.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExecScript(scriptPath, workDir string) (string, error) {
|
2023-08-24 10:20:15 +00:00
|
|
|
cmd := exec.Command("bash", scriptPath)
|
2023-08-23 14:48:14 +00:00
|
|
|
var stdout, stderr bytes.Buffer
|
2024-03-05 10:06:08 +00:00
|
|
|
cmd.Dir = workDir
|
2023-08-23 14:48:14 +00:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2024-03-05 10:06:08 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return "", err
|
2023-08-23 14:48:14 +00:00
|
|
|
}
|
2024-03-05 10:06:08 +00:00
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
done <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
after := time.After(10 * time.Minute)
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
_ = cmd.Process.Kill()
|
|
|
|
return "", buserr.New(constant.ErrCmdTimeout)
|
|
|
|
case err := <-done:
|
|
|
|
if err != nil {
|
|
|
|
return handleErr(stdout, stderr, err)
|
|
|
|
}
|
2023-06-25 10:31:21 +00:00
|
|
|
}
|
2024-03-05 10:06:08 +00:00
|
|
|
|
2023-06-25 10:31:21 +00:00
|
|
|
return stdout.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-10-16 03:08:15 +00:00
|
|
|
func ExecCmd(cmdStr string) error {
|
|
|
|
cmd := exec.Command("bash", "-c", cmdStr)
|
2023-10-16 08:34:15 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
2023-10-16 03:08:15 +00:00
|
|
|
if err != nil {
|
2023-10-16 08:34:15 +00:00
|
|
|
return fmt.Errorf("error : %v, output: %s", err, output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExecCmdWithDir(cmdStr, workDir string) error {
|
|
|
|
cmd := exec.Command("bash", "-c", cmdStr)
|
|
|
|
cmd.Dir = workDir
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error : %v, output: %s", err, output)
|
2023-10-16 03:08:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-25 10:31:21 +00:00
|
|
|
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
|
|
|
|
}
|