version.go 2.3 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. "fmt"
  17. "os"
  18. "path"
  19. "strings"
  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.2.0"
  26. Version = "2.3.0-alpha.0"
  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. DataDir2_0 DataDirVersion = "2.0.0"
  35. DataDir2_0Proxy DataDirVersion = "2.0 proxy"
  36. DataDir2_0_1 DataDirVersion = "2.0.1"
  37. )
  38. type Versions struct {
  39. Server string `json:"etcdserver"`
  40. Cluster string `json:"etcdcluster"`
  41. // TODO: raft state machine version
  42. }
  43. func DetectDataDir(dirpath string) (DataDirVersion, error) {
  44. names, err := fileutil.ReadDir(dirpath)
  45. if err != nil {
  46. if os.IsNotExist(err) {
  47. err = nil
  48. }
  49. // Error reading the directory
  50. return DataDirUnknown, err
  51. }
  52. nameSet := types.NewUnsafeSet(names...)
  53. if nameSet.Contains("member") {
  54. ver, err := DetectDataDir(path.Join(dirpath, "member"))
  55. if ver == DataDir2_0 {
  56. return DataDir2_0_1, nil
  57. }
  58. return ver, err
  59. }
  60. if nameSet.ContainsAll([]string{"snap", "wal"}) {
  61. // .../wal cannot be empty to exist.
  62. walnames, err := fileutil.ReadDir(path.Join(dirpath, "wal"))
  63. if err == nil && len(walnames) > 0 {
  64. return DataDir2_0, nil
  65. }
  66. }
  67. if nameSet.ContainsAll([]string{"proxy"}) {
  68. return DataDir2_0Proxy, nil
  69. }
  70. return DataDirUnknown, nil
  71. }
  72. // Cluster only keeps the major.minor.
  73. func Cluster(v string) string {
  74. vs := strings.Split(v, ".")
  75. if len(vs) <= 2 {
  76. return v
  77. }
  78. return fmt.Sprintf("%s.%s", vs[0], vs[1])
  79. }