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.

193 lines
5.0 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. cmd=(
  130. docker run -i --rm --network "$container_network"
  131. "$DEFAULT_CURL_IMAGE"
  132. -sS
  133. -X POST
  134. -F "master_pwd=${ADMIN_PASSWORD}"
  135. -F "backup_file=@-"
  136. -F "name=${dbname}"
  137. "${curl_opts[@]}"
  138. http://${container_ip}:8069/web/database/restore
  139. )
  140. ## XXXvlab: contains password, left only for advanced debug
  141. #debug "${cmd[@]}"
  142. out=$(set -o pipefail; check_input | "${cmd[@]}") || {
  143. die "Posting to odoo restore API through curl was unsuccessfull."
  144. }
  145. if [[ "$out" == *"<html>"* ]]; then
  146. errmsg=$(echo "$out" | grep "alert-danger")
  147. errmsg=${errmsg#*>}
  148. errmsg=${errmsg%%<*}
  149. if [ "$errmsg" ]; then
  150. errmsg=$(echo "$errmsg" | recode html..utf8)
  151. die "$errmsg"
  152. fi
  153. die "Unexpected output. Restore probably failed."
  154. fi >&2
  155. info "Restored stdin dump to odoo '$dbname'."