agent.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "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 := a.cmd.Process.Kill()
  70. if err != nil {
  71. return err
  72. }
  73. _, err = a.cmd.Process.Wait()
  74. if err != nil {
  75. return err
  76. }
  77. a.state = stateStopped
  78. return nil
  79. }
  80. // restart restarts the stopped etcd process.
  81. func (a *Agent) restart() error {
  82. a.cmd = exec.Command(a.cmd.Path, a.cmd.Args[1:]...)
  83. a.cmd.Stdout = a.logfile
  84. a.cmd.Stderr = a.logfile
  85. err := a.cmd.Start()
  86. if err != nil {
  87. return err
  88. }
  89. a.state = stateStarted
  90. return nil
  91. }
  92. func (a *Agent) cleanup() error {
  93. if err := a.stop(); err != nil {
  94. return err
  95. }
  96. a.state = stateUninitialized
  97. a.logfile.Close()
  98. if err := archiveLogAndDataDir(a.etcdLogPath, a.dataDir()); err != nil {
  99. return err
  100. }
  101. f, err := os.Create(a.etcdLogPath)
  102. a.logfile = f
  103. if err != nil {
  104. return err
  105. }
  106. // https://www.kernel.org/doc/Documentation/sysctl/vm.txt
  107. // https://github.com/torvalds/linux/blob/master/fs/drop_caches.c
  108. cmd := exec.Command("/bin/sh", "-c", `echo "echo 1 > /proc/sys/vm/drop_caches" | sudo sh`)
  109. if err := cmd.Run(); err != nil {
  110. log.Printf("error when cleaning page cache (%v)", err)
  111. }
  112. return nil
  113. }
  114. // terminate stops the exiting etcd process the agent started
  115. // and removes the data dir.
  116. func (a *Agent) terminate() error {
  117. err := a.stop()
  118. if err != nil {
  119. return err
  120. }
  121. err = os.RemoveAll(a.dataDir())
  122. if err != nil {
  123. return err
  124. }
  125. a.state = stateTerminated
  126. return nil
  127. }
  128. func (a *Agent) dropPort(port int) error {
  129. return netutil.DropPort(port)
  130. }
  131. func (a *Agent) recoverPort(port int) error {
  132. return netutil.RecoverPort(port)
  133. }
  134. func (a *Agent) status() client.Status {
  135. return client.Status{State: a.state}
  136. }
  137. func (a *Agent) dataDir() string {
  138. datadir := path.Join(a.cmd.Path, "*.etcd")
  139. args := a.cmd.Args
  140. // only parse the simple case like "--data-dir /var/lib/etcd"
  141. for i, arg := range args {
  142. if arg == "--data-dir" {
  143. datadir = args[i+1]
  144. break
  145. }
  146. }
  147. return datadir
  148. }
  149. func existDir(fpath string) bool {
  150. st, err := os.Stat(fpath)
  151. if err != nil {
  152. if os.IsNotExist(err) {
  153. return false
  154. }
  155. } else {
  156. return st.IsDir()
  157. }
  158. return false
  159. }
  160. func archiveLogAndDataDir(log string, datadir string) error {
  161. dir := path.Join("failure_archive", fmt.Sprint(time.Now().Format(time.RFC3339)))
  162. if existDir(dir) {
  163. dir = path.Join("failure_archive", fmt.Sprint(time.Now().Add(time.Second).Format(time.RFC3339)))
  164. }
  165. if err := os.MkdirAll(dir, 0755); err != nil {
  166. return err
  167. }
  168. if err := os.Rename(log, path.Join(dir, log)); err != nil {
  169. if !os.IsNotExist(err) {
  170. return err
  171. }
  172. }
  173. if err := os.Rename(datadir, path.Join(dir, datadir)); err != nil {
  174. if !os.IsNotExist(err) {
  175. return err
  176. }
  177. }
  178. return nil
  179. }