agent.go 3.4 KB

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