Skip to content

Commit 15734ba

Browse files
committed
feat: Create a "runtime" package that contains all runtime specific code
This is to make it easy to implement an new one.
1 parent 40b46de commit 15734ba

File tree

17 files changed

+444
-385
lines changed

17 files changed

+444
-385
lines changed

pkg/build/build.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
"os"
2222
"os/exec"
2323
"path"
24+
"strings"
2425

2526
"github.com/nitrictech/newcli/pkg/containerengine"
26-
"github.com/nitrictech/newcli/pkg/functiondockerfile"
27+
"github.com/nitrictech/newcli/pkg/runtime"
2728
"github.com/nitrictech/newcli/pkg/stack"
2829
"github.com/nitrictech/newcli/pkg/target"
29-
"github.com/nitrictech/newcli/pkg/utils"
3030
)
3131

3232
func Create(s *stack.Stack, t *target.Target) error {
@@ -50,7 +50,11 @@ func Create(s *stack.Stack, t *target.Target) error {
5050
}
5151
defer func() { os.Remove(fh.Name()) }()
5252

53-
err = functiondockerfile.Generate(&f, f.VersionString(s), t.Provider, fh)
53+
rt, err := runtime.NewRunTimeFromHandler(f.Handler)
54+
if err != nil {
55+
return err
56+
}
57+
err = rt.FunctionDockerfile(f.ContextDirectory, f.VersionString(s), t.Provider, fh)
5458
if err != nil {
5559
return err
5660
}
@@ -81,11 +85,11 @@ func CreateBaseDev(s *stack.Stack) error {
8185
}
8286
imagesToBuild := map[string]string{}
8387
for _, f := range s.Functions {
84-
rt, err := utils.NewRunTimeFromFilename(f.Handler)
88+
rt, err := runtime.NewRunTimeFromHandler(f.Handler)
8589
if err != nil {
8690
return err
8791
}
88-
lang := rt.String()
92+
lang := strings.Replace(path.Ext(f.Handler), ".", "", 1)
8993
_, ok := imagesToBuild[lang]
9094
if ok {
9195
continue
@@ -101,7 +105,7 @@ func CreateBaseDev(s *stack.Stack) error {
101105
os.Remove(f.Name())
102106
}()
103107

104-
if err := functiondockerfile.GenerateForCodeAsConfig("handler."+lang, f); err != nil {
108+
if err := rt.FunctionDockerfileForCodeAsConfig(f); err != nil {
105109
return err
106110
}
107111

pkg/codeconfig/codeconfig.go

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ import (
2020
"fmt"
2121
"net"
2222
"os"
23-
"path"
24-
"runtime"
23+
osruntime "runtime"
2524
"strings"
2625
"sync"
2726

2827
"github.com/docker/docker/api/types"
2928
"github.com/docker/docker/api/types/container"
30-
"github.com/docker/docker/api/types/mount"
31-
"github.com/docker/docker/api/types/strslice"
3229
"github.com/getkin/kin-openapi/openapi3"
3330
"github.com/imdario/mergo"
3431
"github.com/moby/moby/pkg/stdcopy"
@@ -39,7 +36,7 @@ import (
3936
"github.com/nitrictech/newcli/pkg/containerengine"
4037
"github.com/nitrictech/newcli/pkg/cron"
4138
"github.com/nitrictech/newcli/pkg/output"
42-
"github.com/nitrictech/newcli/pkg/run"
39+
"github.com/nitrictech/newcli/pkg/runtime"
4340
"github.com/nitrictech/newcli/pkg/stack"
4441
"github.com/nitrictech/newcli/pkg/utils"
4542
v1 "github.com/nitrictech/nitric/pkg/api/nitric/v1"
@@ -138,10 +135,14 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
138135

139136
// Collect all workers
140137
for handler, f := range c.functions {
138+
rt, err := runtime.NewRunTimeFromHandler(handler)
139+
if err != nil {
140+
return nil, err
141+
}
141142
if f.apis[api] != nil {
142143
for _, w := range f.apis[api].workers {
143144
workers = append(workers, &apiHandler{
144-
target: containerNameFromHandler(handler),
145+
target: rt.ContainerName(),
145146
worker: w,
146147
})
147148
}
@@ -203,7 +204,7 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
203204
Extensions: map[string]interface{}{
204205
"x-nitric-target": map[string]string{
205206
"type": "function",
206-
"name": containerNameFromHandler(w.target),
207+
"name": w.target,
207208
},
208209
},
209210
},
@@ -214,35 +215,6 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
214215
return doc, nil
215216
}
216217

217-
func launchOptsForFunctionCollect(runCtx, handler string) (run.LaunchOpts, error) {
218-
rt, err := utils.NewRunTimeFromFilename(handler)
219-
if err != nil {
220-
return run.LaunchOpts{}, err
221-
}
222-
223-
switch rt {
224-
case utils.RuntimeJavascript, utils.RuntimeTypescript:
225-
return run.LaunchOpts{
226-
Image: rt.DevImageName(),
227-
Entrypoint: strslice.StrSlice{"ts-node"},
228-
Cmd: strslice.StrSlice{"-T " + "/app/" + handler},
229-
}, nil
230-
case utils.RuntimeGolang:
231-
module, err := utils.GoModule(runCtx)
232-
if err != nil {
233-
return run.LaunchOpts{}, err
234-
}
235-
return run.LaunchOpts{
236-
Image: rt.DevImageName(),
237-
TargetWD: path.Join("/go/src", module),
238-
Entrypoint: strslice.StrSlice{"go", "run"},
239-
Cmd: strslice.StrSlice{"./" + handler},
240-
}, nil
241-
default:
242-
return run.LaunchOpts{}, errors.New("could not get launchOpts from " + handler + ", runtime not supported")
243-
}
244-
}
245-
246218
// collectOne - Collects information about a function for a nitric stack
247219
// handler - the specific handler for the application
248220
func (c *codeConfig) collectOne(handler string) error {
@@ -271,26 +243,24 @@ func (c *codeConfig) collectOne(handler string) error {
271243
// Specify the service bind as the port with the docker gateway IP (running in bridge mode)
272244
ce, err := containerengine.Discover()
273245
if err != nil {
274-
return errors.WithMessage(err, "error running the handler")
246+
return errors.WithMessage(err, "error discovering container engine")
275247
}
276248

277-
opts, err := launchOptsForFunctionCollect(c.initialStack.Dir, handler)
249+
rt, err := runtime.NewRunTimeFromHandler(handler)
250+
if err != nil {
251+
return errors.WithMessage(err, "error getting the runtime from handler "+handler)
252+
}
253+
254+
opts, err := rt.LaunchOptsForFunctionCollect(c.initialStack.Dir)
278255
if err != nil {
279256
return err
280257
}
281-
fmt.Println(opts.String())
282258

283259
hostConfig := &container.HostConfig{
284260
AutoRemove: true,
285-
Mounts: []mount.Mount{
286-
{
287-
Type: "bind",
288-
Source: c.initialStack.Dir,
289-
Target: opts.TargetWD,
290-
},
291-
},
261+
Mounts: opts.Mounts,
292262
}
293-
if runtime.GOOS == "linux" {
263+
if osruntime.GOOS == "linux" {
294264
// setup host.docker.internal to route to host gateway
295265
// to access rpc server hosted by local CLI run
296266
hostConfig.ExtraHosts = []string{"host.docker.internal:172.17.0.1"}
@@ -308,7 +278,7 @@ func (c *codeConfig) collectOne(handler string) error {
308278
Cmd: opts.Cmd,
309279
Entrypoint: opts.Entrypoint,
310280
WorkingDir: opts.TargetWD,
311-
}, hostConfig, nil, containerNameFromHandler(handler))
281+
}, hostConfig, nil, rt.ContainerName())
312282
if err != nil {
313283
return err
314284
}
@@ -356,11 +326,6 @@ func (c *codeConfig) collectOne(handler string) error {
356326
return errs.Aggregate()
357327
}
358328

359-
func containerNameFromHandler(handler string) string {
360-
rt, _ := utils.NewRunTimeFromFilename(handler)
361-
return rt.ContainerName(handler)
362-
}
363-
364329
func (c *codeConfig) addFunction(fun *FunctionDependencies, handler string) {
365330
c.lock.Lock()
366331
defer c.lock.Unlock()
@@ -378,7 +343,11 @@ func (c *codeConfig) ToStack() (*stack.Stack, error) {
378343

379344
errs := utils.NewErrorList()
380345
for handler, f := range c.functions {
381-
name := containerNameFromHandler(handler)
346+
rt, err := runtime.NewRunTimeFromHandler(handler)
347+
if err != nil {
348+
return nil, err
349+
}
350+
382351
topicTriggers := make([]string, 0, len(f.subscriptions)+len(f.schedules))
383352

384353
for k := range f.apis {
@@ -459,14 +428,14 @@ func (c *codeConfig) ToStack() (*stack.Stack, error) {
459428
}
460429
}
461430

462-
f, ok := s.Functions[name]
431+
f, ok := s.Functions[rt.ContainerName()]
463432
if !ok {
464433
f = stack.FunctionFromHandler(handler, s.Dir)
465434
}
466435
f.ComputeUnit.Triggers = stack.Triggers{
467436
Topics: topicTriggers,
468437
}
469-
s.Functions[name] = f
438+
s.Functions[rt.ContainerName()] = f
470439
}
471440

472441
return s, errs.Aggregate()

pkg/functiondockerfile/generator.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)