Skip to content

Commit

Permalink
(#2116) Support disown in exec watcher
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Feb 25, 2024
1 parent 3fbd9dc commit c573ba4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ clone_folder: c:\gopath\src\github.com\choria-io\go-choria

environment:
GOPATH: c:\gopath
GOVERSION: "1.20"
GOVERSION: "1.21"
MCOLLECTIVE_CERTNAME: rip.mcollective
RUBY_VERSION: 24
CGO_ENABLED: 0
Expand Down
27 changes: 24 additions & 3 deletions aagent/watchers/execwatcher/exec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2019-2024, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -49,6 +49,7 @@ type Properties struct {
OutputAsData bool `mapstructure:"parse_as_data"`
SuppressSuccessAnnounce bool `mapstructure:"suppress_success_announce"`
GatherInitialState bool `mapstructure:"gather_initial_state"`
Disown bool `mapstructure:"disown"`
Timeout time.Duration
}

Expand Down Expand Up @@ -301,7 +302,12 @@ func (w *Watcher) watch(ctx context.Context) (state State, err error) {
}
defer os.Remove(ff)

cmd := exec.CommandContext(timeoutCtx, splitcmd[0], args...)
var cmd *exec.Cmd
if w.properties.Disown {
cmd = exec.Command(splitcmd[0], args...)
} else {
cmd = exec.CommandContext(timeoutCtx, splitcmd[0], args...)
}
cmd.Dir = w.machine.Directory()

cmd.Env = append(cmd.Env, fmt.Sprintf("MACHINE_WATCHER_NAME=%s", w.name))
Expand All @@ -318,7 +324,22 @@ func (w *Watcher) watch(ctx context.Context) (state State, err error) {
cmd.Env = append(cmd.Env, es)
}

output, err := cmd.CombinedOutput()
var output []byte

if w.properties.Disown {
w.Infof("Running command disowned from parent")
err = disown(cmd)
if err != nil {
return Error, fmt.Errorf("could not disown process: %w", err)
}
err = cmd.Start()
if err == nil {
go func() { cmd.Wait() }()
}
} else {
output, err = cmd.CombinedOutput()
}

if err != nil {
w.Errorf("Exec watcher %s failed: %s", w.properties.Command, err)
return Error, err
Expand Down
19 changes: 19 additions & 0 deletions aagent/watchers/execwatcher/exec_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

package execwatcher

import (
"os/exec"
"syscall"
)

func disown(cmd *exec.Cmd) error {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
}

return nil
}
13 changes: 13 additions & 0 deletions aagent/watchers/execwatcher/exec_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2024, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

package execwatcher

import (
"fmt"
)

func disown(cmd *exec.Cmd) error {
return fmt.Errorf("disown is not supported on windows")
}

0 comments on commit c573ba4

Please sign in to comment.