Skip to content

Commit

Permalink
consolidate python dockerfiles (#3717)
Browse files Browse the repository at this point in the history
  • Loading branch information
fliepeltje committed Jul 9, 2024
1 parent 92b9ea9 commit b57e7fb
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 90 deletions.
28 changes: 21 additions & 7 deletions scanner/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import (
"github.com/superfly/flyctl/terminal"
)

type PyDepStyle string

const (
Poetry PyDepStyle = "poetry"
Pipenv PyDepStyle = "pipenv"
Pep621 PyDepStyle = "pep621"
Pip PyDepStyle = "pip"
)

type PyApp string

const (
Expand Down Expand Up @@ -48,6 +57,7 @@ type PyCfg struct {
pyVersion string
appName string
deps []string
depStyle PyDepStyle
}

func findEntrypoint(dep string) *os.File {
Expand Down Expand Up @@ -128,33 +138,37 @@ func intoSource(cfg PyCfg) (*SourceInfo, error) {
return nil, nil
}
}
vars[string(cfg.depStyle)] = true
objectStorage := slices.Contains(cfg.deps, "boto3") || slices.Contains(cfg.deps, "boto")
if app == "" {
terminal.Warn("No supported Python frameworks found")
return nil, nil
} else if app == FastAPI {
vars["fastapi"] = true
return &SourceInfo{
Files: templatesExecute("templates/python-fastapi", vars),
Files: templatesExecute("templates/python-docker", vars),
Family: "FastAPI",
Port: 8000,
ObjectStorageDesired: objectStorage,
}, nil
} else if app == Flask {
vars["flask"] = true
return &SourceInfo{
Files: templatesExecute("templates/python-flask-poetry", vars),
Files: templatesExecute("templates/python-docker", vars),
Family: "Flask",
Port: 8080,
ObjectStorageDesired: objectStorage,
}, nil
} else if app == Streamlit {
vars["streamlit"] = true
entrypoint := findEntrypoint("streamlit")
if entrypoint == nil {
return nil, nil
} else {
vars["entrypoint"] = entrypoint.Name()
}
return &SourceInfo{
Files: templatesExecute("templates/python-streamlit", vars),
Files: templatesExecute("templates/python-docker", vars),
Family: "Streamlit",
Port: 8501,
ObjectStorageDesired: objectStorage,
Expand Down Expand Up @@ -192,7 +206,7 @@ func configPoetry(sourceDir string, _ *ScannerConfig) (*SourceInfo, error) {
pyVersion := deps["python"].(string)
pyVersion = strings.TrimPrefix(pyVersion, "^")
pyVersion = parsePyDep(pyVersion)
cfg := PyCfg{pyVersion, appName, depList}
cfg := PyCfg{pyVersion, appName, depList, Poetry}
return intoSource(cfg)
}

Expand Down Expand Up @@ -232,7 +246,7 @@ func configPyProject(sourceDir string, _ *ScannerConfig) (*SourceInfo, error) {
})
}

cfg := PyCfg{pyVersion, appName, depList}
cfg := PyCfg{pyVersion, appName, depList, Pep621}
return intoSource(cfg)
}

Expand Down Expand Up @@ -263,7 +277,7 @@ func configPipfile(sourceDir string, _ *ScannerConfig) (*SourceInfo, error) {
return nil, err
}
appName := filepath.Base(sourceDir)
cfg := PyCfg{pyVersion, appName, depList}
cfg := PyCfg{pyVersion, appName, depList, Pipenv}
return intoSource(cfg)
}

Expand Down Expand Up @@ -299,7 +313,7 @@ func configRequirements(sourceDir string, _ *ScannerConfig) (*SourceInfo, error)
return nil, err
}
appName := filepath.Base(sourceDir)
cfg := PyCfg{pyVersion, appName, depList}
cfg := PyCfg{pyVersion, appName, depList, Pip}
return intoSource(cfg)
}

Expand Down
49 changes: 49 additions & 0 deletions scanner/templates/python-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FROM python:{{ .pyVersion }} AS builder

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1

WORKDIR /app
COPY . .

{{ if .pipenv -}}

ENV PIPENV_VENV_IN_PROJECT=1 \
PIPENV_CUSTOM_VENV_NAME=.venv
RUN pip install pipenv
RUN pipenv install

{{ else if .poetry -}}

RUN pip install poetry
RUN poetry config virtualenvs.in-project true
RUN poetry install

{{ else if .pep621 -}}

RUN python -m venv .venv && \
. .venv/bin/activate && \
pip install --upgrade pip && \
pip install .

{{ else if .pip }}

RUN python -m venv .venv && \
. .venv/bin/activate && \
pip install --upgrade pip && \
pip install -r requirements.txt

{{ end -}}

FROM python:{{ .pyVersion }}-slim

WORKDIR /app
COPY --from=builder /app .

{{ if .flask -}}
CMD ["/app/.venv/bin/flask", "run", "--host=0.0.0.0", "--port=8080"]
{{ else if .fastapi -}}
CMD ["/app/.venv/bin/fastapi", "run"]
{{ else if .streamlit -}}
CMD ["/app/.venv/bin/streamlit", "run", "{{ .entrypoint }}"]
{{ end -}}
25 changes: 0 additions & 25 deletions scanner/templates/python-fastapi/Dockerfile

This file was deleted.

4 changes: 0 additions & 4 deletions scanner/templates/python-flask-poetry/.dockerignore

This file was deleted.

25 changes: 0 additions & 25 deletions scanner/templates/python-flask-poetry/Dockerfile

This file was deleted.

4 changes: 0 additions & 4 deletions scanner/templates/python-streamlit/.dockerignore

This file was deleted.

25 changes: 0 additions & 25 deletions scanner/templates/python-streamlit/Dockerfile

This file was deleted.

0 comments on commit b57e7fb

Please sign in to comment.