agent.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "github.com/coreos/etcd/pkg/netutil"
  23. "github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
  24. )
  25. const (
  26. stateUninitialized = "uninitialized"
  27. stateStarted = "started"
  28. stateStopped = "stopped"
  29. stateTerminated = "terminated"
  30. )
  31. type Agent struct {
  32. state string // the state of etcd process
  33. cmd *exec.Cmd
  34. logfile *os.File
  35. etcdLogPath string
  36. l net.Listener
  37. }
  38. func newAgent(etcd, etcdLogPath string) (*Agent, error) {
  39. // check if the file exists
  40. _, err := os.Stat(etcd)
  41. if err != nil {
  42. return nil, err
  43. }
  44. c := exec.Command(etcd)
  45. f, err := os.Create(etcdLogPath)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return &Agent{state: stateUninitialized, cmd: c, logfile: f, etcdLogPath: etcdLogPath}, nil
  50. }
  51. // start starts a new etcd process with the given args.
  52. func (a *Agent) start(args ...string) error {
  53. a.cmd = exec.Command(a.cmd.Path, args...)
  54. a.cmd.Stdout = a.logfile
  55. a.cmd.Stderr = a.logfile
  56. err := a.cmd.Start()
  57. if err != nil {
  58. return err
  59. }
  60. a.state = stateStarted
  61. return nil
  62. }
  63. // stop stops the existing etcd process the agent started.
  64. func (a *Agent) stop() error {
  65. if a.state != stateStarted {
  66. return nil
  67. }
  68. err := a.cmd.Process.Kill()
  69. if err != nil {
  70. return err
  71. }
  72. _, err = a.cmd.Process.Wait()
  73. if err != nil {
  74. return err
  75. }
  76. a.state = stateStopped
  77. return nil
  78. }
  79. // restart restarts the stopped etcd process.
  80. func (a *Agent) restart() error {
  81. a.cmd = exec.Command(a.cmd.Path, a.cmd.Args[1:]...)
  82. a.cmd.Stdout = a.logfile
  83. a.cmd.Stderr = a.logfile
  84. err := a.cmd.Start()
  85. if err != nil {
  86. return err
  87. }
  88. a.state = stateStarted
  89. return nil
  90. }
  91. func (a *Agent) cleanup() error {
  92. if err := a.stop(); err != nil {
  93. return err
  94. }
  95. a.state = stateUninitialized
  96. a.logfile.Close()
  97. if err := archiveLogAndDataDir(a.etcdLogPath, a.dataDir()); err != nil {
  98. return err
  99. }
  100. f, err := os.Create(a.etcdLogPath)
  101. a.logfile = f
  102. return err
  103. }
  104. // terminate stops the exiting etcd process the agent started
  105. // and removes the data dir.
  106. func (a *Agent) terminate() error {
  107. err := a.stop()
  108. if err != nil {
  109. return err
  110. }
  111. err = os.RemoveAll(a.dataDir())
  112. if err != nil {
  113. return err
  114. }
  115. a.state = stateTerminated
  116. return nil
  117. }
  118. func (a *Agent) dropPort(port int) error {
  119. return netutil.DropPort(port)
  120. }
  121. func (a *Agent) recoverPort(port int) error {
  122. return netutil.RecoverPort(port)
  123. }
  124. func (a *Agent) status() client.Status {
  125. return client.Status{State: a.state}
  126. }
  127. func (a *Agent) dataDir() string {
  128. datadir := path.Join(a.cmd.Path, "*.etcd")
  129. args := a.cmd.Args
  130. // only parse the simple case like "--data-dir /var/lib/etcd"
  131. for i, arg := range args {
  132. if arg == "--data-dir" {
  133. datadir = args[i+1]
  134. break
  135. }
  136. }
  137. return datadir
  138. }
  139. func archiveLogAndDataDir(log string, datadir string) error {
  140. dir := path.Join("failure_archive", fmt.Sprint(time.Now().Format(time.RFC3339)))
  141. if err := os.MkdirAll(dir, 0700); err != nil {
  142. return err
  143. }
  144. if err := os.Rename(log, path.Join(dir, log)); err != nil {
  145. return err
  146. }
  147. return os.Rename(datadir, path.Join(dir, datadir))
  148. }