Skip to content

Commit

Permalink
Recover app bootstrapping in Python entrypoints (#5152)
Browse files Browse the repository at this point in the history
Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
  • Loading branch information
helderco committed May 18, 2023
1 parent 50bea15 commit 03ee4e4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
32 changes: 12 additions & 20 deletions core/pythonruntime.go
Expand Up @@ -17,39 +17,31 @@ func (p *Project) pythonRuntime(ctx context.Context, subpath string, gw bkgw.Cli
return nil, err
}
workdir := "/src"
ctrSrcPath := filepath.Join(workdir, filepath.Dir(p.ConfigPath), subpath)
entrypointScript := fmt.Sprintf(`#!/bin/sh
appdir := filepath.Join(workdir, filepath.Dir(p.ConfigPath), subpath)
entrypoint := fmt.Sprintf(`#!/bin/sh
set -exu
# go to the workdir
cd %q
# run the extension
python3 main.py "$@"
exec python -m dagger.server "$@"
`,
ctrSrcPath)
requirementsfile := filepath.Join(ctrSrcPath, "requirements.txt")
appdir)

return NewDirectorySt(ctx,
llb.Merge([]llb.State{
llb.Image("python:3.10-alpine", llb.WithMetaResolver(gw)).
Run(llb.Shlex(`apk add --no-cache file git openssh-client socat`)).Root(),
llb.Image("python:3.11-alpine", llb.WithMetaResolver(gw)).
Run(llb.Shlex(`apk add --no-cache file git openssh-client`)).Root(),
llb.Scratch().
File(llb.Copy(contextState, p.Directory.Dir, "/src")),
}).
// FIXME(samalba): Install python dependencies not as root
// FIXME(samalba): errors while installing requirements.txt will be ignored because of the `|| true`. Need to find a better way.
Run(llb.Shlex(
fmt.Sprintf(
`sh -c 'test -f %q && python3 -m pip install --cache-dir=/root/.cache/pipcache -r %q || true'`,
requirementsfile, requirementsfile,
)),
llb.Dir(workdir),
llb.AddMount("/src", contextState, llb.SourcePath(p.Directory.Dir)),
Run(
llb.Shlex("python -m pip install -r requirements.txt"),
llb.Dir(appdir),
llb.AddMount(
"/root/.cache/pipcache",
"/root/.cache/pip",
llb.Scratch(),
llb.AsPersistentCacheDir("pythonpipcache", llb.CacheMountShared),
),
).
File(llb.Mkfile("/entrypoint", 0755, []byte(entrypointScript))),
File(llb.Mkfile("/entrypoint", 0755, []byte(entrypoint))),
"",
pipeline.Path{},
platform,
Expand Down
5 changes: 0 additions & 5 deletions demos/python-entrypoints/main.py
Expand Up @@ -5,9 +5,6 @@
import dagger
from dagger.server import Server

# TODO: probably not needed
from dagger.server.cli import app


@strawberry.type
class Hello:
Expand Down Expand Up @@ -40,5 +37,3 @@ def hello(self) -> Hello:

server = Server(schema, debug=True)

# TODO: probably not needed
app(prog_name="dagger-server-py")
2 changes: 1 addition & 1 deletion demos/python-entrypoints/requirements.txt
@@ -1 +1 @@
dagger-io[server]
-e ../../sdk/python[server]
2 changes: 1 addition & 1 deletion sdk/python/experimental/hello/requirements.txt
@@ -1 +1 @@
dagger-io
dagger-io[server]
4 changes: 3 additions & 1 deletion sdk/python/src/dagger/__main__.py
@@ -1,3 +1,5 @@
import sys

from .cli import app

app(prog_name="dagger-py")
sys.exit(app(prog_name="dagger-py"))
5 changes: 5 additions & 0 deletions sdk/python/src/dagger/server/__main__.py
@@ -0,0 +1,5 @@
import sys

from .cli import app

sys.exit(app(prog_name="dagger-server-py"))

0 comments on commit 03ee4e4

Please sign in to comment.