Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI | Change Vorpal vorpal-latest-version flag flow (AST-48376) #797

Merged
merged 10 commits into from
Jul 16, 2024
2 changes: 1 addition & 1 deletion internal/commands/scarealtime/sca-realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func RunScaRealtime(scaRealTimeWrapper wrappers.ScaRealTimeWrapper) func(*cobra.
fmt.Println("Running SCA Realtime...")

// Handle SCA Resolver. Checks if it already exists and if it is in the latest version
err = osinstaller.InstallOrUpgrade(&scaconfig.Params)
_, err = osinstaller.InstallOrUpgrade(&scaconfig.Params)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/commands/vorpal/vorpal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ func TestInstallOrUpgrade_firstInstallation_Success(t *testing.T) {

func firstInstallation() error {
os.RemoveAll(vorpalconfig.Params.WorkingDir())
err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
_, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
return err
}

func TestInstallOrUpgrade_installationIsUpToDate_Success(t *testing.T) {
err := firstInstallation()
assert.NilError(t, err, "Error on first installation of vorpal")
err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
_, err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
assert.NilError(t, err, "Error when not need to upgrade")
}

func TestInstallOrUpgrade_installationIsNotUpToDate_Success(t *testing.T) {
err := firstInstallation()
assert.NilError(t, err, "Error on first installation of vorpal")
changeHashFile()
err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
_, err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
assert.NilError(t, err, "Error when need to upgrade")
fileExists, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath())
assert.Assert(t, fileExists, "Executable file not found")
Expand Down
14 changes: 7 additions & 7 deletions internal/services/osinstaller/os-installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,38 @@ func downloadFile(downloadURLPath, filePath string) error {
// InstallOrUpgrade Checks the version according to the hash file,
// downloads the RealTime installation if the version is not up-to-date,
// Extracts the RealTime installation according to the operating system type
func InstallOrUpgrade(installationConfiguration *InstallationConfiguration) error {
func InstallOrUpgrade(installationConfiguration *InstallationConfiguration) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to add an alias to the bool? like - bool doesInstalltion

logger.PrintIfVerbose("Handling RealTime Installation...")
if downloadNotNeeded(installationConfiguration) {
logger.PrintIfVerbose("RealTime installation already exists and is up to date. Skipping download.")
return nil
return false, nil
}

// Create temporary working directory if not exists
err := createWorkingDirectory(installationConfiguration)
if err != nil {
return err
return false, err
}

// Download RealTime installation
err = downloadFile(installationConfiguration.DownloadURL, filepath.Join(installationConfiguration.WorkingDir(), installationConfiguration.FileName))
if err != nil {
return err
return false, err
}

// Download hash file
err = downloadHashFile(installationConfiguration.HashDownloadURL, installationConfiguration.HashFilePath())
if err != nil {
return err
return false, err
}

// Unzip or extract downloaded zip depending on which OS is running
err = UnzipOrExtractFiles(installationConfiguration)
if err != nil {
return err
return false, err
}

return nil
return true, nil
}

// createWorkingDirectory Creates a working directory to handle Realtime functionality
Expand Down
18 changes: 12 additions & 6 deletions internal/services/vorpal.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func CreateVorpalScanRequest(vorpalParams VorpalScanParams, wrapperParams Vorpal
return nil, err
}

err = manageVorpalInstallation(vorpalParams, wrapperParams.VorpalWrapper)
err = manageVorpalInstallation(vorpalParams, wrapperParams.VorpalWrapper, wrapperParams)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -93,16 +93,22 @@ func executeScan(vorpalWrapper grpcs.VorpalWrapper, filePath string) (*grpcs.Sca
return vorpalWrapper.Scan(fileName, sourceCode)
}

func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper) error {
func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper, vorpalWrappers VorpalWrappersParam) error {
vorpalInstalled, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath())

if vorpalParams.VorpalUpdateVersion || !vorpalInstalled {
if err := vorpalWrapper.HealthCheck(); err == nil {
_ = vorpalWrapper.ShutDown()
if !vorpalInstalled || vorpalParams.VorpalUpdateVersion {
if err := checkLicense(vorpalParams.IsDefaultAgent, vorpalWrappers); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Or told us to shut down the service here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to check here for helth check before shutting down. or the shut down check that there sis engine alive bofre>?

return err
}
if err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params); err != nil {
newInstallation, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
if err != nil {
return err
}
if newInstallation && vorpalParams.VorpalUpdateVersion {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if newInstallation && vorpalParams.VorpalUpdateVersion {
if newInstallation {

in a situation we check together that I removed the executable file- we also want to shut down the service

if err := vorpalWrapper.HealthCheck(); err == nil {
_ = vorpalWrapper.ShutDown()
}
}
}
return nil
}
Expand Down
Loading