Skip to content

Commit

Permalink
Consolidate start commands
Browse files Browse the repository at this point in the history
  • Loading branch information
arledesma committed May 11, 2021
1 parent db39317 commit a8f733b
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 179 deletions.
149 changes: 72 additions & 77 deletions compose/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ ARG STAGE=production
COPY ./requirements /requirements
# install build dependencies
RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \
apk update \
&& apk add --no-cache build-base \
# psycopg2 dependencies
&& apk add --no-cache --virtual build-deps gcc python3-dev musl-dev \
&& apk add --no-cache postgresql-dev \
# Pillow dependencies
&& apk add --no-cache jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
&& apk add --no-cache libffi-dev py-cffi \
# XLSX dependencies
&& apk add --no-cache libxml2-dev libxslt-dev \
# Rust and Cargo required by the ``cryptography`` Python package - only required during build
&& apk add --no-cache rust \
&& apk add --no-cache cargo \
# && pip install -r /requirements/${STAGE}.txt \
# build wheels
&& pip install wheel && pip wheel --wheel-dir=/tmp/wheels -r /requirements/${STAGE}.txt \
# remove the virtual package group 'build-deps'
&& apk del build-deps
apk update \
&& apk add --no-cache build-base \
# psycopg2 dependencies
&& apk add --no-cache --virtual build-deps gcc python3-dev musl-dev \
&& apk add --no-cache postgresql-dev \
# Pillow dependencies
&& apk add --no-cache jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
&& apk add --no-cache libffi-dev py-cffi \
# XLSX dependencies
&& apk add --no-cache libxml2-dev libxslt-dev \
# Rust and Cargo required by the ``cryptography`` Python package - only required during build
&& apk add --no-cache rust \
&& apk add --no-cache cargo \
# && pip install -r /requirements/${STAGE}.txt \
# build wheels
&& pip install wheel && pip wheel --wheel-dir=/tmp/wheels -r /requirements/${STAGE}.txt \
# remove the virtual package group 'build-deps'
&& apk del build-deps
# ---------------------------------------------
# END build image stage
# ---------------------------------------------
Expand All @@ -48,38 +48,38 @@ ENV PYTHONPATH="$PYTHONPATH:/app/config"
ARG USER_UID=1000
ARG USER_GID=$USER_UID
RUN if [ -n "$(getent group ${USER_GID})" ]; \
then \
apk --no-cache add shadow; \
groupmod -n "django" "${USER_GID}"; \
else \
addgroup --gid "${USER_GID}" "django"; \
fi && \
if [ -n "$(getent passwd ${USER_UID})" ]; \
then \
apk --no-cache add shadow; \
usermod -l "django" -g "${USER_GID}" -d "/app"; \
else \
adduser \
--home "/app" \
--gecos "MAIL_DIR=/dev/null" \
--shell /bin/ash \
--ingroup "django" \
--system \
--disabled-password \
--no-create-home \
--uid "${USER_UID}" \
"django"; \
fi
then \
apk --no-cache add shadow; \
groupmod -n "django" "${USER_GID}"; \
else \
addgroup --gid "${USER_GID}" "django"; \
fi && \
if [ -n "$(getent passwd ${USER_UID})" ]; \
then \
apk --no-cache add shadow; \
usermod -l "django" -g "${USER_GID}" -d "/app"; \
else \
adduser \
--home "/app" \
--gecos "MAIL_DIR=/dev/null" \
--shell /bin/ash \
--ingroup "django" \
--system \
--disabled-password \
--no-create-home \
--uid "${USER_UID}" \
"django"; \
fi

# install runtime dependencies. `add --no-cache` performs an apk update, adds packages and excludes caching
# in order to not require deletion of apk cache.
RUN apk add --no-cache postgresql-dev \
# Pillow dependencies
jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
libffi-dev py-cffi \
# XLSX dependencies
libxml2-dev libxslt-dev
# Pillow dependencies
jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
libffi-dev py-cffi \
# XLSX dependencies
libxml2-dev libxslt-dev

# ensure that we are using the same requirements from the build stage
COPY --from=build /requirements /tmp/requirements
Expand All @@ -88,11 +88,11 @@ COPY --from=build /requirements /tmp/requirements

# combine build and ${STAGE}.txt - remove --no-binary to installing our own wheels
RUN --mount=type=bind,target=/tmp/wheels,source=/tmp/wheels,from=build \
--mount=type=cache,mode=0755,target=/root/.cache/pip \
du -shc /tmp/wheels \
&& ( cat /tmp/requirements/base.txt; sed -e 's/--no-binary.*//' -e 's/^-r .*//' /tmp/requirements/${STAGE}.txt ) | tee /tmp/requirements.txt >/dev/null \
&& pip install --find-links=/tmp/wheels -r /tmp/requirements.txt \
&& rm -rf /tmp/requirements /tmp/requirements.txt
--mount=type=cache,mode=0755,target=/root/.cache/pip \
du -shc /tmp/wheels \
&& ( cat /tmp/requirements/base.txt; sed -e 's/--no-binary.*//' -e 's/^-r .*//' /tmp/requirements/${STAGE}.txt ) | tee /tmp/requirements.txt >/dev/null \
&& pip install --find-links=/tmp/wheels -r /tmp/requirements.txt \
&& rm -rf /tmp/requirements /tmp/requirements.txt
# ---------------------------------------------
# END django image stage
# ---------------------------------------------
Expand All @@ -106,18 +106,17 @@ FROM django as django-production
COPY --chown=django . /app

# copy the entrypoint and run scripts
RUN cp -ap /app/compose/django/entrypoint /entrypoint \
&& cp -vap /app/compose/django/start /start \
&& cp -vap /app/compose/django/queue/start /start-queue \
&& cp -vap /app/compose/django/seed_data /seed_data \
&& chmod -v 0755 /entrypoint /start /start-queue /seed_data \
# remove all carriage returns in the case that a user checks out the files on a windows system
# and has their git core.eol set to native or crlf
&& sed -i 's/\r$//g' /entrypoint /start /start-queue /seed_data \
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
# so that it is correctly propagated to the named volume
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
RUN for target in /app/compose/django/*; \
do ln "$target" /"$(basename "$target")" \
&& chmod +X /"$(basename "$target")" \
# remove all carriage returns in the case that a user checks out the files on a windows system
# and has their git core.eol set to native or crlf
&& sed -i 's/\r$//g' /entrypoint /start /seed_data; \
done \
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
# so that it is correctly propagated to the named volume
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
# ---------------------------------------------
# END production stage
# ---------------------------------------------
Expand All @@ -128,21 +127,17 @@ RUN cp -ap /app/compose/django/entrypoint /entrypoint \
FROM django as django-local

# add our application
COPY --chown=django ./compose/django/entrypoint /entrypoint
COPY --chown=django ./compose/django/start-dev /start
COPY --chown=django ./compose/django/queue/start /start-queue
COPY --chown=django ./compose/django/seed_data /seed_data

COPY --chown=django ./compose/django/ /

# copy the entrypoint and run scripts
RUN chmod -v 0755 /entrypoint /start /start-queue /seed_data \
# remove all carriage returns in the case that a user checks out the files on a windows system
# and has their git core.eol set to native or crlf
&& sed -i 's/\r$//g' /entrypoint /start /start-queue /seed_data \
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
# so that it is correctly propagated to the named volume
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
RUN chmod -v 0755 /entrypoint /start /seed_data \
# remove all carriage returns in the case that a user checks out the files on a windows system
# and has their git core.eol set to native or crlf
&& sed -i 's/\r$//g' /entrypoint /start /seed_data \
# due to volumes mounted to these locations we must created and set the ownership of the underlying directory
# so that it is correctly propagated to the named volume
&& mkdir -p "/app/ghostwriter/media" "/app/staticfiles" \
&& chown -R "django": "/app/ghostwriter/media" "/app/staticfiles"
# ---------------------------------------------
# END local stage
# ---------------------------------------------
Expand Down
7 changes: 0 additions & 7 deletions compose/django/queue/start

This file was deleted.

82 changes: 68 additions & 14 deletions compose/django/start
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,75 @@ set -o errexit
set -o pipefail
set -o nounset

AVATAR_DIR=/app/ghostwriter/media/user_avatars
EVIDENCE_DIR=/app/ghostwriter/media/evidence
TEMPLATE_DIR=/app/ghostwriter/media/templates
readonly AVATAR_DIR=/app/ghostwriter/media/user_avatars
readonly EVIDENCE_DIR=/app/ghostwriter/media/evidence
readonly TEMPLATE_DIR=/app/ghostwriter/media/templates

TEMPLATE_PATH_DOCX=/app/ghostwriter/reporting/templates/reports/template.docx
TEMPLATE_PATH_PPTX=/app/ghostwriter/reporting/templates/reports/template.pptx
readonly TEMPLATE_PATH_DOCX=/app/ghostwriter/reporting/templates/reports/template.docx
readonly TEMPLATE_PATH_PPTX=/app/ghostwriter/reporting/templates/reports/template.pptx

[[ ! -d "$EVIDENCE_DIR" ]] && mkdir -p "$EVIDENCE_DIR" && chown -R django "$EVIDENCE_DIR"
[[ ! -d "$AVATAR_DIR" ]] && mkdir -p "$AVATAR_DIR" && chown -R django "$AVATAR_DIR"
[[ ! -d "$TEMPLATE_DIR" ]] && mkdir -p "$TEMPLATE_DIR" && chown -R django "$TEMPLATE_DIR"
function create_dir {
local -r target_directory="$1";
local -r target_user="${2:-django}";

cp -u -p "$TEMPLATE_PATH_DOCX" "$TEMPLATE_DIR"
cp -u -p "$TEMPLATE_PATH_PPTX" "$TEMPLATE_DIR"
if test -d "$target_directory";
then return 0;
fi

python /app/manage.py collectstatic --noinput
python /app/manage.py migrate
# /usr/local/bin/daphne -b 0.0.0.0 -p 5000 config.asgi:application
uvicorn config.asgi:application --host 0.0.0.0 --port 5000
mkdir -p "$target_directory";
chown -R "$target_user": "$target_directory";
}

function prepare {
create_dir "$EVIDENCE_DIR" "${USER:-django}";
create_dir "$AVATAR_DIR" "${USER:-django}";
create_dir "$TEMPLATE_DIR" "${USER:-django}";

cp -u -p "$TEMPLATE_PATH_DOCX" "${TEMPLATE_DIR}/";
cp -u -p "$TEMPLATE_PATH_PPTX" "${TEMPLATE_DIR}/";
}

function queue {
python manage.py qcluster;
}

function live {
python /app/manage.py collectstatic --noinput;
python /app/manage.py migrate;
# /usr/local/bin/daphne -b 0.0.0.0 -p 5000 config.asgi:application
uvicorn config.asgi:application --host 0.0.0.0 --port 5000;
}

function dev {
python manage.py makemigrations;
python manage.py migrate;
# python manage.py runserver 0.0.0.0:8000
uvicorn config.asgi:application --host 0.0.0.0 --reload;
}

function debug {
python -m pip install --upgrade pip;
python manage.py makemigrations;
python manage.py migrate;
pip install debugpy -t /tmp;
python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading;
}

function main {
local -r target="${1:-live}";

prepare;
case "$target" in
live | debug | dev)
prepare;
$target;
;;
queue )
$target;
;;
*)
echo >&2 "Unknown target: $target";
exit 1;
;;
esac
}
24 changes: 0 additions & 24 deletions compose/django/start-dev

This file was deleted.

25 changes: 0 additions & 25 deletions compose/local/django/start_debug

This file was deleted.

Loading

0 comments on commit a8f733b

Please sign in to comment.