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

Add 'pid_file' config option #3321

Merged
merged 5 commits into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 35 additions & 0 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import (

// ServerCommand is a Command that starts the Vault server.
type ServerCommand struct {
Config *server.Config
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessary, why not just give the path to storePid?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. Fixed.


AuditBackends map[string]audit.Factory
CredentialBackends map[string]logical.Factory
LogicalBackends map[string]logical.Factory
Expand Down Expand Up @@ -188,6 +190,9 @@ func (c *ServerCommand) Run(args []string) int {
return 1
}

// Store the configuration in the command
c.Config = config

// Ensure that a backend is provided
if config.Storage == nil {
c.Ui.Output("A storage backend must be specified")
Expand Down Expand Up @@ -659,6 +664,12 @@ 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.storePid(); err != nil {
c.Ui.Output(fmt.Sprintf("Error storing PID: %v", err))
return 1
}

// Wait for shutdown
shutdownTriggered := false

Expand Down Expand Up @@ -1225,6 +1236,30 @@ func (c *ServerCommand) AutocompleteFlags() complete.Flags {
}
}

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

// Open the PID file
pidFile, err := os.OpenFile(pidPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These permissions are too open -- certainly the file should not be world-writable. I suggest 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
}

// 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
8 changes: 8 additions & 0 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type Config struct {
ClusterCipherSuites string `hcl:"cluster_cipher_suites"`

PluginDirectory string `hcl:"plugin_directory"`

PidFile string `hcl:"pid_file"`
}

// DevConfig is a Config that is used for dev mode of Vault.
Expand Down Expand Up @@ -293,6 +295,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 @@ -385,6 +392,7 @@ func ParseConfig(d string, logger log.Logger) (*Config, error) {
"cluster_name",
"cluster_cipher_suites",
"plugin_directory",
"pid_file",
}
if err := checkHCLKeys(list, valid); err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions command/server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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 @@ -129,6 +131,7 @@ func TestLoadConfigFile_json(t *testing.T) {
DisableMlockRaw: interface{}(nil),
EnableUI: true,
EnableUIRaw: true,
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
2 changes: 2 additions & 0 deletions command/server/test-fixtures/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ telemetry {
max_lease_ttl = "10h"
default_lease_ttl = "10h"
cluster_name = "testcluster"

pid_file = "./pidfile"
3 changes: 2 additions & 1 deletion command/server/test-fixtures/config.hcl.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"max_lease_ttl": "10h",
"default_lease_ttl": "10h",
"cluster_name":"testcluster",
"ui":true
"ui":true,
"pid_file":"./pidfile"
}
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 @@ -105,6 +105,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 Vault server PID should
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say "Path to the file in which the Vault server's Process ID (PID) should be stored."

Just in case people think PID is something Vault-y. You never know...

be stored.

[storage-backend]: /docs/configuration/storage/index.html
[listener]: /docs/configuration/listener/index.html
[telemetry]: /docs/configuration/telemetry.html