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.

158 lines
4.0 KiB

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