Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding cosmovisor version command #10103

Merged
merged 35 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8688902
setting up cobra for `cosmovisor version` command
spoo-bar Sep 9, 2021
68ac1d3
adding Version variable to store during build time
spoo-bar Sep 13, 2021
2c43589
typo fix
spoo-bar Sep 13, 2021
2550790
regex check to get version in appropriate format
spoo-bar Sep 13, 2021
daf32fb
removing cobra and checking version arg
spoo-bar Sep 13, 2021
5139fde
adding tests related to version command
spoo-bar Sep 13, 2021
824220b
running comsovisor commands after the application
spoo-bar Sep 13, 2021
8164365
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 13, 2021
765b24e
added changelog and some doc
spoo-bar Sep 13, 2021
65baaca
updating makefile to get version from git tag
spoo-bar Sep 13, 2021
61ec885
updating readme docs for `version` cmd
spoo-bar Sep 13, 2021
186f76c
Update cosmovisor/CHANGELOG.md
spoo-bar Sep 13, 2021
2fb3122
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 13, 2021
535f943
Merge branch 'spoorthi/9999-cosmovisor-version-cmd' of https://github…
spoo-bar Sep 14, 2021
bd3c30a
removing version formatting
spoo-bar Sep 14, 2021
17949bb
updating readme
spoo-bar Sep 14, 2021
df73124
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 14, 2021
f541bec
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 14, 2021
9d158bd
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 14, 2021
312b7b0
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 16, 2021
b341057
updating on feedback
spoo-bar Sep 16, 2021
8d0388b
Merge branch 'spoorthi/9999-cosmovisor-version-cmd' of https://github…
spoo-bar Sep 16, 2021
b7f7eda
removing empty line
spoo-bar Sep 16, 2021
892ab21
print cosmovisor version befor application version
spoo-bar Sep 16, 2021
a7314c3
inlining printVersion
spoo-bar Sep 16, 2021
cdce913
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 16, 2021
84b9fd6
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 17, 2021
00103c0
Update cosmovisor/CHANGELOG.md
jgimeno Sep 17, 2021
fb9520f
Update cosmovisor/README.md
jgimeno Sep 17, 2021
816f480
Update cosmovisor/README.md
jgimeno Sep 17, 2021
8a24d16
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
jgimeno Sep 17, 2021
65b44ed
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 21, 2021
ad903e1
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 21, 2021
ccfeb8f
Merge branch 'master' into spoorthi/9999-cosmovisor-version-cmd
spoo-bar Sep 21, 2021
b1e876d
updating go.mod for go1.17
spoo-bar Sep 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cosmovisor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

+ [\#8590](https://github.com/cosmos/cosmos-sdk/pull/8590) File watcher for cosmovisor. Instead of parsing logs from stdin and stderr, we watch the `<DAEMON_HOME>/data/upgrade-info.json` file updates using polling mechanism.

+ [\#9999](https://github.com/cosmos/cosmos-sdk/issues/9999) Added `version` command which returns the version of the cosmovisor along with the version of the application
spoo-bar marked this conversation as resolved.
Show resolved Hide resolved

### Improvements

+ [\#10018](https://github.com/cosmos/cosmos-sdk/pull/10018) Strict boolean argument parsing: cosmovisor will fail if user will not set correctly a boolean variable. Correct values are: "true", "false", "" (not setting) - all case not sensitive.
Expand Down
3 changes: 2 additions & 1 deletion cosmovisor/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/make -f

VERSION := $(shell echo $(shell git describe --always --match "cosmovisor/v*") | sed 's/^cosmovisor[/]//')

all: cosmovisor test

cosmovisor:
go build -mod=readonly ./cmd/cosmovisor
go build -ldflags="-X 'github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor/cmd.Version=$(VERSION)'" -mod=readonly ./cmd/cosmovisor

test:
go test -mod=readonly -race ./...
Expand Down
9 changes: 9 additions & 0 deletions cosmovisor/cmd/cosmovisor/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cmd

func RunCosmovisorCommands(args []string) {
spoo-bar marked this conversation as resolved.
Show resolved Hide resolved

if isVersionCommand(args) {
printVersion()
}

}
51 changes: 51 additions & 0 deletions cosmovisor/cmd/cosmovisor/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"fmt"
"os"
"regexp"
"strings"
)

// Cosmovisor version value. Set during build
spoo-bar marked this conversation as resolved.
Show resolved Hide resolved
var Version string

const (
versionRegex = `^v(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)`
// ^ - Ensuring the text starts with the match
// v - Version string starts with `v`
// (?P<major>0|[1-9]\d*) - Match group `major` - Matches 0 once or digits 1-9 zero or many times
// \. - Dot between `major` and `minor`
// (?P<minor>0|[1-9]\d*) - Match group `minor`- Matches 0 once or digits 1-9 zero or many times
// \. - Dot between `minor` and `patch`
// (?P<patch>0|[1-9]\d*) - Match group `patch`- Matches 0 once or digits 1-9 zero or many times
)

func isVersionCommand(args []string) bool {
return len(args) == 1 && strings.EqualFold(args[0], "version")
}

func printVersion() {

spoo-bar marked this conversation as resolved.
Show resolved Hide resolved
versionFound, version := getVersion(Version)
spoo-bar marked this conversation as resolved.
Show resolved Hide resolved

if versionFound {
fmt.Println("Cosmovisor Version: ", version)
} else {
fmt.Fprintf(os.Stderr, "Could not parse the Cosmovisor version from the binary. \nVersion value found in binary: %s \n", Version)
}

}

// getVersion does regex matching against the version value set in the cosmovisor binary
// Returns if valid version was found, and if valid version is found, returns the version string
// Returns false and empty string when valid version is not found
func getVersion(versionStr string) (bool, string) {
re := regexp.MustCompile(versionRegex) // Requires major, minor and patch versions in the form v1.0.0

for _, version_match := range re.FindAllString(versionStr, -1) {
return true, version_match
}

return false, ""
}
spoo-bar marked this conversation as resolved.
Show resolved Hide resolved
90 changes: 90 additions & 0 deletions cosmovisor/cmd/cosmovisor/cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestIsVersionCommand(t *testing.T) {
cases := []struct {
name string
args []string
expectRes bool
}{{
name: "valid args - lowercase",
args: []string{"version"},
expectRes: true,
}, {
name: "typo",
args: []string{"vrsion"},
expectRes: false,
}, {
name: "non version command",
args: []string{"start"},
expectRes: false,
}, {
name: "incorrect format",
args: []string{"start", "version"},
expectRes: false,
}}

for i := range cases {
tc := cases[i]
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)
res := isVersionCommand(tc.args)
require.Equal(tc.expectRes, res)
})
}
}

func TestGetVersion(t *testing.T) {
cases := []struct {
name string
versionStr string
expectValid bool
expectRes string
}{
{
name: "valid version string",
versionStr: "v1.0.0",
expectValid: true,
expectRes: "v1.0.0",
}, {
name: "valid git tag string",
versionStr: "v1.1.0-alpha2-1-g81f1347e",
expectValid: true,
expectRes: "v1.1.0",
}, {
name: "invalid string",
versionStr: "v1.test",
expectValid: false,
expectRes: "",
}, {
name: "incomplete version string",
versionStr: "v1.1",
expectValid: false,
expectRes: "",
}, {
name: "incomplete git tag string",
versionStr: "v1.1-alpha2-1-g81f1347e",
expectValid: false,
expectRes: "",
},
}

for i := range cases {
tc := cases[i]
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)
isValid, res := getVersion(tc.versionStr)
if tc.expectValid {
require.True(isValid)
require.Equal(tc.expectRes, res)
} else {
require.False(isValid)
}
})
}
}
5 changes: 5 additions & 0 deletions cosmovisor/cmd/cosmovisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/cosmos/cosmos-sdk/cosmovisor"
"github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor/cmd"
)

func main() {
Expand All @@ -16,6 +17,7 @@ func main() {

// Run is the main loop, but returns an error
func Run(args []string) error {

cfg, err := cosmovisor.GetConfigFromEnv()
if err != nil {
return err
Expand All @@ -34,5 +36,8 @@ func Run(args []string) error {
if doUpgrade && err == nil {
fmt.Println("[cosmovisor] upgrade detected, DAEMON_RESTART_AFTER_UPGRADE is off. Verify new upgrade and start cosmovisor again.")
}

// Run cosmosvisor commands e.g `cosmovisor version`
spoo-bar marked this conversation as resolved.
Show resolved Hide resolved
cmd.RunCosmovisorCommands(args)
return err
}
4 changes: 4 additions & 0 deletions cosmovisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module github.com/cosmos/cosmos-sdk/cosmovisor
go 1.15

require (
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-getter v1.4.1
github.com/otiai10/copy v1.4.2
github.com/stretchr/testify v1.7.0
google.golang.org/api v0.44.0 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading