Skip to content

Commit

Permalink
try fix vorpal engine initialize duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAlvo1 committed Jul 14, 2024
1 parent 40cfc67 commit a4ea865
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
6 changes: 3 additions & 3 deletions internal/services/osinstaller/os-installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 38 additions & 8 deletions internal/services/vorpal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit a4ea865

Please sign in to comment.