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.

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