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.

153 lines
3.8 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. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  78. if [ -z "$containers" ]; then
  79. err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  80. die "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  81. fi
  82. ## XXXvlab: taking first container is probably not a good idea
  83. container="$(echo "$containers" | head -n 1)"
  84. ## XXXvlab: taking first ip is probably not a good idea
  85. read-0 container_network container_ip < <(get_container_network_ip "$container")
  86. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  87. check_output() {
  88. local chars
  89. read -n 2 -r chars
  90. if [ "$chars" != "PK" ]; then
  91. out=$(cat)
  92. errmsg=$(echo "$out" | grep "alert-danger")
  93. errmsg=${errmsg#*>}
  94. errmsg=${errmsg%%<*}
  95. if [ -n "$errmsg" ]; then
  96. errmsg=$(echo "$errmsg" | recode html..utf8)
  97. die "$errmsg"
  98. fi
  99. err "Unexpected output not matching ZIP signature. Dump probably failed."
  100. exit 1
  101. fi
  102. {
  103. echo -n "$chars"
  104. cat
  105. }
  106. }
  107. cmd=(
  108. docker run --rm --network "$container_network"
  109. "$DEFAULT_CURL_IMAGE"
  110. -sS
  111. -X POST
  112. -F "master_pwd=${ADMIN_PASSWORD}"
  113. -F "name=${dbname}"
  114. -F "backup_format=zip"
  115. http://${container_ip}:8069/web/database/backup
  116. )
  117. ## XXXvlab: contains password, left only for advanced debug
  118. #debug "${cmd[@]}"
  119. set -o pipefail
  120. "${cmd[@]}" | check_output &&
  121. info "Requested odoo '$dbname' dump and outputted result to stdout."