Skip to content

Commit

Permalink
fixes for projects using prisma orm (#497)
Browse files Browse the repository at this point in the history
We may remove these fixes in future and allow developers to resolve
these issues themselves through custom docker files. For now though,
they have no impact on projects that aren't running prisma.

Related issues can be found here:
- prisma/docs#4365
- prisma/prisma#16901 (comment)
  • Loading branch information
jyecusch committed May 22, 2023
2 parents 7128d55 + 99c3372 commit 5ce52ae
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 104 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/docker/distribution v2.7.1+incompatible
github.com/nitrictech/nitric/cloud/common v0.0.0-20230509232736-48f59463930d
github.com/olahol/melody v1.1.3
github.com/samber/lo v1.38.1
github.com/seaweedfs/seaweedfs v0.0.0-20230109022022-51d4a4b28d0e
go.etcd.io/bbolt v1.3.6
google.golang.org/protobuf v1.30.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/sanposhiho/wastedassign/v2 v2.0.7 h1:J+6nrY4VW+gC9xFzUc+XjPD3g3wF3je/NsJFwFK7Uxc=
github.com/sanposhiho/wastedassign/v2 v2.0.7/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI=
Expand Down
11 changes: 10 additions & 1 deletion pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"

"github.com/pterm/pterm"
"github.com/samber/lo"

"github.com/nitrictech/cli/pkg/containerengine"
"github.com/nitrictech/cli/pkg/project"
Expand Down Expand Up @@ -63,7 +64,15 @@ func BuildBaseImages(s *project.Project) error {

pterm.Debug.Println("Building image for" + f.Name())

if err := ce.Build(filepath.Base(f.Name()), s.Dir, fmt.Sprintf("%s-%s", s.Name, fun.Name), rt.BuildArgs(), rt.BuildIgnore()); err != nil {
ingoreFunctions := lo.Filter(lo.Values(s.Functions), func(item project.Function, index int) bool {
return item.Name != fun.Name
})

ignoreHandlers := lo.Map(ingoreFunctions, func(item project.Function, index int) string {
return item.Handler
})

if err := ce.Build(filepath.Base(f.Name()), s.Dir, fmt.Sprintf("%s-%s", s.Name, fun.Name), rt.BuildArgs(), rt.BuildIgnore(ignoreHandlers...)); err != nil {
return err
}
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/codeconfig/codeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
multierror "github.com/missionMeteora/toolkit/errors"
"github.com/moby/moby/pkg/stdcopy"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"google.golang.org/grpc"

"github.com/nitrictech/cli/pkg/containerengine"
Expand Down Expand Up @@ -494,10 +493,6 @@ func (c *codeConfig) collectOne(handler string) error {
Env: env,
}

if output.VerboseLevel > 2 {
pterm.Debug.Println(containerengine.Cli(cc, hostConfig))
}

cn := strings.Join([]string{c.initialProject.Name, "codeAsConfig", rt.ContainerName()}, "-")

cID, err := ce.ContainerCreate(cc, hostConfig, &network.NetworkingConfig{
Expand Down
51 changes: 0 additions & 51 deletions pkg/containerengine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package containerengine
import (
"errors"
"io"
"strings"
"time"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -71,53 +70,3 @@ func Discover() (ContainerEngine, error) {

return nil, errors.New("Nitric relies on Docker to be running before containerizing your project. Please refer to our installation instructions - https://nitric.io/docs/installation")
}

func Cli(cc *container.Config, hc *container.HostConfig) string {
cmd := []string{"docker", "run"}

if cc.Tty {
cmd = append(cmd, "-t")
}

if len(cc.Entrypoint) > 0 {
cmd = append(cmd, "--entrypoint")
cmd = append(cmd, cc.Entrypoint...)
}

if cc.WorkingDir != "" {
cmd = append(cmd, "-w", cc.WorkingDir)
}

for _, v := range hc.Mounts {
cmd = append(cmd, "-v", v.Source+":"+v.Target)
}

for _, e := range cc.Env {
cmd = append(cmd, "-e", e)
}

for _, h := range hc.ExtraHosts {
cmd = append(cmd, "--add-host", h)
}

if hc.AutoRemove {
cmd = append(cmd, "--rm")
}

if cc.AttachStdout {
cmd = append(cmd, "-a", "stdout")
}

if cc.AttachStdin {
cmd = append(cmd, "-a", "stdin")
}

if cc.AttachStderr {
cmd = append(cmd, "-a", "stderr")
}

cmd = append(cmd, cc.Image)
cmd = append(cmd, cc.Cmd...)

return strings.Join(cmd, " ")
}
3 changes: 0 additions & 3 deletions pkg/run/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/docker/docker/api/types/container"
"github.com/pterm/pterm"

"github.com/nitrictech/cli/pkg/containerengine"
"github.com/nitrictech/cli/pkg/project"
Expand Down Expand Up @@ -74,8 +73,6 @@ func (f *Function) Start(envMap map[string]string) error {
Env: env,
}

pterm.Debug.Print(containerengine.Cli(cc, hc))

cID, err := f.ce.ContainerCreate(cc, hc, nil, strings.Join([]string{f.projectName, "run", f.Name()}, "-"))
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/runtime/csharp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func (t *csharp) ContainerName() string {
return strings.ToLower(strings.Replace(filepath.Base(t.handler), filepath.Ext(t.handler), "", 1))
}

func (t *csharp) BuildIgnore() []string {
return append(commonIgnore, "obj/", "bin/")
func (t *csharp) BuildIgnore(additional ...string) []string {
baseIgnores := append(commonIgnore, additional...)
return append(baseIgnores, "obj/", "bin/")
}

func (t *csharp) BaseDockerFile(w io.Writer) error {
Expand Down
56 changes: 39 additions & 17 deletions pkg/runtime/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,59 @@ func TestGenerate(t *testing.T) {
{
name: "ts",
handler: "functions/list.ts",
wantFwriter: `FROM node:alpine as build
wantFwriter: `# syntax=docker/dockerfile:1
FROM node:alpine as build
ARG HANDLER
# Python and make are required by certain native package build processes in NPM packages.
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 make g++ && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
RUN apk add g++ make py3-pip
RUN yarn global add typescript @vercel/ncc
COPY . .
WORKDIR /usr/app
RUN yarn import || echo Lockfile already exists
COPY package.json *.lock *-lock.json /
RUN yarn import || echo ""
RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN set -ex && \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \
rm -rf /tmp/.cache
RUN test -f tsconfig.json || echo "{\"compilerOptions\":{\"esModuleInterop\":true,\"target\":\"es2015\",\"moduleResolution\":\"node\"}}" > tsconfig.json
RUN ncc build ${HANDLER} -m --v8-cache -o lib/
COPY . .
# make prisma external to bundle - https://github.com/prisma/prisma/issues/16901#issuecomment-1362940774 \
# TODO: remove when custom dockerfile support is available
RUN \
--mount=type=cache,target=/tmp/ncc-cache \
ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client -t
FROM node:alpine as final
WORKDIR /usr/app
RUN apk update && \
apk add --no-cache ca-certificates && \
update-ca-certificates
COPY --from=build "package.json" "package.json"
COPY package.json *.lock *-lock.json ./
COPY --from=build "node_modules/" "node_modules/"
RUN set -ex && \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \
rm -rf /tmp/.cache
COPY --from=build lib/ /
# Copy any other non-ignored assets to be included
COPY . .
ENTRYPOINT ["node", "index.js"]`,
COPY --from=build /usr/app/lib/ ./lib/
# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
RUN test -d ./prisma && npx prisma generate || echo "";
ENTRYPOINT ["node", "lib/index.js"]`,
},
{
name: "go",
Expand Down Expand Up @@ -133,7 +148,8 @@ ENTRYPOINT python $HANDLER
{
name: "js",
handler: "functions/list.js",
wantFwriter: `FROM "node:alpine"
wantFwriter: `# syntax=docker/dockerfile:1
FROM node:alpine
ARG HANDLER
ENV HANDLER=${HANDLER}
Expand All @@ -152,7 +168,13 @@ COPY . .
RUN yarn import || echo Lockfile already exists
RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN \
set -ex; \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \
rm -rf /tmp/.cache; \
# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
test -d ./prisma && npx prisma generate || echo "";
ENTRYPOINT node $HANDLER
`,
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ var _ Runtime = &golang{}
//go:embed golang.dockerfile
var golangDockerfile string

func (t *golang) BuildIgnore() []string {
return commonIgnore
func (t *golang) BuildIgnore(additional ...string) []string {
return append(commonIgnore, additional...)
}

func (t *golang) BaseDockerFile(w io.Writer) error {
Expand Down
11 changes: 9 additions & 2 deletions pkg/runtime/javascript.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM "node:alpine"
# syntax=docker/dockerfile:1
FROM node:alpine

ARG HANDLER
ENV HANDLER=${HANDLER}
Expand All @@ -17,6 +18,12 @@ COPY . .

RUN yarn import || echo Lockfile already exists

RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN \
set -ex; \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \
rm -rf /tmp/.cache; \
# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
test -d ./prisma && npx prisma generate || echo "";

ENTRYPOINT node $HANDLER
4 changes: 2 additions & 2 deletions pkg/runtime/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (t *javascript) ContainerName() string {
return strings.Replace(filepath.Base(t.handler), filepath.Ext(t.handler), "", 1)
}

func (t *javascript) BuildIgnore() []string {
return javascriptIgnoreList
func (t *javascript) BuildIgnore(additional ...string) []string {
return append(javascriptIgnoreList, additional...)
}

func (t *javascript) BaseDockerFile(w io.Writer) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/runtime/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func (t *python) ContainerName() string {
return strings.Replace(filepath.Base(t.handler), filepath.Ext(t.handler), "", 1)
}

func (t *python) BuildIgnore() []string {
return append(commonIgnore, "__pycache__/", "*.py[cod]", "*$py.class")
func (t *python) BuildIgnore(additional ...string) []string {
baseIgnores := append(commonIgnore, additional...)
return append(baseIgnores, "__pycache__/", "*.py[cod]", "*$py.class")
}

func (t *python) BaseDockerFile(w io.Writer) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

type Runtime interface {
ContainerName() string
BuildIgnore() []string
BuildIgnore(additional ...string) []string
BaseDockerFile(w io.Writer) error
BuildArgs() map[string]string
}
Expand Down
43 changes: 29 additions & 14 deletions pkg/runtime/typescript.dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
# syntax=docker/dockerfile:1
FROM node:alpine as build

ARG HANDLER

# Python and make are required by certain native package build processes in NPM packages.
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 make g++ && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
RUN apk add g++ make py3-pip

RUN yarn global add typescript @vercel/ncc

COPY . .
WORKDIR /usr/app

COPY package.json *.lock *-lock.json /

RUN yarn import || echo Lockfile already exists
RUN yarn import || echo ""

RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN set -ex && \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \
rm -rf /tmp/.cache

RUN test -f tsconfig.json || echo "{\"compilerOptions\":{\"esModuleInterop\":true,\"target\":\"es2015\",\"moduleResolution\":\"node\"}}" > tsconfig.json

RUN ncc build ${HANDLER} -m --v8-cache -o lib/
COPY . .

# make prisma external to bundle - https://github.com/prisma/prisma/issues/16901#issuecomment-1362940774 \
# TODO: remove when custom dockerfile support is available
RUN \
--mount=type=cache,target=/tmp/ncc-cache \
ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client -t

FROM node:alpine as final

WORKDIR /usr/app

RUN apk update && \
apk add --no-cache ca-certificates && \
update-ca-certificates

COPY --from=build "package.json" "package.json"

COPY --from=build "node_modules/" "node_modules/"
COPY package.json *.lock *-lock.json ./

COPY --from=build lib/ /
RUN set -ex && \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache && \
rm -rf /tmp/.cache

# Copy any other non-ignored assets to be included
COPY . .

ENTRYPOINT ["node", "index.js"]
COPY --from=build /usr/app/lib/ ./lib/

# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
RUN test -d ./prisma && npx prisma generate || echo "";

ENTRYPOINT ["node", "lib/index.js"]
4 changes: 2 additions & 2 deletions pkg/runtime/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (t *typescript) ContainerName() string {
return strings.Replace(filepath.Base(t.handler), filepath.Ext(t.handler), "", 1)
}

func (t *typescript) BuildIgnore() []string {
return javascriptIgnoreList
func (t *typescript) BuildIgnore(additional ...string) []string {
return append(javascriptIgnoreList, additional...)
}

func (t *typescript) BuildArgs() map[string]string {
Expand Down

0 comments on commit 5ce52ae

Please sign in to comment.