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

feat(super-agent): revert changes from feat(super-agent): Skip core bundle if installed #1612

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions internal/install/bundle_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package install
import (
"context"
"fmt"
"strings"

log "github.com/sirupsen/logrus"

Expand All @@ -27,6 +26,7 @@ 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,6 +42,7 @@ func NewPrompter() *ux.PromptUIPrompter {
}

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

bi.reportBundleStatus(bundle)

installableBundleRecipes := bi.getInstallableBundleRecipes(bundle)
Expand All @@ -51,6 +52,7 @@ 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 @@ -78,6 +80,7 @@ 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 @@ -127,9 +130,6 @@ 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.LastStatus(execution.RecipeStatusTypes.AVAILABLE) {
if !bundleRecipe.HasStatus(execution.RecipeStatusTypes.AVAILABLE) {
//Skip if not available
continue
}
Expand Down
10 changes: 0 additions & 10 deletions internal/install/bundle_installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ 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: 1 addition & 0 deletions internal/install/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func validateProfile(maxTimeoutSeconds int, sg *segment.Segment) *types.DetailEr
}

func checkNetwork() error {

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

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

var infraAgentEntityKey string

Expand Down Expand Up @@ -207,6 +210,10 @@ 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 @@ -406,35 +413,10 @@ 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 @@ -445,11 +427,6 @@ 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 @@ -463,15 +440,7 @@ func (i *RecipeInstall) installAdditionalBundle(bundler RecipeBundler, bundleIns
}

func (i *RecipeInstall) installCoreBundle(bundler RecipeBundler, bundleInstaller RecipeBundleInstaller) error {
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 {
if i.shouldInstallCore() {
coreBundle := bundler.CreateCoreBundle()
log.Debugf("Core bundle recipes:%s", coreBundle)
err := bundleInstaller.InstallStopOnError(coreBundle, i.AssumeYes)
Expand Down Expand Up @@ -780,7 +749,3 @@ func (i *RecipeInstall) finishHandlingFailure(recipeName string) {
i.progressIndicator.Success("Complete!")
}
}

func (i *RecipeInstall) hostHasSuperAgentProcess() bool {
return i.processEvaluator.FindProcess(types.SuperAgentProcessName)
}
54 changes: 4 additions & 50 deletions internal/install/recipe_installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ 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 @@ -89,29 +90,6 @@ 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 @@ -335,32 +313,6 @@ 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 @@ -799,6 +751,7 @@ 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 @@ -817,6 +770,7 @@ 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 _, ok := b.ContainsName(bundleRecipe.Recipe.Name); ok {
if b.ContainsName(bundleRecipe.Recipe.Name) {
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) (int, bool) {
func (b *Bundle) ContainsName(name string) bool {

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

return -1, false
return false
}

func (b *Bundle) GetBundleRecipe(name string) *BundleRecipe {
Expand Down
9 changes: 0 additions & 9 deletions internal/install/recipes/bundle_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ 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 @@ -53,11 +46,9 @@ 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: 4 additions & 10 deletions internal/install/recipes/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ func TestBundle_ShouldAddRecipes(t *testing.T) {
}

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

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

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

require.Equal(t, true, found)
require.Equal(t, true, bundle.ContainsName(bundleRecipe.Recipe.Name))
}

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

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

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

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

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