agent.go 4.7 KB

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