etcd2-restore.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "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 clusterHealthRegex = regexp.MustCompile(".*cluster is healthy.*")
  70. var lineSplit = regexp.MustCompile("\n+")
  71. var colonSplit = regexp.MustCompile("\\:")
  72. func runCommands(maxRetry int, interval time.Duration) error {
  73. var retryCnt int
  74. for retryCnt = 1; retryCnt <= maxRetry; retryCnt++ {
  75. out, err := exec.Command(etcdctlPath, "cluster-health").CombinedOutput()
  76. if err == nil && clusterHealthRegex.Match(out) {
  77. break
  78. }
  79. fmt.Printf("Error: %s: %s\n", err, string(out))
  80. time.Sleep(interval)
  81. }
  82. if retryCnt > maxRetry {
  83. return fmt.Errorf("Timed out waiting for healthy cluster\n")
  84. }
  85. var (
  86. memberID string
  87. out []byte
  88. err error
  89. )
  90. if out, err = exec.Command(etcdctlPath, "member", "list").CombinedOutput(); err != nil {
  91. return fmt.Errorf("Error calling member list: %s", err)
  92. }
  93. members := lineSplit.Split(string(out), 2)
  94. if len(members) < 1 {
  95. return fmt.Errorf("Could not find a cluster member from: \"%s\"", members)
  96. }
  97. parts := colonSplit.Split(members[0], 2)
  98. if len(parts) < 2 {
  99. return fmt.Errorf("Could not parse member id from: \"%s\"", members[0])
  100. }
  101. memberID = parts[0]
  102. out, err = exec.Command(etcdctlPath, "member", "update", memberID, etcdPeerUrls).CombinedOutput()
  103. fmt.Printf("member update result: %s\n", string(out))
  104. return err
  105. }