From 4c1e6a576e6dadd87af0b27f6adcbb467fc4d8f5 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Sun, 12 Mar 2023 21:57:13 +0100 Subject: [PATCH] feat(cosmovisor): add install disclaimer and config command (#15361) --- tools/cosmovisor/CHANGELOG.md | 5 +- tools/cosmovisor/README.md | 48 ++++++++----------- tools/cosmovisor/cmd/cosmovisor/config.go | 23 +++++++++ tools/cosmovisor/cmd/cosmovisor/init.go | 6 +-- tools/cosmovisor/cmd/cosmovisor/main.go | 2 +- tools/cosmovisor/cmd/cosmovisor/root.go | 19 ++++++-- tools/cosmovisor/cmd/cosmovisor/run.go | 4 -- tools/cosmovisor/cmd/cosmovisor/run_test.go | 4 -- tools/cosmovisor/cmd/cosmovisor/version.go | 31 ++++++------ .../cosmovisor/cmd/cosmovisor/version_test.go | 1 + 10 files changed, 80 insertions(+), 63 deletions(-) create mode 100644 tools/cosmovisor/cmd/cosmovisor/config.go delete mode 100644 tools/cosmovisor/cmd/cosmovisor/run_test.go diff --git a/tools/cosmovisor/CHANGELOG.md b/tools/cosmovisor/CHANGELOG.md index b28afab40d..9b66d909a4 100644 --- a/tools/cosmovisor/CHANGELOG.md +++ b/tools/cosmovisor/CHANGELOG.md @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +## Features + +* [#15361](https://github.com/cosmos/cosmos-sdk/pull/15361) Add `cosmovisor config` command to display the configuration used by cosmovisor. + ## Client Breaking Changes * [#14881](https://github.com/cosmos/cosmos-sdk/pull/14881) Cosmovisor supports only upgrade plan with a checksum. This is enforced by the `x/upgrade` module for better security. @@ -46,7 +50,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#14881](https://github.com/cosmos/cosmos-sdk/pull/14881) Refactor Cosmovisor to depend only on the `x/upgrade` module. * [#15362](https://github.com/cosmos/cosmos-sdk/pull/15362) Allow disabling Cosmovisor logs - ## v1.4.0 2022-10-23 ### API Breaking Changes diff --git a/tools/cosmovisor/README.md b/tools/cosmovisor/README.md index d591ba2624..f8d23b00a3 100644 --- a/tools/cosmovisor/README.md +++ b/tools/cosmovisor/README.md @@ -6,20 +6,21 @@ sidebar_position: 1 `cosmovisor` is a small process manager for Cosmos SDK application binaries that monitors the governance module for incoming chain upgrade proposals. If it sees a proposal that gets approved, `cosmovisor` can automatically download the new binary, stop the current binary, switch from the old binary to the new one, and finally restart the node with the new binary. -* [Design](#design) -* [Contributing](#contributing) -* [Setup](#setup) - * [Installation](#installation) - * [Command Line Arguments And Environment Variables](#command-line-arguments-and-environment-variables) - * [Folder Layout](#folder-layout) -* [Usage](#usage) - * [Initialization](#initialization) - * [Detecting Upgrades](#detecting-upgrades) - * [Auto-Download](#auto-download) -* [Example: SimApp Upgrade](#example-simapp-upgrade) - * [Chain Setup](#chain-setup) - * [Prepare Cosmovisor and Start the Chain](#prepare-cosmovisor-and-start-the-chain) - * [Update App](#update-app) +* [Cosmovisor](#cosmovisor) + * [Design](#design) + * [Contributing](#contributing) + * [Setup](#setup) + * [Installation](#installation) + * [Command Line Arguments And Environment Variables](#command-line-arguments-and-environment-variables) + * [Folder Layout](#folder-layout) + * [Usage](#usage) + * [Initialization](#initialization) + * [Detecting Upgrades](#detecting-upgrades) + * [Auto-Download](#auto-download) + * [Example: SimApp Upgrade](#example-simapp-upgrade) + * [Chain Setup](#chain-setup) + * [Prepare Cosmovisor and Start the Chain](#prepare-cosmovisor-and-start-the-chain) + * [Update App](#update-app) ## Design @@ -60,22 +61,11 @@ go install github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor@v0.1.0 Run `cosmovisor version` to check the cosmovisor version. -You can also install from source by pulling the cosmos-sdk repository and switching to the correct version and building as follows: +Alternatively, for building from source, simply run `make cosmovisor`. The binary will be located in `tools/cosmovisor`. -```shell -git clone git@github.com:cosmos/cosmos-sdk -cd cosmos-sdk -git checkout cosmovisor/vx.x.x -make cosmovisor -``` - -This will build cosmovisor in `/cosmovisor` directory. Afterwards you may want to put it into your machine's PATH like as follows: - -```shell -cp cosmovisor/cosmovisor ~/go/bin/cosmovisor -``` - -*Note: If you are using go `v1.15` or earlier, you will need to use `go get`, and you may want to run the command outside a project directory.* +:::warning +Building from source using `make cosmovisor` won't display the correct `cosmovisor` version. +::: ### Command Line Arguments And Environment Variables diff --git a/tools/cosmovisor/cmd/cosmovisor/config.go b/tools/cosmovisor/cmd/cosmovisor/config.go new file mode 100644 index 0000000000..1a4aa22783 --- /dev/null +++ b/tools/cosmovisor/cmd/cosmovisor/config.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + + "cosmossdk.io/tools/cosmovisor" + "github.com/spf13/cobra" +) + +var configCmd = &cobra.Command{ + Use: "config", + Short: "Display cosmovisor config (prints environment variables used by cosmovisor).", + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := cosmovisor.GetConfigFromEnv() + if err != nil { + return err + } + + fmt.Fprint(cmd.OutOrStdout(), cfg.DetailString()) + return nil + }, +} diff --git a/tools/cosmovisor/cmd/cosmovisor/init.go b/tools/cosmovisor/cmd/cosmovisor/init.go index c77342a312..dd49cce197 100644 --- a/tools/cosmovisor/cmd/cosmovisor/init.go +++ b/tools/cosmovisor/cmd/cosmovisor/init.go @@ -16,13 +16,9 @@ import ( "cosmossdk.io/x/upgrade/plan" ) -func init() { - rootCmd.AddCommand(initCmd) -} - var initCmd = &cobra.Command{ Use: "init ", - Short: "Initializes a cosmovisor daemon home directory.", + Short: "Initialize a cosmovisor daemon home directory.", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { logger := cmd.Context().Value(log.ContextKey).(*zerolog.Logger) diff --git a/tools/cosmovisor/cmd/cosmovisor/main.go b/tools/cosmovisor/cmd/cosmovisor/main.go index 26d696552e..44bd80fc62 100644 --- a/tools/cosmovisor/cmd/cosmovisor/main.go +++ b/tools/cosmovisor/cmd/cosmovisor/main.go @@ -12,7 +12,7 @@ func main() { logger := log.NewLoggerWithKV(os.Stdout, log.ModuleKey, "cosmovisor") ctx := context.WithValue(context.Background(), log.ContextKey, logger) - if err := rootCmd.ExecuteContext(ctx); err != nil { + if err := NewRootCmd().ExecuteContext(ctx); err != nil { cverrors.LogErrors(logger, "", err) os.Exit(1) } diff --git a/tools/cosmovisor/cmd/cosmovisor/root.go b/tools/cosmovisor/cmd/cosmovisor/root.go index 69f6cfe075..e4ac242ddc 100644 --- a/tools/cosmovisor/cmd/cosmovisor/root.go +++ b/tools/cosmovisor/cmd/cosmovisor/root.go @@ -4,8 +4,19 @@ import ( "github.com/spf13/cobra" ) -var rootCmd = &cobra.Command{ - Use: "cosmovisor", - Short: "A process manager for Cosmos SDK application binaries.", - Long: GetHelpText(), +func NewRootCmd() *cobra.Command { + rootCmd := &cobra.Command{ + Use: "cosmovisor", + Short: "A process manager for Cosmos SDK application binaries.", + Long: GetHelpText(), + } + + rootCmd.AddCommand( + initCmd, + runCmd, + configCmd, + NewVersionCmd(), + ) + + return rootCmd } diff --git a/tools/cosmovisor/cmd/cosmovisor/run.go b/tools/cosmovisor/cmd/cosmovisor/run.go index 8a6839ba0b..fe300d58fa 100644 --- a/tools/cosmovisor/cmd/cosmovisor/run.go +++ b/tools/cosmovisor/cmd/cosmovisor/run.go @@ -7,10 +7,6 @@ import ( "github.com/spf13/cobra" ) -func init() { - rootCmd.AddCommand(runCmd) -} - var runCmd = &cobra.Command{ Use: "run", Short: "Run an APP command.", diff --git a/tools/cosmovisor/cmd/cosmovisor/run_test.go b/tools/cosmovisor/cmd/cosmovisor/run_test.go deleted file mode 100644 index 7c585d9b64..0000000000 --- a/tools/cosmovisor/cmd/cosmovisor/run_test.go +++ /dev/null @@ -1,4 +0,0 @@ -package main - -// TODO: Write tests for func Run(args []string) error -// https://github.com/cosmos/cosmos-sdk/issues/11852 diff --git a/tools/cosmovisor/cmd/cosmovisor/version.go b/tools/cosmovisor/cmd/cosmovisor/version.go index 4e023d0e3f..3cbbf72cad 100644 --- a/tools/cosmovisor/cmd/cosmovisor/version.go +++ b/tools/cosmovisor/cmd/cosmovisor/version.go @@ -9,25 +9,26 @@ import ( "github.com/spf13/cobra" ) -func init() { - versionCmd.Flags().StringP(OutputFlag, "o", "text", "Output format (text|json)") - rootCmd.AddCommand(versionCmd) -} - // OutputFlag defines the output format flag var OutputFlag = "output" -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Prints the version of Cosmovisor.", - SilenceUsage: true, - RunE: func(cmd *cobra.Command, args []string) error { - if val, err := cmd.Flags().GetString(OutputFlag); val == "json" && err == nil { - return printVersionJSON(cmd, args) - } +func NewVersionCmd() *cobra.Command { + versionCmd := &cobra.Command{ + Use: "version", + Short: "Display cosmovisor and APP version.", + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + if val, err := cmd.Flags().GetString(OutputFlag); val == "json" && err == nil { + return printVersionJSON(cmd, args) + } - return printVersion(cmd, args) - }, + return printVersion(cmd, args) + }, + } + + versionCmd.Flags().StringP(OutputFlag, "o", "text", "Output format (text|json)") + + return versionCmd } func getVersion() string { diff --git a/tools/cosmovisor/cmd/cosmovisor/version_test.go b/tools/cosmovisor/cmd/cosmovisor/version_test.go index d42e3d6bb6..0a7d9e5868 100644 --- a/tools/cosmovisor/cmd/cosmovisor/version_test.go +++ b/tools/cosmovisor/cmd/cosmovisor/version_test.go @@ -12,6 +12,7 @@ import ( func TestVersionCommand_Error(t *testing.T) { logger := log.NewTestLogger(t).With(log.ModuleKey, "cosmovisor") + rootCmd := NewRootCmd() rootCmd.SetArgs([]string{"version"}) out := bytes.NewBufferString("")