Skip to content

Commit

Permalink
feat: add update check to kysor
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed Mar 6, 2024
1 parent b01153c commit 60f9cf5
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 39 deletions.
6 changes: 3 additions & 3 deletions tools/kysor/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ commit=$(shell git rev-parse HEAD)
tag=$(shell git describe --tags --exact-match 2>/dev/null)

ldflags := $(LDFLAGS)
ldflags += -X github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd.Version=$(VERSION) \
-X github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd.Commit=$(commit) \
-X github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd.Tag=$(tag)
ldflags += -X github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/types.Version=$(VERSION) \
-X github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/types.Commit=$(commit) \
-X github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/types.Tag=$(tag)
ldflags := $(strip $(ldflags))

BUILD_FLAGS := -ldflags '$(ldflags)'
Expand Down
18 changes: 9 additions & 9 deletions tools/kysor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Without KYSOR for every pool the node runner has to build and run the protocol n
- Automatically **download** and **run** the correct runtime/protocol version
- Getting the new runtime upgrade during a pool upgrade **automatically** and therefore **don't risk timeout slashes**

**Installation**
### Installation/Update

Get the latest release of the KYSOR binaries [here](https://github.com/KYVENetwork/kyve-rdk/releases?q=kysor&expanded=true)

Expand Down Expand Up @@ -54,7 +54,7 @@ To verify that the KYSOR runs successfully just run
./kysor version
```

**Initialize KYSOR**
### Initialize KYSOR

Once you have successfully downloaded the KYSOR binary you have to initialize it.

Expand Down Expand Up @@ -97,7 +97,7 @@ More help on how to manage valaccounts can be found with `./kysor valaccounts [s
> If you have multiple valaccounts running on the same machine you are required to change the port of the metrics server (if enabled of course) so they don't overlap.

**Run KYSOR**
### Run KYSOR

After you have created the required valaccounts you can simply start running the KYSOR with the following command:

Expand All @@ -116,12 +116,7 @@ To see all available flags you can use the `--help` flag like this:
./kysor start --help
```

**Stop KYSOR**
```bash
./kysor stop
```

**Run KYSOR with systemd**
### Run KYSOR with systemd

For the daemon service root-privileges are required during the setup.
Create a service file. $USER is the Linux user which runs the process. Replace it before you copy the command.
Expand Down Expand Up @@ -163,3 +158,8 @@ You can see its logs with
```
sudo journalctl -u cosmoshubd -f
```

### Stop KYSOR
```bash
./kysor stop
```
3 changes: 2 additions & 1 deletion tools/kysor/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/utils"

commoncmd "github.com/KYVENetwork/kyve-rdk/common/goutils/cmd"
"github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/config"
Expand Down Expand Up @@ -71,7 +72,7 @@ func initCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize KYSOR",
PreRunE: commoncmd.SetupInteractiveMode,
PreRunE: commoncmd.CombineFuncs(utils.CheckUpdateAvailable, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
path, err := config.GetConfigFilePath(cmd)
if err != nil {
Expand Down
13 changes: 5 additions & 8 deletions tools/kysor/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/types"
"io"
"os"
"os/signal"
Expand Down Expand Up @@ -208,18 +209,14 @@ func getMainBranch(repo *git.Repository) (*plumbing.Reference, error) {

// pullRepo clones or pulls the kyve-rdk repository
func pullRepo(repoDir string, silent bool) (*kyveRepo, error) {
// TODO: change this branch to github.com/KYVENetwork/kyve-rdk once it's ready
repoName := "github.com/shifty11/kyve-rdk-fork"
repoUrl := fmt.Sprintf("https://%s.git", repoName)

var repo *git.Repository
if _, err := os.Stat(repoDir); os.IsNotExist(err) {
// Clone the given repository to the given directory
if !silent {
fmt.Printf("📥 Cloning %s\n", repoUrl)
fmt.Printf("📥 Cloning %s\n", types.RepoUrl)
}
repo, err = git.PlainClone(repoDir, false, &git.CloneOptions{
URL: repoUrl,
URL: types.RepoUrl,
Progress: os.Stdout,
})
if err != nil {
Expand Down Expand Up @@ -262,7 +259,7 @@ func pullRepo(repoDir string, silent bool) (*kyveRepo, error) {

return &kyveRepo{
repo: repo,
name: repoName,
name: types.RepoName,
dir: repoDir,
}, nil
}
Expand Down Expand Up @@ -871,7 +868,7 @@ func startCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start data validator",
PreRunE: commoncmd.CombineFuncs(utils.CheckDockerInstalled, config.LoadConfigs, commoncmd.SetupInteractiveMode),
PreRunE: commoncmd.CombineFuncs(utils.CheckDockerInstalled, utils.CheckUpdateAvailable, config.LoadConfigs, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
kyveClient, err := chain.NewKyveClient(config.GetConfigX(), config.ValaccountConfigs)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tools/kysor/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func statusCmd() *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Show KYSOR status",
PreRunE: commoncmd.CombineFuncs(utils.CheckDockerInstalled, config.LoadConfigs, commoncmd.SetupInteractiveMode),
PreRunE: commoncmd.CombineFuncs(utils.CheckDockerInstalled, utils.CheckUpdateAvailable, config.LoadConfigs, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
kyveClient, err := chain.NewKyveClient(config.GetConfigX(), config.ValaccountConfigs)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tools/kysor/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func stopCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "stop",
Short: "Stop the KYVE data validator",
PreRunE: commoncmd.CombineFuncs(utils.CheckDockerInstalled, config.LoadConfigs, commoncmd.SetupInteractiveMode),
PreRunE: commoncmd.CombineFuncs(utils.CheckDockerInstalled, utils.CheckUpdateAvailable, config.LoadConfigs, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
cli, err := client.NewClientWithOpts(client.WithAPIVersionNegotiation())
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions tools/kysor/cmd/types/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

const (
// TODO: change this branch to github.com/KYVENetwork/kyve-rdk once it's ready
RepoName = "github.com/shifty11/kyve-rdk-fork"
RepoUrl = "https://" + RepoName + ".git"
)
7 changes: 7 additions & 0 deletions tools/kysor/cmd/types/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

var (
Version = ""
Commit = ""
Tag = ""
)
52 changes: 52 additions & 0 deletions tools/kysor/cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package utils

import (
"fmt"
"github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/types"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/hashicorp/go-version"
"strings"

"github.com/docker/docker/client"
"github.com/fatih/color"
Expand Down Expand Up @@ -81,3 +87,49 @@ func CheckDockerInstalled(_ *cobra.Command, _ []string) error {
defer cli.Close()
return nil
}

func CheckUpdateAvailable(_ *cobra.Command, _ []string) error {
currentVersion, err := version.NewVersion(types.Version)
if err != nil {
// Silently ignore the error
return nil
}

// Create the remote with repository URL
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
URLs: []string{types.RepoUrl},
})

// We can then use every Remote functions to retrieve wanted information
refs, err := rem.List(&git.ListOptions{
// Returns all references, including peeled references.
PeelingOption: git.AppendPeeled,
})
if err != nil {
return err
}

// Only check for tags that are newer than the current version
var latestTag *version.Version
for _, ref := range refs {
if ref.Name().IsTag() && strings.HasPrefix(ref.Name().Short(), "tools/kysor@") {
v, err := version.NewVersion(strings.TrimPrefix(ref.Name().Short(), "tools/kysor@"))
if err != nil {
continue
}
if v.GreaterThan(currentVersion) {
if latestTag == nil || v.GreaterThan(latestTag) {
latestTag = v
}
}
}
}

if latestTag != nil {
readmeLink := "/tree/main/tools/kysor#installation-update"
updateLink := types.RepoUrl + readmeLink
fmt.Printf("🎉 A new version of KYSOR is available: %s\n Update guide: %s\n\n", latestTag.Original(), updateLink)
}
return nil
}
8 changes: 4 additions & 4 deletions tools/kysor/cmd/valaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func valaccountsCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create a new valaccount",
PreRunE: commoncmd.CombineFuncs(config.LoadConfigs, commoncmd.SetupInteractiveMode),
PreRunE: commoncmd.CombineFuncs(utils.CheckUpdateAvailable, config.LoadConfigs, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
kyveClient, err := chain.NewKyveClient(config.GetConfigX(), nil)
if err != nil {
Expand Down Expand Up @@ -282,7 +282,7 @@ func valaccountsShowBalanceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show-balance",
Short: "Show the balance of a valaccount",
PreRunE: commoncmd.CombineFuncs(config.LoadConfigs, commoncmd.SetupInteractiveMode),
PreRunE: commoncmd.CombineFuncs(utils.CheckUpdateAvailable, config.LoadConfigs, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
kyveClient, err := chain.NewKyveClient(config.GetConfigX(), nil)
if err != nil {
Expand Down Expand Up @@ -359,7 +359,7 @@ func valaccountsTransferCmd() *cobra.Command {
Use: "transfer",
Short: "Transfer tokens from a valaccount to another address",
Example: "kysor valaccounts transfer --from <valaccount> --to <address> --amount <amount>",
PreRunE: commoncmd.CombineFuncs(config.LoadConfigs, commoncmd.SetupInteractiveMode),
PreRunE: commoncmd.CombineFuncs(utils.CheckUpdateAvailable, config.LoadConfigs, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
kyveClient, err := chain.NewKyveClient(config.GetConfigX(), config.ValaccountConfigs)
if err != nil {
Expand Down Expand Up @@ -434,7 +434,7 @@ func valaccountsDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete a valaccount",
PreRunE: commoncmd.CombineFuncs(config.LoadConfigs, commoncmd.SetupInteractiveMode),
PreRunE: commoncmd.CombineFuncs(utils.CheckUpdateAvailable, config.LoadConfigs, commoncmd.SetupInteractiveMode),
RunE: func(cmd *cobra.Command, args []string) error {
// Return if no valaccount exists
flagValaccShowAddressName.Options = config.ValaccountConfigOptions
Expand Down
23 changes: 11 additions & 12 deletions tools/kysor/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ package cmd

import (
"fmt"
"github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/types"
"github.com/KYVENetwork/kyve-rdk/tools/kysor/cmd/utils"
"runtime"

"github.com/spf13/cobra"
)

var (
Version = "(none)"
Commit = "(none)"
Tag = ""
)

func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show KYSOR version",
Use: "version",
Short: "Show KYSOR version",
PreRunE: utils.CheckUpdateAvailable,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Kysor version: %s\n", Version)
if Tag != "" {
fmt.Printf("Git tag: %s\n", Tag)
fmt.Printf("Kysor version: %s\n", types.Version)
if types.Tag != "" {
fmt.Printf("Git tag: %s\n", types.Tag)
}
if types.Commit != "" {
fmt.Printf("Git commit: %s\n", types.Commit)
}
fmt.Printf("Git commit: %s\n", Commit)
fmt.Println()
fmt.Printf("Go version: %s\n", runtime.Version())
fmt.Println()
Expand Down

0 comments on commit 60f9cf5

Please sign in to comment.