version.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "os"
  17. "path"
  18. "github.com/coreos/etcd/pkg/fileutil"
  19. "github.com/coreos/etcd/pkg/types"
  20. )
  21. var (
  22. // MinClusterVersion is the min cluster version this etcd binary is compatible with.
  23. MinClusterVersion = "2.0.0"
  24. Version = "2.1.3+git"
  25. // Git SHA Value will be set during build
  26. GitSHA = "Not provided (use ./build instead of go build)"
  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. Cluster string `json:"etcdcluster"`
  40. // TODO: raft state machine version
  41. }
  42. func DetectDataDir(dirpath string) (DataDirVersion, error) {
  43. names, err := fileutil.ReadDir(dirpath)
  44. if err != nil {
  45. if os.IsNotExist(err) {
  46. err = nil
  47. }
  48. // Error reading the directory
  49. return DataDirUnknown, err
  50. }
  51. nameSet := types.NewUnsafeSet(names...)
  52. if nameSet.Contains("member") {
  53. ver, err := DetectDataDir(path.Join(dirpath, "member"))
  54. if ver == DataDir2_0 {
  55. return DataDir2_0_1, nil
  56. } else if ver == DataDir0_4 {
  57. // How in the blazes did it get there?
  58. return DataDirUnknown, nil
  59. }
  60. return ver, err
  61. }
  62. if nameSet.ContainsAll([]string{"snap", "wal"}) {
  63. // .../wal cannot be empty to exist.
  64. walnames, err := fileutil.ReadDir(path.Join(dirpath, "wal"))
  65. if err == nil && len(walnames) > 0 {
  66. return DataDir2_0, nil
  67. }
  68. }
  69. if nameSet.ContainsAll([]string{"proxy"}) {
  70. return DataDir2_0Proxy, nil
  71. }
  72. if nameSet.ContainsAll([]string{"snapshot", "conf", "log"}) {
  73. return DataDir0_4, nil
  74. }
  75. if nameSet.ContainsAll([]string{"standby_info"}) {
  76. return DataDir0_4, nil
  77. }
  78. return DataDirUnknown, nil
  79. }