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.

117 lines
3.3 KiB

  1. #!/bin/bash
  2. ## XXXvlab: this hook should go into a generic odoo image
  3. ## XXXvlab: should get location of code
  4. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  5. . lib/common
  6. set -e
  7. PASSWORD="$(relation-get password)"
  8. USER="$(relation-get user)"
  9. DBNAME="$(relation-get dbname)"
  10. HOST="$(relation-get host)"
  11. PORT="$(relation-get port)"
  12. ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || {
  13. if [ -e "$CONFIG" ]; then
  14. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  15. fi
  16. if [ -z "$ADMIN_PASSWORD" ]; then
  17. info "Generating odoo admin password"
  18. ADMIN_PASSWORD=$(gen_password)
  19. fi
  20. }
  21. control=$(p0 "$USER" "$DBNAME" "$PASSWORD" "$ADMIN_PASSWORD" "$HOST" "$PORT" | md5_compat)
  22. database=$(options-get database 2>/dev/null) || true
  23. database="${database:-$DBNAME}"
  24. config-add "\
  25. services:
  26. $MASTER_BASE_SERVICE_NAME:
  27. command:
  28. - '--database=$database'
  29. ## All this is to please tecnativa image, but is quite redundant
  30. environment:
  31. PGHOST: \"$HOST\"
  32. PGPORT: \"$PORT\"
  33. PGDATABASE: \"$DBNAME\"
  34. PGPASSWORD: \"$PASSWORD\"
  35. PGUSER: \"$USER\"
  36. ADMIN_PASSWORD: \"$ADMIN_PASSWORD\"
  37. "
  38. [ "$control" == "$(relation-get control 2>/dev/null)" ] && exit 0
  39. file_put $CONFIG <<EOF
  40. [options]
  41. admin_passwd = $ADMIN_PASSWORD
  42. db_user = $USER
  43. db_password = $PASSWORD
  44. EOF
  45. odoo_uid=$(get_odoo_uid)
  46. chown "$odoo_uid" "$CONFIG" && chmod 600 "$CONFIG"
  47. if ! out=$(echo "SELECT datname FROM pg_database;" | sql postgres 2>&1); then
  48. warn "Failed to get database list" >&2
  49. printf "%s\n" "$out" | prefix " " >&2
  50. ## We don't have access to database list, so...
  51. ## if we have a dbfilter set, complain.
  52. if dbfilter=$(options-get dbfilter 2>&1) && [ -n "$dbfilter" ]; then
  53. err "Cannot set ${WHITE}dbfilter${NORMAL} without access to db list"
  54. echo " You don't seem to have access rights on" \
  55. "${DARKYELLOW}$TARGET_SERVICE_NAME${NORMAL} to" \
  56. "the database list" >&2
  57. echo " So you cannot set" \
  58. "${WHITE}dbfilter${NORMAL} option in" \
  59. "${DARKYELLOW}$SERVICE_NAME${NORMAL} options." >&2
  60. exit 1
  61. fi
  62. service_base_image_export_dir \
  63. "$MASTER_BASE_SERVICE_NAME" \
  64. /opt/odoo/custom/src/odoo/odoo/sql_db.py \
  65. "$SERVICE_CONFIGSTORE/odoo-sql_db.py"
  66. chown "$odoo_uid" "$SERVICE_CONFIGSTORE/odoo-sql_db.py"
  67. patch -d "$SERVICE_CONFIGSTORE" -p0 <<EOF
  68. --- odoo-sql_db.py 2024-09-14 20:44:40.540104600 +0200
  69. +++ odoo-sql_db.py 2024-09-14 20:45:39.868394908 +0200
  70. @@ -789,6 +789,9 @@
  71. if _Pool is None:
  72. _Pool = ConnectionPool(int(tools.config['db_maxconn']))
  73. + if to == 'postgres':
  74. + to = tools.config['db_name']
  75. +
  76. db, info = connection_info_for(to)
  77. if not allow_uri and db != to:
  78. raise ValueError('URI connections not allowed')
  79. EOF
  80. ## We need to force DBFILTER to be empty as odoo-tecnativa image
  81. ## will set '.*' by default, which makes odoo switch to a mode
  82. ## where it ignores DBNAME.
  83. config-add "\
  84. services:
  85. $MASTER_BASE_SERVICE_NAME:
  86. volumes:
  87. - '$SERVICE_CONFIGSTORE/odoo-sql_db.py:/opt/odoo/custom/src/odoo/odoo/sql_db.py'
  88. environment:
  89. DBFILTER: ''
  90. "
  91. fi
  92. relation-set control "$control"
  93. info "Configured $SERVICE_NAME code for $HOST:$PORT access."