Skip to content

Commit

Permalink
chore: extract Attach elastic-agent installer to a struct method
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Dec 17, 2021
1 parent 7021e8e commit d2ae43d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
14 changes: 7 additions & 7 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (fts *FleetTestSuite) afterScenario() {
if !fts.StandAlone {
// for the centos/debian flavour we need to retrieve the internal log files for the elastic-agent, as they are not
// exposed as container logs. For that reason we need to go through the installer abstraction
agentInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, fts.InstallerType)
agentInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, fts.InstallerType)

if log.IsLevelEnabled(log.DebugLevel) {
err := agentInstaller.Logs(fts.currentContext)
Expand Down Expand Up @@ -362,7 +362,7 @@ func (fts *FleetTestSuite) anStaleAgentIsDeployedToFleetWithInstaller(version, i

func (fts *FleetTestSuite) installCerts() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, fts.InstallerType)
agentInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, fts.InstallerType)

err := agentInstaller.InstallCerts(fts.currentContext)
if err != nil {
Expand Down Expand Up @@ -536,7 +536,7 @@ func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstallerAndFleetServer(i
return err
}

agentInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, fts.InstallerType)
agentInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, fts.InstallerType)
err = deployAgentToFleet(fts.currentContext, agentInstaller, fts.CurrentToken)
if err != nil {
return err
Expand All @@ -546,7 +546,7 @@ func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstallerAndFleetServer(i

func (fts *FleetTestSuite) processStateChangedOnTheHost(process string, state string) error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, fts.InstallerType)
agentInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, fts.InstallerType)
if state == "started" {
err := agentInstaller.Start(fts.currentContext)
return err
Expand Down Expand Up @@ -836,7 +836,7 @@ func theAgentIsListedInFleetWithStatus(ctx context.Context, desiredStatus string

func (fts *FleetTestSuite) theFileSystemAgentFolderIsEmpty() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, fts.InstallerType)
agentInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, fts.InstallerType)

pkgManifest, _ := agentInstaller.Inspect()
cmd := []string{
Expand Down Expand Up @@ -946,7 +946,7 @@ func (fts *FleetTestSuite) theAgentIsReenrolledOnTheHost() error {
log.Trace("Re-enrolling the agent on the host with same token")

agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, fts.InstallerType)
agentInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, fts.InstallerType)

err := agentInstaller.Enroll(fts.currentContext, fts.CurrentToken)
if err != nil {
Expand Down Expand Up @@ -1298,7 +1298,7 @@ func (fts *FleetTestSuite) anAttemptToEnrollANewAgentFails() error {
return err
}

agentInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, fts.InstallerType)
agentInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, fts.InstallerType)
err = deployAgentToFleet(fts.currentContext, agentInstaller, fts.CurrentToken)

if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion e2e/_suites/fleet/stand-alone.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (fts *FleetTestSuite) startStandAloneAgent(image string, flavour string, en
// a. downloaded from the GCP bucket
// b. fetched from the local beats binaries
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
dockerInstaller, _ := installer.Attach(fts.currentContext, fts.deployer, agentService, "docker")
dockerInstaller, _ := installer.NewElasticAgentDeployer(fts.deployer).AttachInstaller(fts.currentContext, agentService, "docker")
dockerInstaller.Preinstall(fts.currentContext)

arch := types.Architectures[types.GetArchitecture()]
Expand Down
18 changes: 16 additions & 2 deletions internal/installer/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@ import (
"go.elastic.co/apm"
)

// Attach will attach a installer to a deployment allowing
// ElasticAgentDeployer struct representing the deployer for an elastic-agent
type ElasticAgentDeployer struct {
deployer deploy.Deployer
}

// NewElasticAgentDeployer retrives a new instance of the elastic-agent deployer
func NewElasticAgentDeployer(deployer deploy.Deployer) ElasticAgentDeployer {
return ElasticAgentDeployer{
deployer: deployer,
}
}

// AttachInstaller will attach an Elastic Agent installer to a deployment allowing
// the installation of a package to be transparently configured no matter the backend
func Attach(ctx context.Context, deployer deploy.Deployer, service deploy.ServiceRequest, installType string) (deploy.ServiceOperator, error) {
func (ead ElasticAgentDeployer) AttachInstaller(ctx context.Context, service deploy.ServiceRequest, installType string) (deploy.ServiceOperator, error) {
span, _ := apm.StartSpanOptions(ctx, "Attaching installer to host", "elastic-agent.installer.attach", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
})
Expand All @@ -30,6 +42,8 @@ func Attach(ctx context.Context, deployer deploy.Deployer, service deploy.Servic
"installType": installType,
}).Trace("Attaching service for configuration")

deployer := ead.deployer

if strings.EqualFold(service.Name, "elastic-agent") {
switch installType {
case "tar":
Expand Down

0 comments on commit d2ae43d

Please sign in to comment.