version.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 implements etcd version parsing and contains latest version
  15. // information.
  16. package version
  17. import (
  18. "fmt"
  19. "os"
  20. "path"
  21. "strings"
  22. "github.com/coreos/etcd/pkg/fileutil"
  23. "github.com/coreos/etcd/pkg/types"
  24. )
  25. var (
  26. // MinClusterVersion is the min cluster version this etcd binary is compatible with.
  27. MinClusterVersion = "2.2.0"
  28. Version = "2.3.8"
  29. // Git SHA Value will be set during build
  30. GitSHA = "Not provided (use ./build instead of go build)"
  31. )
  32. // DataDirVersion is an enum for versions of etcd logs.
  33. type DataDirVersion string
  34. const (
  35. DataDirUnknown DataDirVersion = "Unknown WAL"
  36. DataDir2_0 DataDirVersion = "2.0.0"
  37. DataDir2_0Proxy DataDirVersion = "2.0 proxy"
  38. DataDir2_0_1 DataDirVersion = "2.0.1"
  39. )
  40. type Versions struct {
  41. Server string `json:"etcdserver"`
  42. Cluster string `json:"etcdcluster"`
  43. // TODO: raft state machine version
  44. }
  45. func DetectDataDir(dirpath string) (DataDirVersion, error) {
  46. names, err := fileutil.ReadDir(dirpath)
  47. if err != nil {
  48. if os.IsNotExist(err) {
  49. err = nil
  50. }
  51. // Error reading the directory
  52. return DataDirUnknown, err
  53. }
  54. nameSet := types.NewUnsafeSet(names...)
  55. if nameSet.Contains("member") {
  56. ver, err := DetectDataDir(path.Join(dirpath, "member"))
  57. if ver == DataDir2_0 {
  58. return DataDir2_0_1, 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. return DataDirUnknown, nil
  73. }
  74. // Cluster only keeps the major.minor.
  75. func Cluster(v string) string {
  76. vs := strings.Split(v, ".")
  77. if len(vs) <= 2 {
  78. return v
  79. }
  80. return fmt.Sprintf("%s.%s", vs[0], vs[1])
  81. }