Skip to content

Commit

Permalink
feat(super-agent): Skip core bundle if installed (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
NSSPKrishna authored May 23, 2024
1 parent 083092c commit 33768eb
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 41 deletions.
10 changes: 5 additions & 5 deletions internal/install/bundle_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package install
import (
"context"
"fmt"
"strings"

log "github.com/sirupsen/logrus"

Expand All @@ -26,7 +27,6 @@ type BundleInstaller struct {
}

func NewBundleInstaller(ctx context.Context, manifest *types.DiscoveryManifest, recipeInstallerInterface RecipeInstaller, statusReporter StatusReporter) *BundleInstaller {

return &BundleInstaller{
ctx: ctx,
manifest: manifest,
Expand All @@ -42,7 +42,6 @@ func NewPrompter() *ux.PromptUIPrompter {
}

func (bi *BundleInstaller) InstallStopOnError(bundle *recipes.Bundle, assumeYes bool) error {

bi.reportBundleStatus(bundle)

installableBundleRecipes := bi.getInstallableBundleRecipes(bundle)
Expand All @@ -52,7 +51,6 @@ func (bi *BundleInstaller) InstallStopOnError(bundle *recipes.Bundle, assumeYes

for _, br := range installableBundleRecipes {
err := bi.InstallBundleRecipe(br, assumeYes)

if err != nil {
return err
}
Expand Down Expand Up @@ -80,7 +78,6 @@ func (bi *BundleInstaller) InstallContinueOnError(bundle *recipes.Bundle, assume
prompter := ux.NewPromptUIPrompter()
msg := "Continue installing? "
isConfirmed, err := prompter.PromptYesNo(msg)

if err != nil {
log.Debug(err)
isConfirmed = false
Expand Down Expand Up @@ -130,6 +127,9 @@ func (bi *BundleInstaller) InstallBundleRecipe(bundleRecipe *recipes.BundleRecip
}

recipeName := bundleRecipe.Recipe.Name
if strings.EqualFold(recipeName, types.SuperAgentRecipeName) && bundleRecipe.HasStatus(execution.RecipeStatusTypes.INSTALLED) {
return nil
}
if bi.installedRecipes[bundleRecipe.Recipe.Name] {
return nil
}
Expand All @@ -154,7 +154,7 @@ func (bi *BundleInstaller) getInstallableBundleRecipes(bundle *recipes.Bundle) [
var bundleRecipes []*recipes.BundleRecipe

for _, bundleRecipe := range bundle.BundleRecipes {
if !bundleRecipe.HasStatus(execution.RecipeStatusTypes.AVAILABLE) {
if !bundleRecipe.LastStatus(execution.RecipeStatusTypes.AVAILABLE) {
//Skip if not available
continue
}
Expand Down
10 changes: 10 additions & 0 deletions internal/install/bundle_installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func TestInstallContinueOnErrorReturnsImmediatelyWhenNoIsEntered(t *testing.T) {
test.mockStatusReporter.AssertNumberOfCalls(t, "ReportStatus", 1)
}

func TestInstallContinueOnErrorReturnsImmediatelyWhenSuperAgentIsInstalled(t *testing.T) {
test := createBundleInstallerTest().withPrompterYesNoVal(false).withRecipeInstallerError()
test.addRecipeToBundle("super-agent", execution.RecipeStatusTypes.INSTALLED)

test.BundleInstaller.InstallContinueOnError(test.bundle, false)

test.mockRecipeInstaller.AssertNumberOfCalls(t, "executeAndValidateWithProgress", 0)
test.mockStatusReporter.AssertNumberOfCalls(t, "ReportStatus", 1)
}

func TestInstallContinueOnErrorIgnoresUxPromptIfBundleIsAdditionalTargeted(t *testing.T) {
test := createBundleInstallerTest().withRecipeInstallerError()
test.addRecipeToBundle("recipe1", execution.RecipeStatusTypes.UNSUPPORTED)
Expand Down
1 change: 0 additions & 1 deletion internal/install/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func validateProfile(maxTimeoutSeconds int, sg *segment.Segment) *types.DetailEr
}

func checkNetwork() error {

if client.NRClient == nil {
return nil
}
Expand Down
53 changes: 44 additions & 9 deletions internal/install/recipe_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import (
"github.com/newrelic/newrelic-client-go/v2/newrelic"
)

const (
validationTimeout = 5 * time.Minute
superAgentProcessName = "newrelic-super-agent"
)
const validationTimeout = 5 * time.Minute

var infraAgentEntityKey string

Expand Down Expand Up @@ -210,10 +207,6 @@ Our Data Privacy Notice: https://newrelic.com/termsandconditions/services-notice
return err
}

// Check if super-agent process is already running on host
superAgentProcessOnHost := i.processEvaluator.FindProcess(superAgentProcessName)
log.Debugf("super agent running: %t\n", superAgentProcessOnHost)

hostname, _ := os.Hostname()
if hostname == "" {
message := "This system is not supported for automatic installation, no host info. Please see our documentation for requirements."
Expand Down Expand Up @@ -413,10 +406,35 @@ func (i *RecipeInstall) isTargetInstallRecipe(recipeName string) bool {
return false
}

// installAdditionalBundle installs additional bundles for the given recipes.
// It checks if the host has a super agent process running, and proceeds with the additional bundle.
// If the list of recipes is provided, it creates a targeted bundle; otherwise, it creates a guided bundle.
// If the host has super agent installed infra agent and logs agent would be NULL
// It then installs the additional bundle and reports any unsupported recipes.
func (i *RecipeInstall) installAdditionalBundle(bundler RecipeBundler, bundleInstaller RecipeBundleInstaller, repo *recipes.RecipeRepository) error {
var additionalBundle *recipes.Bundle
bun, ok := bundler.(*recipes.Bundler)

if i.hostHasSuperAgentProcess() && ok {
bun.HasSuperInstalled = true
log.Debugf("Super agent process found. Proceeding with additional bundle.")
} else {
log.Debugf("Super agent process not found. Proceeding with additional bundle.")
}

if i.RecipeNamesProvided() {
log.Debugf("bundling additional bundle")
log.Debugf("recipes in list %d", len(i.RecipeNames))
additionalBundle = bundler.CreateAdditionalTargetedBundle(i.RecipeNames)
if bun.HasSuperInstalled {
for _, coreRecipe := range bundler.(*recipes.Bundler).GetCoreRecipeNames() {
if i, ok := additionalBundle.ContainsName(coreRecipe); ok {
additionalBundle.BundleRecipes[i].AddDetectionStatus(execution.RecipeStatusTypes.NULL, 0)
}
}
log.Debugf("Additional Targeted bundle recipes in queue:%s", additionalBundle)
}

i.reportUnsupportedTargetedRecipes(additionalBundle, repo)
log.Debugf("Additional Targeted bundle recipes:%s", additionalBundle)
} else {
Expand All @@ -427,6 +445,11 @@ func (i *RecipeInstall) installAdditionalBundle(bundler RecipeBundler, bundleIns
bundleInstaller.InstallContinueOnError(additionalBundle, i.AssumeYes)

if bundleInstaller.InstalledRecipesCount() == 0 {
if bun.HasSuperInstalled {
return &types.UncaughtError{
Err: fmt.Errorf("super Agent is installed, preventing the installation of this recipe"),
}
}
return &types.UncaughtError{
Err: fmt.Errorf("no recipes were installed"),
}
Expand All @@ -440,7 +463,15 @@ func (i *RecipeInstall) installAdditionalBundle(bundler RecipeBundler, bundleIns
}

func (i *RecipeInstall) installCoreBundle(bundler RecipeBundler, bundleInstaller RecipeBundleInstaller) error {
if i.shouldInstallCore() {
if i.hostHasSuperAgentProcess() {
if bun, ok := bundler.(*recipes.Bundler); ok {
bun.HasSuperInstalled = true
log.Debugf("Super agent process found")
}
} else {
log.Debugf("Super agent process not found. Proceeding with installation.")
}
if i.shouldInstallCore() && !bundler.(*recipes.Bundler).HasSuperInstalled {
coreBundle := bundler.CreateCoreBundle()
log.Debugf("Core bundle recipes:%s", coreBundle)
err := bundleInstaller.InstallStopOnError(coreBundle, i.AssumeYes)
Expand Down Expand Up @@ -749,3 +780,7 @@ func (i *RecipeInstall) finishHandlingFailure(recipeName string) {
i.progressIndicator.Success("Complete!")
}
}

func (i *RecipeInstall) hostHasSuperAgentProcess() bool {
return i.processEvaluator.FindProcess(types.SuperAgentProcessName)
}
54 changes: 50 additions & 4 deletions internal/install/recipe_installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"strings"
"testing"

"github.com/newrelic/newrelic-cli/internal/config"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"github.com/newrelic/newrelic-cli/internal/config"
"github.com/newrelic/newrelic-cli/internal/install/execution"
"github.com/newrelic/newrelic-cli/internal/install/recipes"
"github.com/newrelic/newrelic-cli/internal/install/types"
Expand Down Expand Up @@ -90,6 +89,29 @@ func TestInstallWithInvalidDiscoveryResultReturnsError(t *testing.T) {
assert.True(t, strings.Contains(actual.Error(), expected.Error()))
}

func TestInstallGuidedShouldSkipCoreInstallWhileSuperAgentIsInstalled(t *testing.T) {
r := &recipes.RecipeDetectionResult{
Recipe: recipes.NewRecipeBuilder().Name(types.InfraAgentRecipeName).Build(),
Status: execution.RecipeStatusTypes.AVAILABLE,
}
statusReporter := execution.NewMockStatusReporter()
recipeInstall := NewRecipeInstallBuilder().WithRecipeDetectionResult(r).withShouldInstallCore(func() bool { return true }).WithStatusReporter(statusReporter).WithRunningProcess("super-agent-process", types.SuperAgentProcessName).Build()

err := recipeInstall.Install()

assert.Equal(t, "super Agent is installed, preventing the installation of this recipe", err.Error())
assert.Equal(t, 1, statusReporter.RecipeDetectedCallCount, "Detection Count")
assert.Equal(t, 0, statusReporter.RecipeAvailableCallCount, "Available Count")
assert.Equal(t, 0, statusReporter.RecipeInstallingCallCount, "Installing Count")
assert.Equal(t, 0, statusReporter.RecipeFailedCallCount, "Failed Count")
assert.Equal(t, 0, statusReporter.RecipeUnsupportedCallCount, "Unsupported Count")
assert.Equal(t, 0, statusReporter.RecipeInstalledCallCount, "InstalledCount")
assert.Equal(t, 0, statusReporter.RecipeRecommendedCallCount, "Recommendation Count")
assert.Equal(t, 0, statusReporter.RecipeSkippedCallCount, "Skipped Count")
assert.Equal(t, 0, statusReporter.RecipeCanceledCallCount, "Cancelled Count")
assert.Equal(t, 0, statusReporter.ReportInstalled[r.Recipe.Name], "Recipe Installed")
}

func TestInstallGuidedShouldSkipCoreInstall(t *testing.T) {
r := &recipes.RecipeDetectionResult{
Recipe: recipes.NewRecipeBuilder().Name(types.InfraAgentRecipeName).Build(),
Expand Down Expand Up @@ -313,6 +335,32 @@ func TestInstallTargetedInstallShouldInstallCoreIfCoreWasSkipped(t *testing.T) {
assert.Equal(t, 1, statusReporter.ReportInstalled[r.Recipe.Name], "Recipe1 Installed")
}

func TestInstallTargetedInstallShouldInstallCoreIfCoreWasSkippedWhileSuperAgentIsInstalled(t *testing.T) {
r := &recipes.RecipeDetectionResult{
Recipe: recipes.NewRecipeBuilder().Name(types.InfraAgentRecipeName).Build(),
Status: execution.RecipeStatusTypes.AVAILABLE,
}
statusReporter := execution.NewMockStatusReporter()
recipeInstall := NewRecipeInstallBuilder().WithRecipeDetectionResult(r).withShouldInstallCore(func() bool { return false }).
WithTargetRecipeName(types.InfraAgentRecipeName).WithStatusReporter(statusReporter).
WithRunningProcess("super-agent-process", types.SuperAgentProcessName).Build()
recipeInstall.AssumeYes = true

err := recipeInstall.Install()

assert.Equal(t, "super Agent is installed, preventing the installation of this recipe", err.Error())
assert.Equal(t, 1, statusReporter.RecipeDetectedCallCount, "Detection Count")
assert.Equal(t, 1, statusReporter.RecipeAvailableCallCount, "Available Count")
assert.Equal(t, 0, statusReporter.RecipeInstallingCallCount, "Installing Count")
assert.Equal(t, 0, statusReporter.RecipeFailedCallCount, "Failed Count")
assert.Equal(t, 0, statusReporter.RecipeUnsupportedCallCount, "Unsupported Count")
assert.Equal(t, 0, statusReporter.RecipeInstalledCallCount, "InstalledCount")
assert.Equal(t, 0, statusReporter.RecipeRecommendedCallCount, "Recommendation Count")
assert.Equal(t, 0, statusReporter.RecipeSkippedCallCount, "Skipped Count")
assert.Equal(t, 0, statusReporter.RecipeCanceledCallCount, "Cancelled Count")
assert.Equal(t, 0, statusReporter.ReportInstalled[r.Recipe.Name], "Recipe1 Installed")
}

func TestInstallTargetedInstallWithoutRecipeShouldNotInstall(t *testing.T) {
statusReporter := execution.NewMockStatusReporter()
recipeInstall := NewRecipeInstallBuilder().WithTargetRecipeName("Other").WithStatusReporter(statusReporter).Build()
Expand Down Expand Up @@ -751,7 +799,6 @@ func TestWhenSingleInstallRunningNoError(t *testing.T) {
recipeInstall := NewRecipeInstallBuilder().WithRunningProcess("env=123 newrelic install", "newrelic").Build()

err := recipeInstall.Install()

if err != nil {
assert.False(t, strings.Contains(err.Error(), "only 1 newrelic install command can run at one time"))
}
Expand All @@ -770,7 +817,6 @@ func TestWhenSingleInstallRunningNoErrorWindows(t *testing.T) {
recipeInstall := NewRecipeInstallBuilder().WithRunningProcess("env=123 C:\\path\\newrelic.exe install", "C:\\path\\newrelic.exe").Build()

err := recipeInstall.Install()

if err != nil {
assert.False(t, strings.Contains(err.Error(), "only 1 newrelic install command can run at one time"))
}
Expand Down
8 changes: 4 additions & 4 deletions internal/install/recipes/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var BundleTypes = struct {
}

func (b *Bundle) AddRecipe(bundleRecipe *BundleRecipe) {
if b.ContainsName(bundleRecipe.Recipe.Name) {
if _, ok := b.ContainsName(bundleRecipe.Recipe.Name); ok {
return
}
b.BundleRecipes = append(b.BundleRecipes, bundleRecipe)
Expand All @@ -45,15 +45,15 @@ func (b *Bundle) IsAdditionalTargeted() bool {
return b.Type == BundleTypes.ADDITIONALTARGETED
}

func (b *Bundle) ContainsName(name string) bool {
func (b *Bundle) ContainsName(name string) (int, bool) {

for i := range b.BundleRecipes {
if b.BundleRecipes[i].Recipe.Name == name {
return true
return i, true
}
}

return false
return -1, false
}

func (b *Bundle) GetBundleRecipe(name string) *BundleRecipe {
Expand Down
9 changes: 9 additions & 0 deletions internal/install/recipes/bundle_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (br *BundleRecipe) HasStatus(status execution.RecipeStatusType) bool {
return false
}

func (br *BundleRecipe) LastStatus(status execution.RecipeStatusType) bool {
if len(br.DetectedStatuses) > 0 {
return br.DetectedStatuses[len(br.DetectedStatuses)-1].Status == status
}
return false
}

func (br *BundleRecipe) AreAllDependenciesAvailable() bool {
for _, depName := range br.Recipe.Dependencies {
if br.IsNameInDependencies(depName) {
Expand All @@ -46,9 +53,11 @@ func (br *BundleRecipe) AreAllDependenciesAvailable() bool {
}
for _, ds := range br.Dependencies {
if !ds.HasStatus(execution.RecipeStatusTypes.AVAILABLE) {
log.Debugf("recipe %s is not available dependency for %s", br.Recipe.Name, ds.Recipe.Name)
return false
}
}
log.Debugf("all dependencies are available")
return true
}

Expand Down
14 changes: 10 additions & 4 deletions internal/install/recipes/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ func TestBundle_ShouldAddRecipes(t *testing.T) {
}

bundle.AddRecipe(newRecipe)
index, found := bundle.ContainsName(newRecipe.Recipe.Name)
require.Equal(t, len(bundle.BundleRecipes), 2)
require.True(t, true, bundle.ContainsName(newRecipe.Recipe.Name))
require.Equal(t, true, found)
require.Equal(t, 1, index)
}

func TestBundle_ShouldNotUpdateRecipe(t *testing.T) {
Expand All @@ -37,7 +39,9 @@ func TestBundle_ShouldNotUpdateRecipe(t *testing.T) {
bundle.AddRecipe(bundleRecipe)

require.Equal(t, len(bundle.BundleRecipes), 1)
require.Equal(t, true, bundle.ContainsName(bundleRecipe.Recipe.Name))
_, found := bundle.ContainsName(bundleRecipe.Recipe.Name)

require.Equal(t, true, found)
}

func TestBundle_ShouldContainRecipeName(t *testing.T) {
Expand All @@ -48,8 +52,9 @@ func TestBundle_ShouldContainRecipeName(t *testing.T) {
},
},
}
_, found := bundle.ContainsName("recipe1")

require.Equal(t, true, bundle.ContainsName("recipe1"))
require.Equal(t, true, found)
}

func TestBundle_ShouldNotContainRecipeName(t *testing.T) {
Expand All @@ -60,8 +65,9 @@ func TestBundle_ShouldNotContainRecipeName(t *testing.T) {
},
},
}
_, found := bundle.ContainsName("some other name")

require.Equal(t, false, bundle.ContainsName("some other name"))
require.Equal(t, false, found)
}

func TestBundle_ShouldBeGuided(t *testing.T) {
Expand Down
Loading

0 comments on commit 33768eb

Please sign in to comment.