etcd2-restore.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 The etcd Authors
  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. "flag"
  17. "fmt"
  18. "os"
  19. "os/exec"
  20. "path"
  21. "regexp"
  22. "time"
  23. )
  24. var (
  25. etcdctlPath string
  26. etcdPath string
  27. etcdRestoreDir string
  28. etcdName string
  29. etcdPeerUrls string
  30. )
  31. func main() {
  32. flag.StringVar(&etcdctlPath, "etcdctl-path", "/usr/bin/etcdctl", "absolute path to etcdctl executable")
  33. flag.StringVar(&etcdPath, "etcd-path", "/usr/bin/etcd2", "absolute path to etcd2 executable")
  34. flag.StringVar(&etcdRestoreDir, "etcd-restore-dir", "/var/lib/etcd2-restore", "absolute path to etcd2 restore dir")
  35. flag.StringVar(&etcdName, "etcd-name", "default", "name of etcd2 node")
  36. flag.StringVar(&etcdPeerUrls, "etcd-peer-urls", "", "advertise peer urls")
  37. flag.Parse()
  38. if etcdPeerUrls == "" {
  39. panic("must set -etcd-peer-urls")
  40. }
  41. if finfo, err := os.Stat(etcdRestoreDir); err != nil {
  42. panic(err)
  43. } else {
  44. if !finfo.IsDir() {
  45. panic(fmt.Errorf("%s is not a directory", etcdRestoreDir))
  46. }
  47. }
  48. if !path.IsAbs(etcdctlPath) {
  49. panic(fmt.Sprintf("etcdctl-path %s is not absolute", etcdctlPath))
  50. }
  51. if !path.IsAbs(etcdPath) {
  52. panic(fmt.Sprintf("etcd-path %s is not absolute", etcdPath))
  53. }
  54. if err := restoreEtcd(); err != nil {
  55. panic(err)
  56. }
  57. }
  58. func restoreEtcd() error {
  59. etcdCmd := exec.Command(etcdPath, "--force-new-cluster", "--data-dir", etcdRestoreDir)
  60. etcdCmd.Stdout = os.Stdout
  61. etcdCmd.Stderr = os.Stderr
  62. if err := etcdCmd.Start(); err != nil {
  63. return fmt.Errorf("Could not start etcd2: %s", err)
  64. }
  65. defer etcdCmd.Wait()
  66. defer etcdCmd.Process.Kill()
  67. return runCommands(10, 2*time.Second)
  68. }
  69. var (
  70. clusterHealthRegex = regexp.MustCompile(".*cluster is healthy.*")
  71. lineSplit = regexp.MustCompile("\n+")
  72. colonSplit = regexp.MustCompile(`\:`)
  73. )
  74. func runCommands(maxRetry int, interval time.Duration) error {
  75. var retryCnt int
  76. for retryCnt = 1; retryCnt <= maxRetry; retryCnt++ {
  77. out, err := exec.Command(etcdctlPath, "cluster-health").CombinedOutput()
  78. if err == nil && clusterHealthRegex.Match(out) {
  79. break
  80. }
  81. fmt.Printf("Error: %s: %s\n", err, string(out))
  82. time.Sleep(interval)
  83. }
  84. if retryCnt > maxRetry {
  85. return fmt.Errorf("Timed out waiting for healthy cluster\n")
  86. }
  87. var (
  88. memberID string
  89. out []byte
  90. err error
  91. )
  92. if out, err = exec.Command(etcdctlPath, "member", "list").CombinedOutput(); err != nil {
  93. return fmt.Errorf("Error calling member list: %s", err)
  94. }
  95. members := lineSplit.Split(string(out), 2)
  96. if len(members) < 1 {
  97. return fmt.Errorf("Could not find a cluster member from: \"%s\"", members)
  98. }
  99. parts := colonSplit.Split(members[0], 2)
  100. if len(parts) < 2 {
  101. return fmt.Errorf("Could not parse member id from: \"%s\"", members[0])
  102. }
  103. memberID = parts[0]
  104. out, err = exec.Command(etcdctlPath, "member", "update", memberID, etcdPeerUrls).CombinedOutput()
  105. fmt.Printf("member update result: %s\n", string(out))
  106. return err
  107. }