fork 0k-charms
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.

154 lines
3.8 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. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  79. err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
  80. exit 1
  81. }
  82. container_ip=${container_network_ip##*:}
  83. container_network=${container_network_ip%%:*}
  84. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  85. check_input() {
  86. local chars
  87. read -n 2 -r chars
  88. if [ "$chars" != "PK" ]; then
  89. err "Unexpected input not matching ZIP signature. Invalid dump."
  90. echo " First chars: '$chars'"
  91. exit 1
  92. fi
  93. {
  94. echo -n "$chars"
  95. cat
  96. }
  97. }
  98. cmd=(
  99. docker run -i --rm --network "$container_network"
  100. "$DEFAULT_CURL_IMAGE"
  101. -sS
  102. -X POST
  103. -F "master_pwd=${ADMIN_PASSWORD}"
  104. -F "backup_file=@-"
  105. -F "name=${dbname}"
  106. http://${container_ip}:8069/web/database/restore
  107. )
  108. ## XXXvlab: contains password, left only for advanced debug
  109. #debug "${cmd[@]}"
  110. out=$(set -o pipefail; check_input | "${cmd[@]}") || {
  111. die "Posting to odoo restore API through curl was unsuccessfull."
  112. }
  113. if [[ "$out" == *"<html>"* ]]; then
  114. errmsg=$(echo "$out" | grep "alert-danger")
  115. errmsg=${errmsg#*>}
  116. errmsg=${errmsg%%<*}
  117. if [ "$errmsg" ]; then
  118. errmsg=$(echo "$errmsg" | recode html..utf8)
  119. die "$errmsg"
  120. fi
  121. die "Unexpected output. Restore probably failed."
  122. fi >&2
  123. info "Restored stdin dump to odoo '$dbname'."