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.

149 lines
3.6 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] [--force|-f] [DBNAME]"
  9. help="
  10. USAGE:
  11. $usage
  12. DESCRIPTION:
  13. Outputs to stdout the odoo zip dump of DBNAME. If DBNAME is not
  14. provided, it'll take the default odoo database from the
  15. ${DARKCYAN}postgres-database${NORMAL} relation of current service.
  16. EXAMPLES:
  17. $exname > dump.zip
  18. $exname odoo2 > odoo2.zip
  19. "
  20. dbname=
  21. output=
  22. while [ "$1" ]; do
  23. case "$1" in
  24. "--help"|"-h")
  25. print_help >&2
  26. exit 0
  27. ;;
  28. "--force"|"-f")
  29. force=yes
  30. ;;
  31. --*|-*)
  32. err "Unexpected optional argument '$1'"
  33. print_usage >&2
  34. exit 1
  35. ;;
  36. *)
  37. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  38. err "Unexpected positional argument '$1'"
  39. print_usage >&2
  40. exit 1
  41. ;;
  42. esac
  43. shift
  44. done
  45. if [ -z "$dbname" ]; then
  46. ##
  47. ## Fetch default dbname in relation to postgres-database
  48. ##
  49. ## XXXvlab: can't get real config here
  50. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
  51. err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
  52. exit 1
  53. fi
  54. relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
  55. err "Failed to find relation file"
  56. exit 1
  57. }
  58. postgres_config=$(cat "$relation_file"/data) || exit 2
  59. dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
  60. err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation."
  61. exit 1
  62. }
  63. fi
  64. set -e
  65. ## Ensure odoo is launched
  66. service_def=$(get_compose_service_def "$SERVICE_NAME")
  67. ## XXXvlab: should be moved to lib
  68. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  69. ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  70. if [ -e "$CONFIG" ]; then
  71. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  72. fi
  73. if [ -z "$ADMIN_PASSWORD" ]; then
  74. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  75. exit 1
  76. fi
  77. }
  78. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  79. err "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  80. exit 1
  81. }
  82. container_ip=${container_network_ip##*:}
  83. container_network=${container_network_ip%%:*}
  84. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  85. check_output() {
  86. local chars
  87. read -n 2 -r chars
  88. if [ "$chars" != "PK" ]; then
  89. out=$(cat)
  90. errmsg=$(echo "$out" | grep "alert-danger")
  91. errmsg=${errmsg#*>}
  92. errmsg=${errmsg%%<*}
  93. if [ -n "$errmsg" ]; then
  94. errmsg=$(echo "$errmsg" | recode html..utf8)
  95. die "$errmsg"
  96. fi
  97. err "Unexpected output not matching ZIP signature. Dump probably failed."
  98. exit 1
  99. fi
  100. {
  101. echo -n "$chars"
  102. cat
  103. }
  104. }
  105. cmd=(
  106. docker run --rm --network "$container_network"
  107. "$DEFAULT_CURL_IMAGE"
  108. -sS
  109. -X POST
  110. -F "master_pwd=${ADMIN_PASSWORD}"
  111. -F "name=${dbname}"
  112. -F "backup_format=zip"
  113. http://${container_ip}:8069/web/database/backup
  114. )
  115. ## XXXvlab: contains password, left only for advanced debug
  116. #debug "${cmd[@]}"
  117. set -o pipefail
  118. "${cmd[@]}" | check_output &&
  119. info "Requested odoo '$dbname' dump and outputted result to stdout."