in internal/servicedeployer/compose.go [75:194]
func (d *DockerComposeServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceInfo) (DeployedService, error) {
logger.Debug("setting up service using Docker Compose service deployer")
service := dockerComposeDeployedService{
ymlPaths: d.ymlPaths,
project: fmt.Sprintf("elastic-package-service-%s", svcInfo.Test.RunID),
variant: d.variant,
env: []string{
fmt.Sprintf("%s=%s", serviceLogsDirEnv, svcInfo.Logs.Folder.Local),
},
}
p, err := compose.NewProject(service.project, service.ymlPaths...)
if err != nil {
return nil, fmt.Errorf("could not create Docker Compose project for service: %w", err)
}
// Verify the Elastic stack network
err = stack.EnsureStackNetworkUp(d.profile)
if err != nil {
return nil, fmt.Errorf("stack network is not ready: %w", err)
}
// Clean service logs
if d.runTestsOnly {
// service logs folder must no be deleted to avoid breaking log files written
// by the service. If this is required, those files should be rotated or truncated
// so the service can still write to them.
logger.Debugf("Skipping removing service logs folder folder %s", svcInfo.Logs.Folder.Local)
} else {
err = files.RemoveContent(svcInfo.Logs.Folder.Local)
if err != nil {
return nil, fmt.Errorf("removing service logs failed: %w", err)
}
}
// Boot up service
if d.variant.active() {
logger.Infof("Using service variant: %s", d.variant.String())
}
opts := compose.CommandOptions{
Env: append(
service.env,
d.variant.Env...),
ExtraArgs: []string{"--build", "-d"},
}
serviceName := svcInfo.Name
if d.runTearDown || d.runTestsOnly {
logger.Debug("Skipping bringing up docker-compose custom agent project")
} else {
err = p.Up(ctx, opts)
if err != nil {
return nil, fmt.Errorf("could not boot up service using Docker Compose: %w", err)
}
}
err = p.WaitForHealthy(ctx, opts)
if err != nil {
processServiceContainerLogs(context.WithoutCancel(ctx), p, compose.CommandOptions{
Env: opts.Env,
}, svcInfo.Name)
return nil, fmt.Errorf("service is unhealthy: %w", err)
}
// Added a specific alias when connecting the service to the network.
// - There could be container names too long that could not be resolved by the local DNS
// - Not used serviceName directly as alias container, since there could be packages defining
// kibana or elasticsearch services and those DNS names are already present in the Elastic stack.
// This is mainly applicable when the Elastic Agent of the stack is used for testing.
// - Keep the same alias for both implementations for consistency
aliasContainer := fmt.Sprintf("svc-%s", serviceName)
if d.runTearDown || d.runTestsOnly {
logger.Debug("Skipping connect container to network (non setup steps)")
} else {
aliases := []string{
aliasContainer,
}
if d.deployIndependentAgent {
// Connect service network with agent network
err = docker.ConnectToNetworkWithAlias(p.ContainerName(serviceName), svcInfo.AgentNetworkName, aliases)
if err != nil {
return nil, fmt.Errorf("can't attach service container to the agent network: %w", err)
}
} else {
// Connect service network with stack network (for the purpose of metrics collection)
err = docker.ConnectToNetworkWithAlias(p.ContainerName(serviceName), stack.Network(d.profile), aliases)
if err != nil {
return nil, fmt.Errorf("can't attach service container to the stack network: %w", err)
}
}
}
// Build service container name
svcInfo.Hostname = aliasContainer
logger.Debugf("adding service container %s internal ports to context", p.ContainerName(serviceName))
serviceComposeConfig, err := p.Config(ctx, compose.CommandOptions{
Env: service.env,
})
if err != nil {
return nil, fmt.Errorf("could not get Docker Compose configuration for service: %w", err)
}
s := serviceComposeConfig.Services[serviceName]
svcInfo.Ports = make([]int, len(s.Ports))
for idx, port := range s.Ports {
svcInfo.Ports[idx] = port.InternalPort
}
// Shortcut to first port for convenience
if len(svcInfo.Ports) > 0 {
svcInfo.Port = svcInfo.Ports[0]
}
svcInfo.Agent.Host.NamePrefix = "docker-fleet-agent"
service.svcInfo = svcInfo
return &service, nil
}