From a4ea865f403d0bcd9f9d937b0124360815267072 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 09:54:56 +0300 Subject: [PATCH] try fix vorpal engine initialize duplication --- internal/services/osinstaller/os-installer.go | 6 +-- internal/services/vorpal.go | 46 +++++++++++++++---- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/internal/services/osinstaller/os-installer.go b/internal/services/osinstaller/os-installer.go index b508125c4..a8af56741 100644 --- a/internal/services/osinstaller/os-installer.go +++ b/internal/services/osinstaller/os-installer.go @@ -106,13 +106,13 @@ func downloadNotNeeded(installationConfiguration *InstallationConfiguration) boo logger.PrintIfVerbose("RealTime installation exists. Checking if it is the latest version...") - isLastVersion, _ := isLastVersion(installationConfiguration.HashFilePath(), installationConfiguration.HashDownloadURL, installationConfiguration.HashFilePath()) + isLastVersion, _ := IsLastVersion(installationConfiguration.HashFilePath(), installationConfiguration.HashDownloadURL, installationConfiguration.HashFilePath()) return isLastVersion } -// isLastVersion Checks if the Installation is updated by comparing hashes -func isLastVersion(hashFilePath, hashURL, zipFileNameHash string) (bool, error) { +// IsLastVersion Checks if the Installation is updated by comparing hashes +func IsLastVersion(hashFilePath, hashURL, zipFileNameHash string) (bool, error) { existingHash, _ := getHashValue(hashFilePath) // Download hash file err := downloadHashFile(hashURL, zipFileNameHash) diff --git a/internal/services/vorpal.go b/internal/services/vorpal.go index 08a65eeab..b4f3a56ad 100644 --- a/internal/services/vorpal.go +++ b/internal/services/vorpal.go @@ -94,19 +94,44 @@ func executeScan(vorpalWrapper grpcs.VorpalWrapper, filePath string) (*grpcs.Sca } func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper) error { - vorpalInstalled, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath()) + vorpalInstalled, err := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath()) + if err != nil { + return fmt.Errorf("failed to check if Vorpal is installed: %w", err) + } - if vorpalParams.VorpalUpdateVersion || !vorpalInstalled { - if err := vorpalWrapper.HealthCheck(); err == nil { - _ = vorpalWrapper.ShutDown() - } - if err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params); err != nil { - return err - } + if vorpalInstalled { + return handleVorpalUpdate(vorpalParams, vorpalWrapper) + } + + if err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params); err != nil { + return fmt.Errorf("failed to install Vorpal: %w", err) } return nil } +func handleVorpalUpdate(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper) error { + if !shouldUpdateVorpalVersion(vorpalParams.VorpalUpdateVersion) { + return nil + } + + if err := vorpalWrapper.HealthCheck(); err == nil { + _ = vorpalWrapper.ShutDown() + } + + if err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params); err != nil { + return fmt.Errorf("failed to update Vorpal: %w", err) + } + + return nil +} + +func shouldUpdateVorpalVersion(vorpalUpdateVersionFlag bool) bool { + if !vorpalUpdateVersionFlag { + return false + } + return !isLastVersion() +} + func findVorpalPort() (int, error) { port, err := getAvailablePort() if err != nil { @@ -223,3 +248,8 @@ func waitForServer(address string, timeout time.Duration) bool { } return false } + +func isLastVersion() bool { + lastVersion, _ := osinstaller.IsLastVersion(vorpalconfig.Params.HashFilePath(), vorpalconfig.Params.HashDownloadURL, vorpalconfig.Params.HashFilePath()) + return lastVersion +}