Skip to content

Commit 70ad664

Browse files
committed
Fixed version-check when running in docker
1 parent 459efd1 commit 70ad664

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

service/runner_docker.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ type dockerRunner struct {
9090
}
9191

9292
type dockerContainer struct {
93+
log zerolog.Logger
9394
client *docker.Client
9495
container *docker.Container
95-
output io.Writer
96+
waiter docker.CloseWaiter
9697
}
9798

9899
func (r *dockerRunner) GetContainerDir(hostDir, defaultContainerDir string) string {
@@ -215,6 +216,8 @@ func (r *dockerRunner) start(image string, command string, args []string, volume
215216
Entrypoint: []string{command},
216217
Cmd: args,
217218
Tty: r.tty,
219+
AttachStdout: output != nil,
220+
AttachStderr: output != nil,
218221
User: r.user,
219222
ExposedPorts: make(map[docker.Port]struct{}),
220223
Labels: map[string]string{
@@ -259,7 +262,30 @@ func (r *dockerRunner) start(image string, command string, args []string, volume
259262
r.log.Error().Err(err).Interface("options", opts).Msg("Creating container failed")
260263
return nil, maskAny(err)
261264
}
262-
r.recordContainerID(c.ID) // Record ID so we can clean it up later
265+
//r.recordContainerID(c.ID) // Record ID so we can clean it up later
266+
267+
var waiter docker.CloseWaiter
268+
if output != nil {
269+
// Attach output to container
270+
r.log.Debug().Msgf("Attaching to output of container %s", containerName)
271+
success := make(chan struct{})
272+
defer close(success)
273+
waiter, err = r.client.AttachToContainerNonBlocking(docker.AttachToContainerOptions{
274+
Container: c.ID,
275+
OutputStream: output,
276+
Logs: true,
277+
Stdout: true,
278+
Stderr: true,
279+
Success: success,
280+
Stream: true,
281+
RawTerminal: true,
282+
})
283+
if err != nil {
284+
r.log.Error().Err(err).Msgf("Failed to attach to output of container %s", c.ID)
285+
return nil, maskAny(err)
286+
}
287+
<-success
288+
}
263289
r.log.Debug().Msgf("Starting container %s", containerName)
264290
if err := r.client.StartContainer(c.ID, opts.HostConfig); err != nil {
265291
return nil, maskAny(err)
@@ -276,9 +302,10 @@ func (r *dockerRunner) start(image string, command string, args []string, volume
276302
return nil, maskAny(err)
277303
}
278304
return &dockerContainer{
305+
log: r.log.With().Str("container", c.ID).Logger(),
279306
client: r.client,
280307
container: c,
281-
output: output,
308+
waiter: waiter,
282309
}, nil
283310
}
284311

@@ -486,16 +513,14 @@ func (p *dockerContainer) HostPort(containerPort int) (int, error) {
486513
}
487514

488515
func (p *dockerContainer) Wait() {
489-
p.client.WaitContainer(p.container.ID)
490-
if p.output != nil {
491-
// Fetch logs
492-
if err := p.client.Logs(docker.LogsOptions{
493-
Container: p.container.ID,
494-
OutputStream: p.output,
495-
Stdout: true,
496-
}); err != nil {
497-
p.output.Write([]byte(err.Error()))
498-
}
516+
if p.waiter != nil {
517+
p.waiter.Wait()
518+
}
519+
exitCode, err := p.client.WaitContainer(p.container.ID)
520+
if err != nil {
521+
p.log.Error().Err(err).Msg("WaitContainer failed")
522+
} else if exitCode != 0 {
523+
p.log.Debug().Int("exitcode", exitCode).Msg("Container terminated with non-zero exit code")
499524
}
500525
}
501526

service/version_check.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,25 @@ import (
2929
"strings"
3030

3131
driver "github.com/arangodb/go-driver"
32+
"github.com/dchest/uniuri"
3233
)
3334

3435
// DatabaseVersion returns the version of the `arangod` binary that is being
3536
// used by this starter.
3637
func (s *Service) DatabaseVersion(ctx context.Context) (driver.Version, error) {
3738
// Start process to print version info
3839
output := &bytes.Buffer{}
39-
p, err := s.runner.Start(ctx, ProcessTypeArangod, s.cfg.ArangodPath, []string{"--version"}, nil, nil, "", ".", output)
40+
containerName := "arangodb-versioncheck-" + strings.ToLower(uniuri.NewLen(6))
41+
p, err := s.runner.Start(ctx, ProcessTypeArangod, s.cfg.ArangodPath, []string{"--version"}, nil, nil, containerName, ".", output)
4042
if err != nil {
4143
return "", maskAny(err)
4244
}
4345
defer p.Cleanup()
4446
p.Wait()
4547

4648
// Parse output
47-
lines := strings.Split(output.String(), "\n")
49+
stdout := output.String()
50+
lines := strings.Split(stdout, "\n")
4851
for _, l := range lines {
4952
parts := strings.Split(l, ":")
5053
if len(parts) != 2 {
@@ -53,7 +56,9 @@ func (s *Service) DatabaseVersion(ctx context.Context) (driver.Version, error) {
5356
if strings.TrimSpace(parts[0]) != "server-version" {
5457
continue
5558
}
56-
return driver.Version(strings.TrimSpace(parts[1])), nil
59+
v := driver.Version(strings.TrimSpace(parts[1]))
60+
s.log.Debug().Msgf("Found server version '%s'", v)
61+
return v, nil
5762
}
58-
return "", fmt.Errorf("No server-version found")
63+
return "", fmt.Errorf("No server-version found in '%s'", stdout)
5964
}

0 commit comments

Comments
 (0)