version.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. )
  28. // WalVersion is an enum for versions of etcd logs.
  29. type DataDirVersion string
  30. const (
  31. DataDirUnknown DataDirVersion = "Unknown WAL"
  32. DataDir0_4 DataDirVersion = "0.4.x"
  33. DataDir2_0 DataDirVersion = "2.0.0"
  34. DataDir2_0Proxy DataDirVersion = "2.0 proxy"
  35. DataDir2_0_1 DataDirVersion = "2.0.1"
  36. )
  37. type Versions struct {
  38. Server string `json:"etcdserver"`
  39. // TODO: etcdcluster version
  40. // TODO: raft state machine version
  41. }
  42. // MarshalJSON returns the JSON encoding of Versions struct.
  43. func MarshalJSON() []byte {
  44. b, err := json.Marshal(Versions{Server: Version})
  45. if err != nil {
  46. log.Panicf("version: cannot marshal versions to json (%v)", err)
  47. }
  48. return b
  49. }
  50. func DetectDataDir(dirpath string) (DataDirVersion, error) {
  51. names, err := fileutil.ReadDir(dirpath)
  52. if err != nil {
  53. if os.IsNotExist(err) {
  54. err = nil
  55. }
  56. // Error reading the directory
  57. return DataDirUnknown, err
  58. }
  59. nameSet := types.NewUnsafeSet(names...)
  60. if nameSet.Contains("member") {
  61. ver, err := DetectDataDir(path.Join(dirpath, "member"))
  62. if ver == DataDir2_0 {
  63. return DataDir2_0_1, nil
  64. } else if ver == DataDir0_4 {
  65. // How in the blazes did it get there?
  66. return DataDirUnknown, nil
  67. }
  68. return ver, err
  69. }
  70. if nameSet.ContainsAll([]string{"snap", "wal"}) {
  71. // .../wal cannot be empty to exist.
  72. walnames, err := fileutil.ReadDir(path.Join(dirpath, "wal"))
  73. if err == nil && len(walnames) > 0 {
  74. return DataDir2_0, nil
  75. }
  76. }
  77. if nameSet.ContainsAll([]string{"proxy"}) {
  78. return DataDir2_0Proxy, nil
  79. }
  80. if nameSet.ContainsAll([]string{"snapshot", "conf", "log"}) {
  81. return DataDir0_4, nil
  82. }
  83. if nameSet.ContainsAll([]string{"standby_info"}) {
  84. return DataDir0_4, nil
  85. }
  86. return DataDirUnknown, nil
  87. }