agent.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. l net.Listener
  36. }
  37. func newAgent(etcd string) (*Agent, error) {
  38. // check if the file exists
  39. _, err := os.Stat(etcd)
  40. if err != nil {
  41. return nil, err
  42. }
  43. c := exec.Command(etcd)
  44. f, err := os.Create("etcd.log")
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &Agent{state: stateUninitialized, cmd: c, logfile: f}, nil
  49. }
  50. // start starts a new etcd process with the given args.
  51. func (a *Agent) start(args ...string) error {
  52. a.cmd = exec.Command(a.cmd.Path, args...)
  53. a.cmd.Stdout = a.logfile
  54. a.cmd.Stderr = a.logfile
  55. err := a.cmd.Start()
  56. if err != nil {
  57. return err
  58. }
  59. a.state = stateStarted
  60. return nil
  61. }
  62. // stop stops the existing etcd process the agent started.
  63. func (a *Agent) stop() error {
  64. if a.state != stateStarted {
  65. return nil
  66. }
  67. err := a.cmd.Process.Kill()
  68. if err != nil {
  69. return err
  70. }
  71. _, err = a.cmd.Process.Wait()
  72. if err != nil {
  73. return err
  74. }
  75. a.state = stateStopped
  76. return nil
  77. }
  78. // restart restarts the stopped etcd process.
  79. func (a *Agent) restart() error {
  80. a.cmd = exec.Command(a.cmd.Path, a.cmd.Args[1:]...)
  81. a.cmd.Stdout = a.logfile
  82. a.cmd.Stderr = a.logfile
  83. err := a.cmd.Start()
  84. if err != nil {
  85. return err
  86. }
  87. a.state = stateStarted
  88. return nil
  89. }
  90. func (a *Agent) cleanup() error {
  91. err := a.stop()
  92. if err != nil {
  93. return err
  94. }
  95. a.state = stateUninitialized
  96. a.logfile.Close()
  97. if err := archiveLogAndDataDir("etcd.log", a.dataDir()); err != nil {
  98. return err
  99. }
  100. f, err := os.Create("etcd.log")
  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. }