generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
deploy.go
256 lines (227 loc) · 7.02 KB
/
deploy.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
package buildengine
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"connectrpc.com/connect"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/common/moduleconfig"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/sha256"
"github.com/TBD54566975/ftl/internal/slices"
)
type deploymentArtefact struct {
*ftlv1.DeploymentArtefact
localPath string
}
type DeployClient interface {
GetArtefactDiffs(ctx context.Context, req *connect.Request[ftlv1.GetArtefactDiffsRequest]) (*connect.Response[ftlv1.GetArtefactDiffsResponse], error)
UploadArtefact(ctx context.Context, req *connect.Request[ftlv1.UploadArtefactRequest]) (*connect.Response[ftlv1.UploadArtefactResponse], error)
CreateDeployment(ctx context.Context, req *connect.Request[ftlv1.CreateDeploymentRequest]) (*connect.Response[ftlv1.CreateDeploymentResponse], error)
ReplaceDeploy(ctx context.Context, req *connect.Request[ftlv1.ReplaceDeployRequest]) (*connect.Response[ftlv1.ReplaceDeployResponse], error)
Status(ctx context.Context, req *connect.Request[ftlv1.StatusRequest]) (*connect.Response[ftlv1.StatusResponse], error)
}
// Deploy a module to the FTL controller with the given number of replicas. Optionally wait for the deployment to become ready.
func Deploy(ctx context.Context, module Module, replicas int32, waitForDeployOnline bool, client DeployClient) error {
logger := log.FromContext(ctx).Scope(module.Module)
ctx = log.ContextWithLogger(ctx, logger)
logger.Infof("Deploying module")
deployDir := module.AbsDeployDir()
files, err := findFiles(deployDir, module.Deploy)
if err != nil {
logger.Errorf(err, "failed to find files in %s", deployDir)
return err
}
filesByHash, err := hashFiles(deployDir, files)
if err != nil {
return err
}
gadResp, err := client.GetArtefactDiffs(ctx, connect.NewRequest(&ftlv1.GetArtefactDiffsRequest{ClientDigests: maps.Keys(filesByHash)}))
if err != nil {
return err
}
moduleSchema, err := loadProtoSchema(deployDir, module.ModuleConfig, replicas)
if err != nil {
return fmt.Errorf("failed to load protobuf schema from %q: %w", module.Schema, err)
}
logger.Debugf("Uploading %d/%d files", len(gadResp.Msg.MissingDigests), len(files))
for _, missing := range gadResp.Msg.MissingDigests {
file := filesByHash[missing]
content, err := os.ReadFile(file.localPath)
if err != nil {
return err
}
logger.Tracef("Uploading %s", relToCWD(file.localPath))
resp, err := client.UploadArtefact(ctx, connect.NewRequest(&ftlv1.UploadArtefactRequest{
Content: content,
}))
if err != nil {
return err
}
logger.Debugf("Uploaded %s as %s:%s", relToCWD(file.localPath), sha256.FromBytes(resp.Msg.Digest), file.Path)
}
resp, err := client.CreateDeployment(ctx, connect.NewRequest(&ftlv1.CreateDeploymentRequest{
Schema: moduleSchema,
Artefacts: slices.Map(maps.Values(filesByHash), func(a deploymentArtefact) *ftlv1.DeploymentArtefact {
return a.DeploymentArtefact
}),
}))
if err != nil {
return err
}
_, err = client.ReplaceDeploy(ctx, connect.NewRequest(&ftlv1.ReplaceDeployRequest{DeploymentKey: resp.Msg.GetDeploymentKey(), MinReplicas: replicas}))
if err != nil {
return err
}
if waitForDeployOnline {
logger.Debugf("Waiting for deployment %s to become ready", resp.Msg.DeploymentKey)
err = checkReadiness(ctx, client, resp.Msg.DeploymentKey, replicas)
if err != nil {
return err
}
}
return nil
}
func teminateModuleDeployment(ctx context.Context, client ftlv1connect.ControllerServiceClient, module string) error {
logger := log.FromContext(ctx).Scope(module)
status, err := client.Status(ctx, connect.NewRequest(&ftlv1.StatusRequest{}))
if err != nil {
return err
}
var key string
for _, deployment := range status.Msg.Deployments {
if deployment.Name == module {
key = deployment.Key
continue
}
}
if key == "" {
return fmt.Errorf("deployment for module %s not found: %v", module, status.Msg.Deployments)
}
logger.Infof("Terminating deployment %s", key)
_, err = client.UpdateDeploy(ctx, connect.NewRequest(&ftlv1.UpdateDeployRequest{DeploymentKey: key}))
return err
}
func loadProtoSchema(deployDir string, config moduleconfig.ModuleConfig, replicas int32) (*schemapb.Module, error) {
schema := filepath.Join(deployDir, config.Schema)
content, err := os.ReadFile(schema)
if err != nil {
return nil, err
}
module := &schemapb.Module{}
err = proto.Unmarshal(content, module)
if err != nil {
return nil, err
}
module.Runtime = &schemapb.ModuleRuntime{
CreateTime: timestamppb.Now(),
Language: config.Language,
MinReplicas: replicas,
}
return module, nil
}
func findFiles(base string, files []string) ([]string, error) {
var out []string
for _, file := range files {
file = filepath.Join(base, file)
info, err := os.Stat(file)
if err != nil {
return nil, err
}
if info.IsDir() {
dirFiles, err := findFilesInDir(file)
if err != nil {
return nil, err
}
out = append(out, dirFiles...)
} else {
out = append(out, file)
}
}
return out, nil
}
func findFilesInDir(dir string) ([]string, error) {
var out []string
return out, filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
out = append(out, path)
return nil
})
}
func hashFiles(base string, files []string) (filesByHash map[string]deploymentArtefact, err error) {
filesByHash = map[string]deploymentArtefact{}
for _, file := range files {
r, err := os.Open(file)
if err != nil {
return nil, err
}
defer r.Close() //nolint:gosec
hash, err := sha256.SumReader(r)
if err != nil {
return nil, err
}
info, err := r.Stat()
if err != nil {
return nil, err
}
isExecutable := info.Mode()&0111 != 0
path, err := filepath.Rel(base, file)
if err != nil {
return nil, err
}
filesByHash[hash.String()] = deploymentArtefact{
DeploymentArtefact: &ftlv1.DeploymentArtefact{
Digest: hash.String(),
Path: path,
Executable: isExecutable,
},
localPath: file,
}
}
return filesByHash, nil
}
func relToCWD(path string) string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
rel, err := filepath.Rel(cwd, path)
if err != nil {
return path
}
return rel
}
func checkReadiness(ctx context.Context, client DeployClient, deploymentKey string, replicas int32) error {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
status, err := client.Status(ctx, connect.NewRequest(&ftlv1.StatusRequest{}))
if err != nil {
return err
}
for _, deployment := range status.Msg.Deployments {
if deployment.Key == deploymentKey {
if deployment.Replicas >= replicas {
return nil
}
}
}
case <-ctx.Done():
return ctx.Err()
}
}
}