Last active
May 14, 2019 13:09
-
-
Save DuaelFr/c7fe6465218a086f0c1400cd68828ed2 to your computer and use it in GitHub Desktop.
Script that takes cores from Drupal config and tries to create them via Solr REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Return error codes if they happen. | |
set -e | |
# Working directory. | |
# Helper to let you run the install script from anywhere. | |
currentscriptpath () { | |
SOURCE="${BASH_SOURCE[0]}" | |
# resolve $SOURCE until the file is no longer a symlink | |
while [ -h "$SOURCE" ]; do | |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
SOURCE="$(readlink "$SOURCE")" | |
# if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located | |
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" | |
done | |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
echo $DIR | |
} | |
# Preparing vars. | |
SCRIPT_PATH=$(currentscriptpath) | |
APP_ROOT="$SCRIPT_PATH/../.." | |
CONF_PATH="$APP_ROOT/config/sync" | |
# Loading settings files. | |
source $APP_ROOT/scripts/settings.sh | |
if [ -f $APP_ROOT/.env ]; then | |
source $APP_ROOT/.env | |
fi | |
# More vars. | |
SOLR_URL="http://${SOLR_HOST:=localhost}:${SOLR_PORT:=8983}${SOLR_PATH:=/solr}" | |
# Test Solr URL. | |
TEST_RES=$(curl -sIN "$SOLR_URL/" | head -n 1 | awk '{print $2}') | |
echo -n "Trying to reach ${SOLR_URL}/ to check connectivity... " | |
if [[ "$TEST_RES" != "200" ]]; then | |
echo "Error ${TEST_RES}" | |
exit 1 | |
else | |
echo "OK" | |
fi | |
# Get all the cores we need for our application. | |
CORES=$(grep core $CONF_PATH/search_api.server.* | sed -e 's/^.*core: //' | sort | uniq) | |
for CORE in $CORES; do | |
echo -n "Trying to create the '${CORE}' Solr core... " | |
# Check core existence. | |
PING_URL="$SOLR_URL/${CORE}/admin/ping" | |
PING_RES=$(curl -sIN ${PING_URL} | head -n 1 | awk '{print $2}') | |
if [[ "$PING_RES" != "200" ]]; then | |
# Create core. | |
CREATE_URL="$SOLR_URL/admin/cores?action=CREATE&name=${CORE}&configSet=mfr&instanceDir=${CORE}" | |
CREATE_RES=$(curl -sIN ${CREATE_URL} | head -n 1 | awk '{print $2}') | |
if [[ "$CREATE_RES" == "200" ]]; then | |
echo "OK, created" | |
else | |
echo "Error (${CREATE_RES})" | |
echo "Used URL: ${CREATE_URL}" | |
exit 1 | |
fi | |
else | |
echo "OK, already exists" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment