Skip to content

Commit

Permalink
feat: Adding cosmovisor version command (cosmos#10103)
Browse files Browse the repository at this point in the history
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: cosmos#9999

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

* Adding custom `version` command for cosmovisor binary version
* Read and returns Version from the cosmovisor binary which should be set during build

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
spoo-bar authored Sep 21, 2021
1 parent 22be061 commit 2dd4872
Show file tree
Hide file tree
Showing 9 changed files with 484 additions and 32 deletions.
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.
+ [\#10128](https://github.com/cosmos/cosmos-sdk/pull/10128) Change default value of `DAEMON_RESTART_AFTER_UPGRADE` to `true`.
+ [\#9999](https://github.com/cosmos/cosmos-sdk/issues/9999) Added `version` command that returns the cosmovisor version and the application version.


### Improvements

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
15 changes: 15 additions & 0 deletions cosmovisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ In order to support downloadable binaries, a tarball for each upgrade binary wil

The `DAEMON` specific code and operations (e.g. tendermint config, the application db, syncing blocks, etc.) all work as expected. The application binaries' directives such as command-line flags and environment variables also work as expected.

### Commands

Because Cosmovisor is meant to be used as a wrapper for a Cosmos SDK application, it does not require many commands.

To determine the version of Cosmovisor, run the following command:
```
cosmovisor version
```
The output of the `cosmovisor version` command shows the version of the Cosmos SDK application and the version of Cosmovisor:

```
Cosmovisor Version: v0.1.0-85-g65baacac0
0.43.0-beta1-319-ge3aec1840
```


### Detecting Upgrades

Expand Down
8 changes: 8 additions & 0 deletions cosmovisor/cmd/cosmovisor/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cmd

// RunCosmovisorCommands executes cosmosvisor commands e.g `cosmovisor version`
func RunCosmovisorCommands(args []string) {
if isVersionCommand(args) {
printVersion()
}
}
15 changes: 15 additions & 0 deletions cosmovisor/cmd/cosmovisor/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"fmt"
"strings"
)

// Version represents Cosmovisor version value. Set during build
var Version string

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

func printVersion() { fmt.Println("Cosmovisor Version: ", Version) }
40 changes: 40 additions & 0 deletions cosmovisor/cmd/cosmovisor/cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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)
})
}
}
4 changes: 4 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,8 @@ func main() {

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

cfg, err := cosmovisor.GetConfigFromEnv()
if err != nil {
return err
Expand All @@ -34,5 +37,6 @@ 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.")
}

return err
}
35 changes: 21 additions & 14 deletions cosmovisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@ module github.com/cosmos/cosmos-sdk/cosmovisor
go 1.17

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
)

require (
cloud.google.com/go v0.45.1 // indirect
cloud.google.com/go v0.81.0 // indirect
cloud.google.com/go/storage v1.10.0 // indirect
github.com/aws/aws-sdk-go v1.15.78 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/go-cleanhttp v0.5.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/mitchellh/go-homedir v1.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ulikunitz/xz v0.5.5 // indirect
go.opencensus.io v0.22.0 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 // indirect
golang.org/x/text v0.3.2 // indirect
google.golang.org/api v0.9.0 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect
google.golang.org/grpc v1.21.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/tools v0.1.2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
)
Loading

0 comments on commit 2dd4872

Please sign in to comment.