-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathdevserver.go
402 lines (373 loc) · 12.2 KB
/
devserver.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// The MIT License
//
// Copyright (c) 2022 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package testsuite
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/internal"
ilog "go.temporal.io/sdk/internal/log"
"go.temporal.io/sdk/log"
)
// Cached download of the dev server.
type CachedDownload struct {
// Which version to download, by default the latest version compatible with the SDK will be downloaded.
// Acceptable values are specific release versions (e.g v0.3.0), "default", and "latest".
Version string
// Destination directory or the user temp directory if unset.
DestDir string
}
// Configuration for the dev server.
type DevServerOptions struct {
// Existing path on the filesystem for the executable.
ExistingPath string
// Download the executable if not already there.
CachedDownload CachedDownload
// Client options used to create a client for the dev server.
// The provided Namespace or the "default" namespace is automatically registered on startup.
// If HostPort is provided, the host and port will be used to bind the server, otherwise the server will bind to
// localhost and obtain a free port.
ClientOptions *client.Options
// SQLite DB filename if persisting or non-persistent if none.
DBFilename string
// Whether to enable the UI.
EnableUI bool
// Override UI port if EnableUI is true.
// If not provided, a free port will be used.
UIPort string
// Log format - defaults to "pretty".
LogFormat string
// Log level - defaults to "warn".
LogLevel string
// Additional arguments to the dev server.
ExtraArgs []string
// Where to redirect stdout and stderr, if nil they will be redirected to the current process.
Stdout io.Writer
Stderr io.Writer
}
// Temporal CLI based DevServer
type DevServer struct {
cmd *exec.Cmd
client client.Client
frontendHostPort string
}
// StartDevServer starts a Temporal CLI dev server process. This may download the server if not already downloaded.
func StartDevServer(ctx context.Context, options DevServerOptions) (*DevServer, error) {
clientOptions := options.clientOptionsOrDefault()
exePath, err := downloadIfNeeded(ctx, &options, clientOptions.Logger)
if err != nil {
return nil, err
}
if clientOptions.HostPort == "" {
// Make sure this is done after downloading to reduce the chance (however slim) that the free port would be used
// up by the time the download completes.
clientOptions.HostPort, err = getFreeHostPort()
if err != nil {
return nil, err
}
}
host, port, err := net.SplitHostPort(clientOptions.HostPort)
if err != nil {
return nil, fmt.Errorf("invalid HostPort: %w", err)
}
args := prepareCommand(&options, host, port, clientOptions.Namespace)
cmd := newCmd(exePath, args...)
if options.Stdout != nil {
cmd.Stdout = options.Stdout
}
if options.Stderr != nil {
cmd.Stderr = options.Stderr
}
clientOptions.Logger.Info("Starting DevServer", "ExePath", exePath, "Args", args)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed starting: %w", err)
}
returnedClient, err := waitServerReady(ctx, clientOptions)
if err != nil {
return nil, err
}
clientOptions.Logger.Info("DevServer ready")
return &DevServer{
client: returnedClient,
cmd: cmd,
frontendHostPort: clientOptions.HostPort,
}, nil
}
func prepareCommand(options *DevServerOptions, host, port, namespace string) []string {
args := []string{
"server",
"start-dev",
"--ip", host, "--port", port,
"--namespace", namespace,
"--dynamic-config-value", "frontend.enableServerVersionCheck=false",
}
if options.LogLevel != "" {
args = append(args, "--log-level", options.LogLevel)
}
if options.LogFormat != "" {
args = append(args, "--log-format", options.LogFormat)
}
if !options.EnableUI {
args = append(args, "--headless")
}
if options.DBFilename != "" {
args = append(args, "--db-filename", options.DBFilename)
}
if options.UIPort != "" {
args = append(args, "--ui-port", options.UIPort)
}
return append(args, options.ExtraArgs...)
}
func downloadIfNeeded(ctx context.Context, options *DevServerOptions, logger log.Logger) (string, error) {
if options.ExistingPath != "" {
return options.ExistingPath, nil
}
version := options.CachedDownload.Version
if version == "" {
version = "default"
}
destDir := options.CachedDownload.DestDir
if destDir == "" {
destDir = os.TempDir()
}
var exePath string
// Build path based on version and check if already present
if version == "default" {
exePath = filepath.Join(destDir, "temporal-cli-go-sdk-"+internal.SDKVersion)
} else {
exePath = filepath.Join(destDir, "temporal-cli-"+version)
}
if runtime.GOOS == "windows" {
exePath += ".exe"
}
if _, err := os.Stat(exePath); err == nil {
return exePath, nil
}
client := &http.Client{}
// Build info URL
platform := runtime.GOOS
if platform != "windows" && platform != "darwin" && platform != "linux" {
return "", fmt.Errorf("unsupported platform %v", platform)
}
arch := runtime.GOARCH
if arch != "amd64" && arch != "arm64" {
return "", fmt.Errorf("unsupported architecture %v", arch)
}
infoURL := fmt.Sprintf("https://temporal.download/cli/%v?platform=%v&arch=%v&sdk-name=sdk-go&sdk-version=%v", url.QueryEscape(version), platform, arch, internal.SDKVersion)
// Get info
info := struct {
ArchiveURL string `json:"archiveUrl"`
FileToExtract string `json:"fileToExtract"`
}{}
req, err := http.NewRequestWithContext(ctx, "GET", infoURL, nil)
if err != nil {
return "", fmt.Errorf("failed preparing request: %w", err)
}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed fetching info: %w", err)
}
b, err := io.ReadAll(resp.Body)
if closeErr := resp.Body.Close(); closeErr != nil {
logger.Warn("Failed to close response body: %v", closeErr)
}
if err != nil {
return "", fmt.Errorf("failed fetching info body: %w", err)
} else if resp.StatusCode != 200 {
return "", fmt.Errorf("failed fetching info, status: %v, body: %s", resp.Status, b)
} else if err = json.Unmarshal(b, &info); err != nil {
return "", fmt.Errorf("failed unmarshaling info: %w", err)
}
// Download and extract
logger.Info("Downloading temporal CLI", "Url", info.ArchiveURL, "ExePath", exePath)
req, err = http.NewRequestWithContext(ctx, "GET", info.ArchiveURL, nil)
if err != nil {
return "", fmt.Errorf("failed preparing request: %w", err)
}
resp, err = client.Do(req)
if err != nil {
return "", fmt.Errorf("failed downloading: %w", err)
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logger.Warn("Failed to close response body: %v", closeErr)
}
}()
if resp.StatusCode != 200 {
return "", fmt.Errorf("failed downloading, status: %v", resp.Status)
}
// We want to download to a temporary file then rename. A better system-wide
// atomic downloader would use a common temp file and check whether it exists
// and wait on it, but doing multiple downloads in racy situations is
// good/simple enough for now.
// Note that we don't use os.TempDir here, instead we use the user provided destination directory which is
// guaranteed to make the rename atomic.
f, err := os.CreateTemp(destDir, "temporal-cli-downloading-")
if err != nil {
return "", fmt.Errorf("failed creating temp file: %w", err)
}
if strings.HasSuffix(info.ArchiveURL, ".tar.gz") {
err = extractTarball(resp.Body, info.FileToExtract, f)
} else if strings.HasSuffix(info.ArchiveURL, ".zip") {
err = extractZip(resp.Body, info.FileToExtract, f)
} else {
err = fmt.Errorf("unrecognized file extension on %v", info.ArchiveURL)
}
closeErr := f.Close()
if err != nil {
return "", err
} else if closeErr != nil {
return "", fmt.Errorf("failed to close temp file: %w", closeErr)
}
// Chmod it if not Windows
if runtime.GOOS != "windows" {
if err := os.Chmod(f.Name(), 0755); err != nil {
return "", fmt.Errorf("failed chmod'ing file: %w", err)
}
}
if err = os.Rename(f.Name(), exePath); err != nil {
return "", fmt.Errorf("failed moving file: %w", err)
}
return exePath, nil
}
func (opts *DevServerOptions) clientOptionsOrDefault() client.Options {
var out client.Options
if opts.ClientOptions != nil {
// Shallow copy the client options since we intend to overwrite some fields.
out = *opts.ClientOptions
}
if out.Logger == nil {
out.Logger = ilog.NewDefaultLogger()
}
if out.Namespace == "" {
out.Namespace = "default"
}
return out
}
func extractTarball(r io.Reader, toExtract string, w io.Writer) error {
r, err := gzip.NewReader(r)
if err != nil {
return err
}
tarRead := tar.NewReader(r)
for {
h, err := tarRead.Next()
if err != nil {
// This can be EOF which means we never found our file
return err
} else if h.Name == toExtract {
_, err = io.Copy(w, tarRead)
return err
}
}
}
func extractZip(r io.Reader, toExtract string, w io.Writer) error {
// Instead of using a third party zip streamer, and since Go stdlib doesn't
// support streaming read, we'll just put the entire archive in memory for now
b, err := io.ReadAll(r)
if err != nil {
return err
}
zipRead, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
if err != nil {
return err
}
for _, file := range zipRead.File {
if file.Name == toExtract {
r, err := file.Open()
if err != nil {
return err
}
_, err = io.Copy(w, r)
return err
}
}
return fmt.Errorf("could not find file in zip archive")
}
// waitServerReady repeatedly attempts to dial the server with given options until it is ready or it is time to give up.
// Returns a connected client created using the provided options.
func waitServerReady(ctx context.Context, options client.Options) (client.Client, error) {
var returnedClient client.Client
lastErr := retryFor(ctx, 600, 100*time.Millisecond, func() error {
var err error
returnedClient, err = client.DialContext(ctx, options)
return err
})
if lastErr != nil {
return nil, fmt.Errorf("failed connecting after timeout, last error: %w", lastErr)
}
return returnedClient, lastErr
}
// retryFor retries some function until it returns nil or runs out of attempts. Wait interval between attempts.
func retryFor(ctx context.Context, maxAttempts int, interval time.Duration, cond func() error) error {
if maxAttempts < 1 {
// this is used internally, okay to panic
panic("maxAttempts should be at least 1")
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
var lastErr error
for i := 0; i < maxAttempts; i++ {
if curE := cond(); curE == nil {
return nil
} else {
lastErr = curE
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
// Try again after waiting up to interval.
}
}
return lastErr
}
// Stop the running server and wait for shutdown to complete. Error is propagated from server shutdown.
func (s *DevServer) Stop() error {
if err := sendInterrupt(s.cmd.Process); err != nil {
return err
}
return s.cmd.Wait()
}
// Get a connected client, configured to work with the dev server.
func (s *DevServer) Client() client.Client {
return s.client
}
// FrontendHostPort returns the host:port for this server.
func (s *DevServer) FrontendHostPort() string {
return s.frontendHostPort
}