Skip to content

Commit d400e9d

Browse files
committed
Fix custom agent deployer
1 parent b5dbf6a commit d400e9d

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

internal/configuration/locations/locations.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,15 @@ const (
2626
fieldsCachedDir = "cache/fields"
2727

2828
terraformDeployerYmlFile = "terraform-deployer.yml"
29-
30-
dockerCustomAgentDeployerYmlFile = "docker-custom-agent-base.yml"
3129
)
3230

3331
var (
3432
// elasticPackageDataHome is the name of the environment variable used to override data folder for elastic-package
3533
elasticPackageDataHome = environment.WithElasticPackagePrefix("DATA_HOME")
3634

37-
serviceLogsDir = filepath.Join(temporaryDir, "service_logs")
38-
kubernetesDeployerDir = filepath.Join(deployerDir, "kubernetes")
39-
terraformDeployerDir = filepath.Join(deployerDir, "terraform")
40-
dockerCustomAgentDeployerDir = filepath.Join(deployerDir, "docker_custom_agent")
35+
serviceLogsDir = filepath.Join(temporaryDir, "service_logs")
36+
kubernetesDeployerDir = filepath.Join(deployerDir, "kubernetes")
37+
terraformDeployerDir = filepath.Join(deployerDir, "terraform")
4138
)
4239

4340
// LocationManager maintains an instance of a config path location
@@ -101,16 +98,6 @@ func (loc LocationManager) TerraformDeployerYml() string {
10198
return filepath.Join(loc.stackPath, terraformDeployerDir, terraformDeployerYmlFile)
10299
}
103100

104-
// DockerCustomAgentDeployerDir returns the DockerCustomAgent Directory
105-
func (loc LocationManager) DockerCustomAgentDeployerDir() string {
106-
return filepath.Join(loc.stackPath, dockerCustomAgentDeployerDir)
107-
}
108-
109-
// DockerCustomAgentDeployerYml returns the DockerCustomAgent deployer yml file
110-
func (loc LocationManager) DockerCustomAgentDeployerYml() string {
111-
return filepath.Join(loc.stackPath, dockerCustomAgentDeployerDir, dockerCustomAgentDeployerYmlFile)
112-
}
113-
114101
// ServiceLogDir returns the log directory
115102
func (loc LocationManager) ServiceLogDir() string {
116103
return filepath.Join(loc.stackPath, serviceLogsDir)

internal/testrunner/runners/system/servicedeployer/compose.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ import (
2424
// a Docker Compose file.
2525
type DockerComposeServiceDeployer struct {
2626
ymlPaths []string
27-
sv ServiceVariant
27+
variant ServiceVariant
2828
}
2929

3030
type dockerComposeDeployedService struct {
3131
ctxt ServiceContext
3232

3333
ymlPaths []string
3434
project string
35-
sv ServiceVariant
35+
variant ServiceVariant
3636
}
3737

3838
// NewDockerComposeServiceDeployer returns a new instance of a DockerComposeServiceDeployer.
3939
func NewDockerComposeServiceDeployer(ymlPaths []string, sv ServiceVariant) (*DockerComposeServiceDeployer, error) {
4040
return &DockerComposeServiceDeployer{
4141
ymlPaths: ymlPaths,
42-
sv: sv,
42+
variant: sv,
4343
}, nil
4444
}
4545

@@ -49,7 +49,7 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
4949
service := dockerComposeDeployedService{
5050
ymlPaths: d.ymlPaths,
5151
project: "elastic-package-service",
52-
sv: d.sv,
52+
variant: d.variant,
5353
}
5454
outCtxt := inCtxt
5555

@@ -71,15 +71,15 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
7171
}
7272

7373
// Boot up service
74-
if d.sv.active() {
75-
logger.Infof("Using service variant: %s", d.sv.String())
74+
if d.variant.active() {
75+
logger.Infof("Using service variant: %s", d.variant.String())
7676
}
7777

7878
serviceName := inCtxt.Name
7979
opts := compose.CommandOptions{
8080
Env: append(
8181
[]string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, outCtxt.Logs.Folder.Local)},
82-
d.sv.Env...),
82+
d.variant.Env...),
8383
ExtraArgs: []string{"--build", "-d"},
8484
}
8585
err = p.Up(opts)
@@ -161,14 +161,14 @@ func (s *dockerComposeDeployedService) TearDown() error {
161161
opts := compose.CommandOptions{
162162
Env: append(
163163
[]string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, s.ctxt.Logs.Folder.Local)},
164-
s.sv.Env...),
164+
s.variant.Env...),
165165
}
166166
processServiceContainerLogs(p, opts, s.ctxt.Name)
167167

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

internal/testrunner/runners/system/servicedeployer/custom_agent.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "embed"
99
"fmt"
1010
"os"
11+
"path/filepath"
1112

1213
"github.com/pkg/errors"
1314

@@ -21,18 +22,25 @@ import (
2122
"github.com/elastic/elastic-package/internal/stack"
2223
)
2324

24-
const dockerCustomAgentName = "docker-custom-agent"
25+
const (
26+
dockerCustomAgentName = "docker-custom-agent"
27+
dockerCustomAgentDir = "docker_custom_agent"
28+
dockerCustomAgentDockerfile = "docker-custom-agent-base.yml"
29+
)
30+
31+
//go:embed _static/docker-custom-agent-base.yml
32+
var dockerCustomAgentDockerfileContent []byte
2533

2634
// CustomAgentDeployer knows how to deploy a custom elastic-agent defined via
2735
// a Docker Compose file.
2836
type CustomAgentDeployer struct {
29-
cfg string
37+
dockerComposeFile string
3038
}
3139

3240
// NewCustomAgentDeployer returns a new instance of a deployedCustomAgent.
33-
func NewCustomAgentDeployer(cfgPath string) (*CustomAgentDeployer, error) {
41+
func NewCustomAgentDeployer(dockerComposeFile string) (*CustomAgentDeployer, error) {
3442
return &CustomAgentDeployer{
35-
cfg: cfgPath,
43+
dockerComposeFile: dockerComposeFile,
3644
}, nil
3745
}
3846

@@ -66,15 +74,20 @@ func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, err
6674
fmt.Sprintf("%s=%s", localCACertEnv, caCertPath),
6775
)
6876

69-
ymlPaths, err := d.loadComposeDefinitions()
77+
configDir, err := d.installDockerfile()
7078
if err != nil {
71-
return nil, err
79+
return nil, errors.Wrap(err, "could not create resources for custom agent")
80+
}
81+
82+
ymlPaths := []string{
83+
d.dockerComposeFile,
84+
filepath.Join(configDir, dockerCustomAgentDockerfile),
7285
}
7386

7487
service := dockerComposeDeployedService{
7588
ymlPaths: ymlPaths,
7689
project: "elastic-package-service",
77-
sv: ServiceVariant{
90+
variant: ServiceVariant{
7891
Name: dockerCustomAgentName,
7992
Env: env,
8093
},
@@ -149,10 +162,25 @@ func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, err
149162
return &service, nil
150163
}
151164

152-
func (d *CustomAgentDeployer) loadComposeDefinitions() ([]string, error) {
165+
// installDockerfile creates the files needed to run the custom elastic agent and returns
166+
// the directory with these files.
167+
func (d *CustomAgentDeployer) installDockerfile() (string, error) {
153168
locationManager, err := locations.NewLocationManager()
154169
if err != nil {
155-
return nil, errors.Wrap(err, "can't locate Docker Compose file for Custom Agent deployer")
170+
return "", errors.Wrap(err, "failed to find the configuration directory")
156171
}
157-
return []string{d.cfg, locationManager.DockerCustomAgentDeployerYml()}, nil
172+
173+
customAgentDir := filepath.Join(locationManager.DeployerDir(), dockerCustomAgentDir)
174+
err = os.MkdirAll(customAgentDir, 0755)
175+
if err != nil {
176+
return "", errors.Wrap(err, "failed to create directory for custom agent files")
177+
}
178+
179+
customAgentDockerfile := filepath.Join(customAgentDir, dockerCustomAgentDockerfile)
180+
err = os.WriteFile(customAgentDockerfile, dockerCustomAgentDockerfileContent, 0644)
181+
if err != nil {
182+
return "", errors.Wrap(err, "failed to create docker compose file for custom agent")
183+
}
184+
185+
return customAgentDir, nil
158186
}

0 commit comments

Comments
 (0)