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

  1. # -*- mode: shell-script -*-
  2. POSTGRES_IMAGE=docker.0k.io/postgres:12.15.0-myc
  3. ## Mappings
  4. replace_by_rematch_pattern() {
  5. local input="$1" output="" char next_char i
  6. # Loop through each character in the string
  7. for (( i=0; i<${#input}; i++ )); do
  8. char="${input:$i:1}"
  9. # If a dollar sign is found
  10. if [[ "$char" == '$' ]]; then
  11. next_char="${input:$((i+1)):1}"
  12. # Check if next character is a digit
  13. if [[ "$next_char" =~ [0-9] ]]; then
  14. # Replace $N with ${rematch[N]}
  15. output+='${rematch['"$next_char"']}'
  16. ((i++)) # Skip next character as it's already processed
  17. continue
  18. fi
  19. fi
  20. output+="$char"
  21. done
  22. echo "$output"
  23. }
  24. export -f replace_by_rematch_pattern
  25. service:map:get_one() {
  26. local map_label="$1"
  27. res=()
  28. if service_map=$(options-get "service-${map_label}-map" 2>/dev/null) &&
  29. [ -n "$service_map" ]; then
  30. while read-0 regex map; do
  31. if [[ "$BASE_SERVICE_NAME" =~ ^$regex$ ]]; then
  32. rematch=("${BASH_REMATCH[@]}")
  33. maps=()
  34. type=$(e "$map" | shyaml -q get-type 2>/dev/null) || true
  35. value=$(e "$map" | shyaml -q get-value -y 2>/dev/null) || true
  36. case "$type" in
  37. sequence)
  38. while read-0 m; do
  39. if [[ "$m" != "None" ]] && [ -n "$m" ]; then
  40. maps+=("$m")
  41. fi
  42. done < <(e "$value" | shyaml get-values-0)
  43. ;;
  44. str)
  45. if ! m=$(e "$value" | shyaml get-value 2>/dev/null); then
  46. err "Failed to get mapping value from config."
  47. return 1
  48. fi
  49. [ -z "$m" ] && continue
  50. maps+=("$m")
  51. ;;
  52. NoneType|"")
  53. :
  54. ;;
  55. esac
  56. for map in "${maps[@]}"; do
  57. if ! [[ "$map" =~ ^([a-zA-Z0-9_*\{\}\ \,.-]|\$[0-9])+$ ]]; then
  58. err "Invalid mapping value '$map' in ${WHITE}service-${map_label}-map$NORMAL option."
  59. return 1
  60. fi
  61. map="${map//\*/\\*}" ## protect star from eval
  62. map=$(replace_by_rematch_pattern "$map")
  63. res+=($(set -- "${BASH_REMATCH[@]}"; eval echo "${map//\*/\\*}"))
  64. done
  65. break
  66. fi
  67. done < <(e "$service_map" | yq e -0 'to_entries | .[] | [.key, .value] |.[]')
  68. fi
  69. if [ "${#res[@]}" -gt 1 ]; then
  70. err "Multiple databases found for service '$BASE_SERVICE_NAME': ${dbnames[*]}." \
  71. "Please specify a single database in the service-${map_label}-map option."
  72. return 1
  73. fi
  74. echo "${res[0]}"
  75. }
  76. ##
  77. ## DB
  78. ##
  79. ddb () {
  80. docker run --rm -i \
  81. -e PGPASSWORD="$PASSWORD" \
  82. -e PGHOST="$HOST" \
  83. -e PGUSER="$USER" \
  84. -e PGPORT="$PORT" \
  85. --entrypoint psql \
  86. "${POSTGRES_IMAGE}" \
  87. -qAt "$@"
  88. }
  89. ##
  90. ## Entrypoints
  91. ##
  92. db_install_extensions() {
  93. local dbname="$1"
  94. shift
  95. for ext in "$@"; do
  96. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS $ext;") || return 1
  97. info "Installed $ext extension on database '$dbname'."
  98. done
  99. }