From 86a865dba8005036996125618276ea5207fb9391 Mon Sep 17 00:00:00 2001 From: Daniel Shubin Date: Fri, 21 Jun 2024 22:44:00 +0000 Subject: [PATCH] [PLAT-14443] YBA Installer wait for ready time configurable. Summary: We should be able to configure how long yba installer will wait for yba to be "ready" after an install or upgrade. I also improved how we set the current schema version to not require devs to update a constant. Test Plan: tested install and an install + upgrade with the new confi goption Reviewers: muthu, sanketh Reviewed By: muthu Subscribers: yugaware Differential Revision: https://phorge.dev.yugabyte.com/D36067 --- managed/yba-installer/cmd/init.go | 20 ++++++++-- managed/yba-installer/pkg/common/common.go | 11 ++++-- .../pkg/ybactlstate/migration.go | 37 ++++++++++++++++++- .../yba-installer/pkg/ybactlstate/state.go | 5 +-- managed/yba-installer/yba-ctl.yml.reference | 3 ++ 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/managed/yba-installer/cmd/init.go b/managed/yba-installer/cmd/init.go index 9210c56b1291..85501f5bab79 100644 --- a/managed/yba-installer/cmd/init.go +++ b/managed/yba-installer/cmd/init.go @@ -117,6 +117,18 @@ func handleRootCheck(cmdName string) { log.Fatal("Could not determine current user: " + err.Error()) } + if !viper.IsSet("as_root") && cmdName == "yba-ctl upgrade" { + // Upgrading from before "as_root" exists. perform legacy check for /opt/yba-ctl + _, err := os.Stat(common.YbactlRootInstallDir) + if user.Uid == "0" && err != nil { + log.Fatal("no root install found at /opt/yba-ctl, cannot upgrade with root") + } else if user.Uid != "0" && err == nil { + log.Fatal("Detected root install at /opt/yba-ctl, cannot upgrade as non-root") + } + log.Debug("legacy root check passed for upgrade") + return + } + // First, validate that the user (root access) matches the config 'as_root' if user.Uid == "0" && !viper.GetBool("as_root") { log.Fatal("running as root user with 'as_root' set to false is not supported") @@ -124,10 +136,10 @@ func handleRootCheck(cmdName string) { (viper.GetBool("as_root") || common.Exists(common.YbactlRootInstallDir)) { // Allow running certain commands as non-root in root install mode switch cmdName { - case "yba-ctl version", "yba-ctl status", "yba-ctl createBackup": - break - default: - log.Fatal("running as non-root user with 'as_root' set to true is not supported") + case "yba-ctl version", "yba-ctl status", "yba-ctl createBackup": + break + default: + log.Fatal("running as non-root user with 'as_root' set to true is not supported") } } } diff --git a/managed/yba-installer/pkg/common/common.go b/managed/yba-installer/pkg/common/common.go index 4d1e587bf5c0..8e91a5c66717 100644 --- a/managed/yba-installer/pkg/common/common.go +++ b/managed/yba-installer/pkg/common/common.go @@ -590,18 +590,23 @@ func WaitForYBAReady(version string) error { var resp *http.Response var err error - // Check YBA version every 10 seconds - retriesCount := 20 - for i := 0; i < retriesCount; i++ { + waitSecs := viper.GetInt("wait_for_yba_ready_secs") + endTime := time.Now().Add(time.Duration(waitSecs) * time.Second) + success := false + for time.Now().Before(endTime) { resp, err = http.Get(url) if err != nil { log.Info(fmt.Sprintf("YBA at %s not ready. Checking again in 10 seconds.", url)) time.Sleep(10 * time.Second) } else { + success = true break } } + if !success { + return fmt.Errorf("YBA at %s not ready after %d minutes", url, waitSecs/60) + } if resp != nil { defer resp.Body.Close() diff --git a/managed/yba-installer/pkg/ybactlstate/migration.go b/managed/yba-installer/pkg/ybactlstate/migration.go index f3afba24afce..4d1c1e279654 100644 --- a/managed/yba-installer/pkg/ybactlstate/migration.go +++ b/managed/yba-installer/pkg/ybactlstate/migration.go @@ -19,9 +19,13 @@ const ymlTypeFixMV = 4 const promOomConfgMV = 5 const promTLSCipherSuites = 6 const asRoot = 7 +const ybaWait = 8 + +// Please do not use this in ybactlstate package, only use getSchemaVersion() +var schemaVersionCache = -1 func handleMigration(state *State) error { - for state._internalFields.SchemaVersion < schemaVersion { + for state._internalFields.SchemaVersion < getSchemaVersion() { nextSchema := state._internalFields.SchemaVersion + 1 migrate := getMigrationHandler(nextSchema) if err := migrate(state); err != nil { @@ -183,7 +187,24 @@ func migrateAsRootConfig(state *State) error { return nil } -// TODO: Also need to remember to update schemaVersion when adding migration! (automate testing?) +func migrateYbaWait(state *State) error { + if !viper.IsSet("wait_for_yba_ready_secs") { + log.Info("wait for ready not set") + viper.ReadConfig(bytes.NewBufferString(config.ReferenceYbaCtlConfig)) + log.Info(fmt.Sprintf("setting to %d", viper.GetInt("wait_for_yba_ready_secs"))) + err := common.SetYamlValue(common.InputFile(), "wait_for_yba_ready_secs", + viper.GetInt("wait_for_yba_ready_secs")) + if err != nil { + return fmt.Errorf("Error migrating yb_wait config: %s", err.Error()) + } + } else { + log.Info("wait for ready set") + } + + common.InitViper() + return nil +} + var migrations map[int]migrator = map[int]migrator{ defaultMigratorValue: defaultMigrate, promConfigMV: migratePrometheus, @@ -192,6 +213,7 @@ var migrations map[int]migrator = map[int]migrator{ promOomConfgMV: migratePrometheusOOMConfig, promTLSCipherSuites: migratePrometheusTLSCipherSuites, asRoot: migrateAsRootConfig, + ybaWait: migrateYbaWait, } func getMigrationHandler(toSchema int) migrator { @@ -201,3 +223,14 @@ func getMigrationHandler(toSchema int) migrator { } return m } + +func getSchemaVersion() int { + if schemaVersionCache == -1 { + for k := range migrations { + if k > schemaVersionCache { + schemaVersionCache = k + } + } + } + return schemaVersionCache +} diff --git a/managed/yba-installer/pkg/ybactlstate/state.go b/managed/yba-installer/pkg/ybactlstate/state.go index 9a747a2e37c7..caa5a9301108 100644 --- a/managed/yba-installer/pkg/ybactlstate/state.go +++ b/managed/yba-installer/pkg/ybactlstate/state.go @@ -9,8 +9,7 @@ import ( ) const ( - StateFileName = ".yba_installer.state" - schemaVersion int = 6 + StateFileName = ".yba_installer.state" ) type State struct { @@ -63,7 +62,7 @@ func New() *State { }, _internalFields: internalFields{ ChangeID: 0, - SchemaVersion: schemaVersion, + SchemaVersion: getSchemaVersion(), }, } } diff --git a/managed/yba-installer/yba-ctl.yml.reference b/managed/yba-installer/yba-ctl.yml.reference index fc69079fef22..74aade5bc1bb 100644 --- a/managed/yba-installer/yba-ctl.yml.reference +++ b/managed/yba-installer/yba-ctl.yml.reference @@ -30,6 +30,9 @@ service_username: "yugabyte" # by service_username. as_root: true +# Number of seconds to wait for yba to start +wait_for_yba_ready_secs: 300 + # Config parameters for YB-Platform: # =======================================