Skip to content

Commit

Permalink
Add 'pid_file' config option (#3321)
Browse files Browse the repository at this point in the history
* add pid_file config option

* address review feedback

* address review comments
  • Loading branch information
vishalnayak authored Sep 16, 2017
1 parent 0911358 commit de7ac83
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
43 changes: 43 additions & 0 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,18 @@ CLUSTER_SYNTHESIS_COMPLETE:
// Release the log gate.
c.logGate.Flush()

// Write out the PID to the file now that server has successfully started
if err := c.storePidFile(config.PidFile); err != nil {
c.Ui.Output(fmt.Sprintf("Error storing PID: %v", err))
return 1
}

defer func() {
if err := c.removePidFile(config.PidFile); err != nil {
c.Ui.Output(fmt.Sprintf("Error deleting the PID file: %v", err))
}
}()

// Wait for shutdown
shutdownTriggered := false

Expand Down Expand Up @@ -1226,6 +1238,37 @@ func (c *ServerCommand) AutocompleteFlags() complete.Flags {
}
}

// storePidFile is used to write out our PID to a file if necessary
func (c *ServerCommand) storePidFile(pidPath string) error {
// Quit fast if no pidfile
if pidPath == "" {
return nil
}

// Open the PID file
pidFile, err := os.OpenFile(pidPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("could not open pid file: %v", err)
}
defer pidFile.Close()

// Write out the PID
pid := os.Getpid()
_, err = pidFile.WriteString(fmt.Sprintf("%d", pid))
if err != nil {
return fmt.Errorf("could not write to pid file: %v", err)
}
return nil
}

// removePidFile is used to cleanup the PID file if necessary
func (c *ServerCommand) removePidFile(pidPath string) error {
if pidPath == "" {
return nil
}
return os.Remove(pidPath)
}

// MakeShutdownCh returns a channel that can be used for shutdown
// notifications for commands. This channel will send a message for every
// SIGINT or SIGTERM received.
Expand Down
7 changes: 7 additions & 0 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Config struct {

PluginDirectory string `hcl:"plugin_directory"`

PidFile string `hcl:"pid_file"`
EnableRawEndpoint bool `hcl:"-"`
EnableRawEndpointRaw interface{} `hcl:"raw_storage_endpoint"`
}
Expand Down Expand Up @@ -302,6 +303,11 @@ func (c *Config) Merge(c2 *Config) *Config {
result.PluginDirectory = c2.PluginDirectory
}

result.PidFile = c.PidFile
if c2.PidFile != "" {
result.PidFile = c2.PidFile
}

return result
}

Expand Down Expand Up @@ -399,6 +405,7 @@ func ParseConfig(d string, logger log.Logger) (*Config, error) {
"cluster_name",
"cluster_cipher_suites",
"plugin_directory",
"pid_file",
"raw_storage_endpoint",
}
if err := checkHCLKeys(list, valid); err != nil {
Expand Down
22 changes: 12 additions & 10 deletions command/server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func TestLoadConfigFile(t *testing.T) {
DefaultLeaseTTL: 10 * time.Hour,
DefaultLeaseTTLRaw: "10h",
ClusterName: "testcluster",

PidFile: "./pidfile",
}
if !reflect.DeepEqual(config, expected) {
t.Fatalf("expected \n\n%#v\n\n to be \n\n%#v\n\n", config, expected)
Expand Down Expand Up @@ -123,16 +125,16 @@ func TestLoadConfigFile_json(t *testing.T) {
CirconusBrokerSelectTag: "",
},

MaxLeaseTTL: 10 * time.Hour,
MaxLeaseTTLRaw: "10h",
DefaultLeaseTTL: 10 * time.Hour,
DefaultLeaseTTLRaw: "10h",
ClusterName: "testcluster",
DisableCacheRaw: interface{}(nil),
DisableMlockRaw: interface{}(nil),
EnableUI: true,
EnableUIRaw: true,

MaxLeaseTTL: 10 * time.Hour,
MaxLeaseTTLRaw: "10h",
DefaultLeaseTTL: 10 * time.Hour,
DefaultLeaseTTLRaw: "10h",
ClusterName: "testcluster",
DisableCacheRaw: interface{}(nil),
DisableMlockRaw: interface{}(nil),
EnableUI: true,
EnableUIRaw: true,
PidFile: "./pidfile",
EnableRawEndpoint: true,
EnableRawEndpointRaw: true,
}
Expand Down
3 changes: 2 additions & 1 deletion command/server/test-fixtures/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ telemetry {
max_lease_ttl = "10h"
default_lease_ttl = "10h"
cluster_name = "testcluster"
raw_storage_endpoint = true
pid_file = "./pidfile"
raw_storage_endpoint = true
1 change: 1 addition & 0 deletions command/server/test-fixtures/config.hcl.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"default_lease_ttl": "10h",
"cluster_name":"testcluster",
"ui":true,
"pid_file":"./pidfile",
"raw_storage_endpoint":true
}
3 changes: 3 additions & 0 deletions website/source/docs/configuration/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ to specify where the configuration is.
the standard Vault API address will automatically redirect there. This can also
be provided via the environment variable `VAULT_UI`.

- `pid_file` `(string: "")` - Path to the file in which the Vault server's
Process ID (PID) should be stored.
[storage-backend]: /docs/configuration/storage/index.html
[listener]: /docs/configuration/listener/index.html
[telemetry]: /docs/configuration/telemetry.html

0 comments on commit de7ac83

Please sign in to comment.