Skip to content

Commit bfa5ce8

Browse files
authored
Dump all service logs even if there is any error (#2642)
Allow to dump all service logs even if one of them fails. For instance, it could fail in the scenario that one of the containers was not created due to other failures.
1 parent bfebbd1 commit bfa5ce8

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

internal/stack/dump.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package stack
66

77
import (
88
"context"
9+
"errors"
910
"fmt"
1011
"os"
1112
"path/filepath"
@@ -75,7 +76,17 @@ func dumpStackLogs(ctx context.Context, options DumpOptions) ([]DumpResult, erro
7576
}
7677
}
7778

79+
var logsPath string
80+
if options.Output != "" {
81+
logsPath = filepath.Join(options.Output, "logs")
82+
err = os.MkdirAll(logsPath, 0755)
83+
if err != nil {
84+
return nil, fmt.Errorf("can't create output location (path: %s): %w", logsPath, err)
85+
}
86+
}
87+
7888
var results []DumpResult
89+
var containerErrors error
7990
for _, serviceName := range services {
8091
if len(options.Services) > 0 && !slices.Contains(options.Services, serviceName) {
8192
continue
@@ -85,7 +96,8 @@ func dumpStackLogs(ctx context.Context, options DumpOptions) ([]DumpResult, erro
8596

8697
content, err := dockerComposeLogsSince(ctx, serviceName, options.Profile, options.Since)
8798
if err != nil {
88-
return nil, fmt.Errorf("can't fetch service logs (service: %s): %v", serviceName, err)
99+
containerErrors = errors.Join(containerErrors, fmt.Errorf("can't fetch service logs (service: %s): %v", serviceName, err))
100+
continue
89101
}
90102
if options.Output == "" {
91103
results = append(results, DumpResult{
@@ -99,30 +111,30 @@ func dumpStackLogs(ctx context.Context, options DumpOptions) ([]DumpResult, erro
99111
ServiceName: serviceName,
100112
}
101113

102-
logsPath := filepath.Join(options.Output, "logs")
103-
err = os.MkdirAll(logsPath, 0755)
104-
if err != nil {
105-
return nil, fmt.Errorf("can't create output location (path: %s): %w", logsPath, err)
106-
}
107-
108114
logPath, err := writeLogFiles(logsPath, serviceName, content)
109115
if err != nil {
110-
return nil, fmt.Errorf("can't write log files for service %q: %w", serviceName, err)
116+
containerErrors = errors.Join(containerErrors, fmt.Errorf("can't write log files for service %q: %w", serviceName, err))
117+
continue
111118
}
112119
result.LogsFile = logPath
113120

114121
switch serviceName {
115122
case elasticAgentService, fleetServerService:
116123
logPath, err := copyDockerInternalLogs(serviceName, logsPath, options.Profile)
117124
if err != nil {
118-
return nil, fmt.Errorf("can't copy internal logs (service: %s): %w", serviceName, err)
125+
containerErrors = errors.Join(containerErrors, fmt.Errorf("can't copy internal logs for service %q: %w", serviceName, err))
126+
continue
119127
}
120128
result.InternalLogsDir = logPath
121129
}
122130

123131
results = append(results, result)
124132
}
125133

134+
if containerErrors != nil {
135+
return nil, fmt.Errorf("failed to dump stack logs: %w", containerErrors)
136+
}
137+
126138
return results, nil
127139
}
128140

scripts/test-build-install-zip-file.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ trap cleanup EXIT
3131

3232
installAndVerifyPackage() {
3333
local zipFile="$1"
34-
local PACKAGE_NAME_VERSION
34+
35+
local PACKAGE_NAME_VERSION=""
3536
PACKAGE_NAME_VERSION=$(basename "${zipFile}" .zip)
3637

38+
# Replace dash with a slash in the file name to match the API endpoint format
39+
# e.g. "apache-999.999.999" becomes "apache/999.999.999"
40+
PACKAGE_NAME_VERSION="${PACKAGE_NAME_VERSION//-/\/}"
41+
3742
elastic-package install -v --zip "${zipFile}"
3843

3944
# check that the package is installed

scripts/test-build-install-zip.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ cleanup() {
1919
exit $r
2020
}
2121

22+
trap cleanup EXIT
23+
2224
testype() {
2325
basename "$(dirname "$1")"
2426
}
2527

26-
trap cleanup EXIT
27-
2828
OLDPWD=$PWD
2929
# Build packages
3030
export ELASTIC_PACKAGE_SIGNER_PRIVATE_KEYFILE="$OLDPWD/scripts/gpg-private.asc"
@@ -57,9 +57,8 @@ for d in test/packages/*/*/; do
5757
if [ "$(testype $d)" == "false_positives" ]; then
5858
continue
5959
fi
60-
package_name=$(cat "${d}/manifest.yml" | yq -r .name)
61-
package_version=$(cat "${d}/manifest.yml" | yq -r .version)
62-
PACKAGE_NAME_VERSION="${package_name}-${package_version}"
60+
package_name=$(yq -r '.name' "${d}/manifest.yml")
61+
package_version=$(yq -r '.version' "${d}/manifest.yml")
6362

6463
elastic-package install -C "$d" -v
6564

@@ -69,5 +68,5 @@ for d in test/packages/*/*/; do
6968
--cacert "${ELASTIC_PACKAGE_CA_CERT}" \
7069
-H 'content-type: application/json' \
7170
-H 'kbn-xsrf: true' \
72-
-f "${ELASTIC_PACKAGE_KIBANA_HOST}/api/fleet/epm/packages/${PACKAGE_NAME_VERSION}" | grep -q '"status":"installed"'
71+
-f "${ELASTIC_PACKAGE_KIBANA_HOST}/api/fleet/epm/packages/${package_name}/${package_version}" | grep -q '"status":"installed"'
7372
done

0 commit comments

Comments
 (0)