Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: signal handling for Concourse 7.5 #164

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/terraform-resource/runner/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package runner

import (
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"syscall"
)

type Runner struct {
Stdout io.Writer
Stderr io.Writer
cmd *exec.Cmd
sigs chan os.Signal
logger io.Writer
}

func New(cmd *exec.Cmd, logger io.Writer) *Runner {
// Ensure that child is started in process group
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

r := &Runner{
cmd: cmd,
sigs: make(chan os.Signal, 1),
logger: logger,
}
r.startSignalHandler()

return r
}

func (r *Runner) Run() error {
r.cmd.Stdout = r.Stdout
r.cmd.Stderr = r.Stderr
err := r.cmd.Run()

r.stopSignalHandler()
return err
}

func (r *Runner) CombinedOutput() ([]byte, error) {
out, err := r.cmd.CombinedOutput()
r.stopSignalHandler()
return out, err
}

func (r *Runner) Output() ([]byte, error) {
out, err := r.cmd.Output()
r.stopSignalHandler()
return out, err
}

func (r *Runner) terminate() {
if r.cmd.Process != nil {
processGroup := -r.cmd.Process.Pid
if err := syscall.Kill(processGroup, syscall.SIGKILL); err != nil {
fmt.Fprintf(r.logger, "** Error signaling process group %d: %s\n", processGroup, err)
}
} else {
fmt.Fprintln(r.logger, "** Process already terminated.")
}

fmt.Fprintln(r.logger, "** Exiting due to signal")
os.Exit(1)
}

func (r *Runner) startSignalHandler() {
signal.Notify(r.sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
for s := range r.sigs {
fmt.Fprintf(r.logger, "** Received signal: %s\n", s)
r.terminate()
}
}()
}

func (r *Runner) stopSignalHandler() {
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
close(r.sigs)
}
5 changes: 3 additions & 2 deletions src/terraform-resource/terraform/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"

"github.com/ljfranklin/terraform-resource/models"
"github.com/ljfranklin/terraform-resource/runner"
)

const defaultWorkspace = "default"
Expand Down Expand Up @@ -868,7 +869,7 @@ func (c *client) resourceExistsLegacyStorage(tfID string) (bool, error) {
return (len(strings.TrimSpace(string(rawOutput))) > 0), nil
}

func (c *client) terraformCmd(args []string, env []string) (*exec.Cmd, error) {
func (c *client) terraformCmd(args []string, env []string) (*runner.Runner, error) {
cmdPath, err := exec.LookPath("terraform")
if err != nil {
return nil, err
Expand All @@ -892,5 +893,5 @@ func (c *client) terraformCmd(args []string, env []string) (*exec.Cmd, error) {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, value))
}

return cmd, nil
return runner.New(cmd, c.logWriter), nil
}