big refactor
This commit is contained in:
parent
6abf716844
commit
7fbb0fe7e1
25 changed files with 2761 additions and 822 deletions
|
|
@ -3,11 +3,16 @@ package system
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/amidaware/rmmagent/agent/utils"
|
||||
ps "github.com/elastic/go-sysinfo"
|
||||
gocmd "github.com/go-cmd/cmd"
|
||||
)
|
||||
|
||||
|
|
@ -17,9 +22,16 @@ type CmdStatus struct {
|
|||
Stderr string
|
||||
}
|
||||
|
||||
func NewCMDOpts() *CmdOptions {
|
||||
return &CmdOptions{
|
||||
Shell: "/bin/bash",
|
||||
Timeout: 30,
|
||||
}
|
||||
}
|
||||
|
||||
func CmdV2(c *CmdOptions) CmdStatus {
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Disable output buffering, enable streaming
|
||||
|
|
@ -85,7 +97,7 @@ func CmdV2(c *CmdOptions) CmdStatus {
|
|||
return
|
||||
case <-ctx.Done():
|
||||
pid := envCmd.Status().PID
|
||||
KillProc(int32(pid))
|
||||
utils.KillProc(int32(pid))
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -99,3 +111,88 @@ func CmdV2(c *CmdOptions) CmdStatus {
|
|||
|
||||
return ret
|
||||
}
|
||||
|
||||
func RunPythonCode(code string, timeout int, args []string) (string, error) {
|
||||
content := []byte(code)
|
||||
dir, err := ioutil.TempDir("", "tacticalpy")
|
||||
if err != nil {
|
||||
//a.Logger.Debugln(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer os.RemoveAll(dir)
|
||||
tmpfn, _ := ioutil.TempFile(dir, "*.py")
|
||||
if _, err := tmpfn.Write(content); err != nil {
|
||||
//a.Logger.Debugln(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := tmpfn.Close(); err != nil {
|
||||
//a.Logger.Debugln(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
||||
defer cancel()
|
||||
var outb, errb bytes.Buffer
|
||||
cmdArgs := []string{tmpfn.Name()}
|
||||
if len(args) > 0 {
|
||||
cmdArgs = append(cmdArgs, args...)
|
||||
}
|
||||
|
||||
//a.Logger.Debugln(cmdArgs)
|
||||
cmd := exec.CommandContext(ctx, GetPythonBin(), cmdArgs...)
|
||||
cmd.Stdout = &outb
|
||||
cmd.Stderr = &errb
|
||||
|
||||
cmdErr := cmd.Run()
|
||||
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
//a.Logger.Debugln("RunPythonCode:", ctx.Err())
|
||||
return "", ctx.Err()
|
||||
}
|
||||
|
||||
if cmdErr != nil {
|
||||
//a.Logger.Debugln("RunPythonCode:", cmdErr)
|
||||
return "", cmdErr
|
||||
}
|
||||
|
||||
if errb.String() != "" {
|
||||
//a.Logger.Debugln(errb.String())
|
||||
return errb.String(), errors.New("RunPythonCode stderr")
|
||||
}
|
||||
|
||||
return outb.String(), nil
|
||||
}
|
||||
|
||||
func GetHostname() string {
|
||||
host, _ := ps.Host()
|
||||
info := host.Info()
|
||||
return info.Hostname
|
||||
}
|
||||
|
||||
// TotalRAM returns total RAM in GB
|
||||
func TotalRAM() float64 {
|
||||
host, err := ps.Host()
|
||||
if err != nil {
|
||||
return 8.0
|
||||
}
|
||||
|
||||
mem, err := host.Memory()
|
||||
if err != nil {
|
||||
return 8.0
|
||||
}
|
||||
|
||||
return math.Ceil(float64(mem.Total) / 1073741824.0)
|
||||
}
|
||||
|
||||
// BootTime returns system boot time as a unix timestamp
|
||||
func BootTime() int64 {
|
||||
host, err := ps.Host()
|
||||
if err != nil {
|
||||
return 1000
|
||||
}
|
||||
|
||||
info := host.Info()
|
||||
return info.BootTime.Unix()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue