agent.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "net"
  18. "os"
  19. "os/exec"
  20. "path"
  21. "time"
  22. )
  23. type Agent struct {
  24. cmd *exec.Cmd
  25. logfile *os.File
  26. l net.Listener
  27. }
  28. func newAgent(etcd string) (*Agent, error) {
  29. // check if the file exists
  30. _, err := os.Stat(etcd)
  31. if err != nil {
  32. return nil, err
  33. }
  34. c := exec.Command(etcd)
  35. f, err := os.Create("etcd.log")
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &Agent{cmd: c, logfile: f}, nil
  40. }
  41. // start starts a new etcd process with the given args.
  42. func (a *Agent) start(args ...string) error {
  43. a.cmd = exec.Command(a.cmd.Path, args...)
  44. a.cmd.Stdout = a.logfile
  45. a.cmd.Stderr = a.logfile
  46. return a.cmd.Start()
  47. }
  48. // stop stops the existing etcd process the agent started.
  49. func (a *Agent) stop() error {
  50. err := a.cmd.Process.Kill()
  51. if err != nil {
  52. return err
  53. }
  54. _, err = a.cmd.Process.Wait()
  55. return err
  56. }
  57. // restart restarts the stopped etcd process.
  58. func (a *Agent) restart() error {
  59. a.cmd = exec.Command(a.cmd.Path, a.cmd.Args[1:]...)
  60. a.cmd.Stdout = a.logfile
  61. a.cmd.Stderr = a.logfile
  62. return a.cmd.Start()
  63. }
  64. func (a *Agent) cleanup() error {
  65. a.stop()
  66. a.logfile.Close()
  67. if err := archiveLogAndDataDir("etcd.log", a.dataDir()); err != nil {
  68. return err
  69. }
  70. f, err := os.Create("etcd.log")
  71. a.logfile = f
  72. return err
  73. }
  74. // terminate stops the exiting etcd process the agent started
  75. // and removes the data dir.
  76. func (a *Agent) terminate() error {
  77. a.stop()
  78. return os.RemoveAll(a.dataDir())
  79. }
  80. func (a *Agent) dataDir() string {
  81. datadir := path.Join(a.cmd.Path, "*.etcd")
  82. args := a.cmd.Args
  83. // only parse the simple case like "-data-dir /var/lib/etcd"
  84. for i, arg := range args {
  85. if arg == "-data-dir" {
  86. datadir = args[i+1]
  87. break
  88. }
  89. }
  90. return datadir
  91. }
  92. func archiveLogAndDataDir(log string, datadir string) error {
  93. dir := path.Join("failure_archive", fmt.Sprint(time.Now().Format(time.RFC3339)))
  94. if err := os.MkdirAll(dir, 0700); err != nil {
  95. return err
  96. }
  97. if err := os.Rename(log, path.Join(dir, log)); err != nil {
  98. return err
  99. }
  100. return os.Rename(datadir, path.Join(dir, datadir))
  101. }