agent.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. "syscall"
  22. "time"
  23. "github.com/coreos/etcd/pkg/netutil"
  24. "github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
  25. )
  26. const (
  27. stateUninitialized = "uninitialized"
  28. stateStarted = "started"
  29. stateStopped = "stopped"
  30. stateTerminated = "terminated"
  31. )
  32. type Agent struct {
  33. state string // the state of etcd process
  34. cmd *exec.Cmd
  35. logfile *os.File
  36. etcdLogPath string
  37. l net.Listener
  38. }
  39. func newAgent(etcd, etcdLogPath string) (*Agent, error) {
  40. // check if the file exists
  41. _, err := os.Stat(etcd)
  42. if err != nil {
  43. return nil, err
  44. }
  45. c := exec.Command(etcd)
  46. f, err := os.Create(etcdLogPath)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &Agent{state: stateUninitialized, cmd: c, logfile: f, etcdLogPath: etcdLogPath}, nil
  51. }
  52. // start starts a new etcd process with the given args.
  53. func (a *Agent) start(args ...string) error {
  54. a.cmd = exec.Command(a.cmd.Path, args...)
  55. a.cmd.Stdout = a.logfile
  56. a.cmd.Stderr = a.logfile
  57. err := a.cmd.Start()
  58. if err != nil {
  59. return err
  60. }
  61. a.state = stateStarted
  62. return nil
  63. }
  64. // stop stops the existing etcd process the agent started.
  65. func (a *Agent) stop() error {
  66. if a.state != stateStarted {
  67. return nil
  68. }
  69. err := sigtermAndWait(a.cmd)
  70. if err != nil {
  71. return err
  72. }
  73. a.state = stateStopped
  74. return nil
  75. }
  76. func sigtermAndWait(cmd *exec.Cmd) error {
  77. err := cmd.Process.Signal(syscall.SIGTERM)
  78. if err != nil {
  79. return err
  80. }
  81. errc := make(chan error)
  82. go func() {
  83. _, err := cmd.Process.Wait()
  84. errc <- err
  85. close(errc)
  86. }()
  87. select {
  88. case <-time.After(5 * time.Second):
  89. cmd.Process.Kill()
  90. case err := <-errc:
  91. return err
  92. }
  93. err = <-errc
  94. return err
  95. }
  96. // restart restarts the stopped etcd process.
  97. func (a *Agent) restart() error {
  98. a.cmd = exec.Command(a.cmd.Path, a.cmd.Args[1:]...)
  99. a.cmd.Stdout = a.logfile
  100. a.cmd.Stderr = a.logfile
  101. err := a.cmd.Start()
  102. if err != nil {
  103. return err
  104. }
  105. a.state = stateStarted
  106. return nil
  107. }
  108. func (a *Agent) cleanup() error {
  109. if err := a.stop(); err != nil {
  110. return err
  111. }
  112. a.state = stateUninitialized
  113. a.logfile.Close()
  114. if err := archiveLogAndDataDir(a.etcdLogPath, a.dataDir()); err != nil {
  115. return err
  116. }
  117. f, err := os.Create(a.etcdLogPath)
  118. a.logfile = f
  119. if err != nil {
  120. return err
  121. }
  122. // https://www.kernel.org/doc/Documentation/sysctl/vm.txt
  123. // https://github.com/torvalds/linux/blob/master/fs/drop_caches.c
  124. cmd := exec.Command("/bin/sh", "-c", `echo "echo 1 > /proc/sys/vm/drop_caches" | sudo sh`)
  125. if err := cmd.Run(); err != nil {
  126. plog.Printf("error when cleaning page cache (%v)", err)
  127. }
  128. return nil
  129. }
  130. // terminate stops the exiting etcd process the agent started
  131. // and removes the data dir.
  132. func (a *Agent) terminate() error {
  133. err := a.stop()
  134. if err != nil {
  135. return err
  136. }
  137. err = os.RemoveAll(a.dataDir())
  138. if err != nil {
  139. return err
  140. }
  141. a.state = stateTerminated
  142. return nil
  143. }
  144. func (a *Agent) dropPort(port int) error {
  145. return netutil.DropPort(port)
  146. }
  147. func (a *Agent) recoverPort(port int) error {
  148. return netutil.RecoverPort(port)
  149. }
  150. func (a *Agent) status() client.Status {
  151. return client.Status{State: a.state}
  152. }
  153. func (a *Agent) dataDir() string {
  154. datadir := path.Join(a.cmd.Path, "*.etcd")
  155. args := a.cmd.Args
  156. // only parse the simple case like "--data-dir /var/lib/etcd"
  157. for i, arg := range args {
  158. if arg == "--data-dir" {
  159. datadir = args[i+1]
  160. break
  161. }
  162. }
  163. return datadir
  164. }
  165. func existDir(fpath string) bool {
  166. st, err := os.Stat(fpath)
  167. if err != nil {
  168. if os.IsNotExist(err) {
  169. return false
  170. }
  171. } else {
  172. return st.IsDir()
  173. }
  174. return false
  175. }
  176. func archiveLogAndDataDir(log string, datadir string) error {
  177. dir := path.Join("failure_archive", fmt.Sprint(time.Now().Format(time.RFC3339)))
  178. if existDir(dir) {
  179. dir = path.Join("failure_archive", fmt.Sprint(time.Now().Add(time.Second).Format(time.RFC3339)))
  180. }
  181. if err := os.MkdirAll(dir, 0755); err != nil {
  182. return err
  183. }
  184. if err := os.Rename(log, path.Join(dir, path.Base(log))); err != nil {
  185. if !os.IsNotExist(err) {
  186. return err
  187. }
  188. }
  189. if err := os.Rename(datadir, path.Join(dir, path.Base(datadir))); err != nil {
  190. if !os.IsNotExist(err) {
  191. return err
  192. }
  193. }
  194. return nil
  195. }