Skip to content

Commit

Permalink
Move logic to custom_agent deployer
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-gr committed May 4, 2022
1 parent af8e172 commit 31aec19
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 17 deletions.
2 changes: 0 additions & 2 deletions internal/testrunner/runners/system/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
if config.Service != "" {
ctxt.Name = config.Service
}
ctxt.CustomAgent = config.CustomAgent
service, err := serviceDeployer.SetUp(ctxt)
if err != nil {
return result.WithError(errors.Wrap(err, "could not setup service"))
Expand Down Expand Up @@ -631,7 +630,6 @@ func filterAgents(allAgents []kibana.Agent, ctx servicedeployer.ServiceContext)
if ctx.Agent.Host.NamePrefix != "" && !strings.HasPrefix(agent.LocalMetadata.Host.Name, ctx.Agent.Host.NamePrefix) {
continue
}

filtered = append(filtered, agent)
}
return filtered
Expand Down
18 changes: 7 additions & 11 deletions internal/testrunner/runners/system/servicedeployer/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
return nil, errors.Wrap(err, "could not boot up service using Docker Compose")
}

// Connect service network with stack network (for the purpose of metrics collection)
err = docker.ConnectToNetwork(p.ContainerName(serviceName), stack.Network())
if err != nil {
return nil, errors.Wrapf(err, "can't attach service container to the stack network")
}

err = p.WaitForHealthy(opts)
if err != nil {
processServiceContainerLogs(p, compose.CommandOptions{
Expand All @@ -105,6 +99,12 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
// Build service container name
outCtxt.Hostname = p.ContainerName(serviceName)

// Connect service network with stack network (for the purpose of metrics collection)
err = docker.ConnectToNetwork(p.ContainerName(serviceName), stack.Network())
if err != nil {
return nil, errors.Wrapf(err, "can't attach service container to the stack network")
}

logger.Debugf("adding service container %s internal ports to context", p.ContainerName(serviceName))
serviceComposeConfig, err := p.Config(compose.CommandOptions{
Env: []string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, outCtxt.Logs.Folder.Local)},
Expand All @@ -124,11 +124,7 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
outCtxt.Port = outCtxt.Ports[0]
}

if inCtxt.CustomAgent {
outCtxt.Agent.Host.NamePrefix = serviceName
} else {
outCtxt.Agent.Host.NamePrefix = "docker-fleet-agent"
}
outCtxt.Agent.Host.NamePrefix = "docker-fleet-agent"
service.ctxt = outCtxt
return &service, nil
}
Expand Down
3 changes: 0 additions & 3 deletions internal/testrunner/runners/system/servicedeployer/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ type ServiceContext struct {
// Name is the name of the service.
Name string

// CustomAgent indicates that the service to deploy is a custom elastic-agent.
CustomAgent bool

// Hostname is the host name of the service, as addressable from
// the Agent container.
Hostname string
Expand Down
172 changes: 172 additions & 0 deletions internal/testrunner/runners/system/servicedeployer/custom_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package servicedeployer

import (
"fmt"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/compose"
"github.com/elastic/elastic-package/internal/docker"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/stack"
)

// CustomAgentDeployer knows how to deploy a custom elastic-agent defined via
// a Docker Compose file.
type CustomAgentDeployer struct {
ymlPaths []string
}

type deployedCustomAgent struct {
ctxt ServiceContext

ymlPaths []string
project string
}

// NewCustomAgentDeployer returns a new instance of a deployedCustomAgent.
func NewCustomAgentDeployer(ymlPaths []string) (*CustomAgentDeployer, error) {
return &CustomAgentDeployer{
ymlPaths: ymlPaths,
}, nil
}

// SetUp sets up the service and returns any relevant information.
func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, error) {
logger.Debug("setting up service using Docker Compose service deployer")
service := deployedCustomAgent{
ymlPaths: d.ymlPaths,
project: "elastic-package-service",
}
outCtxt := inCtxt

p, err := compose.NewProject(service.project, service.ymlPaths...)
if err != nil {
return nil, errors.Wrap(err, "could not create Docker Compose project for service")
}

// Verify the Elastic stack network
err = stack.EnsureStackNetworkUp()
if err != nil {
return nil, errors.Wrap(err, "Elastic stack network is not ready")
}

// Clean service logs
err = files.RemoveContent(outCtxt.Logs.Folder.Local)
if err != nil {
return nil, errors.Wrap(err, "removing service logs failed")
}

// Boot up service

serviceName := inCtxt.Name
opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, outCtxt.Logs.Folder.Local)},
ExtraArgs: []string{"--build", "-d"},
}
err = p.Up(opts)
if err != nil {
return nil, errors.Wrap(err, "could not boot up service using Docker Compose")
}

// Connect service network with stack network (for the purpose of metrics collection)
err = docker.ConnectToNetwork(p.ContainerName(serviceName), stack.Network())
if err != nil {
return nil, errors.Wrapf(err, "can't attach service container to the stack network")
}

err = p.WaitForHealthy(opts)
if err != nil {
processServiceContainerLogs(p, compose.CommandOptions{
Env: opts.Env,
}, outCtxt.Name)
return nil, errors.Wrap(err, "service is unhealthy")
}

// Build service container name
outCtxt.Hostname = p.ContainerName(serviceName)

logger.Debugf("adding service container %s internal ports to context", p.ContainerName(serviceName))
serviceComposeConfig, err := p.Config(compose.CommandOptions{
Env: []string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, outCtxt.Logs.Folder.Local)},
})
if err != nil {
return nil, errors.Wrap(err, "could not get Docker Compose configuration for service")
}

s := serviceComposeConfig.Services[serviceName]
outCtxt.Ports = make([]int, len(s.Ports))
for idx, port := range s.Ports {
outCtxt.Ports[idx] = port.InternalPort
}

// Shortcut to first port for convenience
if len(outCtxt.Ports) > 0 {
outCtxt.Port = outCtxt.Ports[0]
}

outCtxt.Agent.Host.NamePrefix = serviceName

service.ctxt = outCtxt
return &service, nil
}

// Signal sends a signal to the service.
func (s *deployedCustomAgent) Signal(signal string) error {
p, err := compose.NewProject(s.project, s.ymlPaths...)
if err != nil {
return errors.Wrap(err, "could not create Docker Compose project for service")
}

opts := compose.CommandOptions{ExtraArgs: []string{"-s", signal}}
if s.ctxt.Name != "" {
opts.Services = append(opts.Services, s.ctxt.Name)
}

return errors.Wrapf(p.Kill(opts), "could not send %q signal", signal)
}

// TearDown tears down the service.
func (s *deployedCustomAgent) TearDown() error {
logger.Debugf("tearing down service using Docker Compose runner")
defer func() {
err := files.RemoveContent(s.ctxt.Logs.Folder.Local)
if err != nil {
logger.Errorf("could not remove the service logs (path: %s)", s.ctxt.Logs.Folder.Local)
}
}()

p, err := compose.NewProject(s.project, s.ymlPaths...)
if err != nil {
return errors.Wrap(err, "could not create Docker Compose project for service")
}

opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, s.ctxt.Logs.Folder.Local)},
}
processServiceContainerLogs(p, opts, s.ctxt.Name)

if err := p.Down(compose.CommandOptions{
Env: []string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, s.ctxt.Logs.Folder.Local)},
ExtraArgs: []string{"--volumes"}, // Remove associated volumes.
}); err != nil {
return errors.Wrap(err, "could not shut down service using Docker Compose")
}
return nil
}

// Context returns the current context for the service.
func (s *deployedCustomAgent) Context() ServiceContext {
return s.ctxt
}

// SetContext sets the current context for the service.
func (s *deployedCustomAgent) SetContext(ctxt ServiceContext) error {
s.ctxt = ctxt
return nil
}
5 changes: 5 additions & 0 deletions internal/testrunner/runners/system/servicedeployer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) {
}
return NewDockerComposeServiceDeployer([]string{dockerComposeYMLPath}, sv)
}
case "agent":
dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml")
if _, err := os.Stat(dockerComposeYMLPath); err == nil {
return NewCustomAgentDeployer([]string{dockerComposeYMLPath})
}
case "tf":
if _, err := os.Stat(serviceDeployerPath); err == nil {
return NewTerraformServiceDeployer(serviceDeployerPath)
Expand Down
1 change: 0 additions & 1 deletion internal/testrunner/runners/system/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type testConfig struct {

Input string `config:"input"`
Service string `config:"service"`
CustomAgent bool `config:"custom_agent"`
ServiceNotifySignal string `config:"service_notify_signal"` // Signal to send when the agent policy is applied.
WaitForDataTimeout time.Duration `config:"wait_for_data_timeout"`

Expand Down

0 comments on commit 31aec19

Please sign in to comment.