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.

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