Skip to content

Commit

Permalink
Set version in source instead of injecting via ldflags (#956)
Browse files Browse the repository at this point in the history
* Set version in source instead of injecting via ldflags

* Update version number and compatibility rules

* Add version tests

* Fix tests

* Received version 0.0.0 is compatible with whatever we have

* Add IsCompatible tests for 0.0.0

* Lint fix

* Add Prerelease to protocol definition

* Compile protobuf with the correct version of protoc

* Fix IsCompatible test

* Update protoc and related package versions so that they match everywhere

* One more protoc fix

* Another protoc fix

* More explicit error message with incompatible node version

* Version 0.0.0 should be compatible both ways

* Add DISABLE_VERSION_CHECK env var and hack regression.yml to unblock

* Lint fix

* Fix version numbers in regression test
  • Loading branch information
mcamou committed Apr 7, 2022
1 parent 19ba747 commit 8781997
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 137 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/regression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ on:
pull_request:
branches: [ master ]

env:
DISABLE_VERSION_CHECK: 1
# TODO Remove after https://github.com/drand/drand/pull/956 is merged, this is to get around the regression test failure
MAJOR: 1
MINOR: 4
PATCH: 0

jobs:
regression:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ jobs:
- name: Install Protoc
uses: arduino/setup-protoc@64c0c85d18e984422218383b81c52f8b077404d3 # pin@v1.1.2
with:
version: '3.14.0'
version: '3.19.4'

- name: Install Protoc-gen-go
run: |
GO111MODULE=off go get github.com/golang/protobuf/protoc-gen-go@v1.5.2
GO111MODULE=off go get google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
GO111MODULE=off go get github.com/golang/protobuf/protoc-gen-go@v1.27.1
GO111MODULE=off go get google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0
go mod tidy
- name: Get latest release version number
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ builds:
- env:
- CGO_ENABLED=0
ldflags:
- -s -w -X github.com/drand/drand/cmd/drand-cli.version={{.Version}} -X github.com/drand/drand/cmd/drand-cli.buildDate={{.Date}} -X github.com/drand/drand/cmd/drand-cli.gitCommit={{.Commit}}
- -s -w -X github.com/drand/drand/cmd/drand-cli.buildDate={{.Date}} -X github.com/drand/drand/cmd/drand-cli.gitCommit={{.ShortCommit}}
mod_timestamp: '{{ .CommitTimestamp }}'
archives:
- replacements:
Expand Down
52 changes: 18 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
.PHONY: test test-unit test-integration demo deploy-local linter install build client drand relay-http relay-gossip relay-s3

# Version values
ifeq ($(MAJOR),)
MAJOR := 0
endif

ifeq ($(PATCH),)
PATCH := 0
endif

ifeq ($(MINOR),)
MINOR := 0
endif

VER_PACKAGE=github.com/drand/drand/common
CLI_PACKAGE=github.com/drand/drand/cmd/drand-cli

GIT_REVISION := $(shell git rev-parse HEAD)
GIT_REVISION := $(shell git rev-parse --short HEAD)
BUILD_DATE := $(shell date -u +%d/%m/%Y@%H:%M:%S)

PROTOC_VERSION=3.17.3
PROTOC_VERSION=3.19.4
PROTOC_ZIP=protoc-$(PROTOC_VERSION)-linux-x86_64.zip

drand: build
Expand Down Expand Up @@ -79,56 +63,56 @@ demo:
############################################ Build ############################################

build_proto:
go get -u github.com/golang/protobuf/protoc-gen-go@v1.5.2
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0
cd protobuf && sh ./compile_proto.sh

# create the "drand" binary and install it in $GOBIN
install:
go install -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"
go install -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"

# create the "drand" binary in the current folder
build:
go build -o drand -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"
go build -o drand -mod=readonly -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"

# create the "drand-client" binary in the current folder
client:
go build -o drand-client -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/client
go build -o drand-client -mod=readonly -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/client
drand-client: client

# create the "drand-relay-http" binary in the current folder
relay-http:
go build -o drand-relay-http -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay
go build -o drand-relay-http -mod=readonly -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay
drand-relay-http: relay-http

# create the "drand-relay-gossip" binary in the current folder
relay-gossip:
go build -o drand-relay-gossip -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-gossip
go build -o drand-relay-gossip -mod=readonly -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-gossip
drand-relay-gossip: relay-gossip

# create the "drand-relay-s3" binary in the current folder
relay-s3:
go build -o drand-relay-s3 -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-s3
go build -o drand-relay-s3 -mod=readonly -ldflags "-X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-s3
drand-relay-s3: relay-s3

build_all: drand drand-client drand-relay-http drand-relay-gossip drand-relay-s3

build_docker_all: build_docker build_docker_dev
build_docker:
docker build --build-arg major=$(MAJOR) --build-arg minor=$(MINOR) --build-arg patch=$(PATCH) --build-arg gitCommit=`git rev-parse HEAD` -t drandorg/go-drand:latest .
docker build --build-arg gitCommit=$(GIT_REVISION) --build-arg buildDate=$(BUILD_DATE) -t drandorg/go-drand:latest .

build_docker_dev:
docker build -f test/docker/Dockerfile --build-arg major=$(MAJOR) --build-arg minor=$(MINOR) --build-arg patch=$(PATCH) --build-arg gitCommit=`git rev-parse HEAD` -t drandorg/go-drand-dev:latest .
docker build -f test/docker/Dockerfile --build-arg gitCommit=$(GIT_REVISION) --build-arg buildDate=$(BUILD_DATE) -t drandorg/go-drand-dev:latest .
############################################ Deps ############################################

install_deps_linux:
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP
sudo unzip -o $(PROTOC_ZIP) -d /usr/local bin/protoc 'include/*'
sudo chmod a+x /usr/local/bin/protoc
rm -f $(PROTOC_ZIP)

install_deps_darwin:
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-osx-x86_64.zip
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP
sudo unzip -o $(PROTOC_ZIP) -d /usr/local bin/protoc 'include/*'
sudo chmod a+x /usr/local/bin/protoc
rm -f $(PROTOC_ZIP)
2 changes: 1 addition & 1 deletion chain/beacon/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (b *BeaconTest) CreateNode(t *testing.T, i int) {
}

logger := log.NewLogger(nil, log.LogDebug).Named("BeaconTest").Named(knode.Addr).Named(fmt.Sprint(idx))
version := common.Version{Major: 0, Minor: 0, Patch: 0}
version := common.GetAppVersion()
node.handler, err = NewHandler(net.NewGrpcClient(), store, conf, logger, version)
checkErr(err)
if node.callback != nil {
Expand Down
55 changes: 0 additions & 55 deletions common/constants.go

This file was deleted.

45 changes: 35 additions & 10 deletions common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,48 @@ package common

import (
"fmt"
"os"

pbcommon "github.com/drand/drand/protobuf/common"
)

const (
FallbackMajor = 0
FallbackMinor = 0
FallbackPatch = 0
// Must be manually updated!
// Before releasing: Verify the version number and set Prerelease to ""
// After releasing: Increase the Patch number and set Prerelease to "-pre"
var version = Version{
Major: 1,
Minor: 4,
Patch: 0,
Prerelease: "pre",
}

// Set via -ldflags. Example:
// go install -ldflags "-X common.BUILDDATE=`date -u +%d/%m/%Y@%H:%M:%S` -X common.GITCOMMIT=`git rev-parse HEAD`
//
// See the Makefile and the Dockerfile in the root directory of the repo
var (
COMMIT = ""
BUILDDATE = ""
)

func GetAppVersion() Version {
return version
}

type Version struct {
Major uint32
Minor uint32
Patch uint32
Major uint32
Minor uint32
Patch uint32
Prerelease string
}

func (v Version) IsCompatible(verRcv Version) bool {
if verRcv.Major == FallbackMajor && verRcv.Minor == FallbackMinor && verRcv.Patch == FallbackPatch {
// This is to get around the problem with the regression test - Prerelease versions are compatible with anything
if os.Getenv("DISABLE_VERSION_CHECK") == "1" {
return true
}
if v.Major == verRcv.Major {

if v.Major == verRcv.Major && v.Minor == verRcv.Minor {
return true
}

Expand All @@ -34,5 +55,9 @@ func (v Version) ToProto() *pbcommon.NodeVersion {
}

func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
pre := ""
if v.Prerelease != "" {
pre = "+"
}
return fmt.Sprintf("%d.%d.%d%s%s", v.Major, v.Minor, v.Patch, pre, v.Prerelease)
}
121 changes: 121 additions & 0 deletions common/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package common

import (
"testing"
)

func TestVersionStringNoPre(t *testing.T) {
var version = Version{
Major: 1,
Minor: 2,
Patch: 3,
Prerelease: "",
}

actual := version.String()
expected := "1.2.3"

if actual != expected {
t.Fatalf("Incorrect version string. Actual: %s, expected: %s", actual, expected)
}
}

func TestVersionStringPre(t *testing.T) {
version := Version{
Major: 1,
Minor: 2,
Patch: 3,
Prerelease: "pre",
}

actual := version.String()
expected := "1.2.3+pre"

if actual != expected {
t.Fatalf("Incorrect version string. Actual: %s, expected: %s", actual, expected)
}
}

func TestVersionCompatible(t *testing.T) {
version000 := Version{
Major: 0,
Minor: 0,
Patch: 0,
Prerelease: "",
}

version123 := Version{
Major: 1,
Minor: 2,
Patch: 3,
Prerelease: "",
}

version124 := Version{
Major: 1,
Minor: 2,
Patch: 4,
Prerelease: "",
}

version123pre := Version{
Major: 1,
Minor: 2,
Patch: 3,
Prerelease: "+pre",
}

version130 := Version{
Major: 1,
Minor: 3,
Patch: 0,
Prerelease: "",
}

version130pre := Version{
Major: 1,
Minor: 3,
Patch: 0,
Prerelease: "pre",
}

version200 := Version{
Major: 2,
Minor: 0,
Patch: 0,
Prerelease: "",
}

testCompatible := func(a Version, b Version) {
if !a.IsCompatible(b) || !b.IsCompatible(a) {
t.Fatalf("Version %s should be compatible with %s", a, b)
}
}

testIncompatible := func(a Version, b Version) {
if a.IsCompatible(b) || b.IsCompatible(a) {
t.Fatalf("Version %s should not be compatible with %s", a, b)
}
}

testCompatible(version123, version123)
testCompatible(version123, version123pre)
testCompatible(version123, version124)

testIncompatible(version123, version130)
testIncompatible(version123, version130pre)
testIncompatible(version123, version200)
testIncompatible(version123pre, version130pre)

t.Setenv("DISABLE_VERSION_CHECK", "1")
testCompatible(version123, version000)
testCompatible(version123pre, version000)
testCompatible(version124, version000)
testCompatible(version130, version000)
testCompatible(version130pre, version000)
testCompatible(version200, version000)
testCompatible(version123, version130)
testCompatible(version123, version130pre)
testCompatible(version123, version200)
testCompatible(version123pre, version130pre)
}
Loading

0 comments on commit 8781997

Please sign in to comment.