mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
server: graceful shutdown for fast failover. Fixes #308
This commit is contained in:
@@ -2,6 +2,8 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
auditFile "github.com/hashicorp/vault/builtin/audit/file"
|
auditFile "github.com/hashicorp/vault/builtin/audit/file"
|
||||||
auditSyslog "github.com/hashicorp/vault/builtin/audit/syslog"
|
auditSyslog "github.com/hashicorp/vault/builtin/audit/syslog"
|
||||||
@@ -68,6 +70,7 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
|
|||||||
"transit": transit.Factory,
|
"transit": transit.Factory,
|
||||||
"mysql": mysql.Factory,
|
"mysql": mysql.Factory,
|
||||||
},
|
},
|
||||||
|
ShutdownCh: makeShutdownCh(),
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -268,3 +271,20 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// makeShutdownCh returns a channel that can be used for shutdown
|
||||||
|
// notifications for commands. This channel will send a message for every
|
||||||
|
// interrupt or SIGTERM received.
|
||||||
|
func makeShutdownCh() <-chan struct{} {
|
||||||
|
resultCh := make(chan struct{})
|
||||||
|
|
||||||
|
signalCh := make(chan os.Signal, 4)
|
||||||
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
<-signalCh
|
||||||
|
resultCh <- struct{}{}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return resultCh
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ type ServerCommand struct {
|
|||||||
CredentialBackends map[string]logical.Factory
|
CredentialBackends map[string]logical.Factory
|
||||||
LogicalBackends map[string]logical.Factory
|
LogicalBackends map[string]logical.Factory
|
||||||
|
|
||||||
|
ShutdownCh <-chan struct{}
|
||||||
Meta
|
Meta
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +238,14 @@ func (c *ServerCommand) Run(args []string) int {
|
|||||||
// Release the log gate.
|
// Release the log gate.
|
||||||
logGate.Flush()
|
logGate.Flush()
|
||||||
|
|
||||||
<-make(chan struct{})
|
// Wait for shutdown
|
||||||
|
select {
|
||||||
|
case <-c.ShutdownCh:
|
||||||
|
c.Ui.Output("==> Vault shutdown triggered")
|
||||||
|
if err := core.Shutdown(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Error with core shutdown: %s", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -407,8 +415,8 @@ General Options:
|
|||||||
specified multiple times. If it is a directory, all
|
specified multiple times. If it is a directory, all
|
||||||
files with a ".hcl" or ".json" suffix will be loaded.
|
files with a ".hcl" or ".json" suffix will be loaded.
|
||||||
|
|
||||||
-dev Enables Dev mode. In this mode, Vault is completely
|
-dev Enables Dev mode. In this mode, Vault is completely
|
||||||
in-memory and unsealed. Do not run the Dev server in
|
in-memory and unsealed. Do not run the Dev server in
|
||||||
production!
|
production!
|
||||||
|
|
||||||
-log-level=info Log verbosity. Defaults to "info", will be outputted
|
-log-level=info Log verbosity. Defaults to "info", will be outputted
|
||||||
|
|||||||
Reference in New Issue
Block a user