Skip to content

Commit

Permalink
Add environment vars in terraform runner required when using docker c…
Browse files Browse the repository at this point in the history
…ompose v2 (#1204)

When using docker-compose v2 (v2.15.1) some commands were
missing the needed environment variables and therefore failing
the execution, this PR adds those required environment variables.
  • Loading branch information
mrodm authored Apr 5, 2023
1 parent 3264ed1 commit c4c75a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
12 changes: 6 additions & 6 deletions internal/testrunner/runners/system/servicedeployer/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type dockerComposeDeployedService struct {
ymlPaths []string
project string
variant ServiceVariant
env []string
}

// NewDockerComposeServiceDeployer returns a new instance of a DockerComposeServiceDeployer.
Expand All @@ -50,6 +51,7 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
ymlPaths: d.ymlPaths,
project: "elastic-package-service",
variant: d.variant,
env: []string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, inCtxt.Logs.Folder.Local)},
}
outCtxt := inCtxt

Expand Down Expand Up @@ -78,7 +80,7 @@ func (d *DockerComposeServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedSer
serviceName := inCtxt.Name
opts := compose.CommandOptions{
Env: append(
[]string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, outCtxt.Logs.Folder.Local)},
service.env,
d.variant.Env...),
ExtraArgs: []string{"--build", "-d"},
}
Expand Down Expand Up @@ -137,7 +139,7 @@ func (s *dockerComposeDeployedService) Signal(signal string) error {

opts := compose.CommandOptions{
Env: append(
[]string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, s.ctxt.Logs.Folder.Local)},
s.env,
s.variant.Env...),
ExtraArgs: []string{"-s", signal},
}
Expand Down Expand Up @@ -165,15 +167,13 @@ func (s *dockerComposeDeployedService) TearDown() error {

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

if err := p.Down(compose.CommandOptions{
Env: append(
[]string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, s.ctxt.Logs.Folder.Local)},
s.variant.Env...),
Env: opts.Env,
ExtraArgs: []string{"--volumes"}, // Remove associated volumes.
}); err != nil {
return errors.Wrap(err, "could not shut down service using Docker Compose")
Expand Down
17 changes: 9 additions & 8 deletions internal/testrunner/runners/system/servicedeployer/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ func (tsd TerraformServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedServic
ymlPaths = append(ymlPaths, envYmlPath)
}

tfEnvironment := tsd.buildTerraformExecutorEnvironment(inCtxt)

service := dockerComposeDeployedService{
ymlPaths: ymlPaths,
project: "elastic-package-service",
env: tfEnvironment,
}
outCtxt := inCtxt

Expand All @@ -82,12 +85,11 @@ func (tsd TerraformServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedServic
return nil, errors.Wrap(err, "removing service logs failed")
}

tfEnvironment := tsd.buildTerraformExecutorEnvironment(inCtxt)

opts := compose.CommandOptions{
Env: service.env,
}
// Set custom aliases, which may be used in agent policies.
serviceComposeConfig, err := p.Config(compose.CommandOptions{
Env: tfEnvironment,
})
serviceComposeConfig, err := p.Config(opts)
if err != nil {
return nil, errors.Wrap(err, "could not get Docker Compose configuration for service")
}
Expand All @@ -97,11 +99,10 @@ func (tsd TerraformServiceDeployer) SetUp(inCtxt ServiceContext) (DeployedServic
}

// Boot up service
opts := compose.CommandOptions{
Env: tfEnvironment,
opts = compose.CommandOptions{
Env: service.env,
ExtraArgs: []string{"--build", "-d"},
}

err = p.Up(opts)
if err != nil {
return nil, errors.Wrap(err, "could not boot up service using Docker Compose")
Expand Down

0 comments on commit c4c75a6

Please sign in to comment.