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.

183 lines
5.0 KiB

7 months ago
7 months ago
7 months ago
7 months ago
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/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_DIR/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_DIR/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. #FIXME: load credential from data argument wth yq parsing!!!
  38. credential_username=$(load_yaml_from_expanded credential.username)
  39. credential_password=$(load_yaml_from_expanded credential.password)
  40. cat <<EOF | lxc_exec "$container"
  41. set -Eeuo pipefail
  42. echo reloading systemd
  43. systemctl daemon-reload
  44. systemctl stop $longname
  45. echo initialize database...
  46. 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"
  47. echo initialize database OK
  48. echo install default modules ...
  49. 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"
  50. echo default modules OK
  51. chmod u+w -R "/home/odoo/data-$shortname"
  52. echo "TODO: change administrator password, default credential applied!"
  53. echo "UPDATE res_users SET login='$credential_username', password='$credential_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 --container CONTAINER --name NAME"
  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: -- "$@")
  89. # shellcheck disable=SC2181
  90. [[ "$?" -eq 0 ]] || usage
  91. eval set -- "$TEMP"
  92. action="unset"
  93. port="unset"
  94. container="unset"
  95. shortname="unset"
  96. longname="unset"
  97. # extract options and their arguments into variables.
  98. while true; do
  99. case "$1" in
  100. --port)
  101. port=$2
  102. shift 2
  103. ;;
  104. --fqdn)
  105. shift 2
  106. ;;
  107. --container)
  108. container=$2
  109. shift 2
  110. ;;
  111. --name)
  112. shortname=$2
  113. longname="odoo12-$shortname"
  114. shift 2
  115. ;;
  116. -c)
  117. [[ "$action" == "unset" ]] || usage
  118. action="_create"
  119. shift 1
  120. ;;
  121. -r)
  122. [[ "$action" == "unset" ]] || usage
  123. action="_read"
  124. shift 1
  125. ;;
  126. -u)
  127. [[ "$action" == "unset" ]] || usage
  128. action="_update"
  129. shift 1
  130. ;;
  131. -d)
  132. [[ "$action" == "unset" ]] || usage
  133. action="_delete"
  134. shift 1
  135. ;;
  136. --)
  137. shift
  138. break
  139. ;;
  140. *)
  141. echo "Internal error!"
  142. exit 1
  143. ;;
  144. esac
  145. done
  146. [[
  147. "$action" != unset &&
  148. "$port" != unset &&
  149. "$container" != unset &&
  150. "$shortname" != unset ]] || usage
  151. . "$MIAOU_BASEDIR/lib/init.sh"
  152. $action