Skip to content

Commit b1eb917

Browse files
committed
feat(run): Use CompileDaemon for golang
1 parent 0e60595 commit b1eb917

File tree

4 files changed

+103
-19
lines changed

4 files changed

+103
-19
lines changed

pkg/containerengine/types.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package containerengine
1919
import (
2020
"errors"
2121
"io"
22+
"strings"
2223
"time"
2324

2425
"github.com/docker/docker/api/types"
@@ -80,3 +81,51 @@ func Discover() (ContainerEngine, error) {
8081
func buildTimeout() time.Duration {
8182
return viper.GetDuration("build_timeout")
8283
}
84+
85+
func Cli(cc *container.Config, hc *container.HostConfig) string {
86+
cmd := []string{"docker", "run"}
87+
88+
if cc.Tty {
89+
cmd = append(cmd, "-t")
90+
}
91+
92+
if len(cc.Entrypoint) > 0 {
93+
cmd = append(cmd, "--entrypoint")
94+
cmd = append(cmd, cc.Entrypoint...)
95+
}
96+
97+
if cc.WorkingDir != "" {
98+
cmd = append(cmd, "-w", cc.WorkingDir)
99+
}
100+
101+
for _, v := range hc.Mounts {
102+
cmd = append(cmd, "-v", v.Source+":"+v.Target)
103+
}
104+
105+
for _, e := range cc.Env {
106+
cmd = append(cmd, "-e", e)
107+
}
108+
109+
for _, h := range hc.ExtraHosts {
110+
cmd = append(cmd, "--add-host", h)
111+
}
112+
113+
if hc.AutoRemove {
114+
cmd = append(cmd, "--rm")
115+
}
116+
117+
if cc.AttachStdout {
118+
cmd = append(cmd, "-a", "stdout")
119+
}
120+
if cc.AttachStdin {
121+
cmd = append(cmd, "-a", "stdin")
122+
}
123+
if cc.AttachStderr {
124+
cmd = append(cmd, "-a", "stderr")
125+
}
126+
127+
cmd = append(cmd, cc.Image)
128+
cmd = append(cmd, cc.Cmd...)
129+
130+
return strings.Join(cmd, " ")
131+
}

pkg/run/function.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ package run
1717

1818
import (
1919
"fmt"
20+
"log"
2021
goruntime "runtime"
2122
"time"
2223

2324
"github.com/docker/docker/api/types/container"
2425

2526
"github.com/nitrictech/newcli/pkg/containerengine"
27+
"github.com/nitrictech/newcli/pkg/output"
2628
"github.com/nitrictech/newcli/pkg/runtime"
2729
"github.com/nitrictech/newcli/pkg/stack"
2830
)
@@ -46,18 +48,19 @@ func (f *Function) Start() error {
4648
return err
4749
}
4850

49-
hostConfig := &container.HostConfig{
51+
hc := &container.HostConfig{
5052
AutoRemove: true,
5153
Mounts: launchOpts.Mounts,
5254
LogConfig: *f.ce.Logger(f.runCtx).Config(),
5355
}
56+
5457
if goruntime.GOOS == "linux" {
5558
// setup host.docker.internal to route to host gateway
5659
// to access rpc server hosted by local CLI run
57-
hostConfig.ExtraHosts = []string{"host.docker.internal:172.17.0.1"}
60+
hc.ExtraHosts = []string{"host.docker.internal:172.17.0.1"}
5861
}
5962

60-
cID, err := f.ce.ContainerCreate(&container.Config{
63+
cc := &container.Config{
6164
Image: f.rt.DevImageName(), // Select an image to use based on the handler
6265
// Set the address to the bound port
6366
Env: []string{
@@ -68,7 +71,13 @@ func (f *Function) Start() error {
6871
Entrypoint: launchOpts.Entrypoint,
6972
Cmd: launchOpts.Cmd,
7073
WorkingDir: launchOpts.TargetWD,
71-
}, hostConfig, nil, f.Name())
74+
}
75+
76+
if output.VerboseLevel > 1 {
77+
log.Default().Print(containerengine.Cli(cc, hc))
78+
}
79+
80+
cID, err := f.ce.ContainerCreate(cc, hc, nil, f.Name())
7281
if err != nil {
7382
return err
7483
}

pkg/run/minio.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package run
1717

1818
import (
1919
"fmt"
20+
"log"
2021
"os"
2122
"path/filepath"
2223
"time"
@@ -29,6 +30,7 @@ import (
2930
"github.com/pkg/errors"
3031

3132
"github.com/nitrictech/newcli/pkg/containerengine"
33+
"github.com/nitrictech/newcli/pkg/output"
3234
)
3335

3436
type MinioServer struct {
@@ -80,14 +82,16 @@ func (m *MinioServer) Start() error {
8082
return err
8183
}
8284

83-
cID, err := m.ce.ContainerCreate(&container.Config{
85+
cc := &container.Config{
8486
Image: minioImage,
8587
Cmd: []string{"minio", "server", "/nitric/buckets", "--console-address", fmt.Sprintf(":%d", consolePort)},
8688
ExposedPorts: nat.PortSet{
8789
nat.Port(fmt.Sprintf("%d/tcp", minioPort)): struct{}{},
8890
nat.Port(fmt.Sprintf("%d/tcp", minioConsolePort)): struct{}{},
8991
},
90-
}, &container.HostConfig{
92+
}
93+
94+
hc := &container.HostConfig{
9195
AutoRemove: true,
9296
PortBindings: nat.PortMap{
9397
nat.Port(fmt.Sprintf("%d/tcp", minioPort)): []nat.PortBinding{
@@ -110,15 +114,19 @@ func (m *MinioServer) Start() error {
110114
},
111115
LogConfig: *m.ce.Logger(m.dir).Config(),
112116
NetworkMode: container.NetworkMode("bridge"),
113-
}, &network.NetworkingConfig{
114-
EndpointsConfig: map[string]*network.EndpointSettings{},
115-
}, "minio-"+m.name)
117+
}
118+
119+
cID, err := m.ce.ContainerCreate(cc, hc, &network.NetworkingConfig{EndpointsConfig: map[string]*network.EndpointSettings{}}, "minio-"+m.name)
116120
if err != nil {
117121
return err
118122
}
119123
m.cid = cID
120124
m.apiPort = minioPort
121125

126+
if output.VerboseLevel > 1 {
127+
log.Default().Print(containerengine.Cli(cc, hc))
128+
}
129+
122130
return m.ce.Start(cID)
123131
}
124132

pkg/runtime/golang.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func (t *golang) FunctionDockerfileForCodeAsConfig(w io.Writer) error {
112112
if err != nil {
113113
return err
114114
}
115+
con.Run(dockerfile.RunOptions{Command: []string{"go", "get", "github.com/asalkeld/CompileDaemon@d4b10de"}})
115116

116117
_, err = w.Write([]byte(strings.Join(con.Lines(), "\n")))
117118
return err
@@ -123,10 +124,9 @@ func (t *golang) LaunchOptsForFunctionCollect(runCtx string) (LaunchOpts, error)
123124
return LaunchOpts{}, err
124125
}
125126
return LaunchOpts{
126-
Image: t.DevImageName(),
127-
TargetWD: path.Join("/go/src", module),
128-
Entrypoint: strslice.StrSlice{"go", "run"},
129-
Cmd: strslice.StrSlice{"./" + t.handler},
127+
Image: t.DevImageName(),
128+
TargetWD: path.Join("/go/src", module),
129+
Cmd: strslice.StrSlice{"go", "run", "./" + t.handler},
130130
Mounts: []mount.Mount{
131131
{
132132
Type: "bind",
@@ -147,10 +147,26 @@ func (t *golang) LaunchOptsForFunction(runCtx string) (LaunchOpts, error) {
147147
if err != nil {
148148
return LaunchOpts{}, err
149149
}
150-
return LaunchOpts{
151-
TargetWD: path.Join("/go/src", module),
152-
Entrypoint: strslice.StrSlice{"go", "run"},
153-
Cmd: strslice.StrSlice{"./" + t.handler},
150+
containerRunCtx := path.Join("/go/src", module)
151+
relHandler := t.handler
152+
if strings.HasPrefix(t.handler, runCtx) {
153+
relHandler, err = filepath.Rel(runCtx, t.handler)
154+
if err != nil {
155+
return LaunchOpts{}, err
156+
}
157+
}
158+
159+
opts := LaunchOpts{
160+
TargetWD: containerRunCtx,
161+
Cmd: strslice.StrSlice{
162+
"/go/bin/CompileDaemon",
163+
"-verbose",
164+
"-exclude-dir=.git",
165+
"-exclude-dir=.nitric",
166+
"-directory=.",
167+
fmt.Sprintf("-build=go build -o %s ./%s", t.ContainerName(), relHandler),
168+
"-command=./" + t.ContainerName(),
169+
},
154170
Mounts: []mount.Mount{
155171
{
156172
Type: "bind",
@@ -160,8 +176,10 @@ func (t *golang) LaunchOptsForFunction(runCtx string) (LaunchOpts, error) {
160176
{
161177
Type: "bind",
162178
Source: runCtx,
163-
Target: path.Join("/go/src", module),
179+
Target: containerRunCtx,
164180
},
165181
},
166-
}, nil
182+
}
183+
184+
return opts, nil
167185
}

0 commit comments

Comments
 (0)