Skip to content

Commit 9524caa

Browse files
authored
internal/servicedeployer: expose environment, and project name and construction (#2929)
1 parent 2d26428 commit 9524caa

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

internal/servicedeployer/compose.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ func (d *DockerComposeServiceDeployer) SetUp(ctx context.Context, svcInfo Servic
7676
logger.Debug("setting up service using Docker Compose service deployer")
7777
service := dockerComposeDeployedService{
7878
ymlPaths: d.ymlPaths,
79-
project: fmt.Sprintf("elastic-package-service-%s", svcInfo.Test.RunID),
79+
project: svcInfo.ProjectName(),
8080
variant: d.variant,
8181
env: []string{
8282
fmt.Sprintf("%s=%s", serviceLogsDirEnv, svcInfo.Logs.Folder.Local),
8383
fmt.Sprintf("%s=%s", testRunIDEnv, svcInfo.Test.RunID),
8484
},
8585
}
8686

87-
p, err := compose.NewProject(service.project, service.ymlPaths...)
87+
p, err := service.Project()
8888
if err != nil {
8989
return nil, fmt.Errorf("could not create Docker Compose project for service: %w", err)
9090
}
@@ -114,9 +114,7 @@ func (d *DockerComposeServiceDeployer) SetUp(ctx context.Context, svcInfo Servic
114114
}
115115

116116
opts := compose.CommandOptions{
117-
Env: append(
118-
service.env,
119-
d.variant.Env...),
117+
Env: service.Env(),
120118
ExtraArgs: []string{"--build", "-d"},
121119
}
122120

@@ -194,17 +192,29 @@ func (d *DockerComposeServiceDeployer) SetUp(ctx context.Context, svcInfo Servic
194192
return &service, nil
195193
}
196194

195+
// Project returns the project for the deployed service.
196+
func (s *dockerComposeDeployedService) Project() (*compose.Project, error) {
197+
p, err := compose.NewProject(s.project, s.ymlPaths...)
198+
if err != nil {
199+
return nil, fmt.Errorf("could not create Docker Compose project for service: %w", err)
200+
}
201+
return p, nil
202+
}
203+
204+
// Env returns a copy of the full env for the deployed service including any variant env.
205+
func (s *dockerComposeDeployedService) Env() []string {
206+
return append(s.env[:len(s.env):len(s.env)], s.variant.Env...)
207+
}
208+
197209
// Signal sends a signal to the service.
198210
func (s *dockerComposeDeployedService) Signal(ctx context.Context, signal string) error {
199-
p, err := compose.NewProject(s.project, s.ymlPaths...)
211+
p, err := s.Project()
200212
if err != nil {
201213
return fmt.Errorf("could not create Docker Compose project for service: %w", err)
202214
}
203215

204216
opts := compose.CommandOptions{
205-
Env: append(
206-
s.env,
207-
s.variant.Env...),
217+
Env: s.Env(),
208218
ExtraArgs: []string{"-s", signal},
209219
}
210220
if s.svcInfo.Name != "" {
@@ -220,15 +230,13 @@ func (s *dockerComposeDeployedService) Signal(ctx context.Context, signal string
220230

221231
// ExitCode returns true if the service is exited and its exit code.
222232
func (s *dockerComposeDeployedService) ExitCode(ctx context.Context, service string) (bool, int, error) {
223-
p, err := compose.NewProject(s.project, s.ymlPaths...)
233+
p, err := s.Project()
224234
if err != nil {
225235
return false, -1, fmt.Errorf("could not create Docker Compose project for service: %w", err)
226236
}
227237

228238
opts := compose.CommandOptions{
229-
Env: append(
230-
s.env,
231-
s.variant.Env...),
239+
Env: s.Env(),
232240
}
233241

234242
return p.ServiceExitCode(ctx, service, opts)
@@ -261,9 +269,7 @@ func (s *dockerComposeDeployedService) TearDown(ctx context.Context) error {
261269
}
262270

263271
opts := compose.CommandOptions{
264-
Env: append(
265-
s.env,
266-
s.variant.Env...),
272+
Env: s.Env(),
267273
}
268274

269275
extraArgs := []string{}

internal/servicedeployer/custom_agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (d *CustomAgentDeployer) SetUp(ctx context.Context, svcInfo ServiceInfo) (D
108108

109109
service := dockerComposeDeployedService{
110110
ymlPaths: ymlPaths,
111-
project: fmt.Sprintf("elastic-package-service-%s", svcInfo.Test.RunID),
111+
project: svcInfo.ProjectName(),
112112
variant: ServiceVariant{
113113
Name: dockerCustomAgentName,
114114
Env: env,

internal/servicedeployer/info.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package servicedeployer
66

7+
import "fmt"
8+
79
const (
810
localCACertEnv = "LOCAL_CA_CERT"
911
serviceLogsDirEnv = "SERVICE_LOGS_DIR"
@@ -95,3 +97,8 @@ func (sc *ServiceInfo) Aliases() map[string]interface{} {
9597
}
9698
return m
9799
}
100+
101+
// ProjectName returns the project name for the service.
102+
func (sc *ServiceInfo) ProjectName() string {
103+
return fmt.Sprintf("elastic-package-service-%s", sc.Test.RunID)
104+
}

internal/servicedeployer/terraform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn
113113

114114
service := dockerComposeDeployedService{
115115
ymlPaths: ymlPaths,
116-
project: fmt.Sprintf("elastic-package-service-%s", svcInfo.Test.RunID),
116+
project: svcInfo.ProjectName(),
117117
env: tfEnvironment,
118118
shutdownTimeout: 300 * time.Second,
119119
configDir: configDir,

0 commit comments

Comments
 (0)