-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
59 lines (47 loc) · 1.33 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
package main
import (
"bufio"
"context"
pb "github.com/opencopilot/agent/agent"
pbHealth "github.com/opencopilot/agent/health"
dockerTypes "github.com/docker/docker/api/types"
docker "github.com/docker/docker/client"
consul "github.com/hashicorp/consul/api"
)
type server struct {
dockerCli *docker.Client
consulCli *consul.Client
}
type health struct{}
func (s *server) ToAgent() *Agent {
return &Agent{
dockerCli: s.dockerCli,
consulCli: s.consulCli,
}
}
func (s *server) Check(ctx context.Context, in *pbHealth.HealthCheckRequest) (*pbHealth.HealthCheckResponse, error) {
return &pbHealth.HealthCheckResponse{
Status: pbHealth.HealthCheckResponse_SERVING,
}, nil
}
func (s *server) GetStatus(ctx context.Context, in *pb.AgentStatusRequest) (*pb.AgentStatus, error) {
agent := s.ToAgent()
return agent.AgentGetStatus(ctx)
}
func (s *server) GetServiceLogs(in *pb.GetServiceLogsRequest, stream pb.Agent_GetServiceLogsServer) error {
options := dockerTypes.ContainerLogsOptions{ShowStderr: true}
out, err := s.dockerCli.ContainerLogs(context.Background(), in.ContainerId, options)
if err != nil {
return err
}
defer out.Close()
scanner := bufio.NewScanner(out)
for scanner.Scan() {
t := scanner.Text()
l := &pb.ServiceLogLine{Line: t}
if err := stream.Send(l); err != nil {
return err
}
}
return nil
}