version.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. Version = "2.0.4+git"
  23. InternalVersion = "2"
  24. )
  25. // WalVersion is an enum for versions of etcd logs.
  26. type DataDirVersion string
  27. const (
  28. DataDirUnknown DataDirVersion = "Unknown WAL"
  29. DataDir0_4 DataDirVersion = "0.4.x"
  30. DataDir2_0 DataDirVersion = "2.0.0"
  31. DataDir2_0Proxy DataDirVersion = "2.0 proxy"
  32. DataDir2_0_1 DataDirVersion = "2.0.1"
  33. )
  34. func DetectDataDir(dirpath string) (DataDirVersion, error) {
  35. names, err := fileutil.ReadDir(dirpath)
  36. if err != nil {
  37. if os.IsNotExist(err) {
  38. err = nil
  39. }
  40. // Error reading the directory
  41. return DataDirUnknown, err
  42. }
  43. nameSet := types.NewUnsafeSet(names...)
  44. if nameSet.Contains("member") {
  45. ver, err := DetectDataDir(path.Join(dirpath, "member"))
  46. if ver == DataDir2_0 {
  47. return DataDir2_0_1, nil
  48. } else if ver == DataDir0_4 {
  49. // How in the blazes did it get there?
  50. return DataDirUnknown, nil
  51. }
  52. return ver, err
  53. }
  54. if nameSet.ContainsAll([]string{"snap", "wal"}) {
  55. // .../wal cannot be empty to exist.
  56. walnames, err := fileutil.ReadDir(path.Join(dirpath, "wal"))
  57. if err == nil && len(walnames) > 0 {
  58. return DataDir2_0, nil
  59. }
  60. }
  61. if nameSet.ContainsAll([]string{"proxy"}) {
  62. return DataDir2_0Proxy, nil
  63. }
  64. if nameSet.ContainsAll([]string{"snapshot", "conf", "log"}) {
  65. return DataDir0_4, nil
  66. }
  67. if nameSet.ContainsAll([]string{"standby_info"}) {
  68. return DataDir0_4, nil
  69. }
  70. return DataDirUnknown, nil
  71. }