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.

253 lines
7.7 KiB

  1. # -*- mode: shell-script -*-
  2. get_odoo_uid() {
  3. uid_label="odoo"
  4. odoo_uid=$(cached_cmd_on_base_image "$SERVICE_NAME" "id -u \"$uid_label\"") || {
  5. debug "Failed to query for '$uid_label' uid in ${DARKYELLOW}$SERVICE_NAME${NORMAL} base image."
  6. return 1
  7. }
  8. info "openerp uid from ${DARKYELLOW}$SERVICE_NAME${NORMAL} is '$odoo_uid'"
  9. echo "$odoo_uid"
  10. }
  11. sql() {
  12. local dbname="$1"
  13. (
  14. DBNAME="$(relation:get "$SERVICE_NAME":postgres-database dbname)" || return 1
  15. ts=$(service:traverse "$SERVICE_NAME":"postgres-database") || return 1
  16. export SERVICE_NAME="$ts"
  17. export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME"
  18. target_charm=$(get_service_charm "$ts") || return 1
  19. target_charm_path=$(charm.get_dir "$target_charm") || return 1
  20. set +e
  21. . "$target_charm_path/lib/common"
  22. set -e
  23. metadata_service_def=$(_get_service_metadata "$ts") || return 1
  24. type=$(e "$metadata_service_def" | yq -r '.type') || true
  25. if [[ "$type" != "stub" ]]; then
  26. DOCKER_BASE_IMAGE=$(service_ensure_image_ready "$SERVICE_NAME") || return 1
  27. export DOCKER_BASE_IMAGE
  28. ensure_db_docker_running
  29. fi
  30. ddb "${dbname:-$DBNAME}"
  31. )
  32. }
  33. odoo:get-admin-password() {
  34. local service_def config admin_password
  35. service_def=$(get_compose_service_def "$SERVICE_NAME")
  36. config=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  37. admin_password=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  38. if [ -e "$config" ]; then
  39. admin_password=$(grep ^admin_passwd "$config" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  40. fi
  41. if [ -z "$admin_password" ]; then
  42. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  43. return 1
  44. fi
  45. }
  46. echo "$admin_password"
  47. }
  48. ## XXXvlab: to include in `compose-core`
  49. get_running_container_for_service() {
  50. local containers service="$1"
  51. containers=($(get_running_containers_for_service "$service"))
  52. if [ "${#containers[@]}" == 0 ]; then
  53. err "No containers running for service $DARKYELLOW$service$NORMAL."
  54. return 1
  55. fi
  56. if [ "${#containers[@]}" -gt 1 ]; then
  57. err "More than 1 container running for service $DARKYELLOW$service$NORMAL."
  58. echo " Please contact administrator to fix this issue." >&2
  59. return 1
  60. fi
  61. echo "${containers[0]}"
  62. }
  63. odoo:save() {
  64. local ADMIN_PASSWORD container_ip container_network_ip container_network cmd dbname="$1"
  65. ADMIN_PASSWORD=$(odoo:get-admin-password) || {
  66. err "Couldn't retrieve admin password for $SERVICE_NAME."
  67. return 1
  68. }
  69. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  70. err "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  71. return 1
  72. }
  73. container_ip=${container_network_ip##*:}
  74. container_network=${container_network_ip%%:*}
  75. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  76. check_output() {
  77. local chars
  78. read -n 2 -r chars
  79. if [ "$chars" != "PK" ]; then
  80. out=$(cat)
  81. errmsg=$(echo "$out" | grep "alert-danger")
  82. errmsg=${errmsg#*>}
  83. errmsg=${errmsg%%<*}
  84. if [ -n "$errmsg" ]; then
  85. errmsg=$(echo "$errmsg" | recode html..utf8)
  86. die "$errmsg"
  87. fi
  88. err "Unexpected output not matching ZIP signature. Dump probably failed."
  89. return 1
  90. fi
  91. {
  92. echo -n "$chars"
  93. cat
  94. }
  95. }
  96. cmd=(
  97. docker run --rm --network "$container_network"
  98. "$DEFAULT_CURL_IMAGE"
  99. -sS
  100. -X POST
  101. -F "master_pwd=${ADMIN_PASSWORD}"
  102. -F "name=${dbname}"
  103. -F "backup_format=zip"
  104. http://${container_ip}:8069/web/database/backup
  105. )
  106. ## XXXvlab: contains password, left only for advanced debug
  107. #debug "${cmd[@]}"
  108. set -o pipefail
  109. "${cmd[@]}" | check_output &&
  110. info "Requested odoo '$dbname' dump and outputted result to stdout."
  111. }
  112. odoo:version() {
  113. local odoo_version container
  114. container="$(get_running_container_for_service "$SERVICE_NAME")" || {
  115. err "Couldn't find running container for service ${DARKYELLOW}$SERVICE_NAME${NORMAL}."
  116. return 1
  117. }
  118. odoo_version=$(docker exec "$container" python -c 'import odoo.release; print(odoo.release.version)') || {
  119. err "Failed to get odoo version from ${DARKYELLOW}$SERVICE_NAME${NORMAL}."
  120. return 1
  121. }
  122. echo "$odoo_version"
  123. }
  124. odoo:load() {
  125. local ADMIN_PASSWORD container_ip container_network_ip container_network cmd \
  126. dbname="$1" neutralize="$2" DEFAULT_CURL_IMAGE RESTORE_TMPDIR
  127. curl_opts=()
  128. ADMIN_PASSWORD=$(odoo:get-admin-password) || {
  129. err "Couldn't retrieve admin password for ${DARKYELLOW}$SERVICE_NAME${NORMAL}."
  130. return 1
  131. }
  132. odoo_version=$(odoo:version) || return 1
  133. if version:lt "$odoo_version" "16.0" && [ -n "$neutralize" ]; then
  134. err "Option \`\`--neutralize\`\` (or \`\`-n\`\`)" \
  135. "is only available for odoo ${WHITE}16.0${NORMAL}+."
  136. echo " Service ${DARKYELLOW}$SERVICE_NAME${NORMAL} is running odoo ${WHITE}$odoo_version${NORMAL}" >&2
  137. return 1
  138. fi
  139. if [ -n "$neutralize" ]; then
  140. curl_opts+=(-F "neutralize_database=true")
  141. fi
  142. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  143. err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
  144. return 1
  145. }
  146. container_ip=${container_network_ip##*:}
  147. container_network=${container_network_ip%%:*}
  148. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  149. check_input() {
  150. local chars
  151. read -n 2 -r chars
  152. if [ "$chars" != "PK" ]; then
  153. err "Unexpected input not matching ZIP signature. Invalid dump."
  154. echo " First chars: '$chars'"
  155. return 1
  156. fi
  157. {
  158. printf "%s" "$chars"
  159. cat
  160. }
  161. }
  162. ## Beware that we are not on the host, so we need to create the
  163. ## fifo in a place we can share with the curl container.
  164. export TMPDIR=/var/cache/compose
  165. settmpdir RESTORE_TMPDIR
  166. mkfifo "$RESTORE_TMPDIR/fifo"
  167. ## Using fifo because if using ``@-`` curl will load all the database
  168. ## in memory before sending it, which is NOT desirable as the size of
  169. ## the database can be greater than available memory. Using fifo, we
  170. ## force the data to be streamed.
  171. cmd=(
  172. docker run -i --rm --network "$container_network"
  173. -v "$RESTORE_TMPDIR/fifo:/tmp/restore/fifo"
  174. "$DEFAULT_CURL_IMAGE"
  175. -sS
  176. -X POST
  177. -F "master_pwd=${ADMIN_PASSWORD}"
  178. -F "name=${dbname}"
  179. "${curl_opts[@]}"
  180. -F "backup_file=@/tmp/restore/fifo"
  181. http://${container_ip}:8069/web/database/restore
  182. )
  183. ## XXXvlab: contains password, left only for advanced debug
  184. #echo "COMMAND: ${cmd[@]}" >&2
  185. "${cmd[@]}" > "$RESTORE_TMPDIR/out" &
  186. pid=$!
  187. check_input > "$RESTORE_TMPDIR/fifo"
  188. wait "$pid" || {
  189. die "Posting to odoo restore API through curl was unsuccessfull."
  190. }
  191. out=$(cat "$RESTORE_TMPDIR/out")
  192. if [[ "$out" == *"<html>"* ]]; then
  193. errmsg=$(echo "$out" | grep "alert-danger")
  194. errmsg=${errmsg#*>}
  195. errmsg=${errmsg%%<*}
  196. if [ "$errmsg" ]; then
  197. errmsg=$(echo "$errmsg" | recode html..utf8)
  198. die "$errmsg"
  199. fi
  200. die "Unexpected output. Restore probably failed."
  201. fi >&2
  202. info "Restored stdin dump to odoo '$dbname'."
  203. }