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.

155 lines
3.8 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. output=
  23. while [ "$1" ]; do
  24. case "$1" in
  25. "--help"|"-h")
  26. print_help >&2
  27. exit 0
  28. ;;
  29. "--force"|"-f")
  30. force=yes
  31. ;;
  32. --*|-*)
  33. err "Unexpected optional argument '$1'"
  34. print_usage >&2
  35. exit 1
  36. ;;
  37. *)
  38. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  39. err "Unexpected positional argument '$1'"
  40. print_usage >&2
  41. exit 1
  42. ;;
  43. esac
  44. shift
  45. done
  46. if [ -z "$dbname" ]; then
  47. ##
  48. ## Fetch default dbname in relation to postgres-database
  49. ##
  50. ## XXXvlab: can't get real config here
  51. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
  52. err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
  53. exit 1
  54. fi
  55. relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
  56. err "Failed to find relation file"
  57. exit 1
  58. }
  59. postgres_config=$(cat "$relation_file"/data) || exit 2
  60. dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
  61. err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
  62. exit 1
  63. }
  64. fi
  65. set -e
  66. ## Ensure odoo is launched
  67. service_def=$(get_compose_service_def "$SERVICE_NAME")
  68. ## XXXvlab: should be moved to lib
  69. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  70. ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  71. if [ -e "$CONFIG" ]; then
  72. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  73. fi
  74. if [ -z "$ADMIN_PASSWORD" ]; then
  75. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  76. exit 1
  77. fi
  78. }
  79. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  80. err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
  81. exit 1
  82. }
  83. container_ip=${container_network_ip##*:}
  84. container_network=${container_network_ip%%:*}
  85. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  86. check_input() {
  87. local chars
  88. read -n 2 -r chars
  89. if [ "$chars" != "PK" ]; then
  90. err "Unexpected input not matching ZIP signature. Invalid dump."
  91. echo " First chars: '$chars'"
  92. exit 1
  93. fi
  94. {
  95. echo -n "$chars"
  96. cat
  97. }
  98. }
  99. cmd=(
  100. docker run -i --rm --network "$container_network"
  101. "$DEFAULT_CURL_IMAGE"
  102. -sS
  103. -X POST
  104. -F "master_pwd=${ADMIN_PASSWORD}"
  105. -F "backup_file=@-"
  106. -F "name=${dbname}"
  107. http://${container_ip}:8069/web/database/restore
  108. )
  109. ## XXXvlab: contains password, left only for advanced debug
  110. #debug "${cmd[@]}"
  111. out=$(set -o pipefail; check_input | "${cmd[@]}") || {
  112. die "Posting to odoo restore API through curl was unsuccessfull."
  113. }
  114. if [[ "$out" == *"<html>"* ]]; then
  115. errmsg=$(echo "$out" | grep "alert-danger")
  116. errmsg=${errmsg#*>}
  117. errmsg=${errmsg%%<*}
  118. if [ "$errmsg" ]; then
  119. errmsg=$(echo "$errmsg" | recode html..utf8)
  120. die "$errmsg"
  121. fi
  122. die "Unexpected output. Restore probably failed."
  123. fi >&2
  124. info "Restored stdin dump to odoo '$dbname'."