From 1342a87922b1e07e4c99b15bf41ac75def09a283 Mon Sep 17 00:00:00 2001 From: ali-encord <112628737+ali-encord@users.noreply.github.com> Date: Fri, 5 May 2023 17:06:58 +0100 Subject: [PATCH] Ar/fixing fastapi ea endpoint (#378) * adding health check endpoint - GKE ingress needs for checking service status * changing endpoint path as static already exist for streamlit app * updating Dockerfile file to work with new fastapi endpoint --- Dockerfile | 47 +++++++++++++++++++++++-------- README.md | 2 +- docs/docs/getting-started.md | 2 +- docs/docs/installation.mdx | 4 +-- run.sh | 27 ++++++++++++++++++ src/encord_active/server/main.py | 7 ++++- src/encord_active/server/utils.py | 2 +- 7 files changed, 74 insertions(+), 17 deletions(-) create mode 100755 run.sh diff --git a/Dockerfile b/Dockerfile index 2fb94e29e..c12929e9c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,30 +2,55 @@ FROM python:3.9-slim ARG POETRY_VERSION="1.2.2" -WORKDIR /app +RUN groupadd --gid 1000 appuser \ + && useradd --uid 1000 --gid 1000 -ms /bin/bash appuser -COPY .env pyproject.toml poetry.lock README.md /app -COPY ./src/ /app/src - -ENV PYTHONPATH=${PYTHONPATH}:${PWD} +RUN pip3 install --no-cache-dir --upgrade \ + pip \ + virtualenv RUN apt-get update && apt-get install -y \ build-essential \ curl \ software-properties-common \ git \ - ffmpeg + ffmpeg \ + sqlite3 RUN apt-get -y clean \ && rm -rf /var/lib/apt/lists/* -RUN pip install . -WORKDIR /data +USER appuser +WORKDIR /home/appuser + +RUN curl -sSL https://install.python-poetry.org | \ + python - --version ${POETRY_VERSION} + +ENV PATH="/home/appuser/.local/bin:$PATH" + +COPY .env pyproject.toml poetry.lock /home/appuser + +RUN poetry cache clear pypi --all +RUN poetry config virtualenvs.create true \ + && poetry config virtualenvs.in-project true \ + && poetry config experimental.new-installer true \ + && poetry install --no-root --without dev + +COPY ./src/ /home/appuser/src +COPY README.md /home/appuser + +RUN poetry install --only-root EXPOSE 8501 -ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0 +COPY run.sh /home/appuser + +USER root +RUN mkdir /data && chown appuser: /data -R +USER appuser + +RUN git config --global --add safe.directory '*' +EXPOSE 8000 -HEALTHCHECK CMD ecord-active --version -ENTRYPOINT ["encord-active"] +ENTRYPOINT ["./run.sh"] diff --git a/README.md b/README.md index e2b961cb5..9f26c5a4b 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ encord-active quickstart or you can use : ```shell -docker run -it --rm -p 8501:8501 -v ${PWD}:/data encord/encord-active quickstart +docker run -it --rm -p 8501:8501 -p 8000:8000 -v ${PWD}:/data encord/encord-active quickstart ``` After opening the UI, we recommend you to head to the [workflow documentation][encord-active-docs-workflow] to see some common workflows. diff --git a/docs/docs/getting-started.md b/docs/docs/getting-started.md index b9bfa2ff5..58dd39713 100644 --- a/docs/docs/getting-started.md +++ b/docs/docs/getting-started.md @@ -33,7 +33,7 @@ If you have trouble installing `encord-active`, you can find more detailed instr You can also run the docker image instead of install Encord Active ```shell -docker run -it -p 8501:8501 -v ${PWD}:/data encord/encord-active +docker run -it -p 8501:8501 -p 8000:8000 -v ${PWD}:/data encord/encord-active ``` ::: diff --git a/docs/docs/installation.mdx b/docs/docs/installation.mdx index b555420b8..c10004858 100644 --- a/docs/docs/installation.mdx +++ b/docs/docs/installation.mdx @@ -60,7 +60,7 @@ To learn more about how to use the command line interface, see the [Command Line We also provide a docker image which works exctly the same as the cli. ```shell -docker run -it --rm -p 8501:8501 -v ${PWD}:/data encord/encord-active +docker run -it --rm -p 8501:8501 -p 8000:8000 -v ${PWD}:/data encord/encord-active ``` This will mount your current working directory, so everything that happens inside the docker container will persist after it is done. @@ -70,7 +70,7 @@ This will mount your current working directory, so everything that happens insid If you intend to use Encord Active with an Encord Annotate project you'll need to mount a voulume with your SSH key as well. ```shell -docker run -it --rm -p 8501:8501 -v ${PWD}:/data -v ${HOME}/.ssh:/root/.ssh encord/encord-active +docker run -it --rm -p 8501:8501 -p 8000:8000 -v ${PWD}:/data -v ${HOME}/.ssh:/root/.ssh encord/encord-active ``` Then, when asked for your SSH key, you can point to `~/.ssh/` diff --git a/run.sh b/run.sh new file mode 100755 index 000000000..29bb9deb7 --- /dev/null +++ b/run.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +APP_PID= +stopRunningProcess() { + # Based on https://linuxconfig.org/how-to-propagate-a-signal-to-child-processes-from-a-bash-script + if test ! "${APP_PID}" = '' && ps -p ${APP_PID} > /dev/null ; then + > /proc/1/fd/1 echo "Stopping ${COMMAND_PATH} which is running with process ID ${APP_PID}" + + kill -TERM ${APP_PID} + > /proc/1/fd/1 echo "Waiting for ${COMMAND_PATH} to process SIGTERM signal" + + wait ${APP_PID} + > /proc/1/fd/1 echo "All processes have stopped running" + else + > /proc/1/fd/1 echo "${COMMAND_PATH} was not started when the signal was sent or it has already been stopped" + fi +} + +trap stopRunningProcess EXIT TERM + +source .venv/bin/activate + +encord-active visualize --target /data + +APP_ID=${!} + +wait ${APP_ID} diff --git a/src/encord_active/server/main.py b/src/encord_active/server/main.py index feead19a5..28e220b3c 100644 --- a/src/encord_active/server/main.py +++ b/src/encord_active/server/main.py @@ -48,7 +48,7 @@ allow_headers=["*"], ) -app.mount("/static", StaticFiles(directory=path), name="static") +app.mount("/ea-static", StaticFiles(directory=path), name="static") async def get_project_file_structure(project: str) -> ProjectFileStructure: @@ -187,3 +187,8 @@ def search(project: ProjectFileStructureDep, query: str, type: SearchType, scope "ids": [item.identifier for item in result.result_identifiers], "snippet": snippet, } + + +@app.get("/health") +def health_check() -> bool: + return True diff --git a/src/encord_active/server/utils.py b/src/encord_active/server/utils.py index a0869ed0d..34ce3d250 100644 --- a/src/encord_active/server/utils.py +++ b/src/encord_active/server/utils.py @@ -49,7 +49,7 @@ def _get_url(label_row_structure: LabelRowStructure, du_hash: str, frame: str): label_row_structure.iter_data_unit(du_hash), None ) if data_unit: - return f"static/{parse.quote(data_unit.path.relative_to(label_row_structure.path.parents[2]).as_posix())}" + return f"ea-static/{parse.quote(data_unit.path.relative_to(label_row_structure.path.parents[2]).as_posix())}" def _transform_object(object_: dict):