-
Notifications
You must be signed in to change notification settings - Fork 79
/
handlers.go
116 lines (94 loc) · 2.58 KB
/
handlers.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package deploy
import (
"bytes"
"io"
"time"
context "golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/luizalabs/teresa/pkg/goutil"
dpb "github.com/luizalabs/teresa/pkg/protobuf/deploy"
"github.com/luizalabs/teresa/pkg/server/database"
)
const (
keepAliveMessage = "\u200B" // Zero width space
)
type Options struct {
KeepAliveTimeout time.Duration `split_words:"true" default:"30s"`
RevisionHistoryLimit int `split_words:"true" default:"5"`
SlugBuilderImage string `split_words:"true" default:"luizalabs/slugbuilder:v3.0.0"`
SlugRunnerImage string `split_words:"true" default:"luizalabs/slugrunner:v2.4.0"`
BuildLimitCPU string `split_words:"true" default:"800m"`
BuildLimitMemory string `split_words:"true" default:"1Gi"`
}
type Service struct {
ops Operations
options *Options
}
func (s *Service) Make(stream dpb.Deploy_MakeServer) error {
var appName, description string
content := new(bytes.Buffer)
ctx := stream.Context()
u := ctx.Value("user").(*database.User)
for {
in, err := stream.Recv()
if err != nil {
if err == io.EOF {
break
}
return err
}
if info := in.GetInfo(); info != nil {
appName = info.App
description = info.Description
}
if data := in.GetFile(); data != nil {
content.Write(data.Chunk)
}
}
rs := bytes.NewReader(content.Bytes())
rc, errChan := s.ops.Deploy(ctx, u, appName, rs, description, s.options)
if rc == nil {
return <-errChan
}
defer rc.Close()
deployMsgs := goutil.ChannelFromReader(rc, true)
var msg string
for {
select {
case <-time.After(s.options.KeepAliveTimeout):
msg = keepAliveMessage
case err := <-errChan:
return err
case m, ok := <-deployMsgs:
if !ok {
return nil
}
msg = m
}
if err := stream.Send(&dpb.DeployResponse{Text: msg}); err != nil {
return err
}
}
}
func (s *Service) List(ctx context.Context, req *dpb.ListRequest) (*dpb.ListResponse, error) {
user := ctx.Value("user").(*database.User)
items, err := s.ops.List(user, req.AppName)
if err != nil {
return nil, err
}
return newListResponse(items), nil
}
func (s *Service) Rollback(ctx context.Context, req *dpb.RollbackRequest) (*dpb.Empty, error) {
user := ctx.Value("user").(*database.User)
err := s.ops.Rollback(user, req.AppName, req.Revision)
if err != nil {
return nil, err
}
return &dpb.Empty{}, nil
}
func (s *Service) RegisterService(grpcServer *grpc.Server) {
dpb.RegisterDeployServer(grpcServer, s)
}
func NewService(ops Operations, options *Options) *Service {
return &Service{ops: ops, options: options}
}