Skip to content

Commit ac83511

Browse files
committed
switch to tc implementation, fix container name back
1 parent 0996679 commit ac83511

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

framework/components/clnode/clnode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ func newNode(in *Input, pgOut *postgres.Output) (*NodeOut, error) {
295295
ContainerName: containerName,
296296
InternalIP: ip,
297297
HostURL: fmt.Sprintf("http://%s:%s", host, mp.Port()),
298-
DockerURL: fmt.Sprintf("http://%s:%s", host, DefaultHTTPPort),
299-
DockerP2PUrl: fmt.Sprintf("http://%s:%s", host, DefaultP2PPort),
298+
DockerURL: fmt.Sprintf("http://%s:%s", containerName, DefaultHTTPPort),
299+
DockerP2PUrl: fmt.Sprintf("http://%s:%s", containerName, DefaultP2PPort),
300300
}, nil
301301
}
302302

framework/docker.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -230,65 +230,63 @@ func (dc *DockerClient) copyToContainer(containerID, sourceFile, targetPath stri
230230
return nil
231231
}
232232

233-
// WriteAllContainersLogs writes all docker containers logs to default logs directory
233+
// WriteAllContainersLogs writes all Docker container logs to the default logs directory
234234
func WriteAllContainersLogs() error {
235-
L.Info().Msg("Writing docker containers logs")
235+
L.Info().Msg("Writing Docker containers logs")
236236
if _, err := os.Stat(DefaultCTFLogsDir); os.IsNotExist(err) {
237237
if err := os.MkdirAll(DefaultCTFLogsDir, 0755); err != nil {
238238
return fmt.Errorf("failed to create directory %s: %w", DefaultCTFLogsDir, err)
239239
}
240240
}
241-
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
241+
provider, err := tc.NewDockerProvider()
242242
if err != nil {
243-
return fmt.Errorf("failed to create Docker client: %w", err)
243+
return fmt.Errorf("failed to create Docker provider: %w", err)
244244
}
245-
conts, err := cli.ContainerList(context.Background(), container.ListOptions{
246-
All: true,
247-
})
245+
containers, err := provider.Client().ContainerList(context.Background(), container.ListOptions{All: true})
248246
if err != nil {
249-
return err
247+
return fmt.Errorf("failed to list Docker containers: %w", err)
250248
}
251-
for _, cont := range conts {
249+
250+
for _, containerInfo := range containers {
251+
containerName := containerInfo.Names[0]
252252
logOptions := container.LogsOptions{ShowStdout: true, ShowStderr: true}
253-
logs, err := cli.ContainerLogs(context.Background(), cont.Names[0], logOptions)
253+
logs, err := provider.Client().ContainerLogs(context.Background(), containerInfo.ID, logOptions)
254254
if err != nil {
255-
L.Error().Err(err).Str("Container", cont.Names[0]).Msg("failed to fetch logs for container")
255+
L.Error().Err(err).Str("Container", containerName).Msg("failed to fetch logs for container")
256256
continue
257257
}
258-
logFilePath := filepath.Join(DefaultCTFLogsDir, fmt.Sprintf("%s.log", cont.Names[0]))
258+
logFilePath := filepath.Join(DefaultCTFLogsDir, fmt.Sprintf("%s.log", containerName))
259259
logFile, err := os.Create(logFilePath)
260260
if err != nil {
261-
L.Error().Err(err).Str("Container", cont.Names[0]).Msg("failed to create container log file")
261+
L.Error().Err(err).Str("Container", containerName).Msg("failed to create container log file")
262262
continue
263263
}
264-
265-
// Read and parse logs
264+
// Parse and write logs
266265
header := make([]byte, 8) // Docker stream header is 8 bytes
267266
for {
268-
// Read the header
269267
_, err := io.ReadFull(logs, header)
270268
if err == io.EOF {
271-
break // End of logs
269+
break
272270
}
273271
if err != nil {
274-
L.Error().Err(err).Str("Container", cont.Names[0]).Msg("failed to read log stream header")
272+
L.Error().Err(err).Str("Container", containerName).Msg("failed to read log stream header")
275273
break
276274
}
277275

278-
// Extract log message size from the header
276+
// Extract log message size
279277
msgSize := binary.BigEndian.Uint32(header[4:8])
280278

281279
// Read the log message
282280
msg := make([]byte, msgSize)
283281
_, err = io.ReadFull(logs, msg)
284282
if err != nil {
285-
L.Error().Err(err).Str("Container", cont.Names[0]).Msg("failed to read log message")
283+
L.Error().Err(err).Str("Container", containerName).Msg("failed to read log message")
286284
break
287285
}
288286

289287
// Write the log message to the file
290288
if _, err := logFile.Write(msg); err != nil {
291-
L.Error().Err(err).Str("Container", cont.Names[0]).Msg("failed to write log message to file")
289+
L.Error().Err(err).Str("Container", containerName).Msg("failed to write log message to file")
292290
break
293291
}
294292
}

0 commit comments

Comments
 (0)