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.

180 lines
4.9 KiB

10 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. credential_username=$(load_yaml_from_expanded credential.username)
  38. credential_password=$(load_yaml_from_expanded credential.password)
  39. cat <<EOF | lxc_exec "$container"
  40. set -Eeuo pipefail
  41. echo reloading systemd
  42. systemctl daemon-reload
  43. systemctl stop $longname
  44. echo initialize database...
  45. 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"
  46. echo initialize database OK
  47. echo install default modules ...
  48. 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"
  49. echo default modules OK
  50. chmod u+w -R "/home/odoo/data-$shortname"
  51. echo "TODO: change administrator password, default credential applied!"
  52. echo "UPDATE res_users SET login='$credential_username', password='$credential_password' WHERE id=2" | PGPASSWORD=$longname psql -U $longname -h ct1.lxd
  53. EOF
  54. else
  55. echo "database already exists!"
  56. fi
  57. echo "initialize odoo $shortname $longname ..."
  58. cat <<EOF | lxc_exec "$container"
  59. set -Eeuo pipefail
  60. echo reloading systemd
  61. systemctl daemon-reload
  62. if ! systemctl is-active --quiet $longname; then
  63. echo start service $longname
  64. systemctl start $longname
  65. systemctl is-active --quiet $longname
  66. systemctl enable $longname
  67. else
  68. echo service $longname already started!
  69. fi
  70. EOF
  71. echo "initialize odoo $shortname $longname ... OK"
  72. }
  73. function _update() {
  74. echo "TODO: update"
  75. }
  76. function _delete() {
  77. echo "TODO: delete"
  78. }
  79. function usage() {
  80. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  81. exit 2
  82. }
  83. ### MAIN
  84. # init_strict
  85. COMMAND_NAME=$(basename "$0")
  86. # read the options
  87. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn: -- "$@")
  88. # shellcheck disable=SC2181
  89. [[ "$?" -eq 0 ]] || usage
  90. eval set -- "$TEMP"
  91. action="unset"
  92. port="unset"
  93. container="unset"
  94. shortname="unset"
  95. longname="unset"
  96. # extract options and their arguments into variables.
  97. while true; do
  98. case "$1" in
  99. --port)
  100. port=$2
  101. shift 2
  102. ;;
  103. --fqdn)
  104. shift 2
  105. ;;
  106. --container)
  107. container=$2
  108. shift 2
  109. ;;
  110. --name)
  111. shortname=$2
  112. longname="odoo12-$shortname"
  113. shift 2
  114. ;;
  115. -c)
  116. [[ "$action" == "unset" ]] || usage
  117. action="_create"
  118. shift 1
  119. ;;
  120. -r)
  121. [[ "$action" == "unset" ]] || usage
  122. action="_read"
  123. shift 1
  124. ;;
  125. -u)
  126. [[ "$action" == "unset" ]] || usage
  127. action="_update"
  128. shift 1
  129. ;;
  130. -d)
  131. [[ "$action" == "unset" ]] || usage
  132. action="_delete"
  133. shift 1
  134. ;;
  135. --)
  136. shift
  137. break
  138. ;;
  139. *)
  140. echo "Internal error!"
  141. exit 1
  142. ;;
  143. esac
  144. done
  145. [[
  146. "$action" != unset &&
  147. "$port" != unset &&
  148. "$container" != unset &&
  149. "$shortname" != unset ]] || usage
  150. . "$MIAOU_BASEDIR/lib/init.sh"
  151. $action