Skip to content

Commit

Permalink
Merge pull request #21 from GPP-Woo/feature/10-index-document-metadata
Browse files Browse the repository at this point in the history
Index registration documents.
  • Loading branch information
sergei-maertens authored Feb 12, 2025
2 parents 4fe351e + 0d8dfc2 commit 1e731e0
Show file tree
Hide file tree
Showing 46 changed files with 1,208 additions and 19 deletions.
8 changes: 2 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,15 @@ RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-reco

WORKDIR /app
COPY ./bin/docker_start.sh /start.sh
# Uncomment if you use celery
# COPY ./bin/celery_worker.sh /celery_worker.sh
# COPY ./bin/celery_beat.sh /celery_beat.sh
# COPY ./bin/celery_flower.sh /celery_flower.sh
COPY ./bin/celery_worker.sh ./bin/celery_beat.sh ./bin/celery_flower.sh ./bin/wait_for_it.sh /
RUN mkdir /app/bin /app/log /app/media

VOLUME ["/app/log", "/app/media"]

# copy backend build deps
COPY --from=backend-build /usr/local/lib/python3.12 /usr/local/lib/python3.12
COPY --from=backend-build /usr/local/bin/uwsgi /usr/local/bin/uwsgi
# Uncomment if you use celery
# COPY --from=backend-build /usr/local/bin/celery /usr/local/bin/celery
COPY --from=backend-build /usr/local/bin/celery /usr/local/bin/celery
COPY --from=backend-build /app/src/ /app/src/

# copy frontend build statics
Expand Down
4 changes: 1 addition & 3 deletions bin/celery_beat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ LOGLEVEL=${CELERY_LOGLEVEL:-INFO}
mkdir -p celerybeat

echo "Starting celery beat"
exec celery beat \
--app woo_search \
exec celery --workdir src --app woo_search.celery beat \
-l $LOGLEVEL \
--workdir src \
-s ../celerybeat/beat
5 changes: 1 addition & 4 deletions bin/celery_worker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ QUEUE=${1:-${CELERY_WORKER_QUEUE:=celery}}
WORKER_NAME=${2:-${CELERY_WORKER_NAME:="${QUEUE}"@%n}}

echo "Starting celery worker $WORKER_NAME with queue $QUEUE"
exec celery worker \
--app woo_search \
exec celery --workdir src --app woo_search.celery worker \
-Q $QUEUE \
-n $WORKER_NAME \
-l $LOGLEVEL \
--workdir src \
-O fair \
-c $CONCURRENCY

12 changes: 12 additions & 0 deletions bin/docker_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ uwsgi_threads=${UWSGI_THREADS:-1}

mountpoint=${SUBPATH:-/}

# Figure out abspath of this script
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")

until pg_isready; do
>&2 echo "Waiting for database connection..."
sleep 1
Expand Down Expand Up @@ -49,6 +53,14 @@ if [ -n "${ODS_SUPERUSER_USERNAME}" ]; then
unset ODS_SUPERUSER_USERNAME ODS_SUPERUSER_EMAIL DJANGO_SUPERUSER_PASSWORD
fi

# run management command to initialize the ES cluster - this is idempotent.
if [ $INIT_ES_INDICES == "true" ]; then
es_host=$(echo "$ELASTICSEARCH_HOST" | sed -E 's|https?://||' | sed 's|/$||')
${SCRIPTPATH}/wait_for_it.sh "$es_host" -t 60 -- echo "ES is up"
python src/manage.py initialize_mappings --wait
unset INIT_ES_INDICES
fi

# Start server
>&2 echo "Starting server"
exec uwsgi \
Expand Down
205 changes: 205 additions & 0 deletions bin/wait_for_it.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available
#
# The MIT License (MIT)
# Copyright (c) 2016 Giles Hall
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Taken from https://github.com/vishnubob/wait-for-it

WAITFORIT_cmdname=${0##*/}

echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

usage()
{
cat << USAGE >&2
Usage:
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}

wait_for()
{
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
else
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
fi
WAITFORIT_start_ts=$(date +%s)
while :
do
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
WAITFORIT_result=$?
else
(echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
WAITFORIT_result=$?
fi
if [[ $WAITFORIT_result -eq 0 ]]; then
WAITFORIT_end_ts=$(date +%s)
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
break
fi
sleep 1
done
return $WAITFORIT_result
}

wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
else
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
fi
WAITFORIT_PID=$!
trap "kill -INT -$WAITFORIT_PID" INT
wait $WAITFORIT_PID
WAITFORIT_RESULT=$?
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
fi
return $WAITFORIT_RESULT
}

# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
WAITFORIT_hostport=(${1//:/ })
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
shift 1
;;
--child)
WAITFORIT_CHILD=1
shift 1
;;
-q | --quiet)
WAITFORIT_QUIET=1
shift 1
;;
-s | --strict)
WAITFORIT_STRICT=1
shift 1
;;
-h)
WAITFORIT_HOST="$2"
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
WAITFORIT_HOST="${1#*=}"
shift 1
;;
-p)
WAITFORIT_PORT="$2"
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
WAITFORIT_PORT="${1#*=}"
shift 1
;;
-t)
WAITFORIT_TIMEOUT="$2"
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
WAITFORIT_TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
WAITFORIT_CLI=("$@")
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done

if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi

WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}

# Check to see if timeout is from busybox?
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)

WAITFORIT_BUSYTIMEFLAG=""
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
WAITFORIT_ISBUSY=1
# Check if busybox timeout uses -t flag
# (recent Alpine versions don't support -t anymore)
if timeout &>/dev/stdout | grep -q -e '-t '; then
WAITFORIT_BUSYTIMEFLAG="-t"
fi
else
WAITFORIT_ISBUSY=0
fi

if [[ $WAITFORIT_CHILD -gt 0 ]]; then
wait_for
WAITFORIT_RESULT=$?
exit $WAITFORIT_RESULT
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
wait_for
WAITFORIT_RESULT=$?
fi
fi

if [[ $WAITFORIT_CLI != "" ]]; then
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
exit $WAITFORIT_RESULT
fi
exec "${WAITFORIT_CLI[@]}"
else
exit $WAITFORIT_RESULT
fi
9 changes: 7 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ services:
test:
[
"CMD-SHELL",
"curl --output /dev/null --silent --head --fail -u elastic:${ES_PASSWORD:-insecure-elastic} http://elasticsearch:9200",
"curl --output /dev/null --silent --head --fail -u elastic:${ES_PASSWORD:-insecure-elastic} http://es:9200",
]
interval: 5s
timeout: 5s
retries: 10
ports:
- "9200:9200"
- 9200:9200
# - "9300:9300"

web: &web_build
Expand All @@ -87,6 +87,10 @@ services:
- DISABLE_2FA=true
- DJANGO_SUPERUSER_PASSWORD=admin
- ALLOWED_HOSTS=*
- INIT_ES_INDICES=true
- ELASTICSEARCH_HOST=http://es:9200/
- ELASTICSEARCH_USER=elastic
- ELASTICSEARCH_PASSWORD=insecure-elastic
# Environment labeling
- SHOW_ENVIRONMENT=yes
- ENVIRONMENT_LABEL=docker-compose
Expand All @@ -103,6 +107,7 @@ services:
depends_on:
- db
- redis
- es

volumes:
db:
Expand Down
40 changes: 40 additions & 0 deletions docker/docker-compose.es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Docker compose for elastic search for (unit) tests

services:
# Based on https://www.elastic.co/guide/en/elasticsearch/reference/current/run-elasticsearch-locally.html
es:
image: docker.elastic.co/elasticsearch/elasticsearch:${ES_VERSION:-8.17.1}
environment:
- discovery.type=single-node
- cluster.name=test-gpp-search
- node.name=test-es01
- node.roles=master,data
- ingest.geoip.downloader.enabled=false
- xpack.security.enabled=false
- xpack.security.http.ssl.enabled=false
# - xpack.license.self_generated.type=trial
- xpack.ml.use_auto_machine_memory_percent=true
- ES_JAVA_OPTS=-Xms${ES_HEAP_INIT:-128m} -Xmx${ES_HEAP_MAX:-2g}
- cluster.routing.allocation.disk.watermark.low=${ES_DISK_SPACE_REQUIRED:-1gb}
- cluster.routing.allocation.disk.watermark.high=${ES_DISK_SPACE_REQUIRED:-1gb}
- cluster.routing.allocation.disk.watermark.flood_stage=${ES_DISK_SPACE_REQUIRED:-1gb}
volumes:
- es-test-data:/usr/share/elasticsearch/data
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test:
[
"CMD-SHELL",
"curl --output /dev/null --silent --head --fail http://es:9200",
]
interval: 5s
timeout: 5s
retries: 10
ports:
- 9201:9200

volumes:
es-test-data:
1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Pure python dependencies
elasticsearch-dsl~=8.0

# Django packages
django-axes[ipware]
Expand Down
Loading

0 comments on commit 1e731e0

Please sign in to comment.