Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to unit #79

Merged
merged 15 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 72 additions & 20 deletions containers/fastapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,86 @@
FROM docker.io/mambaorg/micromamba:1.5.1-jammy
FROM docker.io/mambaorg/micromamba:2.0.2-ubuntu24.04
#FROM docker.io/mambaorg/micromamba:1.5.1-jammy

LABEL maintainer="trygveas@met.no"

ENV MAPGEN_REPO=https://github.com/metno/mapgen-fastapi.git \
MAPGEN_VERSION=main

COPY ./app /app
# Install dependencies:
COPY --chown=$MAMBA_USER:$MAMBA_USER environment.yaml /tmp/environment.yaml
COPY --chown=$MAMBA_USER:$MAMBA_USER containers/fastapi/environment.yaml /tmp/environment.yaml
RUN micromamba install -y -n base --file /tmp/environment.yaml

USER root
RUN apt-get update && apt-get install -y --no-install-recommends \
git lsof less\
&& rm -rf /var/lib/apt/lists/*
RUN set -ex \
&& ln -s /opt/conda/bin/curl /usr/bin/curl \
&& savedAptMark="$(apt-mark showmanual)" \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y ca-certificates git lsof less build-essential libssl-dev libpcre2-dev pkg-config procps \
&& mkdir -p /usr/lib/unit/modules /usr/lib/unit/debug-modules \
&& mkdir -p /usr/src/unit \
&& cd /usr/src/unit \
&& git clone --depth 1 -b 1.33.0-1 https://github.com/nginx/unit \
&& cd unit \
&& NCPU="$(getconf _NPROCESSORS_ONLN)" \
&& DEB_HOST_MULTIARCH="$(dpkg-architecture -q DEB_HOST_MULTIARCH)" \
&& CC_OPT="$(DEB_BUILD_MAINT_OPTIONS="hardening=+all,-pie" DEB_CFLAGS_MAINT_APPEND="-Wp,-D_FORTIFY_SOURCE=2 -fPIC $(dpkg-buildflags --get CFLAGS)")" \
&& LD_OPT="$(DEB_BUILD_MAINT_OPTIONS="hardening=+all,-pie" DEB_LDFLAGS_MAINT_APPEND="-Wl,--as-needed -pie" dpkg-buildflags --get LDFLAGS)" \
&& CONFIGURE_ARGS_MODULES="--prefix=/usr \
--statedir=/var/lib/unit \
--control=unix:/var/run/unit/control.unit.sock \
--runstatedir=/var/run/unit \
--pid=/var/run/unit/unit.pid \
--logdir=/var/log/unit \
--log=/var/log/unit/unit.log \
--tmpdir=/var/tmp \
--user=$MAMBA_USER \
--group=$MAMBA_USER \
--openssl \
--libdir=/usr/lib/$DEB_HOST_MULTIARCH" \
&& CONFIGURE_ARGS="$CONFIGURE_ARGS_MODULES \
--njs" \
&& make -j $NCPU -C pkg/contrib .njs \
&& export PKG_CONFIG_PATH=$(pwd)/pkg/contrib/njs/build \
&& ./configure $CONFIGURE_ARGS --cc-opt="$CC_OPT" --ld-opt="$LD_OPT" --modulesdir=/usr/lib/unit/debug-modules --debug \
&& make -j $NCPU unitd \
&& install -pm755 build/sbin/unitd /usr/sbin/unitd-debug \
&& make clean \
&& ./configure $CONFIGURE_ARGS --cc-opt="$CC_OPT" --ld-opt="$LD_OPT" --modulesdir=/usr/lib/unit/modules \
&& make -j $NCPU unitd \
&& install -pm755 build/sbin/unitd /usr/sbin/unitd \
&& make clean \
&& /bin/true \
&& ./configure $CONFIGURE_ARGS_MODULES --cc-opt="$CC_OPT" --modulesdir=/usr/lib/unit/debug-modules --debug \
&& ./configure python --config=/opt/conda/bin/python3-config \
&& make -j $NCPU python3-install \
&& make clean \
&& ./configure $CONFIGURE_ARGS_MODULES --cc-opt="$CC_OPT" --modulesdir=/usr/lib/unit/modules \
&& ./configure python --config=/opt/conda/bin/python3-config \
&& make -j $NCPU python3-install \
&& cd \
&& rm -rf /usr/src/unit \
&& for f in /usr/sbin/unitd /usr/lib/unit/modules/*.unit.so; do \
ldd $f | awk '/=>/{print $(NF-1)}' | while read n; do dpkg-query -S `basename $n`; done | sed 's/^\([^:]\+\):.*$/\1/' | sort | uniq >> /requirements.apt; \
done \
&& apt-mark showmanual | xargs apt-mark auto > /dev/null \
&& { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } \
&& /bin/true \
&& mkdir -p /var/lib/unit/ \
&& mkdir -p /docker-entrypoint.d/ \
&& apt-get update \
&& apt-get --no-install-recommends --no-install-suggests -y install $(cat /requirements.apt) \
&& apt-get purge -y --auto-remove build-essential \
&& rm -rf /var/lib/apt/lists/* \
&& rm -f /requirements.apt

USER $MAMBA_USER
RUN /opt/conda/bin/pip install "git+${MAPGEN_REPO}@${MAPGEN_VERSION}" xncml

COPY --chown=$MAMBA_USER:$MAMBA_USER ./start.sh /start.sh

COPY --chown=$MAMBA_USER:$MAMBA_USER ./gunicorn_conf.py /gunicorn_conf.py

COPY --chown=$MAMBA_USER:$MAMBA_USER ./start-reload.sh /start-reload.sh
RUN chmod +x /start.sh \
&& chmod +x /start-reload.sh

USER root
WORKDIR /app

EXPOSE 80

# Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations)
# And then will start Gunicorn with Uvicorn
CMD ["/start.sh"]
ENV LD_LIBRARY_PATH=/usr/local/lib:/opt/conda/lib:/usr/local/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu
COPY ./docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN mkdir -pv /usr/share/unit/welcome && chown -R $MAMBA_USER:$MAMBA_USER /usr/share/unit
COPY --chown=$MAMBA_USER:$MAMBA_USER welcome.* /usr/share/unit/welcome/
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["unitd", "--no-daemon", "--control", "unix:/var/run/unit/control.unit.sock", "--statedir", "/var/lib/unit", "--pid", "/var/run/unit/unit.pid", "--log", "/proc/1/fd/1"]
35 changes: 35 additions & 0 deletions containers/fastapi/Dockerfile-http
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM docker.io/mambaorg/micromamba:1.5.1-jammy

LABEL maintainer="trygveas@met.no"

ENV MAPGEN_REPO=https://github.com/metno/mapgen-fastapi.git \
MAPGEN_VERSION=httpserver

COPY ./app /app
# Install dependencies:
COPY --chown=$MAMBA_USER:$MAMBA_USER environment.yaml /tmp/environment.yaml
RUN micromamba install -y -n base --file /tmp/environment.yaml

USER root
RUN apt-get update && apt-get install -y --no-install-recommends \
git lsof less\
&& rm -rf /var/lib/apt/lists/*
USER $MAMBA_USER
RUN /opt/conda/bin/pip install "git+${MAPGEN_REPO}@${MAPGEN_VERSION}" xncml

#COPY --chown=$MAMBA_USER:$MAMBA_USER ./start.sh /start.sh

#COPY --chown=$MAMBA_USER:$MAMBA_USER ./gunicorn_conf.py /gunicorn_conf.py

#COPY --chown=$MAMBA_USER:$MAMBA_USER ./start-reload.sh /start-reload.sh
#RUN chmod +x /start.sh \
# && chmod +x /start-reload.sh

WORKDIR /app

#EXPOSE 80

# Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations)
# And then will start Gunicorn with Uvicorn
#CMD ["/start.sh"]
CMD ["/opt/conda/bin/python", "/opt/conda/lib/python3.12/site-packages/mapgen/main.py"]
14 changes: 8 additions & 6 deletions containers/fastapi/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ name: base
channels:
- conda-forge
dependencies:
- python=3.11
- python=3.12
- pip
- wheel
- Jinja2
- gunicorn
#- gunicorn
- pyyaml
- xarray=2024.6.0
- xarray=2024.9.0 #=2024.6.0
- boto3
- netcdf4
- jinja2
- pytest-mock
- rasterio
- pandas
- satpy=0.49.0
- mapserver=8.0.2
- gdal=3.9.0
- satpy=0.52.1
- mapserver=8.2.2
- gdal=3.9.2 #3.9.0
- libgdal=3.9.2
- cartopy
- metpy
- lxml
- cmocean
- pyspectral
- curl
104 changes: 104 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/bin/sh

set -e

WAITLOOPS=5
SLEEPSEC=1

curl_put()
{
RET=$(/usr/bin/curl -s -w '%{http_code}' -X PUT --data-binary @$1 --unix-socket /var/run/unit/control.unit.sock http://localhost/$2)
RET_BODY=$(echo $RET | /bin/sed '$ s/...$//')
RET_STATUS=$(echo $RET | /usr/bin/tail -c 4)
if [ "$RET_STATUS" -ne "200" ]; then
echo "$0: Error: HTTP response status code is '$RET_STATUS'"
echo "$RET_BODY"
return 1
else
echo "$0: OK: HTTP response status code is '$RET_STATUS'"
echo "$RET_BODY"
fi
return 0
}

if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
mkdir -pv /var/lib/unit
mkdir -pv /var/run/unit
mkdir -pv /var/log/unit
if /usr/bin/find "/var/lib/unit/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
echo "$0: /var/lib/unit/ is not empty, skipping initial configuration..."
else
echo "$0: Launching Unit daemon to perform initial configuration..."
/usr/sbin/$1 --control unix:/var/run/unit/control.unit.sock --log /proc/1/fd/1 --statedir /var/lib/unit --pid /var/run/unit/unit.pid

for i in $(/usr/bin/seq $WAITLOOPS); do
if [ ! -S /var/run/unit/control.unit.sock ]; then
echo "$0: Waiting for control socket to be created..."
/bin/sleep $SLEEPSEC
else
break
fi
done
# even when the control socket exists, it does not mean unit has finished initialisation
# this curl call will get a reply once unit is fully launched
/usr/bin/curl -s -X GET --unix-socket /var/run/unit/control.unit.sock http://localhost/

if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
echo "$0: /docker-entrypoint.d/ is not empty, applying initial configuration..."

echo "$0: Looking for certificate bundles in /docker-entrypoint.d/..."
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.pem"); do
echo "$0: Uploading certificates bundle: $f"
curl_put $f "certificates/$(basename $f .pem)"
done

echo "$0: Looking for JavaScript modules in /docker-entrypoint.d/..."
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.js"); do
echo "$0: Uploading JavaScript module: $f"
curl_put $f "js_modules/$(basename $f .js)"
done

echo "$0: Looking for configuration snippets in /docker-entrypoint.d/..."
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.json"); do
echo "$0: Applying configuration $f";
curl_put $f "config"
done

echo "$0: Looking for shell scripts in /docker-entrypoint.d/..."
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.sh"); do
echo "$0: Launching $f";
"$f"
done

# warn on filetypes we don't know what to do with
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -not -name "*.sh" -not -name "*.json" -not -name "*.pem" -not -name "*.js"); do
echo "$0: Ignoring $f";
done
else
echo "$0: /docker-entrypoint.d/ is empty, creating 'welcome' configuration..."
curl_put /usr/share/unit/welcome/welcome.json "config"
fi

echo "$0: Stopping Unit daemon after initial configuration..."
kill -TERM $(/bin/cat /var/run/unit/unit.pid)

for i in $(/usr/bin/seq $WAITLOOPS); do
if [ -S /var/run/unit/control.unit.sock ]; then
echo "$0: Waiting for control socket to be removed..."
/bin/sleep $SLEEPSEC
else
break
fi
done
if [ -S /var/run/unit/control.unit.sock ]; then
kill -KILL $(/bin/cat /var/run/unit/unit.pid)
rm -f /var/run/unit/control.unit.sock
fi

echo
echo "$0: Unit initial configuration complete; ready for start up..."
echo
fi
fi

exec "$@"
Loading
Loading