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.

148 lines
3.6 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. Outputs to stdout the odoo zip dump of DBNAME. If DBNAME is not
  13. provided, it'll take the default odoo database from the
  14. ${DARKCYAN}postgres-database${NORMAL} relation of current service.
  15. EXAMPLES:
  16. $exname > dump.zip
  17. $exname odoo2 > odoo2.zip
  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_NAME$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. check_output() {
  85. local chars
  86. read -n 2 -r chars
  87. if [ "$chars" != "PK" ]; then
  88. out=$(cat)
  89. errmsg=$(echo "$out" | grep "alert-danger")
  90. errmsg=${errmsg#*>}
  91. errmsg=${errmsg%%<*}
  92. if [ -n "$errmsg" ]; then
  93. errmsg=$(echo "$errmsg" | recode html..utf8)
  94. die "$errmsg"
  95. fi
  96. err "Unexpected output not matching ZIP signature. Dump probably failed."
  97. exit 1
  98. fi
  99. {
  100. echo -n "$chars"
  101. cat
  102. }
  103. }
  104. cmd=(
  105. docker run --rm --network "$container_network"
  106. "$DEFAULT_CURL_IMAGE"
  107. -sS
  108. -X POST
  109. -F "master_pwd=${ADMIN_PASSWORD}"
  110. -F "name=${dbname}"
  111. -F "backup_format=zip"
  112. http://${container_ip}:8069/web/database/backup
  113. )
  114. ## XXXvlab: contains password, left only for advanced debug
  115. #debug "${cmd[@]}"
  116. set -o pipefail
  117. "${cmd[@]}" | check_output &&
  118. info "Requested odoo '$dbname' dump and outputted result to stdout."