From e656a3c46a2264f70998c5b338bfd8b07631226e Mon Sep 17 00:00:00 2001 From: Shyam Sunder Date: Fri, 28 Aug 2020 14:43:10 -0400 Subject: [PATCH] server/docker: unify test and main Dockerfiles --- .pre-commit-config.yaml | 16 ++++----- client/Dockerfile | 2 +- server/Dockerfile | 68 +++++++++++++++++++++++++++---------- server/Dockerfile.test | 50 --------------------------- server/hooks/test | 2 +- server/szurubooru/config.py | 9 +++-- 6 files changed, 66 insertions(+), 81 deletions(-) delete mode 100644 server/Dockerfile.test diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2b35471c..d2448172 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,21 +1,21 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.4.0 + rev: v3.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - repo: https://github.com/psf/black - rev: stable + rev: 20.8b1 hooks: - id: black files: server/szurubooru/ language_version: python3.8 - repo: https://github.com/timothycrosley/isort - rev: '4.3.21-2' + rev: '5.4.2' hooks: - id: isort files: server/szurubooru/ @@ -24,7 +24,7 @@ repos: - toml - repo: https://github.com/prettier/prettier - rev: '2.0.5' + rev: '2.1.1' hooks: - id: prettier files: client/js/ @@ -32,7 +32,7 @@ repos: args: ['--config', 'client/.prettierrc.yml'] - repo: https://github.com/pre-commit/mirrors-eslint - rev: v7.1.0 + rev: v7.7.0 hooks: - id: eslint files: client/js/ @@ -41,7 +41,7 @@ repos: - eslint-config-prettier - repo: https://gitlab.com/pycqa/flake8 - rev: '3.8.2' + rev: '3.8.3' hooks: - id: flake8 files: server/szurubooru/ @@ -69,7 +69,7 @@ repos: - id: pytest name: pytest - entry: bash -c 'docker run --rm -t $(docker build -f server/Dockerfile.test -q server/) szurubooru/' + entry: bash -c 'docker run --rm -t $(docker build --target testing -q server/) szurubooru/' language: system types: [python] files: server/szurubooru/ @@ -79,7 +79,7 @@ repos: - id: pytest-cov name: pytest - entry: bash -c 'docker run --rm -t $(docker build -f server/Dockerfile.test -q server/) --cov-report=term-missing:skip-covered --cov=szurubooru szurubooru/' + entry: bash -c 'docker run --rm -t $(docker build --target testing -q server/) --cov-report=term-missing:skip-covered --cov=szurubooru szurubooru/' language: system types: [python] files: server/szurubooru/ diff --git a/client/Dockerfile b/client/Dockerfile index 8039ed29..f3b99558 100644 --- a/client/Dockerfile +++ b/client/Dockerfile @@ -22,7 +22,7 @@ WORKDIR /var/www COPY --from=builder /opt/app/public/ . -FROM nginx:alpine +FROM nginx:alpine as release RUN apk --no-cache add dumb-init COPY --from=approot / / diff --git a/server/Dockerfile b/server/Dockerfile index 50cd700c..17c174c1 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,13 +1,12 @@ -FROM alpine:3.12 +ARG ALPINE_VERSION=3.12 + + +FROM alpine:$ALPINE_VERSION as prereqs WORKDIR /opt/app -RUN \ - apk --no-cache add \ +RUN apk --no-cache add \ python3 \ - dumb-init \ ffmpeg \ - py3-waitress \ - py3-setuptools \ py3-pip \ # from requirements.txt: py3-yaml \ @@ -19,30 +18,63 @@ RUN \ py3-pynacl \ py3-tz \ py3-pyrfc3339 \ - && \ - pip3 install --no-cache-dir --disable-pip-version-check \ + && pip3 install --no-cache-dir --disable-pip-version-check \ alembic \ "coloredlogs==5.0" \ youtube-dl \ - && apk --no-cache del py3-pip + && apk --no-cache del py3-pip + +COPY ./ /opt/app/ +RUN rm -rf /opt/app/szurubooru/tests + + +FROM prereqs as testing +WORKDIR /opt/app + +RUN apk --no-cache add \ + py3-pip \ + py3-pytest \ + py3-pytest-cov \ + postgresql \ + && pip3 install --no-cache-dir --disable-pip-version-check \ + pytest-pgsql \ + freezegun \ + && apk --no-cache del py3-pip \ + && addgroup app \ + && adduser -SDH -h /opt/app -g '' -G app app \ + && chown app:app /opt/app + +COPY --chown=app:app ./szurubooru/tests /opt/app/szurubooru/tests/ + +ENV TEST_ENVIRONMENT="true" +USER app +ENTRYPOINT ["pytest", "--tb=short"] +CMD ["szurubooru/"] + + +FROM prereqs as release +WORKDIR /opt/app ARG PUID=1000 ARG PGID=1000 -RUN \ - # Set users - mkdir -p /opt/app /data && \ - addgroup -g ${PGID} app && \ - adduser -SDH -h /opt/app -g '' -G app -u ${PUID} app && \ - chown -R app:app /opt/app /data -USER app -COPY --chown=app:app ./ /opt/app/ +RUN apk --no-cache add \ + dumb-init \ + py3-setuptools \ + py3-waitress \ + && mkdir -p /opt/app /data \ + && addgroup -g ${PGID} app \ + && adduser -SDH -h /opt/app -g '' -G app -u ${PUID} app \ + && chown -R app:app /opt/app /data + +USER app +CMD ["/opt/app/docker-start.sh"] ARG PORT=6666 ENV PORT=${PORT} EXPOSE ${PORT} + VOLUME ["/data/"] -CMD ["/opt/app/docker-start.sh"] ARG DOCKER_REPO ARG BUILD_DATE diff --git a/server/Dockerfile.test b/server/Dockerfile.test deleted file mode 100644 index 16c61218..00000000 --- a/server/Dockerfile.test +++ /dev/null @@ -1,50 +0,0 @@ -FROM alpine:3.12 -WORKDIR /opt/app - -RUN \ - apk --no-cache add \ - python3 \ - ffmpeg \ - py3-pip \ - # from requirements.txt: - py3-yaml \ - py3-psycopg2 \ - py3-sqlalchemy \ - py3-certifi \ - py3-numpy \ - py3-pillow \ - py3-pynacl \ - py3-tz \ - py3-pyrfc3339 \ - # for testing - py3-pytest \ - py3-pytest-cov \ - postgresql \ - && \ - pip3 install --no-cache-dir --disable-pip-version-check \ - alembic \ - "coloredlogs==5.0" \ - youtube-dl \ - # for testing - pytest-pgsql \ - freezegun \ - && apk --no-cache del py3-pip - -ARG PUID=1000 -ARG PGID=1000 -RUN \ - # Set users - mkdir -p /opt/app /data && \ - addgroup -g ${PGID} app && \ - adduser -SDH -h /opt/app -g '' -G app -u ${PUID} app && \ - chown -R app:app /opt/app /data -USER app - -ENV POSTGRES_HOST=x \ - POSTGRES_USER=x \ - POSTGRES_PASSWORD=x - -COPY --chown=app:app ./ /opt/app/ - -ENTRYPOINT ["pytest", "--tb=short"] -CMD ["szurubooru/"] diff --git a/server/hooks/test b/server/hooks/test index 1edf7016..377139ca 100755 --- a/server/hooks/test +++ b/server/hooks/test @@ -2,7 +2,7 @@ set -e docker run --rm \ - -t $(docker build -f ${DOCKERFILE_PATH:-Dockerfile}.test -q .) \ + -t $(docker build --target testing -q .) \ --color=no szurubooru/ exit $? diff --git a/server/szurubooru/config.py b/server/szurubooru/config.py index a0fc3e7a..1515a54f 100644 --- a/server/szurubooru/config.py +++ b/server/szurubooru/config.py @@ -22,9 +22,12 @@ def _merge(left: Dict, right: Dict) -> Dict: def _docker_config() -> Dict: - for key in ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_HOST"]: - if not os.getenv(key, False): - raise errors.ConfigError(f'Environment variable "{key}" not set') + if "TEST_ENVIRONMENT" not in os.environ: + for key in ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_HOST"]: + if key not in os.environ: + raise errors.ConfigError( + f'Environment variable "{key}" not set' + ) return { "debug": True, "show_sql": int(os.getenv("LOG_SQL", 0)),