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.

138 lines
3.3 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. if [ -z "$dbname" ]; then
  45. ##
  46. ## Fetch default dbname in relation to postgres-database
  47. ##
  48. ## XXXvlab: can't get real config here
  49. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
  50. err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
  51. exit 1
  52. fi
  53. relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
  54. err "Failed to find relation file"
  55. exit 1
  56. }
  57. postgres_config=$(cat "$relation_file"/data) || exit 2
  58. dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
  59. err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation."
  60. exit 1
  61. }
  62. fi
  63. set -e
  64. ## Ensure odoo is launched
  65. service_def=$(get_compose_service_def "$SERVICE_NAME")
  66. ## XXXvlab: should be moved to lib
  67. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  68. ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  69. if [ -e "$CONFIG" ]; then
  70. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  71. fi
  72. if [ -z "$ADMIN_PASSWORD" ]; then
  73. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  74. exit 1
  75. fi
  76. }
  77. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  78. err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
  79. exit 1
  80. }
  81. container_ip=${container_network_ip##*:}
  82. container_network=${container_network_ip%%:*}
  83. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  84. cmd=(
  85. docker run -i --rm --network "$container_network"
  86. "$DEFAULT_CURL_IMAGE"
  87. -sS
  88. -X POST
  89. -F "master_pwd=${ADMIN_PASSWORD}"
  90. -F "name=${dbname}"
  91. http://${container_ip}:8069/web/database/drop
  92. )
  93. ## XXXvlab: contains password, left only for advanced debug
  94. #debug "${cmd[@]}"
  95. out=$("${cmd[@]}") || {
  96. die "Posting to odoo drop API through curl was unsuccessfull."
  97. }
  98. if [[ "$out" == *"<html>"* ]]; then
  99. errmsg=$(echo "$out" | grep "alert-danger")
  100. errmsg=${errmsg#*>}
  101. errmsg=${errmsg%%<*}
  102. if [ "$errmsg" ]; then
  103. errmsg=$(echo "$errmsg" | recode html..utf8)
  104. die "$errmsg"
  105. fi
  106. die "Unexpected output. Drop probably failed."
  107. fi >&2
  108. info "Dropped odoo database '$dbname'."