2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-23 03:55:33 +04:00
|
|
|
|
|
|
|
package version
|
|
|
|
|
2015-03-25 01:13:34 +03:00
|
|
|
import (
|
2015-08-15 18:30:06 +03:00
|
|
|
"fmt"
|
2015-03-25 06:19:42 +03:00
|
|
|
"os"
|
2015-03-25 01:13:34 +03:00
|
|
|
"path"
|
2015-08-15 18:30:06 +03:00
|
|
|
"strings"
|
2015-03-25 01:13:34 +03:00
|
|
|
|
|
|
|
"github.com/coreos/etcd/pkg/fileutil"
|
|
|
|
"github.com/coreos/etcd/pkg/types"
|
|
|
|
)
|
|
|
|
|
2014-10-23 03:55:33 +04:00
|
|
|
var (
|
*: add cluster version and cluster version detection.
Cluster version is the min major.minor of all members in
the etcd cluster. Cluster version is set to the min version
that a etcd member is compatible with when first bootstrapp.
During a rolling upgrades, the cluster version will be updated
automatically.
For example:
```
Cluster [a:1, b:1 ,c:1] -> clusterVersion 1
update a -> 2, b -> 2
after a detection
Cluster [a:2, b:2 ,c:1] -> clusterVersion 1, since c is still 1
update c -> 2
after a detection
Cluster [a:2, b:2 ,c:2] -> clusterVersion 2
```
The API/raft component can utilize clusterVersion to determine if
it can accept a client request or a raft RPC.
We choose polling rather than pushing since we want to use the same
logic for cluster version detection and (TODO) cluster version checking.
Before a member actually joins a etcd cluster, it should check the version
of the cluster. Push does not work since the other members cannot push
version info to it before it actually joins. Moreover, we do not want our
raft RPC system (which is doing the heartbeat pushing) to coordinate cluster version.
2015-04-29 20:56:34 +03:00
|
|
|
// MinClusterVersion is the min cluster version this etcd binary is compatible with.
|
2015-08-13 21:41:19 +03:00
|
|
|
MinClusterVersion = "2.1.0"
|
2015-08-14 02:01:31 +03:00
|
|
|
Version = "2.2.0-alpha.1+git"
|
2015-05-09 02:49:12 +03:00
|
|
|
|
|
|
|
// Git SHA Value will be set during build
|
|
|
|
GitSHA = "Not provided (use ./build instead of go build)"
|
2014-10-23 03:55:33 +04:00
|
|
|
)
|
2015-03-25 01:13:34 +03:00
|
|
|
|
|
|
|
// WalVersion is an enum for versions of etcd logs.
|
|
|
|
type DataDirVersion string
|
|
|
|
|
|
|
|
const (
|
|
|
|
DataDirUnknown DataDirVersion = "Unknown WAL"
|
|
|
|
DataDir2_0 DataDirVersion = "2.0.0"
|
|
|
|
DataDir2_0Proxy DataDirVersion = "2.0 proxy"
|
|
|
|
DataDir2_0_1 DataDirVersion = "2.0.1"
|
|
|
|
)
|
|
|
|
|
2015-04-21 01:30:52 +03:00
|
|
|
type Versions struct {
|
2015-05-13 19:33:38 +03:00
|
|
|
Server string `json:"etcdserver"`
|
|
|
|
Cluster string `json:"etcdcluster"`
|
2015-04-21 01:30:52 +03:00
|
|
|
// TODO: raft state machine version
|
|
|
|
}
|
|
|
|
|
2015-03-25 01:13:34 +03:00
|
|
|
func DetectDataDir(dirpath string) (DataDirVersion, error) {
|
|
|
|
names, err := fileutil.ReadDir(dirpath)
|
|
|
|
if err != nil {
|
2015-03-25 06:19:42 +03:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
2015-03-25 01:13:34 +03:00
|
|
|
// Error reading the directory
|
|
|
|
return DataDirUnknown, err
|
|
|
|
}
|
|
|
|
nameSet := types.NewUnsafeSet(names...)
|
|
|
|
if nameSet.Contains("member") {
|
|
|
|
ver, err := DetectDataDir(path.Join(dirpath, "member"))
|
|
|
|
if ver == DataDir2_0 {
|
|
|
|
return DataDir2_0_1, nil
|
|
|
|
}
|
|
|
|
return ver, err
|
|
|
|
}
|
|
|
|
if nameSet.ContainsAll([]string{"snap", "wal"}) {
|
|
|
|
// .../wal cannot be empty to exist.
|
|
|
|
walnames, err := fileutil.ReadDir(path.Join(dirpath, "wal"))
|
|
|
|
if err == nil && len(walnames) > 0 {
|
|
|
|
return DataDir2_0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nameSet.ContainsAll([]string{"proxy"}) {
|
|
|
|
return DataDir2_0Proxy, nil
|
|
|
|
}
|
|
|
|
return DataDirUnknown, nil
|
|
|
|
}
|
2015-08-15 18:30:06 +03:00
|
|
|
|
|
|
|
// Cluster only keeps the major.minor.
|
|
|
|
func Cluster(v string) string {
|
|
|
|
vs := strings.Split(v, ".")
|
|
|
|
if len(vs) <= 2 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s.%s", vs[0], vs[1])
|
|
|
|
}
|