fork 0k-charms
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.

114 lines
3.2 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. file_put $CONFIG <<EOF
  39. [options]
  40. admin_passwd = $ADMIN_PASSWORD
  41. db_user = $USER
  42. db_password = $PASSWORD
  43. EOF
  44. odoo_uid=$(get_odoo_uid)
  45. chown "$odoo_uid" "$CONFIG" && chmod 600 "$CONFIG"
  46. if ! out=$(echo "SELECT datname FROM pg_database;" | sql postgres 2>&1); then
  47. warn "Failed to get database list" >&2
  48. printf "%s\n" "$out" | prefix " " >&2
  49. ## We don't have access to database list, so...
  50. ## if we have a dbfilter set, complain.
  51. if dbfilter=$(options-get dbfilter 2>&1) && [ -n "$dbfilter" ]; then
  52. err "Cannot set ${WHITE}dbfilter${NORMAL} without access to db list"
  53. echo " You don't seem to have access rights on" \
  54. "${DARKYELLOW}$TARGET_SERVICE_NAME${NORMAL} to" \
  55. "the database list" >&2
  56. echo " So you cannot set" \
  57. "${WHITE}dbfilter${NORMAL} option in" \
  58. "${DARKYELLOW}$SERVICE_NAME${NORMAL} options." >&2
  59. exit 1
  60. fi
  61. service_base_image_export_dir \
  62. "$MASTER_BASE_SERVICE_NAME" \
  63. /opt/odoo/custom/src/odoo/odoo/sql_db.py \
  64. "$SERVICE_CONFIGSTORE/odoo-sql_db.py"
  65. chown "$odoo_uid" "$SERVICE_CONFIGSTORE/odoo-sql_db.py"
  66. patch -d "$SERVICE_CONFIGSTORE" -p0 <<EOF
  67. --- odoo-sql_db.py 2024-09-14 20:44:40.540104600 +0200
  68. +++ odoo-sql_db.py 2024-09-14 20:45:39.868394908 +0200
  69. @@ -789,6 +789,9 @@
  70. if _Pool is None:
  71. _Pool = ConnectionPool(int(tools.config['db_maxconn']))
  72. + if to == 'postgres':
  73. + to = tools.config['db_name']
  74. +
  75. db, info = connection_info_for(to)
  76. if not allow_uri and db != to:
  77. raise ValueError('URI connections not allowed')
  78. EOF
  79. ## We need to force DBFILTER to be empty as odoo-tecnativa image
  80. ## will set '.*' by default, which makes odoo switch to a mode
  81. ## where it ignores DBNAME.
  82. config-add "\
  83. services:
  84. $MASTER_BASE_SERVICE_NAME:
  85. volumes:
  86. - '$SERVICE_CONFIGSTORE/odoo-sql_db.py:/opt/odoo/custom/src/odoo/odoo/sql_db.py'
  87. environment:
  88. DB_FILTER: ''
  89. "
  90. fi
  91. info "Configured $SERVICE_NAME code for $HOST:$PORT access."