version.go 2.4 KB

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