Skip to content

Commit ef28f0c

Browse files
committed
internal/osbuildexecutor/ec2: return logs on build failures
If osbuild crashes on the worker-executor side its build endpoint will return an error because it is unable to move the osbuild json result to the output directory and create a tarball. This causes the status scanner to error because it will receive invalid JSON. Because the full stack trace coming from osbuild should be present in the error that is handed back to the worker (so it can construct the job result with all the error details), the osbuild output needs to be fetched in case of build failures. The status scanner will put the line it cannot scan into the error, but it only reads up until the first newline. Thus osbuild's output cannot be forwarded via the status scanner.
1 parent e35ab89 commit ef28f0c

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

internal/osbuildexecutor/runner-impl-aws-ec2.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ func handleBuild(inputArchive, host string, logger logrus.FieldLogger, job worke
153153
return handleProgress(osbuildStatus, logger, job)
154154
}
155155

156+
func fetchLog(host string) (string, error) {
157+
client := http.Client{
158+
Timeout: time.Minute,
159+
}
160+
resp, err := client.Get(fmt.Sprintf("%s/api/v1/log", host))
161+
if err != nil {
162+
return "", err
163+
}
164+
defer resp.Body.Close()
165+
if resp.StatusCode != 200 {
166+
body, err := io.ReadAll(resp.Body)
167+
if err != nil {
168+
return "", fmt.Errorf("cannot fetch output archive: %w, http status: %d", err, resp.StatusCode)
169+
}
170+
return "", fmt.Errorf("cannot fetch output archive: %w, http status: %d, body: %s", err, resp.StatusCode, body)
171+
}
172+
173+
body, err := io.ReadAll(resp.Body)
174+
if err != nil {
175+
return "", fmt.Errorf("cannot read log response: %w, http status: %d", err, resp.StatusCode)
176+
}
177+
return string(body), nil
178+
}
179+
156180
func fetchOutputArchive(cacheDir, host string) (string, error) {
157181
client := http.Client{
158182
Timeout: time.Minute * 30,
@@ -282,8 +306,13 @@ func (ec2e *awsEC2Executor) RunOSBuild(manifest []byte, logger logrus.FieldLogge
282306
}
283307

284308
if err := handleBuild(inputArchive, executorHost, logger, job); err != nil {
285-
logrus.Errorf("something went wrong handling the executor's build: %v", err)
286-
return nil, err
309+
log, logErr := fetchLog(executorHost)
310+
if logErr != nil {
311+
logrus.Errorf("something went wrong during the executor's build: %v, unable to fetch log: %v", err, logErr)
312+
return nil, fmt.Errorf("something went wrong during the executor's build: %w, unable to fetch log: %w", err, logErr)
313+
}
314+
logrus.Errorf("something went wrong handling the executor's build: %v\nosbuild log: %v", err, log)
315+
return nil, fmt.Errorf("osbuild failed: %s", log)
287316
}
288317

289318
outputArchive, err := fetchOutputArchive(ec2e.tmpDir, executorHost)

0 commit comments

Comments
 (0)