2019-04-03 20:13:39 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2019-04-10 07:04:24 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-04-03 20:13:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
verError = errs.Class("version error")
|
2019-04-03 20:13:39 +01:00
|
|
|
// the following fields are set by linker flags. if any of them
|
|
|
|
// are set and fail to parse, the program will fail to start
|
|
|
|
buildTimestamp string // unix seconds since epoch
|
|
|
|
buildCommitHash string
|
|
|
|
buildVersion string // semantic version format
|
|
|
|
buildRelease string // true/false
|
|
|
|
|
|
|
|
// Build is a struct containing all relevant build information associated with the binary
|
|
|
|
Build Info
|
|
|
|
)
|
|
|
|
|
|
|
|
// Info is the versioning information for a binary
|
|
|
|
type Info struct {
|
2019-04-10 07:38:26 +01:00
|
|
|
// sync/atomic cache
|
|
|
|
commitHashCRC uint32
|
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
Timestamp time.Time `json:"timestamp,omitempty"`
|
|
|
|
CommitHash string `json:"commitHash,omitempty"`
|
|
|
|
Version SemVer `json:"version"`
|
|
|
|
Release bool `json:"release,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SemVer represents a semantic version
|
|
|
|
type SemVer struct {
|
|
|
|
Major int64 `json:"major"`
|
|
|
|
Minor int64 `json:"minor"`
|
|
|
|
Patch int64 `json:"patch"`
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
// AllowedVersions provides the Minimum SemVer per Service
|
2019-04-03 20:13:39 +01:00
|
|
|
type AllowedVersions struct {
|
2019-07-02 16:28:06 +01:00
|
|
|
Bootstrap SemVer
|
|
|
|
Satellite SemVer
|
|
|
|
Storagenode SemVer
|
|
|
|
Uplink SemVer
|
|
|
|
Gateway SemVer
|
|
|
|
Identity SemVer
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SemVerRegex is the regular expression used to parse a semantic version.
|
|
|
|
// https://github.com/Masterminds/semver/blob/master/LICENSE.txt
|
|
|
|
const SemVerRegex string = `v?([0-9]+)\.([0-9]+)\.([0-9]+)`
|
|
|
|
|
|
|
|
var versionRegex = regexp.MustCompile("^" + SemVerRegex + "$")
|
|
|
|
|
|
|
|
// NewSemVer parses a given version and returns an instance of SemVer or
|
|
|
|
// an error if unable to parse the version.
|
2019-07-02 16:28:06 +01:00
|
|
|
func NewSemVer(v string) (sv SemVer, err error) {
|
2019-04-03 20:13:39 +01:00
|
|
|
m := versionRegex.FindStringSubmatch(v)
|
|
|
|
if m == nil {
|
2019-07-02 16:28:06 +01:00
|
|
|
return SemVer{}, verError.New("invalid semantic version for build %s", v)
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// first entry of m is the entire version string
|
|
|
|
sv.Major, err = strconv.ParseInt(m[1], 10, 64)
|
|
|
|
if err != nil {
|
2019-07-02 16:28:06 +01:00
|
|
|
return SemVer{}, err
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sv.Minor, err = strconv.ParseInt(m[2], 10, 64)
|
|
|
|
if err != nil {
|
2019-07-02 16:28:06 +01:00
|
|
|
return SemVer{}, err
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sv.Patch, err = strconv.ParseInt(m[3], 10, 64)
|
|
|
|
if err != nil {
|
2019-07-02 16:28:06 +01:00
|
|
|
return SemVer{}, err
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
return sv, nil
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-09-05 22:10:05 +01:00
|
|
|
// Compare compare two versions, return -1 if compared version is greater, 0 if equal and 1 if less.
|
|
|
|
func (sem *SemVer) Compare(version SemVer) int {
|
|
|
|
result := sem.Major - version.Major
|
|
|
|
if result > 0 {
|
|
|
|
return 1
|
|
|
|
} else if result < 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
result = sem.Minor - version.Minor
|
|
|
|
if result > 0 {
|
|
|
|
return 1
|
|
|
|
} else if result < 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
result = sem.Patch - version.Patch
|
|
|
|
if result > 0 {
|
|
|
|
return 1
|
|
|
|
} else if result < 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
// String converts the SemVer struct to a more easy to handle string
|
|
|
|
func (sem *SemVer) String() (version string) {
|
|
|
|
return fmt.Sprintf("v%d.%d.%d", sem.Major, sem.Minor, sem.Patch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates Version_Info from a json byte array
|
|
|
|
func New(data []byte) (v Info, err error) {
|
|
|
|
err = json.Unmarshal(data, &v)
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal converts the existing Version Info to any json byte array
|
|
|
|
func (v Info) Marshal() (data []byte, err error) {
|
|
|
|
data, err = json.Marshal(v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
// Proto converts an Info struct to a pb.NodeVersion
|
|
|
|
// TODO: shouldn't we just use pb.NodeVersion everywhere? gogoproto will let
|
|
|
|
// us make it match Info.
|
|
|
|
func (v Info) Proto() (*pb.NodeVersion, error) {
|
|
|
|
return &pb.NodeVersion{
|
|
|
|
Version: v.Version.String(),
|
|
|
|
CommitHash: v.CommitHash,
|
2019-07-08 19:24:42 +01:00
|
|
|
Timestamp: v.Timestamp,
|
2019-04-10 07:04:24 +01:00
|
|
|
Release: v.Release,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
// isAcceptedVersion compares and checks if the passed version is greater/equal than the minimum required version
|
|
|
|
func isAcceptedVersion(test SemVer, target SemVer) bool {
|
|
|
|
return test.Major > target.Major || (test.Major == target.Major && (test.Minor > target.Minor || (test.Minor == target.Minor && test.Patch >= target.Patch)))
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if buildVersion == "" && buildTimestamp == "" && buildCommitHash == "" && buildRelease == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
timestamp, err := strconv.ParseInt(buildTimestamp, 10, 64)
|
|
|
|
if err != nil {
|
2019-07-02 16:28:06 +01:00
|
|
|
panic(verError.Wrap(err))
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|
|
|
|
Build = Info{
|
|
|
|
Timestamp: time.Unix(timestamp, 0),
|
|
|
|
CommitHash: buildCommitHash,
|
|
|
|
Release: strings.ToLower(buildRelease) == "true",
|
|
|
|
}
|
|
|
|
|
|
|
|
sv, err := NewSemVer(buildVersion)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:28:06 +01:00
|
|
|
Build.Version = sv
|
2019-04-03 20:13:39 +01:00
|
|
|
|
|
|
|
if Build.Timestamp.Unix() == 0 || Build.CommitHash == "" {
|
|
|
|
Build.Release = false
|
|
|
|
}
|
2019-04-10 07:38:26 +01:00
|
|
|
|
2019-04-03 20:13:39 +01:00
|
|
|
}
|