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.

185 lines
5.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] [DBNAME]"
  8. help="
  9. USAGE:
  10. $usage
  11. DESCRIPTION:
  12. Drop odoo database DBNAME. If DBNAME is not provided, it'll take the
  13. default odoo database from the ${DARKCYAN}postgres-database${NORMAL} relation of
  14. current service.
  15. EXAMPLES:
  16. $exname
  17. $exname odoo2
  18. "
  19. dbname=
  20. while [ "$1" ]; do
  21. case "$1" in
  22. "--help"|"-h")
  23. print_help >&2
  24. exit 0
  25. ;;
  26. --*|-*)
  27. err "Unexpected optional argument '$1'"
  28. print_usage >&2
  29. exit 1
  30. ;;
  31. *)
  32. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  33. err "Unexpected positional argument '$1'"
  34. print_usage >&2
  35. exit 1
  36. ;;
  37. esac
  38. shift
  39. done
  40. db_drop() {
  41. local dbname="$1"
  42. (
  43. switch_to_relation_service postgres-database
  44. set +e
  45. . lib/common
  46. set -e
  47. ensure_db_docker_running || exit 1
  48. if db_has_database "$dbname"; then
  49. info "Found a postgres database '$dbname' and deleting it."
  50. PGM rm -f "$dbname" || exit 1
  51. else
  52. info "No postgres database '$dbname' found."
  53. fi
  54. ) || exit 1
  55. if [ -d "$SERVICE_DATASTORE"/var/lib/odoo/filestore/"$dbname" ]; then
  56. rm -rf "$SERVICE_DATASTORE"/var/lib/odoo/filestore/"$dbname" >&2 || exit 1
  57. info "Deleted filestore for database '$dbname'."
  58. else
  59. info "No filestore for database '$dbname' found."
  60. fi
  61. }
  62. if [ -z "$dbname" ]; then
  63. ##
  64. ## Fetch default dbname in relation to postgres-database
  65. ##
  66. ## XXXvlab: can't get real config here
  67. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
  68. err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
  69. exit 1
  70. fi
  71. relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
  72. err "Failed to find relation file"
  73. exit 1
  74. }
  75. postgres_config=$(cat "$relation_file"/data) || exit 2
  76. dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
  77. err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation."
  78. exit 1
  79. }
  80. fi
  81. set -e
  82. ## Ensure odoo is launched
  83. service_def=$(get_compose_service_def "$SERVICE_NAME")
  84. ## XXXvlab: should be moved to lib
  85. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  86. ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  87. if [ -e "$CONFIG" ]; then
  88. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  89. fi
  90. if [ -z "$ADMIN_PASSWORD" ]; then
  91. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  92. exit 1
  93. fi
  94. }
  95. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  96. err "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  97. exit 1
  98. }
  99. container_ip=${container_network_ip##*:}
  100. container_network=${container_network_ip%%:*}
  101. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  102. cmd=(
  103. docker run -i --rm --network "$container_network"
  104. "$DEFAULT_CURL_IMAGE"
  105. -sS
  106. -X POST
  107. -F "master_pwd=${ADMIN_PASSWORD}"
  108. -F "name=${dbname}"
  109. http://${container_ip}:8069/web/database/drop
  110. )
  111. ## XXXvlab: contains password, left only for advanced debug
  112. #echo "${cmd[@]}" >&2
  113. out=$("${cmd[@]}") || {
  114. errlvl="$?"
  115. if [ "$errlvl" == "28" ]; then
  116. ## Curl timeout
  117. warn "Standard method through curl timed out. Trying manual drop of database."
  118. db_drop "$dbname" || exit 1
  119. exit 0
  120. else
  121. die "Posting to odoo drop API through curl was unsuccessfull ($errlvl)."
  122. fi
  123. }
  124. if [[ "$out" == *"<html>"* ]]; then
  125. errmsg=$(echo "$out" | grep -m1 "alert-danger") ||
  126. errmsg=$(echo "$out" | grep -m1 "errormsg") ||
  127. errmsg=$(echo "$out" | grep -m1 "<title>")
  128. errmsg="${errmsg#*>}"
  129. errmsg="${errmsg%%<*}"
  130. if [ -n "$errmsg" ]; then
  131. errmsg=$(echo "$errmsg" | recode html..utf8)
  132. if [[ "$errmsg" == "psycopg2.OperationalError: FATAL: database \"$dbname\" does not exist" ]]; then
  133. warn "Received error we can probably ignore."
  134. elif [[ "$errmsg" == "KeyError: 'ir.http'" ]] ||
  135. [[ "$errmsg" == \
  136. "psycopg2.ProgrammingError: relation \"base_registry_signaling\" already exists" ]]; then
  137. ## Hooks must have created an empty database, messing up things
  138. warn "Standard method failed as if postgres db was empty but existing."
  139. echo " Trying manual drop of database.." >&2
  140. db_drop "$dbname" || exit 1 ## Let's do it the legacy way !
  141. else
  142. die "$errmsg"
  143. fi
  144. else
  145. die "Unexpected output. Drop probably failed."
  146. fi
  147. else
  148. info "Dropped odoo database '$dbname'."
  149. fi >&2