-
Notifications
You must be signed in to change notification settings - Fork 126
/
runtime.go
135 lines (119 loc) · 2.99 KB
/
runtime.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
package runtime
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"github.com/sst/ion/pkg/project"
)
type Runtime interface {
Match(runtime string) bool
Build(ctx context.Context, input *BuildInput) (*BuildOutput, error)
Run(ctx context.Context, input *RunInput) (Worker, error)
ShouldRebuild(functionID string, path string) bool
}
type Worker interface {
Stop()
Logs() io.ReadCloser
}
type BuildInput struct {
Warp project.Warp
Project *project.Project
Links project.Links
Dev bool
}
func (input *BuildInput) Out() string {
return filepath.Join(input.Project.PathWorkingDir(), "artifacts", input.Warp.FunctionID)
}
type BuildOutput struct {
Out string
Handler string
Errors []string
}
type RunInput struct {
Project *project.Project
Server string
FunctionID string
WorkerID string
Runtime string
Build *BuildOutput
Env []string
}
var runtimes = []Runtime{
newNodeRuntime(),
newWorkerRuntime(),
}
func GetRuntime(input string) (Runtime, bool) {
for _, runtime := range runtimes {
if runtime.Match(input) {
return runtime, true
}
}
return nil, false
}
func Build(ctx context.Context, input *BuildInput) (*BuildOutput, error) {
slog.Info("building function", "runtime", input.Warp.Runtime, "functionID", input.Warp.FunctionID)
defer slog.Info("function built", "runtime", input.Warp.Runtime, "functionID", input.Warp.FunctionID)
runtime, ok := GetRuntime(input.Warp.Runtime)
if !ok {
return nil, fmt.Errorf("Runtime not found: %v", input.Warp.Runtime)
}
out := input.Out()
if err := os.RemoveAll(out); err != nil {
return nil, err
}
if err := os.MkdirAll(out, 0755); err != nil {
return nil, err
}
links := project.Links{}
for _, name := range input.Warp.Links {
value := input.Links[name]
links[name] = value
}
input.Links = links
result, err := runtime.Build(ctx, input)
if err != nil {
return nil, err
}
result.Out = out
if len(input.Warp.CopyFiles) > 0 {
for _, item := range input.Warp.CopyFiles {
from, err := filepath.Abs(item.From)
if err != nil {
return nil, err
}
dest := filepath.Join(out, item.To)
if item.To == "" {
dest = filepath.Join(out, item.From)
err := os.MkdirAll(filepath.Dir(dest), 0755)
if err != nil {
return nil, err
}
}
err = os.Symlink(from, dest)
if err != nil {
return nil, err
}
}
}
return result, nil
}
func Run(ctx context.Context, input *RunInput) (Worker, error) {
slog.Info("running function", "runtime", input.Runtime, "functionID", input.FunctionID)
runtime, ok := GetRuntime(input.Runtime)
input.Env = append(input.Env, "SST_LIVE=true")
if !ok {
return nil, fmt.Errorf("runtime not found")
}
return runtime.Run(ctx, input)
}
func ShouldRebuild(runtime string, functionID string, file string) bool {
slog.Info("checking if function should be rebuilt", "runtime", runtime, "functionID", functionID, "file", file)
r, ok := GetRuntime(runtime)
if !ok {
return false
}
return r.ShouldRebuild(functionID, file)
}