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

Don't restart agent after enrolling if installed via deb or rpm #6494

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions internal/pkg/agent/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"strings"
"syscall"

"github.com/elastic/elastic-agent/internal/pkg/agent/install/pkgmgr"

"github.com/spf13/cobra"

"github.com/elastic/elastic-agent/internal/pkg/agent/application"
Expand Down Expand Up @@ -478,6 +480,12 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command) error {
fixPermissions = nil
}

// If agent was installed via a deb or rpm package, we want to avoid restarting because it may not be running yet.
// In this case, the user is expected to (re)start agent via systemd instead.
if pkgmgr.InstalledViaExternalPkgMgr() {
skipDaemonReload = true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this only for external package manager? Wouldn't it be good to make this change for all installation types?


options := enrollCmdOption{
EnrollAPIKey: enrollmentToken,
URL: url,
Expand Down
24 changes: 12 additions & 12 deletions pkg/testing/fixture_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,6 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, shou
}
})

// start elastic-agent
out, err = exec.CommandContext(ctx, "sudo", "systemctl", "start", "elastic-agent").CombinedOutput()
if err != nil {
return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err)
}

if !shouldEnroll {
return nil, nil
}
Expand Down Expand Up @@ -504,6 +498,12 @@ func (f *Fixture) installDeb(ctx context.Context, installOpts *InstallOpts, shou
return out, fmt.Errorf("elastic-agent enroll failed: %w, output: %s args: %v", err, string(out), enrollArgs)
}

// start elastic-agent
out, err = exec.CommandContext(ctx, "sudo", "systemctl", "start", "elastic-agent").CombinedOutput()
if err != nil {
return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err)
}

return nil, nil
}

Expand Down Expand Up @@ -556,12 +556,6 @@ func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, shou
}
})

// start elastic-agent
out, err = exec.CommandContext(ctx, "sudo", "systemctl", "start", "elastic-agent").CombinedOutput()
if err != nil {
return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err)
}

if !shouldEnroll {
return nil, nil
}
Expand Down Expand Up @@ -592,6 +586,12 @@ func (f *Fixture) installRpm(ctx context.Context, installOpts *InstallOpts, shou
return out, fmt.Errorf("elastic-agent enroll failed: %w, output: %s args: %v", err, string(out), enrollArgs)
}

// start elastic-agent
out, err = exec.CommandContext(ctx, "sudo", "systemctl", "start", "elastic-agent").CombinedOutput()
if err != nil {
return out, fmt.Errorf("systemctl start elastic-agent failed: %w", err)
}

return nil, nil
}

Expand Down
70 changes: 69 additions & 1 deletion testing/integration/linux_deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,75 @@ func TestDebLogIngestFleetManaged(t *testing.T) {
})
}

func TestDebEnroll(t *testing.T) {
info := define.Require(t, define.Requirements{
Group: Deb,
Stack: &define.Stack{},
OS: []define.OS{
{
Type: define.Linux,
Distro: "ubuntu",
},
},
Local: false,
Sudo: true,
})

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version(), atesting.WithPackageFormat("deb"))
require.NoError(t, err)

// 1. Create a policy in Fleet without any
// To ensure there are no conflicts with previous test runs against
// the same ESS stack, we add the current time at the end of the policy
// name. This policy does not contain any integration.
t.Log("Creating a test policy")
createPolicyReq := kibana.AgentPolicy{
Name: fmt.Sprintf("test-policy-enroll-%s", uuid.Must(uuid.NewV4()).String()),
Namespace: info.Namespace,
Description: "test policy for agent enrollment",
AgentFeatures: []map[string]interface{}{
{
"name": "test_enroll",
"enabled": true,
},
},
}

policy, err := info.KibanaClient.CreatePolicy(ctx, createPolicyReq)
require.NoError(t, err)

enrollmentToken, err := tools.CreateEnrollmentToken(t, ctx, info.KibanaClient, policy.ID)
require.NoError(t, err)

fleetServerURL, err := fleettools.DefaultURL(ctx, info.KibanaClient)
require.NoError(t, err)

installOpts := atesting.InstallOpts{
NonInteractive: true,
Force: true,
}

// 2. Install agent without enrolling
t.Log("Installing agent from deb package")
out, err := agentFixture.InstallWithoutEnroll(ctx, &installOpts)
require.NoError(t, err, "failed to install agent, got error: %s", string(out))

// 3. Enroll the agent, should succeed without the agent having been started
t.Log("Enrolling agent")
enrollArgs := []string{"elastic-agent", "enroll", "--url", fleetServerURL, "--enrollment-token", enrollmentToken.APIKey, "--force"}
out, err = exec.CommandContext(ctx, "sudo", enrollArgs...).CombinedOutput()
require.NoError(t, err, "failed to enroll agent, got error: %s", string(out))

// 4. Start the agent, it should connect to fleet correctly
t.Log("Starting agent")
out, err = exec.CommandContext(ctx, "sudo", "systemctl", "start", "elastic-agent").CombinedOutput()
require.NoError(t, err, "failed to start elastic-agent, got error: %s", string(out))
check.ConnectedToFleet(ctx, t, agentFixture, 5*time.Minute)
}

func TestDebFleetUpgrade(t *testing.T) {
info := define.Require(t, define.Requirements{
Group: Deb,
Expand Down Expand Up @@ -154,7 +223,6 @@ func TestDebFleetUpgrade(t *testing.T) {
NonInteractive: true,
Force: true,
}

// 2. Install the Elastic-Agent with the policy that
// was just created.
policy, err := tools.InstallAgentWithPolicy(
Expand Down
69 changes: 69 additions & 0 deletions testing/integration/linux_rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,75 @@ func TestRpmLogIngestFleetManaged(t *testing.T) {
})
}

func TestRpmEnroll(t *testing.T) {
info := define.Require(t, define.Requirements{
Group: RPM,
Stack: &define.Stack{},
OS: []define.OS{
{
Type: define.Linux,
Distro: "rhel",
},
},
Local: false,
Sudo: true,
})

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
defer cancel()

agentFixture, err := define.NewFixtureFromLocalBuild(t, define.Version(), atesting.WithPackageFormat("rpm"))
require.NoError(t, err)

// 1. Create a policy in Fleet without any
// To ensure there are no conflicts with previous test runs against
// the same ESS stack, we add the current time at the end of the policy
// name. This policy does not contain any integration.
t.Log("Creating a test policy")
createPolicyReq := kibana.AgentPolicy{
Name: fmt.Sprintf("test-policy-enroll-%s", uuid.Must(uuid.NewV4()).String()),
Namespace: info.Namespace,
Description: "test policy for agent enrollment",
AgentFeatures: []map[string]interface{}{
{
"name": "test_enroll",
"enabled": true,
},
},
}

policy, err := info.KibanaClient.CreatePolicy(ctx, createPolicyReq)
require.NoError(t, err)

enrollmentToken, err := tools.CreateEnrollmentToken(t, ctx, info.KibanaClient, policy.ID)
require.NoError(t, err)

fleetServerURL, err := fleettools.DefaultURL(ctx, info.KibanaClient)
require.NoError(t, err)

installOpts := atesting.InstallOpts{
NonInteractive: true,
Force: true,
}

// 2. Install agent without enrolling
t.Log("Installing agent from rpm package")
out, err := agentFixture.InstallWithoutEnroll(ctx, &installOpts)
require.NoError(t, err, "failed to install agent, got error: %s", string(out))

// 3. Enroll the agent, should succeed without the agent having been started
t.Log("Enrolling agent")
enrollArgs := []string{"elastic-agent", "enroll", "--url", fleetServerURL, "--enrollment-token", enrollmentToken.APIKey, "--force"}
out, err = exec.CommandContext(ctx, "sudo", enrollArgs...).CombinedOutput()
require.NoError(t, err, "failed to enroll agent, got error: %s", string(out))

// 4. Start the agent, it should connect to fleet correctly
t.Log("Starting agent")
out, err = exec.CommandContext(ctx, "sudo", "systemctl", "start", "elastic-agent").CombinedOutput()
require.NoError(t, err, "failed to start elastic-agent, got error: %s", string(out))
check.ConnectedToFleet(ctx, t, agentFixture, 5*time.Minute)
}

func TestRpmFleetUpgrade(t *testing.T) {
info := define.Require(t, define.Requirements{
Group: RPM,
Expand Down
Loading