-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathmain.go
More file actions
436 lines (375 loc) · 11.6 KB
/
main.go
File metadata and controls
436 lines (375 loc) · 11.6 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
version "sf-core/src"
"sf-core/src/certs"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"golang.org/x/mod/semver"
)
const (
PROD_INSTALL_URL = "https://install.serverless.com"
DEV_INSTALL_URL = "https://install.serverless-dev.com"
)
func createServerlessDirectoryIfNotExists() {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(fmt.Errorf("resolving home dir: %w", err))
}
if _, err := os.Stat(fmt.Sprintf("%s/.serverless/binaries", homeDir)); os.IsNotExist(err) {
err = os.MkdirAll(fmt.Sprintf("%s/.serverless/binaries", homeDir), 0755)
if err != nil {
panic(fmt.Errorf("creating binaries directory: %w", err))
}
}
}
func getConfigPathFromArgs(args []string) (string, bool) {
for i, arg := range args {
if arg == "--" {
break
}
switch {
case arg == "--config":
if i+1 < len(args) {
return args[i+1], true
}
case strings.HasPrefix(arg, "--config="):
return strings.TrimPrefix(arg, "--config="), true
case arg == "-c":
if i+1 < len(args) {
return args[i+1], true
}
case strings.HasPrefix(arg, "-c="):
return strings.TrimPrefix(arg, "-c="), true
}
}
return "", false
}
func resolveConfigFilePath(args []string) string {
if configPath, ok := getConfigPathFromArgs(args); ok {
if configPath == "" {
return getLocalServerlessConfigFilePath()
}
if strings.HasPrefix(configPath, "~"+string(os.PathSeparator)) {
if homeDir, err := os.UserHomeDir(); err == nil {
configPath = filepath.Join(homeDir, configPath[2:])
}
}
if !filepath.IsAbs(configPath) {
if cwd, err := os.Getwd(); err == nil {
configPath = filepath.Join(cwd, configPath)
}
}
configPath = filepath.Clean(configPath)
if info, err := os.Stat(configPath); err == nil {
if info.IsDir() {
return getLocalServerlessConfigFilePath()
}
return configPath
}
return getLocalServerlessConfigFilePath()
}
return getLocalServerlessConfigFilePath()
}
func shouldCheckForUpdates() bool {
if isUpdateCommand() {
return true
}
if _, isSet := os.LookupEnv("SERVERLESS_FRAMEWORK_FORCE_UPDATE"); isSet {
return true
}
return false
}
func isNodeUpToDate() bool {
cmd := exec.Command("node", "--version")
output, err := cmd.Output()
if err != nil {
return false
}
outputStr := strings.TrimSpace(string(output))
re := regexp.MustCompile(`^v(\d+)\.\d+\.\d+`)
matches := re.FindStringSubmatch(outputStr)
if len(matches) < 2 {
return false
}
majorVersion, err := strconv.Atoi(matches[1])
if err != nil || majorVersion < 18 {
return false
}
return true
}
func doesNodeExistAndIsItAccessible() bool {
if _, err := exec.LookPath("node"); err != nil {
return false
}
if _, err := exec.LookPath("npm"); err != nil {
return false
}
return true
}
type PackageJson struct {
Version string `json:"version"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
}
func runLocalVersionIfAvailable() {
// In V4 all versions are global and versions will be eventually pinned and used from the global set of versions.
// However users expect that a global serverless version can use a locally installed one, i.e. in the package.json.
// For now we need to support locally installed v3 instances from the global v4 install.
// 1. First check if node_modules/serverless/bin/serverless.js and node_modules/serverless/package.json exists
// 2. Check node_modules/serverless/package.json version is equal to v3 or lower
// 3. Check if package.json contains serverless v3 or lower as a dependency
cwd, err := os.Getwd()
if err != nil {
return
}
fullPathToBinScript := filepath.Join(cwd, "node_modules/serverless/bin/serverless.js")
fullPathToPackageJson := filepath.Join(cwd, "node_modules/serverless/package.json")
localPackageJson := filepath.Join(cwd, "package.json")
_, err = os.Stat(fullPathToBinScript)
if os.IsNotExist(err) {
return
}
_, err = os.Stat(fullPathToPackageJson)
if os.IsNotExist(err) {
return
}
_, err = os.Stat(localPackageJson)
if os.IsNotExist(err) {
return
}
b, err := os.ReadFile(localPackageJson)
if err != nil {
// Really shouldn't be possible
return
}
var packageJson PackageJson
err = json.Unmarshal(b, &packageJson)
if err != nil {
return
}
if ver, exists := packageJson.DevDependencies["serverless"]; exists {
if semver.Compare(ver, "v4.0.0") >= 0 {
return
}
} else {
return
}
args := os.Args[1:]
args = append([]string{fullPathToBinScript}, args...)
cmd := exec.Command("node", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
if err := cmd.Start(); err != nil {
panic(fmt.Errorf("starting node: %w", err))
}
if err := cmd.Wait(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}
panic(fmt.Errorf("node process wait: %w", err))
}
os.Exit(0)
}
func getBinaryName() string {
osType := runtime.GOOS
architecture := runtime.GOARCH
// Check for supported architectures
if architecture != "arm64" && architecture != "amd64" {
fmt.Fprintf(os.Stderr, "Architecture %s is not supported.\n", architecture)
os.Exit(1)
}
// Check for unsupported OS-architecture combinations
if architecture == "arm64" && osType == "windows" {
fmt.Fprintf(os.Stderr, "Platform %s - %s is not supported.\n", osType, architecture)
os.Exit(1)
}
// getInstallBaseUrl is conditionally compiled based on build tags
// The implementation is in install_base_url.go and install_base_url_canary.go
baseUrl := getInstallBaseUrl()
return fmt.Sprintf("%s/installer-builds/serverless-%s-%s", baseUrl, osType, architecture)
}
func updateInstaller() {
useSpinner := !version.IsCIEnvironment()
var updateSpinner *spinner.Spinner
if useSpinner {
updateSpinner = spinner.New(spinner.CharSets[14], 100*time.Millisecond)
updateSpinner.Suffix = " Updating"
updateSpinner.Color("red")
updateSpinner.Start()
defer func() {
updateSpinner.Stop()
}()
} else {
fmt.Fprintln(os.Stderr, "Updating installer...")
}
// Get the correct binary name based on OS and architecture
binaryName := getBinaryName()
client := http.Client{Timeout: 2 * time.Minute}
// Download the latest installer
response, err := client.Get(binaryName)
if err != nil {
panic(fmt.Errorf("downloading installer from %s: %w", binaryName, err))
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
limited := io.LimitReader(response.Body, 2048)
b, _ := io.ReadAll(limited)
panic(fmt.Errorf("installer download failed: GET %s returned %s; body: %s", binaryName, response.Status, strings.TrimSpace(string(b))))
}
// Get the path of the current executable
name, err := os.Executable()
if err != nil {
panic(fmt.Errorf("locating executable: %w", err))
}
name, err = filepath.EvalSymlinks(name)
if err != nil {
panic(fmt.Errorf("resolving executable symlink: %w", err))
}
newName := name + ".new"
oldName := name + ".old"
// Create a new file to write the downloaded installer
out, err := os.OpenFile(newName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
panic(fmt.Errorf("creating temp installer %s: %w", newName, err))
}
// Write the downloaded installer to .new file
_, err = io.Copy(out, response.Body)
if err != nil {
panic(fmt.Errorf("writing temp installer %s: %w", newName, err))
}
// Remove the .old (from the last update) if it exists
if _, err := os.Stat(oldName); err == nil {
if err := os.Remove(oldName); err != nil {
panic(fmt.Errorf("removing old installer %s: %w", oldName, err))
}
}
// Rename the current executable to .old
if err := os.Rename(name, oldName); err != nil {
panic(fmt.Errorf("renaming %s to %s: %w", name, oldName, err))
}
// Close the file to avoid "The process cannot access the file because it is being used by another process." error
out.Close()
// Rename the .new installer to the current executable
if err := os.Rename(newName, name); err != nil {
panic(fmt.Errorf("renaming %s to %s: %w", newName, name, err))
}
if !useSpinner {
fmt.Fprintln(os.Stderr, "Installer update complete")
}
}
func getLocalServerlessConfigFilePath() string {
supportedFilenames := []string{"serverless", "serverless-compose", "serverless.containers", "serverless.ai"}
supportedExtensions := []string{"yml", "yaml", "js", "ts", "cjs", "mjs", "json"}
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
// Check if the current working directory contains a supported serverless config file
for _, baseName := range supportedFilenames {
for _, extension := range supportedExtensions {
filePath := filepath.Join(cwd, baseName+"."+extension)
if _, err := os.Stat(filePath); err == nil {
return filePath
}
}
}
return ""
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", r)
os.Exit(1)
}
}()
if value, ok := os.LookupEnv("SLS_DISABLE_EXTRA_CA_CERTS"); ok && value != "false" {
// Configure HTTP clients to honor extra CA certs from env
certs.ConfigureHTTPRootCAs()
}
createServerlessDirectoryIfNotExists()
if isUpdateCommand() {
updateInstaller()
}
runLocalVersionIfAvailable()
args := os.Args[1:]
configFilePath := resolveConfigFilePath(args)
release, err := version.GetFrameworkVersion(configFilePath, shouldCheckForUpdates())
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting framework version: %v\n", err)
os.Exit(1)
}
if release == nil {
fmt.Fprintln(os.Stderr, "Could not find a valid version to run")
os.Exit(1)
}
if ok := doesNodeExistAndIsItAccessible(); !ok {
fmt.Fprintln(os.Stderr, "Nodejs is not installed, please install Nodejs and run the command again")
os.Exit(1)
}
if ok := isNodeUpToDate(); !ok {
fmt.Fprintln(os.Stderr, "Your Nodejs version is too old, please upgrade to Node 18 or newer and rerun Serverless")
os.Exit(1)
}
if isUpdateCommand() {
fmt.Printf("✔ Update completed\n")
if release.LatestVersion != nil && *release.LatestVersion != release.Version {
color.Yellow(fmt.Sprintf("A new version, %s, has been released. Update your `frameworkVersion` property to use it", *release.LatestVersion))
}
return
}
enableSourcemaps := false
for _, arg := range args {
if arg == "--debug" {
enableSourcemaps = true
}
}
args = append([]string{filepath.Join(release.ReleasePath, "package/dist/sf-core.js")}, args...)
cmd := exec.Command("node", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
if enableSourcemaps {
cmd.Env = append(cmd.Env, "NODE_OPTIONS=--enable-source-maps")
}
if err := cmd.Start(); err != nil {
panic(fmt.Errorf("starting node: %w", err))
}
// Ignore SIGINT and SIGTERM signals to prevent Go from exiting before the child Node process
// The child Node process has the same process group as the Go process, so it will receive the signals as well
// and handle them accordingly. We're not using signal.Ignore because the process does not have permission to ignore CTRL+C.
// signal.Ignore(os.Interrupt) does nothing - Windows will still terminate the process when CTRL+C is pressed.
// Instead we're using signal.Notify which explicitly registers a console control handler.
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
go func() {
<-sigs // Needed to discard signals
}()
if err := cmd.Wait(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}
panic(fmt.Errorf("node process wait: %w", err))
}
}
func isUpdateCommand() bool {
return len(os.Args) >= 2 && os.Args[1] == "update"
}