-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
290 lines (259 loc) · 8.48 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
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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ssh
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"strings"
"syscall"
"github.com/google/uuid"
"golang.org/x/exp/maps"
itool "github.com/TiagoMalhadas/xcweaver/internal/tool"
"github.com/TiagoMalhadas/xcweaver/internal/tool/config"
"github.com/TiagoMalhadas/xcweaver/internal/tool/ssh/impl"
"github.com/TiagoMalhadas/xcweaver/runtime"
"github.com/TiagoMalhadas/xcweaver/runtime/bin"
"github.com/TiagoMalhadas/xcweaver/runtime/codegen"
"github.com/TiagoMalhadas/xcweaver/runtime/colors"
"github.com/TiagoMalhadas/xcweaver/runtime/logging"
"github.com/TiagoMalhadas/xcweaver/runtime/tool"
"github.com/TiagoMalhadas/xcweaver/runtime/version"
)
const (
configKey = "github.com/TiagoMalhadas/xcweaver/ssh"
shortConfigKey = "ssh"
)
var deployCmd = tool.Command{
Name: "deploy",
Description: "Deploy a Service Weaver app",
Help: "Usage:\n weaver ssh deploy <configfile>",
Flags: flag.NewFlagSet("deploy", flag.ContinueOnError),
Fn: deploy,
}
// deploy deploys an application on a cluster of machines using an SSH deployer.
// Note that each component is deployed as a separate OS process.
func deploy(ctx context.Context, args []string) error {
// Validate command line arguments.
if len(args) == 0 {
return fmt.Errorf("no config file provided")
}
if len(args) > 1 {
return fmt.Errorf("too many arguments")
}
// Load the config file.
cfgFile := args[0]
cfg, err := os.ReadFile(cfgFile)
if err != nil {
return fmt.Errorf("load config file %q: %w", cfgFile, err)
}
// Parse and sanity-check the app config.
app, err := runtime.ParseConfig(cfgFile, string(cfg), codegen.ComponentConfigValidator)
if err != nil {
return fmt.Errorf("load config file %q: %w", cfgFile, err)
}
if _, err := os.Stat(app.Binary); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("binary %q doesn't exist", app.Binary)
}
// Parse and finalize the SSH config.
config, err := config.GetDeployerConfig[impl.SshConfig, impl.SshConfig_ListenerOptions](configKey, shortConfigKey, app)
if err != nil {
return err
}
config.App = app
config.DepId = uuid.New().String()
// Check version compatibility.
versions, err := bin.ReadVersions(app.Binary)
if err != nil {
return fmt.Errorf("read versions: %w", err)
}
if versions.DeployerVersion != version.DeployerVersion {
// Try to relativize the binary, defaulting to the absolute path if
// there are any errors..
binary := app.Binary
if cwd, err := os.Getwd(); err == nil {
if rel, err := filepath.Rel(cwd, app.Binary); err == nil {
binary = rel
}
}
selfVersion, err := itool.SelfVersion()
if err != nil {
return fmt.Errorf("read self version: %w", err)
}
return fmt.Errorf(`
ERROR: The binary you're trying to deploy (%q) was built with
github.com/TiagoMalhadas/xcweaver module version %s. However, the 'weaver
ssh' binary you're using was built with weaver module version %s.
These versions are incompatible.
We recommend updating both the weaver module your application is built with and
updating the 'weaver ssh' command by running the following.
go get github.com/TiagoMalhadas/xcweaver@latest
go install github.com/TiagoMalhadas/xcweaver/cmd/weaver@latest
Then, re-build your code and re-run 'weaver ssh deploy'. If the problem
persists, please file an issue at https://github.com/TiagoMalhadas/xcweaver/issues.`,
binary, versions.ModuleVersion, selfVersion)
}
// Retrieve the list of locations to deploy.
locs, err := getLocations(config)
if err != nil {
return err
}
// Copy the binaries to each location.
locations, err := copyBinaries(locs, app.Binary, config.DepId)
if err != nil {
return err
}
// Run the manager.
stopFn, err := impl.RunManager(ctx, config, locations)
if err != nil {
return fmt.Errorf("cannot instantiate the manager: %w", err)
}
// Wait for the user to kill the app.
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-done // Will block here until user hits ctrl+c
if err := terminateDeployment(locs, config.DepId); err != nil {
fmt.Fprintf(os.Stderr, "failed to terminate deployment: %v\n", err)
}
fmt.Fprintf(os.Stderr, "Application %s terminated\n", app.Name)
if err := stopFn(); err != nil {
fmt.Fprintf(os.Stderr, "stop the manager: %v\n", err)
}
os.Exit(1)
}()
// Follow the logs.
source := logging.FileSource(impl.LogDir)
query := fmt.Sprintf(`full_version == %q && !("serviceweaver/system" in attrs)`, config.DepId)
r, err := source.Query(ctx, query, true)
if err != nil {
return err
}
pp := logging.NewPrettyPrinter(colors.Enabled())
for {
entry, err := r.Read(ctx)
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
fmt.Println(pp.Format(entry))
}
}
// copyBinaries copies the tool and the application binary
// to the given set of locations. It produces a map which
// returns the paths to the directories where the binaries
// were copied, keyed by locations.
func copyBinaries(locs []string, appBinary string, depId string) (map[string]string, error) {
ex, err := os.Executable()
if err != nil {
return nil, err
}
tmpDirs, err := getTmpDirs(locs, depId)
if err != nil {
return nil, err
}
for loc, tmpDir := range tmpDirs {
cmd := exec.Command("ssh", loc, "mkdir", "-p", tmpDir)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("unable to create deployment directory at location %s: %w\n", loc, err)
}
cmd = exec.Command("scp", ex, appBinary, loc+":"+tmpDir)
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("unable to copy app binary at location %s: %w\n", loc, err)
}
}
return tmpDirs, nil
}
// terminateDeployment terminates all the processes corresponding to the deployment
// at all locations.
//
// TODO(rgrandl): Find a different way to kill the deployment if the pkill command
// is not installed.
func terminateDeployment(locs []string, depId string) error {
for _, loc := range locs {
cmd := exec.Command("ssh", loc, "pkill", "-f", depId)
if err := cmd.Run(); err != nil {
return fmt.Errorf("unable to terminate deployment at location %s: %w", loc, err)
}
}
return nil
}
// getLocations returns the list of locations at which to deploy the application.
func getLocations(config *impl.SshConfig) ([]string, error) {
file, err := getAbsoluteFilePath(config.Locations)
if err != nil {
return nil, err
}
readFile, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("unable to open locations file: %w", err)
}
defer readFile.Close()
locations := map[string]bool{}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
loc := fileScanner.Text()
if _, ok := locations[loc]; ok {
return nil, fmt.Errorf("duplicate locations in the locations file")
}
locations[loc] = true
}
if len(locations) == 0 {
return nil, fmt.Errorf("no locations to deploy using the ssh deployer")
}
return maps.Keys(locations), nil
}
// getAbsoluteFilePath returns the absolute path for a file.
func getAbsoluteFilePath(file string) (string, error) {
if len(file) == 0 {
return "", fmt.Errorf("file not specified")
}
if file[0] == '~' {
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, file[1:]), nil
}
// Getting absolute path of the file.
abs, err := filepath.Abs(file)
if err != nil {
return "", fmt.Errorf("unable to find file %s: %w", file, err)
}
return abs, nil
}
// getTmpDirs returns the path to the tmp directories where
// the weaver binaries will be stored at each remote location.
func getTmpDirs(locs []string, depId string) (map[string]string, error) {
tmpDirs := make(map[string]string, len(locs))
for _, loc := range locs {
cmd := exec.Command("ssh", loc, "mktemp", "-u")
tmpDir, err := cmd.Output()
if err != nil {
return nil, err
}
tmpDirs[loc] = filepath.Join(strings.TrimSpace(string(tmpDir)), depId)
}
return tmpDirs, nil
}