Skip to content

Commit

Permalink
Collect service container logs (#534)
Browse files Browse the repository at this point in the history
* Export service container logs to file

* Fixes
  • Loading branch information
mtojek authored Oct 7, 2021
1 parent c409b5b commit c25f38d
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions internal/testrunner/runners/system/servicedeployer/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ package servicedeployer

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/builder"
"github.com/elastic/elastic-package/internal/compose"
"github.com/elastic/elastic-package/internal/docker"
"github.com/elastic/elastic-package/internal/files"
Expand Down Expand Up @@ -151,15 +156,19 @@ func (s *dockerComposeDeployedService) TearDown() error {
return errors.Wrap(err, "could not create Docker Compose project for service")
}

downOptions := compose.CommandOptions{
containerLogs, err := p.Logs(compose.CommandOptions{
Env: append(
[]string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, s.ctxt.Logs.Folder.Local)},
s.sv.Env...),
})
s.processServiceContainerLogs(containerLogs, err)

// Remove associated volumes.
ExtraArgs: []string{"--volumes"},
}
if err := p.Down(downOptions); err != nil {
if err := p.Down(compose.CommandOptions{
Env: append(
[]string{fmt.Sprintf("%s=%s", serviceLogsDirEnv, s.ctxt.Logs.Folder.Local)},
s.sv.Env...),
ExtraArgs: []string{"--volumes"}, // Remove associated volumes.
}); err != nil {
return errors.Wrap(err, "could not shut down service using Docker Compose")
}
return nil
Expand All @@ -175,3 +184,41 @@ func (s *dockerComposeDeployedService) SetContext(ctxt ServiceContext) error {
s.ctxt = ctxt
return nil
}

func (s *dockerComposeDeployedService) processServiceContainerLogs(content []byte, err error) {
if err != nil {
logger.Errorf("can't export service container logs: %v", err)
return
}

if len(content) == 0 {
logger.Info("service container hasn't written anything logs.")
return
}

err = s.writeServiceContainerLogs(content)
if err != nil {
logger.Errorf("can't write service container logs: %v", err)
}
}

func (s *dockerComposeDeployedService) writeServiceContainerLogs(content []byte) error {
buildDir, err := builder.BuildDirectory()
if err != nil {
return errors.Wrap(err, "locating build directory failed")
}

containerLogsDir := filepath.Join(buildDir, "container-logs")
err = os.MkdirAll(containerLogsDir, 0755)
if err != nil {
return errors.Wrapf(err, "can't create directory for service container logs (path: %s)", containerLogsDir)
}

containerLogsFilepath := filepath.Join(containerLogsDir, fmt.Sprintf("%s-%d.log", s.ctxt.Name, time.Now().UnixNano()))
logger.Infof("Write container logs to file: %s", containerLogsFilepath)
err = ioutil.WriteFile(containerLogsFilepath, content, 0644)
if err != nil {
return errors.Wrapf(err, "can't write container logs to file (path: %s)", containerLogsFilepath)
}
return nil
}

0 comments on commit c25f38d

Please sign in to comment.