Merge pull request #128 from carlosdp/pid-file

Add pid-file flag to agent
This commit is contained in:
William Tisäter 2014-05-06 19:57:12 +02:00
commit 1591c4d393
5 changed files with 79 additions and 0 deletions

View File

@ -107,6 +107,11 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
return nil, err
}
err = agent.storePid()
if err != nil {
return nil, err
}
return agent, nil
}
@ -250,6 +255,11 @@ func (a *Agent) Shutdown() error {
err = a.client.Shutdown()
}
pidErr := a.deletePid()
if pidErr != nil {
a.logger.Println("[WARN] agent: could not delete pid file ", pidErr)
}
a.logger.Println("[INFO] agent: shutdown complete")
a.shutdown = true
close(a.shutdownCh)
@ -496,3 +506,50 @@ func (a *Agent) Stats() map[string]map[string]string {
}
return stats
}
func (a *Agent) storePid() error {
pidPath := a.config.PidFile
if pidPath != "" {
pid := os.Getpid()
pidFile, err := os.OpenFile(pidPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
return fmt.Errorf("Could not open pid file: %v", err)
}
defer pidFile.Close()
_, err = pidFile.WriteString(fmt.Sprintf("%d", pid))
if err != nil {
return fmt.Errorf("Could not write to pid file: %s", err)
}
}
return nil
}
func (a *Agent) deletePid() error {
pidPath := a.config.PidFile
if pidPath != "" {
stat, err := os.Stat(pidPath)
if err != nil {
return fmt.Errorf("Could not remove pid file: %s", err)
}
if stat.IsDir() {
return fmt.Errorf("Specified pid file path is directory")
}
err = os.Remove(pidPath)
if err != nil {
return fmt.Errorf("Could not remove pid file: %s", err)
}
}
return nil
}

View File

@ -50,6 +50,7 @@ func (c *Command) readConfig() *Config {
cmdFlags.StringVar(&cmdConfig.Datacenter, "dc", "", "node datacenter")
cmdFlags.StringVar(&cmdConfig.DataDir, "data-dir", "", "path to the data directory")
cmdFlags.StringVar(&cmdConfig.UiDir, "ui-dir", "", "path to the web UI directory")
cmdFlags.StringVar(&cmdConfig.PidFile, "pid-file", "", "path to file to store PID")
cmdFlags.BoolVar(&cmdConfig.Server, "server", false, "run agent as server")
cmdFlags.BoolVar(&cmdConfig.Bootstrap, "bootstrap", false, "enable server bootstrap mode")
@ -485,6 +486,7 @@ Options:
-protocol=N Sets the protocol version. Defaults to latest.
-server Switches agent to server mode.
-ui-dir=path Path to directory containing the Web UI resources
-pid-file=path Path to file to store agent PID
`
return strings.TrimSpace(helpText)

View File

@ -128,6 +128,9 @@ type Config struct {
// If provided, the UI endpoints will be enabled.
UiDir string `mapstructure:"ui_dir"`
// PidFile is the file to store our PID in
PidFile string `mapstructure:"pid_file"`
// AEInterval controls the anti-entropy interval. This is how often
// the agent attempts to reconcile it's local state with the server'
// representation of our state. Defaults to every 60s.
@ -423,6 +426,9 @@ func MergeConfig(a, b *Config) *Config {
if b.UiDir != "" {
result.UiDir = b.UiDir
}
if b.PidFile != "" {
result.PidFile = b.PidFile
}
// Copy the start join addresses
result.StartJoin = make([]string, 0, len(a.StartJoin)+len(b.StartJoin))

View File

@ -257,6 +257,17 @@ func TestDecodeConfig(t *testing.T) {
if config.UiDir != "/opt/consul-ui" {
t.Fatalf("bad: %#v", config)
}
// Pid File
input = `{"pid_file": "/tmp/consul/pid"}`
config, err = DecodeConfig(bytes.NewReader([]byte(input)))
if err != nil {
t.Fatalf("err: %s", err)
}
if config.PidFile != "/tmp/consul/pid" {
t.Fatalf("bad: %#v", config)
}
}
func TestDecodeConfig_Service(t *testing.T) {

View File

@ -96,6 +96,9 @@ The options below are all specified on the command-line.
* `-ui-dir` - This flag provides a the directory containing the Web UI resources
for Consul. This must be provided to enable the Web UI. Directory must be readable.
* `-pid-file` - This flag provides the file path for the agent to store it's PID. This is useful for
sending signals to the agent, such as `SIGINT` to close it or `SIGHUP` to update check definitions.
## Configuration Files
In addition to the command-line options, configuration can be put into