You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.5 KiB
115 lines
3.5 KiB
# -*- mode: shell-script -*-
|
|
|
|
POSTGRES_IMAGE=docker.0k.io/postgres:12.15.0-myc
|
|
|
|
|
|
## Mappings
|
|
|
|
replace_by_rematch_pattern() {
|
|
local input="$1" output="" char next_char i
|
|
# Loop through each character in the string
|
|
for (( i=0; i<${#input}; i++ )); do
|
|
char="${input:$i:1}"
|
|
|
|
# If a dollar sign is found
|
|
if [[ "$char" == '$' ]]; then
|
|
next_char="${input:$((i+1)):1}"
|
|
# Check if next character is a digit
|
|
if [[ "$next_char" =~ [0-9] ]]; then
|
|
# Replace $N with ${rematch[N]}
|
|
output+='${rematch['"$next_char"']}'
|
|
((i++)) # Skip next character as it's already processed
|
|
continue
|
|
fi
|
|
fi
|
|
|
|
output+="$char"
|
|
done
|
|
|
|
echo "$output"
|
|
}
|
|
export -f replace_by_rematch_pattern
|
|
|
|
|
|
service:map:get_one() {
|
|
local map_label="$1"
|
|
res=()
|
|
if service_map=$(options-get "service-${map_label}-map" 2>/dev/null) &&
|
|
[ -n "$service_map" ]; then
|
|
while read-0 regex map; do
|
|
if [[ "$BASE_SERVICE_NAME" =~ ^$regex$ ]]; then
|
|
rematch=("${BASH_REMATCH[@]}")
|
|
maps=()
|
|
type=$(e "$map" | shyaml -q get-type 2>/dev/null) || true
|
|
value=$(e "$map" | shyaml -q get-value -y 2>/dev/null) || true
|
|
case "$type" in
|
|
sequence)
|
|
while read-0 m; do
|
|
if [[ "$m" != "None" ]] && [ -n "$m" ]; then
|
|
maps+=("$m")
|
|
fi
|
|
done < <(e "$value" | shyaml get-values-0)
|
|
;;
|
|
str)
|
|
if ! m=$(e "$value" | shyaml get-value 2>/dev/null); then
|
|
err "Failed to get mapping value from config."
|
|
return 1
|
|
fi
|
|
[ -z "$m" ] && continue
|
|
maps+=("$m")
|
|
;;
|
|
NoneType|"")
|
|
:
|
|
;;
|
|
esac
|
|
for map in "${maps[@]}"; do
|
|
if ! [[ "$map" =~ ^([a-zA-Z0-9_*\{\}\ \,.-]|\$[0-9])+$ ]]; then
|
|
err "Invalid mapping value '$map' in ${WHITE}service-${map_label}-map$NORMAL option."
|
|
return 1
|
|
fi
|
|
|
|
map="${map//\*/\\*}" ## protect star from eval
|
|
map=$(replace_by_rematch_pattern "$map")
|
|
|
|
res+=($(set -- "${BASH_REMATCH[@]}"; eval echo "${map//\*/\\*}"))
|
|
done
|
|
break
|
|
fi
|
|
done < <(e "$service_map" | yq e -0 'to_entries | .[] | [.key, .value] |.[]')
|
|
fi
|
|
if [ "${#res[@]}" -gt 1 ]; then
|
|
err "Multiple databases found for service '$BASE_SERVICE_NAME': ${dbnames[*]}." \
|
|
"Please specify a single database in the service-${map_label}-map option."
|
|
return 1
|
|
fi
|
|
echo "${res[0]}"
|
|
}
|
|
|
|
##
|
|
## DB
|
|
##
|
|
|
|
ddb () {
|
|
docker run --rm -i \
|
|
-e PGPASSWORD="$PASSWORD" \
|
|
-e PGHOST="$HOST" \
|
|
-e PGUSER="$USER" \
|
|
-e PGPORT="$PORT" \
|
|
--entrypoint psql \
|
|
"${POSTGRES_IMAGE}" \
|
|
-qAt "$@"
|
|
}
|
|
|
|
|
|
##
|
|
## Entrypoints
|
|
##
|
|
|
|
db_install_extensions() {
|
|
local dbname="$1"
|
|
shift
|
|
for ext in "$@"; do
|
|
ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS $ext;") || return 1
|
|
info "Installed $ext extension on database '$dbname'."
|
|
done
|
|
}
|