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.

198 lines
5.4 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
2 months ago
10 months ago
10 months ago
2 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
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_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 to 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 to 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=$(echo -e "$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=$(echo -e "$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:,domain:,subdomain:,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. --domain)
  120. shift 2
  121. ;;
  122. --subdomain)
  123. shift 2
  124. ;;
  125. --data)
  126. data=$2
  127. shift 2
  128. ;;
  129. -c)
  130. [[ "$action" == "unset" ]] || usage
  131. action="_create"
  132. shift 1
  133. ;;
  134. -r)
  135. [[ "$action" == "unset" ]] || usage
  136. action="_read"
  137. shift 1
  138. ;;
  139. -u)
  140. [[ "$action" == "unset" ]] || usage
  141. action="_update"
  142. shift 1
  143. ;;
  144. -d)
  145. [[ "$action" == "unset" ]] || usage
  146. action="_delete"
  147. shift 1
  148. ;;
  149. --)
  150. shift
  151. break
  152. ;;
  153. *)
  154. echoerr "Internal error: unrecognized option: <$1>"
  155. exit 1
  156. ;;
  157. esac
  158. done
  159. [[
  160. "$action" != unset &&
  161. "$port" != unset &&
  162. "$fqdn" != unset &&
  163. "$container" != unset &&
  164. "$shortname" != unset &&
  165. "$data" != unset ]] || usage
  166. . "$MIAOU_BASEDIR/lib/init.sh"
  167. $action