Commit c730d4c1 authored by Exbrayat Cédric's avatar Exbrayat Cédric Committed by Exbrayat Cédric
Browse files

chore: make index script compatible for macOS users

With these modifications, a macOS developer can:

- install the required packages via Homebrew
- run the index script (made OS agnostic by using dat/gdate, sed/gsed, readling/greadlink)
parent 8879bd8b
......@@ -54,6 +54,9 @@ You need to install:
- a recent enough JDK8
The docker images need quite a bit of resources,
so make sure you have at least 4g of RAM configured (Docker Desktop / Resources / Memory).
The application expects to connect on an Elasticsearch instance running on `http://127.0.0.1:9200`.
To have such an instance, simply run:
......@@ -175,7 +178,14 @@ Before all, if you have cloned the repository without fetching the data (see [Da
### Portability
Feedback related to portability on MacOS and other GNU/Linux distro is really welcomed. For MacOS, care to use latest GNU Parallel and Bash v4 versions, not the version provided by default via Brew.
Feedback related to portability on MacOS and other GNU/Linux distro is really welcomed.
For MacOS, care to use latest GNU Parallel and Bash v4 versions, not the version provided by default via Brew.
Install the following packages to be able to run the scripts:
```sh
brew install gnu-sed coreutils parallel
```
Harvesting (i.e. importing JSON documents into Elasticsearch) consists in creating the necessary index and aliases and Elasticsearch templates.
......
......@@ -75,7 +75,12 @@ if [ -z "$ES_HOST" ] || [ -z "$ES_PORT" ] || [ -z "$APP_NAME" ] || [ -z "$APP_EN
exit 4
fi
DATE_TMSTP=$(date -d @${TIMESTAMP})
DATE_CMD="date"
if [[ $OSTYPE == darwin* ]]; then
# Use gdate on the mac
DATE_CMD="gdate"
fi
DATE_TMSTP=$(${DATE_CMD} -d @${TIMESTAMP})
[ $? != 0 ] && { echo -e "Given timestamp ($TIMESTAMP) is malformed and cannot be transformed to a valid date." ; exit 1; }
echo "Using timestamp corresponding to date: ${DATE_TMSTP}"
......
......@@ -75,7 +75,12 @@ while [ -n "$1" ]; do
done
BASEDIR=$(dirname "$0")
DATADIR=$(readlink -f "$BASEDIR/../data/$APP_NAME/")
READLINK_CMD="readlink"
if [[ $OSTYPE == darwin* ]]; then
# Use greadlink on the mac
READLINK_CMD="greadlink"
fi
DATADIR=$("${READLINK_CMD}" -f "$BASEDIR/../data/$APP_NAME/")
if [ -z "$APP_NAME" ] || [ -z "$APP_ENV" ]; then
echo -e "${RED}ERROR: -app and -env parameters are mandatory!${NC}"
......@@ -88,7 +93,12 @@ if [ -z "$ES_HOST" ]; then
exit 4
fi
DATE_TMSTP=$(date -d @${TIMESTAMP})
DATE_CMD="date"
if [[ $OSTYPE == darwin* ]]; then
# Use gdate on the mac
DATE_CMD="gdate"
fi
DATE_TMSTP=$(${DATE_CMD} -d @${TIMESTAMP})
[ $? != 0 ] && { echo -e "Given timestamp ($TIMESTAMP) is malformed and cannot be transformed to a valid date." ; exit 1; }
echo "Using timestamp corresponding to date: ${DATE_TMSTP}"
......@@ -165,7 +175,12 @@ EOF
exit $code
}
#set -x
PREVIOUS_TIMESTAMP=$(curl -s "${ES_HOST}:${ES_PORT}/_cat/indices/${APP_NAME}*${APP_ENV}-tmstp*" | sed -r "s/.*-tmstp([0-9]+).*/\1/g" | sort -ru | head -2 | tail -1) # current timestamp index has already been created so looking for the 2nd last one
SED_CMD="sed"
if [[ $OSTYPE == darwin* ]]; then
# Use gsed on the mac
SED_CMD="gsed"
fi
PREVIOUS_TIMESTAMP=$(curl -s "${ES_HOST}:${ES_PORT}/_cat/indices/${APP_NAME}*${APP_ENV}-tmstp*" | "${SED_CMD}" -r "s/.*-tmstp([0-9]+).*/\1/g" | sort -ru | head -2 | tail -1) # current timestamp index has already been created so looking for the 2nd last one
{
echo -e "Updating aliases for latest resources and suggestions indices with timestamp ${TIMESTAMP} instead of previous ${PREVIOUS_TIMESTAMP}..."
curl -s -H 'Content-Type: application/json' -XPOST "${ES_HOST}:${ES_PORT}/_aliases?pretty" --data-binary '@-' <<EOF
......
......@@ -73,7 +73,12 @@ curl -s -m 5 ${ES_HOST}:${ES_PORT} > /dev/null
}
# Get previous existing timestamp
PREVIOUS_TIMESTAMP=$(curl -s "${ES_HOST}:${ES_PORT}/_cat/indices/${APP_NAME}*${APP_ENV}-tmstp*" | sed -r "s/.*-tmstp([0-9]+).*/\1/g" | sort -ru | head -1) # no index yet created with current timestamp. So using the latest as previous timestamp.
SED_CMD="sed"
if [[ $OSTYPE == darwin* ]]; then
# Use gsed on the mac
SED_CMD="gsed"
fi
PREVIOUS_TIMESTAMP=$(curl -s "${ES_HOST}:${ES_PORT}/_cat/indices/${APP_NAME}*${APP_ENV}-tmstp*" | "${SED_CMD}" -r "s/.*-tmstp([0-9]+).*/\1/g" | sort -ru | head -1) # no index yet created with current timestamp. So using the latest as previous timestamp.
# Create index, aliases with their mapping
sh "${BASEDIR}"/createIndexAndAliases4CI.sh -host "$ES_HOST" -port "$ES_PORT" -app "$APP_NAME" -env "$APP_ENV" -timestamp "$TIMESTAMP"
......@@ -91,7 +96,12 @@ CODE=$?
# Clean if asked
[ $CLEAN != 0 ] && {
echo -e "Found previous timestamp: ${GREEN}${PREVIOUS_TIMESTAMP}${NC}"
date -d "@${PREVIOUS_TIMESTAMP}" &>/dev/null
DATE_CMD="date"
if [[ $OSTYPE == darwin* ]]; then
# Use gdate on the mac
DATE_CMD="gdate"
fi
"${DATE_CMD}" -d "@${PREVIOUS_TIMESTAMP}" &>/dev/null
if [ $? != 0 ]; then
echo -e "Cannot clean previous indices and aliases because no valid previous timestamp has been found: ${ORANGE}${PREVIOUS_TIMESTAMP}${NC}."
else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment