From e71a3447a703fe870101719022a4fe9ee8e95ba3 Mon Sep 17 00:00:00 2001 From: Murat Ursavas Date: Fri, 27 Jan 2023 09:35:31 +0300 Subject: [PATCH] Added production parameter recognition feature (#468) --- .github/workflows/compile-docker.yml | 2 ++ Dockerfile | 49 +++++++++++++++------------- setenv.sh | 48 +++++++++++++++++++++++++++ start_diagnostics.sh | 6 ++++ 4 files changed, 82 insertions(+), 23 deletions(-) create mode 100755 setenv.sh create mode 100755 start_diagnostics.sh diff --git a/.github/workflows/compile-docker.yml b/.github/workflows/compile-docker.yml index 5b0a3473..e4509855 100644 --- a/.github/workflows/compile-docker.yml +++ b/.github/workflows/compile-docker.yml @@ -48,6 +48,8 @@ jobs: push: true labels: ${{ steps.meta.outputs.labels }} tags: ${{ steps.meta.outputs.tags }} + cache-from: type=registry,ref=nebraltd/hm-diag:buildcache + cache-to: type=registry,ref=nebraltd/hm-diag:buildcache,mode=max - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/Dockerfile b/Dockerfile index 871fd09d..7a4c36e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,21 +2,24 @@ #################################################################################################### ################################## Stage: builder ################################################## - -FROM balenalib/raspberry-pi-debian-python:bullseye-build-20221215 as builder +FROM balenalib/raspberry-pi-debian-python:bullseye-build-20221215 AS builder ENV PYTHON_DEPENDENCIES_DIR=/opt/python-dependencies RUN mkdir /tmp/build -COPY ./ /tmp/build +COPY quectel/ /tmp/build/quectel +COPY hw_diag/ /tmp/build/hw_diag +COPY bigquery/ /tmp/build/bigquery +COPY requirements.txt /tmp/build/requirements.txt +COPY setup.py /tmp/build/setup.py +COPY MANIFEST.in /tmp/build/MANIFEST.in WORKDIR /tmp/build RUN \ install_packages \ build-essential \ - libdbus-glib-1-dev - -RUN pip3 install --no-cache-dir --target="$PYTHON_DEPENDENCIES_DIR" . + libdbus-glib-1-dev && \ + pip3 install --no-cache-dir --target="$PYTHON_DEPENDENCIES_DIR" . # firehose build, the tar is obtained from quectel. # there is no install target in Makefile, doing manual copy @@ -31,8 +34,7 @@ RUN make && \ #################################################################################################### ################################### Stage: runner ################################################## - -FROM balenalib/raspberry-pi-debian-python:bullseye-run-20221215 as runner +FROM balenalib/raspberry-pi-debian-python:bullseye-run-20221215 AS runner ENV PYTHON_DEPENDENCIES_DIR=/opt/python-dependencies @@ -48,16 +50,6 @@ WORKDIR /opt/ # Import gpg key COPY keys/manufacturing-key.gpg ./ -RUN gpg --import manufacturing-key.gpg -RUN rm manufacturing-key.gpg - -# @TODO: Re-enable health-check once Balena supports it fully. -# HEALTHCHECK \ -# --interval=120s \ -# --timeout=5s \ -# --start-period=15s \ -# --retries=10 \ -# CMD wget -q -O - http://0.0.0.0:5000/initFile.txt || exit 1 # Copy packages from builder COPY --from=builder "$PYTHON_DEPENDENCIES_DIR" "$PYTHON_DEPENDENCIES_DIR" @@ -69,15 +61,26 @@ COPY --from=builder /usr/sbin/QFirehose /usr/sbin/QFirehose COPY --from=builder /tmp/build/quectel /quectel # copy db migration files -COPY --from=builder /tmp/build/migrations /opt/migrations/migrations -COPY --from=builder /tmp/build/alembic.ini /opt/migrations/alembic.ini +COPY migrations/ /opt/migrations/migrations +COPY alembic.ini /opt/migrations/alembic.ini # copy start admin session script -COPY --from=builder /tmp/build/start_admin_session /usr/sbin/start_admin_session -RUN chmod 700 /usr/sbin/start_admin_session +COPY start_admin_session /usr/sbin/start_admin_session + +# Getting RUN layers together +RUN gpg --import manufacturing-key.gpg && \ + rm manufacturing-key.gpg && \ + chmod 700 /usr/sbin/start_admin_session && \ + mkdir -p /opt/nebra # Add python dependencies to PYTHONPATH ENV PYTHONPATH="${PYTHON_DEPENDENCIES_DIR}:${PYTHONPATH}" ENV PATH="${PYTHON_DEPENDENCIES_DIR}/bin:${PATH}" -ENTRYPOINT ["gunicorn", "--bind", "0.0.0.0:5000", "--timeout", "300", "hw_diag.wsgi:wsgi_app"] +# Copy environment variables startup script +COPY setenv.sh /opt/nebra/setenv.sh + +# Copy container startup script +COPY start_diagnostics.sh /opt/start_diagnostics.sh + +ENTRYPOINT ["/opt/start_diagnostics.sh"] diff --git a/setenv.sh b/setenv.sh new file mode 100755 index 00000000..93ff58ee --- /dev/null +++ b/setenv.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +base_path="/var/nebra/envvars" + +# Create necessary folder if it doesn't exist +if [ ! -d $base_path ]; then + mkdir -p $base_path +fi + +create_file_if_not_exists() { + local name="$1" + local contents="$2" + local fullpath="${base_path}/${name}" + + if [ ! -f "${fullpath}" ]; then + echo "${name} parameter file doesn't exist. Setting it as ${contents} now."; + echo "${contents}" > "${fullpath}" + fi +} + +export_param_if_not_exists() { + local name="$1" + local fullpath="${base_path}/${name}" + + if [ -f "${fullpath}" ]; then + contents=$(<"${fullpath}"); + export "${name}"="${contents}" + echo "${name} is set as ${contents}."; + else + echo "Error: Cannot set ${name} variable."; + fi +} + +#FREQ +if [ -z ${FREQ+x} ]; then + export_param_if_not_exists FREQ +else + echo "FREQ variable is already set as ${FREQ}."; + create_file_if_not_exists FREQ "${FREQ}" +fi + +#VARIANT +if [ -z ${VARIANT+x} ]; then + export_param_if_not_exists VARIANT +else + echo "VARIANT variable is already set as ${VARIANT}."; + create_file_if_not_exists VARIANT "${VARIANT}" +fi diff --git a/start_diagnostics.sh b/start_diagnostics.sh new file mode 100755 index 00000000..57bf3977 --- /dev/null +++ b/start_diagnostics.sh @@ -0,0 +1,6 @@ +#! /bin/bash + +# shellcheck source=/dev/null +source /opt/nebra/setenv.sh + +gunicorn --bind 0.0.0.0:5000 --timeout 300 hw_diag.wsgi:wsgi_app