Merge pull request #5318 from hashicorp/sigtermcleanup
Gracefully clean up on SIGTERM
This commit is contained in:
commit
997f8e4a2a
|
@ -9,6 +9,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/hashicorp/packer/helper/enumflag"
|
"github.com/hashicorp/packer/helper/enumflag"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
|
@ -146,7 +147,7 @@ func (c BuildCommand) Run(args []string) int {
|
||||||
|
|
||||||
// Handle interrupts for this build
|
// Handle interrupts for this build
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, os.Interrupt)
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||||
defer signal.Stop(sigCh)
|
defer signal.Stop(sigCh)
|
||||||
go func(b packer.Build) {
|
go func(b packer.Build) {
|
||||||
<-sigCh
|
<-sigCh
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/hashicorp/atlas-go/archive"
|
"github.com/hashicorp/atlas-go/archive"
|
||||||
"github.com/hashicorp/atlas-go/v1"
|
"github.com/hashicorp/atlas-go/v1"
|
||||||
|
@ -267,7 +268,7 @@ func (c *PushCommand) Run(args []string) int {
|
||||||
|
|
||||||
// Make a ctrl-C channel
|
// Make a ctrl-C channel
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, os.Interrupt)
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||||
defer signal.Stop(sigCh)
|
defer signal.Stop(sigCh)
|
||||||
|
|
||||||
err = nil
|
err = nil
|
||||||
|
|
2
main.go
2
main.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/go-uuid"
|
"github.com/hashicorp/go-uuid"
|
||||||
|
@ -88,6 +89,7 @@ func realMain() int {
|
||||||
wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter)
|
wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter)
|
||||||
wrapConfig.Stdout = outW
|
wrapConfig.Stdout = outW
|
||||||
wrapConfig.DetectDuration = 500 * time.Millisecond
|
wrapConfig.DetectDuration = 500 * time.Millisecond
|
||||||
|
wrapConfig.ForwardSignals = []os.Signal{syscall.SIGTERM}
|
||||||
exitStatus, err := panicwrap.Wrap(&wrapConfig)
|
exitStatus, err := panicwrap.Wrap(&wrapConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Couldn't start Packer: %s", err)
|
fmt.Fprintf(os.Stderr, "Couldn't start Packer: %s", err)
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
packrpc "github.com/hashicorp/packer/packer/rpc"
|
packrpc "github.com/hashicorp/packer/packer/rpc"
|
||||||
|
@ -88,7 +89,7 @@ func Server() (*packrpc.Server, error) {
|
||||||
|
|
||||||
// Eat the interrupts
|
// Eat the interrupts
|
||||||
ch := make(chan os.Signal, 1)
|
ch := make(chan os.Signal, 1)
|
||||||
signal.Notify(ch, os.Interrupt)
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
||||||
go func() {
|
go func() {
|
||||||
var count int32 = 0
|
var count int32 = 0
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -180,7 +180,7 @@ func (rw *BasicUi) Ask(query string) (string, error) {
|
||||||
rw.scanner = bufio.NewScanner(rw.Reader)
|
rw.scanner = bufio.NewScanner(rw.Reader)
|
||||||
}
|
}
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, os.Interrupt)
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||||
defer signal.Stop(sigCh)
|
defer signal.Stop(sigCh)
|
||||||
|
|
||||||
log.Printf("ui: ask: %s", query)
|
log.Printf("ui: ask: %s", query)
|
||||||
|
|
3
stdin.go
3
stdin.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// setupStdin switches out stdin for a pipe. We do this so that we can
|
// setupStdin switches out stdin for a pipe. We do this so that we can
|
||||||
|
@ -26,7 +27,7 @@ func setupStdin() {
|
||||||
// Register a signal handler for interrupt in order to close the
|
// Register a signal handler for interrupt in order to close the
|
||||||
// writer end of our pipe so that readers get EOF downstream.
|
// writer end of our pipe so that readers get EOF downstream.
|
||||||
ch := make(chan os.Signal, 1)
|
ch := make(chan os.Signal, 1)
|
||||||
signal.Notify(ch, os.Interrupt)
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer signal.Stop(ch)
|
defer signal.Stop(ch)
|
||||||
|
|
Loading…
Reference in New Issue