Skip to content

Commit eb4f762

Browse files
committed
fix(ct,solr): repair source and target schema handling #11959
- Reordered some startup checks for readability - Added checks that source schema exists and is readable to permissions checks (if source and target are different) - Cleaned up field application method: - Skip unnecessary additional tempfile - Remove nonsense cp from target to temp - Fix handling of source and target: make source and target equal if no source path has been explicitly given. Otherwise, our logic in copying the source during application of the fields makes no sense, as it would not copy the target!
1 parent bdb349e commit eb4f762

1 file changed

Lines changed: 96 additions & 80 deletions

File tree

conf/solr/solr-driver.sh

Lines changed: 96 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ Environment Variables (used as defaults if command-line options not provided):
185185
WORK_DIR Working directory (default: ${DEFAULT_WORK_DIR})
186186
MODE Execution mode: 'watch' or 'oneshot' (default: ${DEFAULT_MODE})
187187
UPGRADE_MODE Enable upgrade mode: 'true' or 'false' (default: ${DEFAULT_UPGRADE_MODE})
188-
UPGRADE_SOURCE_PATH Template schema path for upgrade mode (default: ${DEFAULT_UPGRADE_SOURCE_PATH})
189188
STARTUP_CHECK Startup check mode: 'fail', 'warn', or 'wait' (default: ${DEFAULT_STARTUP_CHECK})
190189
HEALTH_CHECKS_ENABLED Enable health checks: 'true' or 'false' (default: ${DEFAULT_HEALTH_CHECKS_ENABLED})
191190
LIVENESS_FILE Path to liveness indicator file (default: ${DEFAULT_LIVENESS_FILE})
@@ -229,7 +228,7 @@ Examples:
229228
$0 --mode oneshot --upgrade --schema-source-path /custom/template/schema.xml
230229
231230
# Upgrade mode with custom template via environment
232-
UPGRADE_SOURCE_PATH=/custom/template.xml $0 --mode oneshot --upgrade
231+
SCHEMA_SOURCE_PATH=/custom/template.xml $0 --mode oneshot --upgrade
233232
234233
# Watch mode that waits for services to be ready with custom retry settings
235234
$0 --startup-check wait --wait-retry-period 10 --wait-max-retries 30
@@ -322,38 +321,54 @@ check_update_script() {
322321
# Check read/write permissions
323322
check_permissions() {
324323
local schema_dir
325-
schema_dir="$(dirname "${SCHEMA_TARGET_PATH}")"
324+
325+
# Validate that source schema exists (if given by user)
326+
if [[ "${SCHEMA_SOURCE_PATH}" != "${SCHEMA_TARGET_PATH}" ]]; then
327+
if [[ ! -f "${SCHEMA_SOURCE_PATH}" ]]; then
328+
log_error "Source Schema not found: ${SCHEMA_SOURCE_PATH}"
329+
log_error "Please ensure the template schema exists or use -P to specify a different location"
330+
return 1
331+
fi
332+
if [[ ! -r "${SCHEMA_SOURCE_PATH}" ]]; then
333+
log_error "Source Schema is not readable: ${SCHEMA_SOURCE_PATH}"
334+
return 1
335+
fi
336+
fi
326337

327338
# Check schema directory is writable (for creating backups and updating schema)
339+
schema_dir="$(dirname "${SCHEMA_TARGET_PATH}")"
328340
if [[ ! -d "${schema_dir}" ]]; then
329-
log_error "Schema directory does not exist: ${schema_dir}"
341+
log_error "Target Schema directory does not exist: ${schema_dir}"
330342
return 1
331343
fi
332-
333344
if [[ ! -w "${schema_dir}" ]]; then
334-
log_error "Schema directory is not writable: ${schema_dir}"
345+
log_error "Target Schema directory is not writable: ${schema_dir}"
335346
return 1
336347
fi
348+
log_info "Target Schema directory exists and is writable: ${schema_dir}"
337349

338350
# If schema file exists, check if it's readable and writable
339351
if [[ -f "${SCHEMA_TARGET_PATH}" ]]; then
340352
if [[ ! -r "${SCHEMA_TARGET_PATH}" ]]; then
341-
log_error "Schema file is not readable: ${SCHEMA_TARGET_PATH}"
353+
log_error "Target Schema file is not readable: ${SCHEMA_TARGET_PATH}"
342354
return 1
343355
fi
344-
345356
if [[ ! -w "${SCHEMA_TARGET_PATH}" ]]; then
346-
log_error "Schema file is not writable: ${SCHEMA_TARGET_PATH}"
357+
log_error "Target Schema file is not writable: ${SCHEMA_TARGET_PATH}"
347358
return 1
348359
fi
360+
log_info "Target Schema file is readable and writable: ${SCHEMA_TARGET_PATH}"
361+
362+
# We already checked for the source to exist, so we will copy it later on
363+
elif [[ "${SCHEMA_SOURCE_PATH}" != "${SCHEMA_TARGET_PATH}" ]]; then
364+
log_warn "Target Schema file does not exist yet: ${SCHEMA_TARGET_PATH}"
365+
log_info "Will be created on first update."
349366

350-
log_info "Schema file is readable and writable: ${SCHEMA_TARGET_PATH}"
351367
else
352-
log_warn "Schema file does not exist yet: ${SCHEMA_TARGET_PATH}"
353-
log_info "Will be created on first update"
368+
log_warn "Target Schema file does not exist: ${SCHEMA_TARGET_PATH}"
369+
return 1
354370
fi
355371

356-
log_info "Schema directory is writable: ${schema_dir}"
357372
return 0
358373
}
359374

@@ -576,30 +591,23 @@ apply_field_definitions() {
576591

577592
log_info "Applying field definitions using ${UPDATE_FIELDS_SCRIPT}"
578593

579-
# The update-fields.sh script takes: schema_file source_file
580-
# It modifies the schema file in place, so we need to work with a copy
581-
local temp_schema="${WORK_DIR}/schema.xml.temp"
582-
583594
# Use source schema as base for updates
595+
# NOTE: By default, SCHEMA_SOURCE_PATH == SCHEMA_TARGET_PATH
596+
# NOTE: target_schema != SCHEMA_TARGET_PATH, as we want to work on a copy!
584597
if [[ -f "${SCHEMA_SOURCE_PATH}" ]]; then
585-
log_info "Using source schema as base: ${SCHEMA_SOURCE_PATH}"
586-
cp "${SCHEMA_SOURCE_PATH}" "${temp_schema}"
587-
elif [[ -f "${target_schema}" ]]; then
588-
log_info "Source schema not found at '$SCHEMA_SOURCE_PATH', using target schema as base: ${target_schema}"
589-
cp "${target_schema}" "${temp_schema}"
598+
log_info "Using base schema file from ${SCHEMA_SOURCE_PATH}"
599+
cp "${SCHEMA_SOURCE_PATH}" "${target_schema}"
590600
else
591-
log_error "No base schema file found (neither source '$SCHEMA_SOURCE_PATH' nor target '$target_schema' exist)"
601+
log_error "No base schema file ${SCHEMA_SOURCE_PATH} found"
592602
return 1
593603
fi
594604

595-
if ! "${UPDATE_FIELDS_SCRIPT}" "${temp_schema}" "${metadata_file}"; then
605+
# Run the update script
606+
if ! "${UPDATE_FIELDS_SCRIPT}" "${target_schema}" "${metadata_file}"; then
596607
log_error "Failed to apply field definitions"
597608
return 1
598609
fi
599610

600-
# Move the updated temp schema to the target location
601-
mv "${temp_schema}" "${target_schema}"
602-
603611
log_info "Field definitions applied successfully"
604612
return 0
605613
}
@@ -755,17 +763,13 @@ restore_schema() {
755763

756764
# Process schema update (steps 2-4, optionally 5)
757765
process_schema_update() {
758-
local reload_solr="${1:-false}"
759-
local metadata_file="${WORK_DIR}/metadata_fields.xml"
766+
local metadata_file="${1:-${WORK_DIR}/metadata_fields.xml}"
767+
local reload_solr="${2:-ignore}"
760768
local new_schema="${WORK_DIR}/schema.xml.new"
761769
local backup_file=""
762770
local update_success=false
763771

764-
# Step 2: Download and apply field definitions
765-
if ! fetch_metadata_fields "${metadata_file}"; then
766-
return 1
767-
fi
768-
772+
# Step 2b: Apply downloaded field definitions to a schema file
769773
if ! apply_field_definitions "${metadata_file}" "${new_schema}"; then
770774
return 1
771775
fi
@@ -797,7 +801,7 @@ process_schema_update() {
797801
fi
798802

799803
# Step 5: Reload Solr (only in watch or upgrade mode)
800-
if [[ "${reload_solr}" == "true" ]]; then
804+
if [[ "${reload_solr}" == "reload" ]]; then
801805
if ! reload_solr_core; then
802806
log_error "Solr reload failed, attempting to restore backup"
803807
if [[ -n "${backup_file}" ]]; then
@@ -830,13 +834,21 @@ run_oneshot() {
830834
update_liveness
831835

832836
# In oneshot, default to not reload Solr. But if upgrading, we want to reload.
833-
local reload_solr="false"
837+
local reload_solr="ignore"
834838
if [[ "${UPGRADE_MODE}" == "true" ]]; then
835839
log_info "Will attempt to RELOAD Solr after upgrading the schema."
836-
reload_solr="true"
840+
reload_solr="reload"
841+
fi
842+
843+
# Step 2a: Download field definitions
844+
local metadata_file="${WORK_DIR}/metadata_fields.xml"
845+
if ! fetch_metadata_fields "${metadata_file}"; then
846+
log_error "Oneshot execution failed"
847+
return 1
837848
fi
838849

839-
if process_schema_update "$reload_solr"; then
850+
# Steps 2b, 3, 4 and 5
851+
if process_schema_update "${metadata_file}" "$reload_solr"; then
840852
mark_ready
841853
log_info "Oneshot execution completed successfully"
842854
return 0
@@ -889,7 +901,7 @@ run_watch() {
889901

890902
# Process pending update if needed
891903
if [[ "${needs_update}" == "true" && -n "${pending_metadata_file}" ]]; then
892-
if process_schema_update "true"; then
904+
if process_schema_update "${pending_metadata_file}" "reload"; then
893905
# Update successful - use the stored checksum
894906
last_checksum="${pending_checksum}"
895907
mark_ready
@@ -992,19 +1004,30 @@ main() {
9921004
esac
9931005
done
9941006

995-
# Load secrets from files or environment variables
996-
# Solr authentication
997-
if [[ -n "${SOLR_USERNAME_FILE:-}" && -f "${SOLR_USERNAME_FILE}" ]]; then
998-
SOLR_USERNAME=$(cat "${SOLR_USERNAME_FILE}")
999-
fi
1000-
if [[ -n "${SOLR_PASSWORD_FILE:-}" && -f "${SOLR_PASSWORD_FILE}" ]]; then
1001-
SOLR_PASSWORD=$(cat "${SOLR_PASSWORD_FILE}")
1002-
fi
1007+
# Validate startup check mode
1008+
case "${STARTUP_CHECK}" in
1009+
fail|warn|wait)
1010+
;;
1011+
*)
1012+
log_error "Invalid startup check mode: ${STARTUP_CHECK}. Must be 'fail', 'warn', or 'wait'"
1013+
exit 1
1014+
;;
1015+
esac
10031016

1004-
if [[ -n "${SOLR_USERNAME:-}" && -n "${SOLR_PASSWORD:-}" ]]; then
1005-
SOLR_AUTH_HEADER="Authorization: Basic $(echo -n "${SOLR_USERNAME}:${SOLR_PASSWORD}" | base64 -w 0)"
1006-
log_info "Solr authentication configured (HTTP Basic)"
1007-
fi
1017+
# Validate mode
1018+
case "${MODE}" in
1019+
watch|oneshot)
1020+
;;
1021+
*)
1022+
log_error "Invalid mode: ${MODE}. Must be 'watch' or 'oneshot'"
1023+
exit 1
1024+
;;
1025+
esac
1026+
1027+
# Set metadata endpoint based on Dataverse URL
1028+
METADATA_ENDPOINT="${DATAVERSE_URL}/api/admin/index/solr/schema"
1029+
1030+
# Load secrets from files or environment variables
10081031

10091032
# Dataverse authentication
10101033
# Priority 1: Bearer token (env var or file)
@@ -1039,8 +1062,26 @@ main() {
10391062
fi
10401063
fi
10411064

1042-
# Set metadata endpoint based on Dataverse URL
1043-
METADATA_ENDPOINT="${DATAVERSE_URL}/api/admin/index/solr/schema"
1065+
# Solr authentication
1066+
if [[ -n "${SOLR_USERNAME_FILE:-}" && -f "${SOLR_USERNAME_FILE}" ]]; then
1067+
SOLR_USERNAME=$(cat "${SOLR_USERNAME_FILE}")
1068+
fi
1069+
if [[ -n "${SOLR_PASSWORD_FILE:-}" && -f "${SOLR_PASSWORD_FILE}" ]]; then
1070+
SOLR_PASSWORD=$(cat "${SOLR_PASSWORD_FILE}")
1071+
fi
1072+
1073+
if [[ -n "${SOLR_USERNAME:-}" && -n "${SOLR_PASSWORD:-}" ]]; then
1074+
SOLR_AUTH_HEADER="Authorization: Basic $(echo -n "${SOLR_USERNAME}:${SOLR_PASSWORD}" | base64 -w 0)"
1075+
log_info "Solr authentication configured (HTTP Basic)"
1076+
fi
1077+
1078+
# Handle schema source and upgrade mode
1079+
1080+
# If the schema source has not been explicitly set by the user (independent of any mode),
1081+
# but the target path has been, make sure to make them the same!
1082+
if [[ "${SCHEMA_SOURCE_PATH_SET_BY_USER}" == "false" && "${SCHEMA_TARGET_PATH}" != "${DEFAULT_SCHEMA_PATH}" ]]; then
1083+
SCHEMA_SOURCE_PATH="${SCHEMA_TARGET_PATH}"
1084+
fi
10441085

10451086
# Validate upgrade mode restrictions
10461087
if [[ "${UPGRADE_MODE}" == "true" && "${MODE}" == "watch" ]]; then
@@ -1052,35 +1093,10 @@ main() {
10521093
# Handle upgrade mode: override source path if not explicitly set by user
10531094
if [[ "${UPGRADE_MODE}" == "true" && "${SCHEMA_SOURCE_PATH_SET_BY_USER}" == "false" ]]; then
10541095
log_info "Upgrade mode enabled: using template schema as source"
1055-
SCHEMA_SOURCE_PATH="${UPGRADE_SOURCE_PATH}"
1056-
fi
1057-
1058-
# Validate that source schema exists in upgrade mode
1059-
if [[ "${UPGRADE_MODE}" == "true" && ! -f "${SCHEMA_SOURCE_PATH}" ]]; then
1060-
log_error "Upgrade mode enabled but source schema not found: ${SCHEMA_SOURCE_PATH}"
1061-
log_error "Please ensure the template schema exists or use -P to specify a different location"
1062-
exit 1
1096+
SCHEMA_SOURCE_PATH="${DEFAULT_UPGRADE_SOURCE_PATH}"
10631097
fi
10641098

1065-
# Validate startup check mode
1066-
case "${STARTUP_CHECK}" in
1067-
fail|warn|wait)
1068-
;;
1069-
*)
1070-
log_error "Invalid startup check mode: ${STARTUP_CHECK}. Must be 'fail', 'warn', or 'wait'"
1071-
exit 1
1072-
;;
1073-
esac
1074-
1075-
# Validate mode
1076-
case "${MODE}" in
1077-
watch|oneshot)
1078-
;;
1079-
*)
1080-
log_error "Invalid mode: ${MODE}. Must be 'watch' or 'oneshot'"
1081-
exit 1
1082-
;;
1083-
esac
1099+
# Log config info, then run preflight checks
10841100

10851101
log_info "Starting Solr Driver for Dataverse Metadata Schemas"
10861102
log_info "Mode: ${MODE}"

0 commit comments

Comments
 (0)