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.

209 lines
5.6 KiB

  1. #!/bin/bash
  2. ## compose: no-hooks
  3. if [ -z "$SERVICE_DATASTORE" ]; then
  4. echo "This script is meant to be run through 'compose' to work properly." >&2
  5. exit 1
  6. fi
  7. version=0.1
  8. usage="$exname [-h|--help] [--force|-f] [DBNAME]"
  9. help="
  10. USAGE:
  11. $usage
  12. DESCRIPTION:
  13. Read stdin and send it to the restore API of odoo service to restore
  14. in the database DBNAME. If DBNAME is not provided, it'll take the
  15. default odoo database from the ${DARKCYAN}postgres-database${NORMAL} relation of
  16. current service.
  17. EXAMPLES:
  18. $exname < dump.zip
  19. $exname odoo2 < odoo2.zip
  20. "
  21. dbname=
  22. neutralize=
  23. while [ "$1" ]; do
  24. case "$1" in
  25. "--help"|"-h")
  26. print_help >&2
  27. exit 0
  28. ;;
  29. "--neutralize"|"-n")
  30. neutralize=yes
  31. ;;
  32. "--force"|"-f")
  33. force=yes
  34. ;;
  35. --*|-*)
  36. err "Unexpected optional argument '$1'"
  37. print_usage >&2
  38. exit 1
  39. ;;
  40. *)
  41. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  42. err "Unexpected positional argument '$1'"
  43. print_usage >&2
  44. exit 1
  45. ;;
  46. esac
  47. shift
  48. done
  49. version:ge() { [ "$(printf '%s\n' "$@" | sort -rV | head -n 1)" == "$1" ]; }
  50. version:lt() { ! version:ge "$@"; }
  51. if [ -z "$dbname" ]; then
  52. ##
  53. ## Fetch default dbname in relation to postgres-database
  54. ##
  55. ## XXXvlab: can't get real config here
  56. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
  57. err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
  58. exit 1
  59. fi
  60. relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
  61. err "Failed to find relation file"
  62. exit 1
  63. }
  64. postgres_config=$(cat "$relation_file"/data) || exit 2
  65. dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
  66. err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
  67. exit 1
  68. }
  69. fi
  70. set -e
  71. curl_opts=()
  72. ## Ensure odoo is launched
  73. service_def=$(get_compose_service_def "$SERVICE_NAME")
  74. ## XXXvlab: should be moved to lib
  75. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  76. ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  77. if [ -e "$CONFIG" ]; then
  78. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  79. fi
  80. if [ -z "$ADMIN_PASSWORD" ]; then
  81. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  82. exit 1
  83. fi
  84. }
  85. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  86. if [ -z "$containers" ]; then
  87. err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  88. exit 1
  89. fi
  90. if [ "$(echo "$containers" | wc -l)" -gt 1 ]; then
  91. err "More than 1 container running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  92. echo " Please contact administrator to fix this issue." >&2
  93. exit 1
  94. fi
  95. container="$(echo "$containers" | head -n 1)"
  96. odoo_version=$(docker exec "$container" python -c 'import odoo.release; print(odoo.release.version)') || {
  97. err "Failed to get odoo version."
  98. exit 1
  99. }
  100. if version:lt "$odoo_version" "16.0" && [ -n "$neutralize" ]; then
  101. err "Option \`\`--neutralize\`\` (or \`\`-n\`\`)" \
  102. "is only available for odoo ${WHITE}16.0${NORMAL}+."
  103. echo " Service ${DARKYELLOW}$SERVICE_NAME${NORMAL} is running odoo ${WHITE}$odoo_version${NORMAL}" >&2
  104. exit 1
  105. fi
  106. if [ -n "$neutralize" ]; then
  107. curl_opts+=(-F "neutralize_database=true")
  108. fi
  109. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  110. err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
  111. exit 1
  112. }
  113. container_ip=${container_network_ip##*:}
  114. container_network=${container_network_ip%%:*}
  115. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  116. check_input() {
  117. local chars
  118. read -n 2 -r chars
  119. if [ "$chars" != "PK" ]; then
  120. err "Unexpected input not matching ZIP signature. Invalid dump."
  121. echo " First chars: '$chars'"
  122. exit 1
  123. fi
  124. {
  125. printf "%s" "$chars"
  126. cat
  127. }
  128. }
  129. ## Beware that we are not on the host, so we need to create the
  130. ## fifo in a place we can share with the curl container.
  131. export TMPDIR=/var/cache/compose
  132. settmpdir RESTORE_TMPDIR
  133. mkfifo "$RESTORE_TMPDIR/fifo"
  134. ## Using fifo because if using ``@-`` curl will load all the database
  135. ## in memory before sending it, which is NOT desirable as the size of
  136. ## the database can be greater than available memory. Using fifo, we
  137. ## force the data to be streamed.
  138. cmd=(
  139. docker run -i --rm --network "$container_network"
  140. -v "$RESTORE_TMPDIR/fifo:/tmp/restore/fifo"
  141. "$DEFAULT_CURL_IMAGE"
  142. -sS
  143. -X POST
  144. -F "master_pwd=${ADMIN_PASSWORD}"
  145. -F "name=${dbname}"
  146. "${curl_opts[@]}"
  147. -F "backup_file=@/tmp/restore/fifo"
  148. http://${container_ip}:8069/web/database/restore
  149. )
  150. ## XXXvlab: contains password, left only for advanced debug
  151. #echo "COMMAND: ${cmd[@]}" >&2
  152. "${cmd[@]}" > "$RESTORE_TMPDIR/out" &
  153. pid=$!
  154. check_input > "$RESTORE_TMPDIR/fifo"
  155. wait "$pid" || {
  156. die "Posting to odoo restore API through curl was unsuccessfull."
  157. }
  158. out=$(cat "$RESTORE_TMPDIR/out")
  159. if [[ "$out" == *"<html>"* ]]; then
  160. errmsg=$(echo "$out" | grep "alert-danger")
  161. errmsg=${errmsg#*>}
  162. errmsg=${errmsg%%<*}
  163. if [ "$errmsg" ]; then
  164. errmsg=$(echo "$errmsg" | recode html..utf8)
  165. die "$errmsg"
  166. fi
  167. die "Unexpected output. Restore probably failed."
  168. fi >&2
  169. info "Restored stdin dump to odoo '$dbname'."