Skip to content

Commit 72aa6e8

Browse files
authored
[Feature]: Support for execution logs files (#92)
* added support for execution log file * updated makefile * fixed linting * updated linting * updated linting * made function internal
1 parent 84c03c2 commit 72aa6e8

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ mould.log
8080
.env
8181

8282
.synapse.json
83+
logs

Makefile

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ SYNAPSE_IMAGE_NAME ?= lambdatest/synapse:latest
77
usage: ## Show this help
88
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/:.*##\s*/##/g' | awk -F'##' '{ printf "%-25s -> %s\n", $$1, $$2 }'
99

10-
build-nucleus-image: ## builds nucleus docker image
10+
lint: ## Runs linting
11+
golangci-lint run
12+
13+
build-nucleus-image: ## Builds nucleus docker image
1114
docker build -t ${NUCLEUS_IMAGE_NAME} --file $(NUCLEUS_DOCKER_FILE) .
1215

13-
build-nucleus-bin: ## builds nucleus binary
16+
build-nucleus-bin: ## Builds nucleus binary
1417
bash build/nucleus/build.sh
1518

16-
build-synapse-image: ## builds synapse docker image
19+
build-synapse-image: ## Builds synapse docker image
1720
docker build -t ${SYNAPSE_IMAGE_NAME} --file $(SYNAPSE_DOCKER_FILE) .
1821

19-
build-synapse-bin: ## builds synapse binary
22+
build-synapse-bin: ## Builds synapse binary
2023
bash build/synapse/build.sh

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ services:
1414
- "/tmp/synapse:/tmp/synapse"
1515
- ".synapse.json:/home/synapse/.synapse.json"
1616
- "/etc/machine-id:/etc/machine-id"
17+
- "./logs/synapse:/var/log/synapse"
1718

1819
networks:
1920
test-at-scale:

pkg/global/synapseconstants.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
AutoRemove = true
2222
Local = true
2323
MaxConnectionAttempts = 10
24+
ExecutionLogsPath = "/var/log/synapse"
2425
)
2526

2627
// SocketURL lambdatest url for synapse socket

pkg/runner/docker/docker.go

+40
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import (
1414
"github.com/LambdaTest/synapse/pkg/errs"
1515
"github.com/LambdaTest/synapse/pkg/global"
1616
"github.com/LambdaTest/synapse/pkg/lumber"
17+
"github.com/LambdaTest/synapse/pkg/synapse"
18+
"github.com/LambdaTest/synapse/pkg/utils"
1719
"github.com/docker/docker/api/types"
1820
"github.com/docker/docker/api/types/container"
1921
"github.com/docker/docker/client"
22+
"github.com/docker/docker/pkg/stdcopy"
2023
)
2124

2225
const (
@@ -149,6 +152,10 @@ func (d *docker) Run(ctx context.Context, r *core.RunnerOptions) core.ContainerS
149152
}
150153
d.RunningContainers = append(d.RunningContainers, r)
151154

155+
if err := d.writeLogs(ctx, r); err != nil {
156+
d.logger.Errorf("error writing logs to stdout: %+v", err)
157+
}
158+
152159
return containerStatus
153160
}
154161

@@ -271,3 +278,36 @@ func (d *docker) PullImage(containerImageConfig *core.ContainerImageConfig, r *c
271278
}
272279
return nil
273280
}
281+
282+
// writeLogs writes container logs to a file
283+
func (d *docker) writeLogs(ctx context.Context, r *core.RunnerOptions) error {
284+
reader, err := d.client.ContainerLogs(ctx,
285+
r.ContainerID,
286+
types.ContainerLogsOptions{
287+
ShowStdout: true,
288+
ShowStderr: true,
289+
Follow: true,
290+
})
291+
if err != nil {
292+
return err
293+
}
294+
defer reader.Close()
295+
296+
buildLogsPath := fmt.Sprintf("%s/%s", global.ExecutionLogsPath, r.Label[synapse.BuildID])
297+
298+
if errDir := utils.CreateDirectory(buildLogsPath); err != nil {
299+
return errDir
300+
}
301+
302+
f, err := os.Create(fmt.Sprintf("%s/%s-%s.log", buildLogsPath, r.ContainerName, r.PodType))
303+
if err != nil {
304+
return err
305+
}
306+
defer f.Close()
307+
308+
if _, errCopy := stdcopy.StdCopy(f, f, reader); err != nil {
309+
return errCopy
310+
}
311+
312+
return nil
313+
}

0 commit comments

Comments
 (0)