agent.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. err := a.cmd.Process.Kill()
  64. if err != nil {
  65. return err
  66. }
  67. _, err = a.cmd.Process.Wait()
  68. if err != nil {
  69. return err
  70. }
  71. a.state = stateStopped
  72. return nil
  73. }
  74. // restart restarts the stopped etcd process.
  75. func (a *Agent) restart() error {
  76. a.cmd = exec.Command(a.cmd.Path, a.cmd.Args[1:]...)
  77. a.cmd.Stdout = a.logfile
  78. a.cmd.Stderr = a.logfile
  79. err := a.cmd.Start()
  80. if err != nil {
  81. return err
  82. }
  83. a.state = stateStarted
  84. return nil
  85. }
  86. func (a *Agent) cleanup() error {
  87. err := a.stop()
  88. if err != nil {
  89. return err
  90. }
  91. a.state = stateUninitialized
  92. a.logfile.Close()
  93. if err := archiveLogAndDataDir("etcd.log", a.dataDir()); err != nil {
  94. return err
  95. }
  96. f, err := os.Create("etcd.log")
  97. a.logfile = f
  98. return err
  99. }
  100. // terminate stops the exiting etcd process the agent started
  101. // and removes the data dir.
  102. func (a *Agent) terminate() error {
  103. err := a.stop()
  104. if err != nil {
  105. return err
  106. }
  107. err = os.RemoveAll(a.dataDir())
  108. if err != nil {
  109. return err
  110. }
  111. a.state = stateTerminated
  112. return nil
  113. }
  114. func (a *Agent) status() client.Status {
  115. return client.Status{State: a.state}
  116. }
  117. func (a *Agent) dataDir() string {
  118. datadir := path.Join(a.cmd.Path, "*.etcd")
  119. args := a.cmd.Args
  120. // only parse the simple case like "-data-dir /var/lib/etcd"
  121. for i, arg := range args {
  122. if arg == "-data-dir" {
  123. datadir = args[i+1]
  124. break
  125. }
  126. }
  127. return datadir
  128. }
  129. func archiveLogAndDataDir(log string, datadir string) error {
  130. dir := path.Join("failure_archive", fmt.Sprint(time.Now().Format(time.RFC3339)))
  131. if err := os.MkdirAll(dir, 0700); err != nil {
  132. return err
  133. }
  134. if err := os.Rename(log, path.Join(dir, log)); err != nil {
  135. return err
  136. }
  137. return os.Rename(datadir, path.Join(dir, datadir))
  138. }