Skip to content

Commit

Permalink
add env
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Nov 29, 2022
1 parent 6ac14b6 commit 7358907
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ type CmdOptions struct {
IsScript bool
IsExecutable bool
Detached bool
Env []string
EnvVars []string
}

func (a *Agent) NewCMDOpts() *CmdOptions {
Expand Down Expand Up @@ -250,10 +250,10 @@ func (a *Agent) CmdV2(c *CmdOptions) CmdStatus {
})
}

if len(c.Env) > 0 {
if len(c.EnvVars) > 0 {
cmdOptions.BeforeExec = append(cmdOptions.BeforeExec, func(cmd *exec.Cmd) {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, c.Env...)
cmd.Env = append(cmd.Env, c.EnvVars...)
})
}

Expand Down
6 changes: 3 additions & 3 deletions agent/agent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewAgentConfig() *rmm.AgentConfig {
}
}

func (a *Agent) RunScript(code string, shell string, args []string, timeout int, runasuser bool, envs []string) (stdout, stderr string, exitcode int, e error) {
func (a *Agent) RunScript(code string, shell string, args []string, timeout int, runasuser bool, envVars []string) (stdout, stderr string, exitcode int, e error) {

content := []byte(code)

Expand Down Expand Up @@ -158,9 +158,9 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
cmd.Stdout = &outb
cmd.Stderr = &errb

if len(envs) > 0 {
if len(envVars) > 0 {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, envs...)
cmd.Env = append(cmd.Env, envVars...)
}

if cmdErr := cmd.Start(); cmdErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion agent/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ type ScriptCheckResult struct {
// ScriptCheck runs either bat, powershell or python script
func (a *Agent) ScriptCheck(data rmm.Check, r *resty.Client) {
start := time.Now()
stdout, stderr, retcode, _ := a.RunScript(data.Script.Code, data.Script.Shell, data.ScriptArgs, data.Timeout, data.Script.RunAsUser, data.Script.Env)
stdout, stderr, retcode, _ := a.RunScript(data.Script.Code, data.Script.Shell, data.ScriptArgs, data.Timeout, data.Script.RunAsUser, data.Script.EnvVars)

payload := ScriptCheckResult{
ID: data.CheckPK,
Expand Down
6 changes: 3 additions & 3 deletions agent/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type NatsMsg struct {
ID int `json:"id"`
Code string `json:"code"`
RunAsUser bool `json:"run_as_user"`
Env []string `json:"env"`
EnvVars []string `json:"env_vars"`
}

var (
Expand Down Expand Up @@ -262,7 +262,7 @@ func (a *Agent) RunRPC() {
var resultData rmm.RunScriptResp
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
start := time.Now()
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser, p.Env)
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser, p.EnvVars)
resultData.ExecTime = time.Since(start).Seconds()
resultData.ID = p.ID

Expand Down Expand Up @@ -292,7 +292,7 @@ func (a *Agent) RunRPC() {
var retData rmm.RunScriptResp
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
start := time.Now()
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser, p.Env)
stdout, stderr, retcode, err := a.RunScript(p.Data["code"], p.Data["shell"], p.ScriptArgs, p.Timeout, p.RunAsUser, p.EnvVars)

retData.ExecTime = time.Since(start).Seconds()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion agent/tasks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (a *Agent) RunTask(id int) error {

action_start := time.Now()
if action.ActionType == "script" {
stdout, stderr, retcode, err := a.RunScript(action.Code, action.Shell, action.Args, action.Timeout, action.RunAsUser, action.Env)
stdout, stderr, retcode, err := a.RunScript(action.Code, action.Shell, action.Args, action.Timeout, action.RunAsUser, action.EnvVars)

if err != nil {
a.Logger.Debugln(err)
Expand Down
4 changes: 2 additions & 2 deletions shared/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type Script struct {
Shell string `json:"shell"`
Code string `json:"code"`
RunAsUser bool `json:"run_as_user"`
Env []string `json:"env"`
EnvVars []string `json:"env_vars"`
}

type CheckInfo struct {
Expand Down Expand Up @@ -191,7 +191,7 @@ type TaskAction struct {
Args []string `json:"script_args"`
Timeout int `json:"timeout"`
RunAsUser bool `json:"run_as_user"`
Env []string `json:"env"`
EnvVars []string `json:"env_vars"`
}

type AutomatedTask struct {
Expand Down

0 comments on commit 7358907

Please sign in to comment.