-
Notifications
You must be signed in to change notification settings - Fork 9
/
logsapi.go
49 lines (41 loc) · 1.52 KB
/
logsapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package devsession
import (
"context"
"fmt"
"io"
"net/http"
"github.com/gorilla/websocket"
"google.golang.org/grpc/codes"
"namespacelabs.dev/foundation/framework/rpcerrors"
"namespacelabs.dev/foundation/internal/runtime"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
)
func serveLogs(s sessionLike, w http.ResponseWriter, r *http.Request, serverID string) {
serveStream("server.logs", w, r, func(ctx context.Context, conn *websocket.Conn, wsWriter io.Writer) error {
// XXX rather than obtaining the current one, it should be encoded in the request to logs.
cluster, server, err := s.ResolveServer(ctx, serverID)
if err != nil {
return err
}
refs, err := cluster.ResolveContainers(ctx, server)
if err != nil {
return err
}
for _, ref := range refs {
if ref.Kind == runtimepb.ContainerKind_PRIMARY {
return cluster.Cluster().FetchLogsTo(ctx, ref,
runtime.FetchLogsOpts{Follow: true, TailLines: 100},
runtime.WriteToWriter(wsWriter))
}
}
return rpcerrors.Errorf(codes.FailedPrecondition, "server has no identifiable primary container")
})
}
func serveTaskOutput(s *Session, w http.ResponseWriter, r *http.Request, taskID, name string) {
copyStream(fmt.Sprintf("task.output[%s]", name), w, r, func(ctx context.Context) (io.ReadCloser, error) {
return s.TaskLogByName(taskID, name), nil
})
}