Skip to content

Commit f265574

Browse files
authored
Use "docker cp" to copy internal logs (#509)
* WIP * Adjust Jenkinsfile * Fix * Fix archive artifacts
1 parent 4b7a23d commit f265574

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

.ci/Jenkinsfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ pipeline {
9191
dir("${BASE_DIR}") {
9292
script {
9393
parallel([
94-
'stack-command': generateTestCommandStage(command: 'test-stack-command', artifacts: ['build/elastic-stack-dump/**/*.log']),
95-
'check-packages': generateTestCommandStage(command: 'test-check-packages', artifacts: ['build/test-results/*.xml', 'build/kubectl-dump.txt', 'build/elastic-stack-dump/**/*.log'], junitArtifacts: true, publishCoverage: true),
94+
'stack-command': generateTestCommandStage(command: 'test-stack-command', artifacts: ['build/elastic-stack-dump/stack/logs/*.log', 'build/elastic-stack-dump/stack/logs/fleet-server-internal/*']),
95+
'check-packages': generateTestCommandStage(command: 'test-check-packages', artifacts: ['build/test-results/*.xml', 'build/kubectl-dump.txt', 'build/elastic-stack-dump/check/logs/*.log', 'build/elastic-stack-dump/check/logs/fleet-server-internal/*'], junitArtifacts: true, publishCoverage: true),
9696
'profiles-command': generateTestCommandStage(command: 'test-profiles-command'),
9797
])
9898
}

internal/docker/docker.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func Pull(image string) error {
6060
cmd.Stderr = os.Stderr
6161
}
6262

63-
logger.Debugf("running command: %s", cmd)
63+
logger.Debugf("run command: %s", cmd)
6464
err := cmd.Run()
6565
if err != nil {
6666
return errors.Wrap(err, "running docker command failed")
@@ -112,7 +112,7 @@ func ConnectToNetwork(containerID, network string) error {
112112
errOutput := new(bytes.Buffer)
113113
cmd.Stderr = errOutput
114114

115-
logger.Debugf("output command: %s", cmd)
115+
logger.Debugf("run command: %s", cmd)
116116
if err := cmd.Run(); err != nil {
117117
return errors.Wrapf(err, "could not attach container to the stack network (stderr=%q)", errOutput.String())
118118
}
@@ -142,19 +142,15 @@ func InspectContainers(containerIDs ...string) ([]ContainerDescription, error) {
142142
return containerDescriptions, nil
143143
}
144144

145-
// Exec function executes command inside of the container
146-
func Exec(containerName string, args ...string) ([]byte, error) {
147-
cmdArgs := []string{"exec", "-t", containerName}
148-
cmdArgs = append(cmdArgs, args...)
149-
150-
cmd := exec.Command("docker", cmdArgs...)
145+
// Copy function copies resources from the container to the local destination.
146+
func Copy(containerName, containerPath, localPath string) error {
147+
cmd := exec.Command("docker", "cp", containerName+":"+containerPath, localPath)
151148
errOutput := new(bytes.Buffer)
152149
cmd.Stderr = errOutput
153150

154-
logger.Debugf("output command: %s", cmd)
155-
output, err := cmd.Output()
156-
if err != nil {
157-
return nil, errors.Wrapf(err, "exec failed (stderr=%q)", errOutput.String())
151+
logger.Debugf("run command: %s", cmd)
152+
if err := cmd.Run(); err != nil {
153+
return errors.Wrapf(err, "could not copy files from the container (stderr=%q)", errOutput.String())
158154
}
159-
return output, nil
155+
return nil
160156
}

internal/stack/dump.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import (
1515
"github.com/elastic/elastic-package/internal/profile"
1616
)
1717

18-
const fleetServerService = "fleet-server"
18+
const (
19+
elasticAgentService = "elastic-agent"
20+
fleetServerService = "fleet-server"
21+
)
1922

20-
var observedServices = []string{"elasticsearch", "elastic-agent", fleetServerService, "kibana", "package-registry"}
23+
var observedServices = []string{"elasticsearch", elasticAgentService, fleetServerService, "kibana", "package-registry"}
2124

2225
// DumpOptions defines dumping options for Elatic stack data.
2326
type DumpOptions struct {
@@ -46,7 +49,7 @@ func dumpStackLogs(options DumpOptions) error {
4649
logsPath := filepath.Join(options.Output, "logs")
4750
err = os.MkdirAll(logsPath, 0755)
4851
if err != nil {
49-
return errors.Wrap(err, "can't create output location")
52+
return errors.Wrapf(err, "can't create output location (path: %s)", logsPath)
5053
}
5154

5255
snapshotPath := options.Profile.FetchPath(profile.SnapshotFile)
@@ -61,11 +64,9 @@ func dumpStackLogs(options DumpOptions) error {
6164
writeLogFiles(logsPath, serviceName, content)
6265
}
6366

64-
content, ok, err := dockerInternalLogs(serviceName)
67+
err = copyDockerInternalLogs(serviceName, logsPath)
6568
if err != nil {
66-
logger.Errorf("can't fetch internal logs (service: %s): %v", serviceName, err)
67-
} else if ok {
68-
writeLogFiles(logsPath, serviceName+"-internal", content)
69+
logger.Errorf("can't copy internal logs (service: %s): %v", serviceName, err)
6970
}
7071
}
7172
return nil

internal/stack/logs.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package stack
66

77
import (
8+
"path/filepath"
9+
810
"github.com/pkg/errors"
911

1012
"github.com/elastic/elastic-package/internal/compose"
@@ -28,20 +30,23 @@ func dockerComposeLogs(serviceName string, snapshotFile string) ([]byte, error)
2830
return out, nil
2931
}
3032

31-
func dockerInternalLogs(serviceName string) ([]byte, bool, error) {
32-
if serviceName != fleetServerService {
33-
return nil, false, nil // we need to pull internal logs only from the Fleet Server container
33+
func copyDockerInternalLogs(serviceName, outputPath string) error {
34+
switch serviceName {
35+
case elasticAgentService, fleetServerService:
36+
default:
37+
return nil // we need to pull internal logs only from Elastic-Agent and Fleets Server container
3438
}
3539

3640
p, err := compose.NewProject(DockerComposeProjectName)
3741
if err != nil {
38-
return nil, false, errors.Wrap(err, "could not create docker compose project")
42+
return errors.Wrap(err, "could not create docker compose project")
3943
}
4044

45+
outputPath = filepath.Join(outputPath, serviceName+"-internal")
4146
serviceContainer := p.ContainerName(serviceName)
42-
out, err := docker.Exec(serviceContainer, "sh", "-c", `find data/logs/default/fleet-server-json* -printf '%p\n' -exec cat {} \;`)
47+
err = docker.Copy(serviceContainer, "/usr/share/elastic-agent/data/logs/default", outputPath)
4348
if err != nil {
44-
return nil, false, errors.Wrap(err, "docker exec failed")
49+
return errors.Wrap(err, "docker copy failed")
4550
}
46-
return out, true, nil
51+
return nil
4752
}

0 commit comments

Comments
 (0)