From 6caff0a99ea01d640b1db80bf0bc07d8032c31a0 Mon Sep 17 00:00:00 2001 From: arturrez Date: Wed, 13 Nov 2024 19:19:27 -0800 Subject: [PATCH 1/3] add config to disable automatic update use it for CLI running inside of the docker --- Dockerfile | 1 + Dockerfile.release | 1 + cmd/configcmd/config.go | 1 + cmd/configcmd/update.go | 49 ++++++++++++++++++++++++++++++++++++++ cmd/root.go | 12 ++++++++++ pkg/constants/constants.go | 1 + 6 files changed, 65 insertions(+) create mode 100644 cmd/configcmd/update.go diff --git a/Dockerfile b/Dockerfile index 415ef9f6a..167b8cd8e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,4 +18,5 @@ WORKDIR / # Copy the executables into the container COPY --from=builder /build/bin/avalanche . RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/* +RUN /avalanche config update disable ENTRYPOINT [ "./avalanche" ] diff --git a/Dockerfile.release b/Dockerfile.release index 94a187362..509665106 100644 --- a/Dockerfile.release +++ b/Dockerfile.release @@ -1,4 +1,5 @@ FROM debian:11-slim RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/* COPY avalanche / +RUN /avalanche config update disable ENTRYPOINT [ "/avalanche" ] diff --git a/cmd/configcmd/config.go b/cmd/configcmd/config.go index af6b401ee..c54619301 100644 --- a/cmd/configcmd/config.go +++ b/cmd/configcmd/config.go @@ -20,6 +20,7 @@ func NewCmd(injectedApp *application.Avalanche) *cobra.Command { app = injectedApp // set user metrics collection preferences cmd cmd.AddCommand(newMetricsCmd()) + cmd.AddCommand(newUpdateCmd()) cmd.AddCommand(newMigrateCmd()) cmd.AddCommand(newSingleNodeCmd()) cmd.AddCommand(newAuthorizeCloudAccessCmd()) diff --git a/cmd/configcmd/update.go b/cmd/configcmd/update.go new file mode 100644 index 000000000..deec93ebe --- /dev/null +++ b/cmd/configcmd/update.go @@ -0,0 +1,49 @@ +// Copyright (C) 2022, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. +package configcmd + +import ( + "errors" + + "github.com/ava-labs/avalanche-cli/pkg/cobrautils" + "github.com/ava-labs/avalanche-cli/pkg/constants" + "github.com/ava-labs/avalanche-cli/pkg/ux" + "github.com/spf13/cobra" +) + +// avalanche config metrics command +func newUpdateCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "update [enable | disable]", + Short: "opt in or out of update check", + Long: "set user preference between update check or not", + RunE: handleUpdateSettings, + Args: cobrautils.ExactArgs(1), + } + + return cmd +} + +func handleUpdateSettings(_ *cobra.Command, args []string) error { + switch args[0] { + case constants.Enable: + ux.Logger.PrintToUser("Thank you for opting in Avalanche CLI automated update check") + err := saveUpdatePreferences(true) + if err != nil { + return err + } + case constants.Disable: + ux.Logger.PrintToUser("Avalanche CLI automated update check will no longer be performed") + err := saveUpdatePreferences(false) + if err != nil { + return err + } + default: + return errors.New("Invalid update argument '" + args[0] + "'") + } + return nil +} + +func saveUpdatePreferences(enableUpdate bool) error { + return app.Conf.SetConfigValue(constants.ConfigUpdatesEnabledKey, enableUpdate) +} diff --git a/cmd/root.go b/cmd/root.go index 00ea688a2..fb70910cd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -158,6 +158,14 @@ func createApp(cmd *cobra.Command, _ []string) error { return nil } +func UpdateCheckDisabled(app *application.Avalanche) bool { + // returns true obly if explicitly disabled in the config + if app.Conf.ConfigFileExists() { + return !app.Conf.GetConfigBoolValue(constants.ConfigUpdatesEnabledKey) + } + return false +} + // checkForUpdates evaluates first if the user is maybe wanting to skip the update check // if there's no skip, it runs the update check func checkForUpdates(cmd *cobra.Command, app *application.Avalanche) error { @@ -165,6 +173,10 @@ func checkForUpdates(cmd *cobra.Command, app *application.Avalanche) error { lastActs *application.LastActions err error ) + // check if update check is skipped + if UpdateCheckDisabled(app) { + return nil + } // we store a timestamp of the last skip check in a file lastActs, err = app.ReadLastActionsFile() if err != nil { diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 904daecd6..9b7ae5bca 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -215,6 +215,7 @@ const ( ConfigAPMAdminAPIEndpointKey = "admin-api-endpoint" ConfigNodeConfigKey = "node-config" ConfigMetricsEnabledKey = "MetricsEnabled" + ConfigUpdatesEnabledKey = "UpdatesEnabled" ConfigAuthorizeCloudAccessKey = "AuthorizeCloudAccess" ConfigSingleNodeEnabledKey = "SingleNodeEnabled" ConfigSnapshotsAutoSaveKey = "SnapshotsAutoSaveEnabled" From 5b90ea7b1668fbfa346ecad1658e4e6c3eeab0b9 Mon Sep 17 00:00:00 2001 From: arturrez Date: Wed, 13 Nov 2024 19:20:11 -0800 Subject: [PATCH 2/3] go 1.22.8 for dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 167b8cd8e..9c0138559 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # ============= Compilation Stage ================ -FROM golang:1.22.7-bullseye AS builder +FROM golang:1.22.8-bullseye AS builder WORKDIR /build # Copy and download avalanche dependencies using go mod From 7f41e0d1a8b4437c6f425c7c28d36ca68978fa06 Mon Sep 17 00:00:00 2001 From: arturrez Date: Tue, 10 Dec 2024 10:10:31 -0800 Subject: [PATCH 3/3] inverse logic --- cmd/configcmd/update.go | 8 ++++---- cmd/root.go | 2 +- pkg/constants/constants.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/configcmd/update.go b/cmd/configcmd/update.go index deec93ebe..160a23aab 100644 --- a/cmd/configcmd/update.go +++ b/cmd/configcmd/update.go @@ -28,13 +28,13 @@ func handleUpdateSettings(_ *cobra.Command, args []string) error { switch args[0] { case constants.Enable: ux.Logger.PrintToUser("Thank you for opting in Avalanche CLI automated update check") - err := saveUpdatePreferences(true) + err := saveUpdateDisabledPreferences(false) if err != nil { return err } case constants.Disable: ux.Logger.PrintToUser("Avalanche CLI automated update check will no longer be performed") - err := saveUpdatePreferences(false) + err := saveUpdateDisabledPreferences(true) if err != nil { return err } @@ -44,6 +44,6 @@ func handleUpdateSettings(_ *cobra.Command, args []string) error { return nil } -func saveUpdatePreferences(enableUpdate bool) error { - return app.Conf.SetConfigValue(constants.ConfigUpdatesEnabledKey, enableUpdate) +func saveUpdateDisabledPreferences(disableUpdate bool) error { + return app.Conf.SetConfigValue(constants.ConfigUpdatesDisabledKey, disableUpdate) } diff --git a/cmd/root.go b/cmd/root.go index fb70910cd..d0991cf1a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -161,7 +161,7 @@ func createApp(cmd *cobra.Command, _ []string) error { func UpdateCheckDisabled(app *application.Avalanche) bool { // returns true obly if explicitly disabled in the config if app.Conf.ConfigFileExists() { - return !app.Conf.GetConfigBoolValue(constants.ConfigUpdatesEnabledKey) + return app.Conf.GetConfigBoolValue(constants.ConfigUpdatesDisabledKey) } return false } diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 9b7ae5bca..08bf4837b 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -215,7 +215,7 @@ const ( ConfigAPMAdminAPIEndpointKey = "admin-api-endpoint" ConfigNodeConfigKey = "node-config" ConfigMetricsEnabledKey = "MetricsEnabled" - ConfigUpdatesEnabledKey = "UpdatesEnabled" + ConfigUpdatesDisabledKey = "UpdatesDisabled" ConfigAuthorizeCloudAccessKey = "AuthorizeCloudAccess" ConfigSingleNodeEnabledKey = "SingleNodeEnabled" ConfigSnapshotsAutoSaveKey = "SnapshotsAutoSaveEnabled"