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.

192 lines
5.3 KiB

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