version.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 version
  15. import (
  16. "encoding/json"
  17. "log"
  18. "os"
  19. "path"
  20. "github.com/coreos/etcd/pkg/fileutil"
  21. "github.com/coreos/etcd/pkg/types"
  22. )
  23. var (
  24. // MinClusterVersion is the min cluster version this etcd binary is compatible with.
  25. MinClusterVersion = "2.0.0"
  26. Version = "2.1.0-alpha.0+git"
  27. // Git SHA Value will be set during build
  28. GitSHA = "Not provided (use ./build instead of go build)"
  29. )
  30. // WalVersion is an enum for versions of etcd logs.
  31. type DataDirVersion string
  32. const (
  33. DataDirUnknown DataDirVersion = "Unknown WAL"
  34. DataDir0_4 DataDirVersion = "0.4.x"
  35. DataDir2_0 DataDirVersion = "2.0.0"
  36. DataDir2_0Proxy DataDirVersion = "2.0 proxy"
  37. DataDir2_0_1 DataDirVersion = "2.0.1"
  38. )
  39. type Versions struct {
  40. Server string `json:"etcdserver"`
  41. // TODO: etcdcluster version
  42. // TODO: raft state machine version
  43. }
  44. // MarshalJSON returns the JSON encoding of Versions struct.
  45. func MarshalJSON() []byte {
  46. b, err := json.Marshal(Versions{Server: Version})
  47. if err != nil {
  48. log.Panicf("version: cannot marshal versions to json (%v)", err)
  49. }
  50. return b
  51. }
  52. func DetectDataDir(dirpath string) (DataDirVersion, error) {
  53. names, err := fileutil.ReadDir(dirpath)
  54. if err != nil {
  55. if os.IsNotExist(err) {
  56. err = nil
  57. }
  58. // Error reading the directory
  59. return DataDirUnknown, err
  60. }
  61. nameSet := types.NewUnsafeSet(names...)
  62. if nameSet.Contains("member") {
  63. ver, err := DetectDataDir(path.Join(dirpath, "member"))
  64. if ver == DataDir2_0 {
  65. return DataDir2_0_1, nil
  66. } else if ver == DataDir0_4 {
  67. // How in the blazes did it get there?
  68. return DataDirUnknown, nil
  69. }
  70. return ver, err
  71. }
  72. if nameSet.ContainsAll([]string{"snap", "wal"}) {
  73. // .../wal cannot be empty to exist.
  74. walnames, err := fileutil.ReadDir(path.Join(dirpath, "wal"))
  75. if err == nil && len(walnames) > 0 {
  76. return DataDir2_0, nil
  77. }
  78. }
  79. if nameSet.ContainsAll([]string{"proxy"}) {
  80. return DataDir2_0Proxy, nil
  81. }
  82. if nameSet.ContainsAll([]string{"snapshot", "conf", "log"}) {
  83. return DataDir0_4, nil
  84. }
  85. if nameSet.ContainsAll([]string{"standby_info"}) {
  86. return DataDir0_4, nil
  87. }
  88. return DataDirUnknown, nil
  89. }