provisioning tool for building opinionated architecture
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.

196 lines
5.4 KiB

7 months ago
  1. #!/bin/bash
  2. function check_database_exists() {
  3. db-psql list | grep -q "$longname"
  4. }
  5. function check_port_used() {
  6. # shellcheck disable=SC2034
  7. usedport=$(lxc exec "$container" -- bash -c "grep xmlrpc_port /etc/odoo15/$shortname.conf | cut -d' ' -f3")
  8. [[ "$usedport" == "$port" ]]
  9. }
  10. function check_service_running() {
  11. lxc exec "$container" -- bash -c "systemctl is-active --quiet ${longname}.service"
  12. }
  13. function _read() {
  14. disable_trace
  15. check_database_exists
  16. check_container "$container"
  17. check_port_used
  18. check_service_running
  19. enable_trace
  20. return 0
  21. }
  22. function _create() {
  23. echo "creating templates ... "
  24. mkdir -p "$MIAOU_CONFIGDIR/apps/odoo15"
  25. longport=$((port + 1000))
  26. APP_PORT=$port LONG_PORT=$longport APP_NAME=$shortname tera -e -t "$MIAOU_DIR/templates/apps/odoo15/odoo.conf.j2" -o "$MIAOU_CONFIGDIR/apps/odoo15/$shortname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml" >/dev/null
  27. APP_NAME=$shortname tera -t "$MIAOU_DIR/templates/apps/odoo15/odoo.service.j2" --env-only -o "$MIAOU_CONFIGDIR/apps/odoo15/$longname.service" >/dev/null
  28. echo "creating templates ... OK"
  29. echo "copying files over container <$container> ... "
  30. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/odoo15/$shortname.conf" "$container/etc/odoo15/$shortname.conf"
  31. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/odoo15/$longname.service" "$container/etc/systemd/system/$longname.service"
  32. echo "copying files over container <$container> ... OK"
  33. echo "create data folder for $shortname"
  34. cat <<EOF | lxc_exec "$container"
  35. set -Eeuo pipefail
  36. echo allow addons writable for user odoo
  37. mkdir -p /home/odoo/data-$shortname/addons/15.0
  38. chmod u+w /home/odoo/data-$shortname/addons/15.0
  39. chown -R odoo:odoo /home/odoo/data-$shortname
  40. EOF
  41. if ! (db-psql list | grep -q "$longname"); then
  42. echo "create empty database <$longname> ... "
  43. db-psql create "$longname"
  44. echo "create empty database <$longname> ... OK"
  45. credential_username=$(load_yaml_from_expanded credential.username)
  46. credential_password=$(load_yaml_from_expanded credential.password)
  47. cat <<EOF | lxc_exec "$container"
  48. set -Eeuo pipefail
  49. echo reloading systemd
  50. systemctl daemon-reload
  51. systemctl stop $longname
  52. echo initialize database...
  53. su odoo -c "python3.9 /home/odoo/odoo15/odoo-bin -c /etc/odoo15/$shortname.conf -i base --without-demo=all --stop-after-init"
  54. echo initialize database OK
  55. echo install default modules ...
  56. su odoo -c "python3.9 /home/odoo/odoo15/odoo-bin -c /etc/odoo15/$shortname.conf -i account,contacts,l10n_fr,account,sale,point_of_sale -d $longname --worker=0 --stop-after-init"
  57. echo default modules OK
  58. echo activate french language
  59. su odoo -c "python3.9 /home/odoo/odoo15/odoo-bin -c /etc/odoo15/$shortname.conf --load-language fr_FR -d $longname --worker=0 --stop-after-init"
  60. echo change administrator password, default credential applied!
  61. echo "UPDATE res_users SET login='$credential_username', password='$credential_password' WHERE id=2" | PGPASSWORD=$longname psql -U $longname -h ct1.lxd
  62. EOF
  63. else
  64. echo "database already exists!"
  65. fi
  66. echo "initialize odoo $shortname $longname ..."
  67. cat <<EOF | lxc_exec "$container"
  68. set -Eeuo pipefail
  69. echo reloading systemd
  70. systemctl daemon-reload
  71. if ! systemctl is-active --quiet $longname; then
  72. echo start service $longname
  73. systemctl enable $longname
  74. systemctl start $longname
  75. systemctl is-active --quiet $longname
  76. else
  77. echo service $longname already started!
  78. fi
  79. EOF
  80. echo "initialize odoo $shortname $longname ... OK"
  81. }
  82. function _update() {
  83. echo "update"
  84. }
  85. function _delete() {
  86. echo "delete"
  87. }
  88. function usage() {
  89. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  90. exit 2
  91. }
  92. ### MAIN
  93. # init_strict
  94. COMMAND_NAME=$(basename "$0")
  95. # read the options
  96. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn: -- "$@")
  97. # shellcheck disable=SC2181
  98. [[ "$?" -eq 0 ]] || usage
  99. eval set -- "$TEMP"
  100. action="unset"
  101. port="unset"
  102. container="unset"
  103. shortname="unset"
  104. longname="unset"
  105. # extract options and their arguments into variables.
  106. while true; do
  107. case "$1" in
  108. --port)
  109. port=$2
  110. shift 2
  111. ;;
  112. --fqdn)
  113. shift 2
  114. ;;
  115. --container)
  116. container=$2
  117. shift 2
  118. ;;
  119. --name)
  120. shortname=$2
  121. longname="odoo15-$shortname"
  122. shift 2
  123. ;;
  124. -c)
  125. [[ "$action" == "unset" ]] || usage
  126. action="_create"
  127. shift 1
  128. ;;
  129. -r)
  130. [[ "$action" == "unset" ]] || usage
  131. action="_read"
  132. shift 1
  133. ;;
  134. -u)
  135. [[ "$action" == "unset" ]] || usage
  136. action="_update"
  137. shift 1
  138. ;;
  139. -d)
  140. [[ "$action" == "unset" ]] || usage
  141. action="_delete"
  142. shift 1
  143. ;;
  144. --)
  145. shift
  146. break
  147. ;;
  148. *)
  149. echo "Internal error!"
  150. exit 1
  151. ;;
  152. esac
  153. done
  154. [[
  155. "$action" != unset &&
  156. "$port" != unset &&
  157. "$container" != unset &&
  158. "$shortname" != unset ]] || usage
  159. . "$MIAOU_BASEDIR/lib/init.sh"
  160. $action