/
server.go
87 lines (66 loc) · 1.99 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package builder
import (
"context"
"fmt"
"io"
api "github.com/dodo-cli/dodo-core/api/v1alpha3"
"github.com/dodo-cli/dodo-core/pkg/plugin"
"github.com/golang/protobuf/ptypes/empty"
"golang.org/x/sync/errgroup"
)
type server struct {
impl ImageBuilder
stdio *plugin.StdioServer
}
func (s *server) GetPluginInfo(_ context.Context, _ *empty.Empty) (*api.PluginInfo, error) {
return s.impl.PluginInfo(), nil
}
func (s *server) InitPlugin(_ context.Context, _ *empty.Empty) (*api.InitPluginResponse, error) {
config, err := s.impl.Init()
if err != nil {
return nil, fmt.Errorf("could not initialize plugin: %w", err)
}
return &api.InitPluginResponse{Config: config}, nil
}
func (s *server) GetStreamingConnection(_ context.Context, _ *api.GetStreamingConnectionRequest) (*api.GetStreamingConnectionResponse, error) {
stdio, err := plugin.NewStdioServer()
if err != nil {
return nil, fmt.Errorf("could not get stdio server: %w", err)
}
s.stdio = stdio
return &api.GetStreamingConnectionResponse{Url: stdio.Endpoint()}, nil
}
func (s *server) CreateImage(_ context.Context, request *api.CreateImageRequest) (*api.CreateImageResponse, error) {
resp := &api.CreateImageResponse{}
inReader, inWriter := io.Pipe()
outReader, outWriter := io.Pipe()
errReader, errWriter := io.Pipe()
eg, _ := errgroup.WithContext(context.Background())
eg.Go(func() error {
return s.stdio.Copy(inWriter, outReader, errReader)
})
eg.Go(func() error {
defer func() {
inWriter.Close()
outWriter.Close()
errWriter.Close()
}()
imageID, err := s.impl.CreateImage(request.Config, &plugin.StreamConfig{
Stdin: inReader,
Stdout: outWriter,
Stderr: errWriter,
TerminalHeight: request.Height,
TerminalWidth: request.Width,
})
if err != nil {
return fmt.Errorf("could not build image: %w", err)
}
resp.ImageId = imageID
return nil
})
err := eg.Wait()
if err != nil {
return nil, fmt.Errorf("error during image build: %w", err)
}
return resp, nil
}