Skip to content

Commit

Permalink
Merge pull request #164 from blgm/master
Browse files Browse the repository at this point in the history
fix: signal handling for Concourse 7.5
  • Loading branch information
ljfranklin authored Nov 2, 2021
2 parents af368b9 + d94b449 commit 2e64e3e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
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
}

0 comments on commit 2e64e3e

Please sign in to comment.