-
Notifications
You must be signed in to change notification settings - Fork 312
/
daemon.go
320 lines (280 loc) · 8.14 KB
/
daemon.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Package daemon implements the Encore daemon gRPC server.
package daemon
import (
"bytes"
"context"
"io"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cockroachdb/errors"
"github.com/golang/protobuf/ptypes/empty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"encr.dev/cli/daemon/apps"
"encr.dev/cli/daemon/run"
"encr.dev/cli/daemon/secret"
"encr.dev/cli/daemon/sqldb"
"encr.dev/cli/internal/platform"
"encr.dev/cli/internal/update"
"encr.dev/internal/clientgen"
"encr.dev/internal/version"
"encr.dev/pkg/builder"
"encr.dev/pkg/builder/builderimpl"
"encr.dev/pkg/errlist"
daemonpb "encr.dev/proto/encore/daemon"
meta "encr.dev/proto/encore/parser/meta/v1"
)
var _ daemonpb.DaemonServer = (*Server)(nil)
// Server implements daemonpb.DaemonServer.
type Server struct {
apps *apps.Manager
mgr *run.Manager
cm *sqldb.ClusterManager
sm *secret.Manager
mu sync.Mutex
streams map[string]*streamLog // run id -> stream
availableVerInit sync.Once
availableVer atomic.Value // string
appDebounceMu sync.Mutex
appDebouncers map[*apps.Instance]debouncer
daemonpb.UnimplementedDaemonServer
}
// New creates a new Server.
func New(appsMgr *apps.Manager, mgr *run.Manager, cm *sqldb.ClusterManager, sm *secret.Manager) *Server {
srv := &Server{
apps: appsMgr,
mgr: mgr,
cm: cm,
sm: sm,
streams: make(map[string]*streamLog),
appDebouncers: make(map[*apps.Instance]debouncer),
}
mgr.AddListener(srv)
// Check immediately for the latest version to avoid blocking 'encore run'
go srv.availableUpdate()
// Begin watching known apps for changes
go srv.watchApps()
return srv
}
// GenClient generates a client based on the app's API.
func (s *Server) GenClient(ctx context.Context, params *daemonpb.GenClientRequest) (*daemonpb.GenClientResponse, error) {
var md *meta.Data
if params.EnvName == "local" {
// Determine the app root
app, err := s.apps.FindLatestByPlatformID(params.AppId)
if errors.Is(err, apps.ErrNotFound) {
return nil, status.Errorf(codes.FailedPrecondition, "the app %s must be run locally before generating a client for the 'local' environment.",
params.AppId)
} else if err != nil {
return nil, status.Errorf(codes.Internal, "unable to query app info: %v", err)
}
// Get the app metadata
expSet, err := app.Experiments(nil)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to parse app experiments: %v", err)
}
// Parse the app to figure out what infrastructure is needed.
bld := builderimpl.Resolve(expSet)
parse, err := bld.Parse(ctx, builder.ParseParams{
Build: builder.DefaultBuildInfo(),
App: app,
Experiments: expSet,
WorkingDir: ".",
ParseTests: false,
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to parse app metadata: %v", err)
}
md = parse.Meta
} else {
envName := params.EnvName
if envName == "" {
envName = "@primary"
}
meta, err := platform.GetEnvMeta(ctx, params.AppId, envName)
if err != nil {
if strings.Contains(err.Error(), "env_not_found") || strings.Contains(err.Error(), "env_not_deployed") {
if envName == "@primary" {
return nil, status.Error(codes.NotFound, "You have no deployments of this application.\n\nYou can generate the client for your local code by setting `--env=local`.")
}
return nil, status.Errorf(codes.NotFound, "A deployed environment called `%s` not found.\n\nYou can generate the client for your local code by setting `--env=local`.", envName)
}
return nil, status.Errorf(codes.Unavailable, "could not fetch API metadata: %v", err)
}
md = meta
}
lang := clientgen.Lang(params.Lang)
serviceNames := params.Services
if slices.Contains(serviceNames, "*") {
serviceNames = nil
}
code, err := clientgen.Client(lang, params.AppId, md, serviceNames)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return &daemonpb.GenClientResponse{Code: code}, nil
}
func (s *Server) SecretsRefresh(ctx context.Context, req *daemonpb.SecretsRefreshRequest) (*daemonpb.SecretsRefreshResponse, error) {
app, err := s.apps.Track(req.AppRoot)
if err != nil {
return nil, err
}
s.sm.UpdateKey(app.PlatformID(), req.Key, req.Value)
return &daemonpb.SecretsRefreshResponse{}, nil
}
// Version reports the daemon version.
func (s *Server) Version(context.Context, *empty.Empty) (*daemonpb.VersionResponse, error) {
configHash, err := version.ConfigHash()
if err != nil {
return nil, err
}
return &daemonpb.VersionResponse{
Version: version.Version,
ConfigHash: configHash,
}, nil
}
// availableUpdate checks for updates to Encore.
// If there is a new version it returns it as a semver string.
func (s *Server) availableUpdate() *update.LatestVersion {
check := func() *update.LatestVersion {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ver, err := update.Check(ctx)
if err != nil {
log.Error().Err(err).Msg("could not check for new encore release")
}
return ver
}
s.availableVerInit.Do(func() {
ver := check()
s.availableVer.Store(ver)
go func() {
for {
time.Sleep(1 * time.Hour)
if ver := check(); ver != nil {
s.availableVer.Store(ver)
}
}
}()
})
curr := version.Version
latest := s.availableVer.Load().(*update.LatestVersion)
if latest.IsNewer(curr) {
return latest
}
return nil
}
var errDatabaseNotFound = (func() error {
st := status.New(codes.NotFound, "database not found")
return st.Err()
})()
var errNotLinked = (func() error {
st, err := status.New(codes.FailedPrecondition, "app not linked").WithDetails(
&errdetails.PreconditionFailure{
Violations: []*errdetails.PreconditionFailure_Violation{
{
Type: "NOT_LINKED",
Description: "app is not linked with Encore Cloud",
},
},
},
)
if err != nil {
panic(err)
}
return st.Err()
})()
type commandStream interface {
Send(msg *daemonpb.CommandMessage) error
}
func newStreamLogger(slog *streamLog) zerolog.Logger {
return zerolog.New(zerolog.SyncWriter(slog.Stderr(false))).With().Timestamp().Logger()
}
type streamWriter struct {
mu *sync.Mutex
sl *streamLog
stderr bool // if true write to stderr, otherwise stdout
buffer bool
}
func (w streamWriter) Write(b []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.buffer && w.sl.buffered {
if w.stderr {
return w.sl.writeBuffered(&w.sl.stderr, b)
} else {
return w.sl.writeBuffered(&w.sl.stdout, b)
}
}
return w.sl.writeStream(w.stderr, b)
}
func streamExit(stream commandStream, code int) {
stream.Send(&daemonpb.CommandMessage{Msg: &daemonpb.CommandMessage_Exit{
Exit: &daemonpb.CommandExit{
Code: int32(code),
},
}})
}
type streamLog struct {
stream commandStream
mu sync.Mutex
buffered bool
stdout *bytes.Buffer // lazily allocated
stderr *bytes.Buffer // lazily allocated
}
func (log *streamLog) Stdout(buffer bool) io.Writer {
return streamWriter{mu: &log.mu, sl: log, stderr: false, buffer: buffer}
}
func (log *streamLog) Stderr(buffer bool) io.Writer {
return streamWriter{mu: &log.mu, sl: log, stderr: true, buffer: buffer}
}
func (log *streamLog) Error(err *errlist.List) {
log.mu.Lock()
defer log.mu.Unlock()
err.SendToStream(log.stream)
}
func (log *streamLog) FlushBuffers() {
var stdout, stderr []byte
log.mu.Lock()
defer log.mu.Unlock()
if b := log.stdout; b != nil {
stdout = b.Bytes()
log.stdout = nil
}
if b := log.stderr; b != nil {
stderr = b.Bytes()
log.stderr = nil
}
log.writeStream(false, stderr)
log.writeStream(true, stdout)
log.buffered = false
}
func (log *streamLog) writeBuffered(b **bytes.Buffer, p []byte) (int, error) {
if *b == nil {
*b = &bytes.Buffer{}
}
return (*b).Write(p)
}
func (log *streamLog) writeStream(stderr bool, b []byte) (int, error) {
out := &daemonpb.CommandOutput{}
if stderr {
out.Stderr = b
} else {
out.Stdout = b
}
err := log.stream.Send(&daemonpb.CommandMessage{
Msg: &daemonpb.CommandMessage_Output{
Output: out,
},
})
if err != nil {
return 0, err
}
return len(b), nil
}