agent.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if err := a.stop(); err != nil {
  92. return err
  93. }
  94. a.state = stateUninitialized
  95. a.logfile.Close()
  96. if err := archiveLogAndDataDir("etcd.log", a.dataDir()); err != nil {
  97. return err
  98. }
  99. f, err := os.Create("etcd.log")
  100. a.logfile = f
  101. return err
  102. }
  103. // terminate stops the exiting etcd process the agent started
  104. // and removes the data dir.
  105. func (a *Agent) terminate() error {
  106. err := a.stop()
  107. if err != nil {
  108. return err
  109. }
  110. err = os.RemoveAll(a.dataDir())
  111. if err != nil {
  112. return err
  113. }
  114. a.state = stateTerminated
  115. return nil
  116. }
  117. func (a *Agent) dropPort(port int) error {
  118. return netutil.DropPort(port)
  119. }
  120. func (a *Agent) recoverPort(port int) error {
  121. return netutil.RecoverPort(port)
  122. }
  123. func (a *Agent) status() client.Status {
  124. return client.Status{State: a.state}
  125. }
  126. func (a *Agent) dataDir() string {
  127. datadir := path.Join(a.cmd.Path, "*.etcd")
  128. args := a.cmd.Args
  129. // only parse the simple case like "-data-dir /var/lib/etcd"
  130. for i, arg := range args {
  131. if arg == "-data-dir" {
  132. datadir = args[i+1]
  133. break
  134. }
  135. }
  136. return datadir
  137. }
  138. func archiveLogAndDataDir(log string, datadir string) error {
  139. dir := path.Join("failure_archive", fmt.Sprint(time.Now().Format(time.RFC3339)))
  140. if err := os.MkdirAll(dir, 0700); err != nil {
  141. return err
  142. }
  143. if err := os.Rename(log, path.Join(dir, log)); err != nil {
  144. return err
  145. }
  146. return os.Rename(datadir, path.Join(dir, datadir))
  147. }