From 9b39a2fd1f6cfee4431499b605b15665aa215e56 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Tue, 13 Feb 2024 02:21:51 -0500 Subject: [PATCH] added fula.sh.log fetch --- wap/cmd/main.go | 6 +++--- wap/pkg/wifi/properties.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/wap/cmd/main.go b/wap/cmd/main.go index 691c848..4f1bb71 100644 --- a/wap/cmd/main.go +++ b/wap/cmd/main.go @@ -187,14 +187,14 @@ func handleServerLifecycle(ctx context.Context, serverControl chan bool) { if server == nil { // Start the server server = mdns.StartServer(ctx, 8080) // Adjust port as necessary - log.Info("mDNS server started") + log.Debug("mDNS server started") } } else { if server != nil { // Stop the server server.Shutdown() server = nil - log.Info("mDNS server stopped") + log.Debug("mDNS server stopped") } } serverMutex.Unlock() @@ -328,7 +328,7 @@ func main() { for range ticker.C { // Toggle server state serverControl <- false // Stop the server - time.Sleep(1 * time.Second) // Wait a bit before restarting + time.Sleep(3 * time.Second) // Wait a bit before restarting serverControl <- true // Start the server } }() diff --git a/wap/pkg/wifi/properties.go b/wap/pkg/wifi/properties.go index 463b623..169b62a 100644 --- a/wap/pkg/wifi/properties.go +++ b/wap/pkg/wifi/properties.go @@ -1,6 +1,7 @@ package wifi import ( + "bytes" "context" "crypto/rand" "crypto/sha256" @@ -222,6 +223,15 @@ func GetContainerInfo(containerName string) (DockerInfo, error) { } func FetchContainerLogs(ctx context.Context, req FetchContainerLogsRequest) (string, error) { + switch req.ContainerName { + case "MainService": + return fetchLogsFromFile(ctx, req.ContainerName, req.TailCount) + default: + return fetchLogsFromDocker(ctx, req.ContainerName, req.TailCount) + } +} + +func fetchLogsFromDocker(ctx context.Context, containerName string, tailCount string) (string, error) { cli, err := client.NewClientWithOpts(client.WithAPIVersionNegotiation(), client.WithHost("unix:///var/run/docker.sock")) if err != nil { return "", fmt.Errorf("creating Docker client: %w", err) @@ -230,9 +240,9 @@ func FetchContainerLogs(ctx context.Context, req FetchContainerLogsRequest) (str options := types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, - Tail: req.TailCount, // Adjust the number of lines as needed + Tail: tailCount, // Adjust the number of lines as needed } - logs, err := cli.ContainerLogs(ctx, req.ContainerName, options) + logs, err := cli.ContainerLogs(ctx, containerName, options) if err != nil { return "", fmt.Errorf("getting container logs: %w", err) } @@ -245,3 +255,26 @@ func FetchContainerLogs(ctx context.Context, req FetchContainerLogsRequest) (str return string(logBytes), nil } + +func fetchLogsFromFile(ctx context.Context, name string, tailCount string) (string, error) { + fileName := "" + switch name { + case "MainService": + fileName = "/home/pi/fula.sh.log" + default: + fileName = "" + } + + if fileName != "" { + cmd := exec.Command("tail", "-n", tailCount, fileName) + var out, stderr bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &stderr // Capture stderr for logging errors. + err := cmd.Run() + if err != nil { + return "", fmt.Errorf("reading system logs: %w | stderr: %s", err, stderr.String()) + } + return out.String(), nil + } + return "", fmt.Errorf("name is undefined") +}