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.

214 lines
6.0 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
2 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
  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_BASEDIR/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_BASEDIR/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 to 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 to 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. admin_username=$(echo -e "$data" | yq '.admin.username')
  46. [[ $admin_username == 'null ' ]] && echoerr "odoo12 recipe requires \`data:admin:username\` into miaou.yaml service definition of <$fqdn>" && exit 1
  47. admin_password=$(echo -e "$data" | yq '.admin.password')
  48. [[ $admin_password == 'null ' ]] && echoerr "odoo12 recipe requires \`data:admin:password\` into miaou.yaml service definition of <$fqdn>" && exit 1
  49. cat <<EOF | lxc_exec "$container"
  50. set -Eeuo pipefail
  51. echo reloading systemd
  52. systemctl daemon-reload
  53. systemctl stop $longname
  54. echo initialize database...
  55. su odoo -c "python3.9 /home/odoo/odoo15/odoo-bin -c /etc/odoo15/$shortname.conf -i base --without-demo=all --stop-after-init"
  56. echo initialize database OK
  57. echo install default modules ...
  58. 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"
  59. echo default modules OK
  60. echo activate french language
  61. 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"
  62. echo change administrator password, default credential applied!
  63. echo "UPDATE res_users SET login='$admin_username', password='$admin_password' WHERE id=2" | PGPASSWORD=$longname psql -U $longname -h ct1.lxd
  64. EOF
  65. else
  66. echo "database already exists!"
  67. fi
  68. echo "initialize odoo $shortname $longname ..."
  69. cat <<EOF | lxc_exec "$container"
  70. set -Eeuo pipefail
  71. echo reloading systemd
  72. systemctl daemon-reload
  73. if ! systemctl is-active --quiet $longname; then
  74. echo start service $longname
  75. systemctl enable $longname
  76. systemctl start $longname
  77. systemctl is-active --quiet $longname
  78. else
  79. echo service $longname already started!
  80. fi
  81. EOF
  82. echo "initialize odoo $shortname $longname ... OK"
  83. }
  84. function _update() {
  85. echo "update"
  86. }
  87. function _delete() {
  88. echo "delete"
  89. }
  90. function usage() {
  91. echo "Usage: $COMMAND_NAME -c|r|u|d --port $port --fqdn $fqdn --container $container --name $name --data $data"
  92. exit 2
  93. }
  94. ### MAIN
  95. # init_strict
  96. COMMAND_NAME=$(basename "$0")
  97. # read the options
  98. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,domain:,subdomain:,data: -- "$@")
  99. # shellcheck disable=SC2181
  100. [[ "$?" -eq 0 ]] || usage
  101. eval set -- "$TEMP"
  102. action="unset"
  103. port="unset"
  104. fqdn="unset"
  105. container="unset"
  106. shortname="unset"
  107. longname="unset"
  108. data="unset"
  109. # extract options and their arguments into variables.
  110. while true; do
  111. case "$1" in
  112. --port)
  113. port=$2
  114. shift 2
  115. ;;
  116. --fqdn)
  117. fqdn=$2
  118. shift 2
  119. ;;
  120. --container)
  121. container=$2
  122. shift 2
  123. ;;
  124. --name)
  125. shortname=$2
  126. longname="odoo15-$shortname"
  127. shift 2
  128. ;;
  129. --data)
  130. data=$2
  131. shift 2
  132. ;;
  133. --domain)
  134. shift 2
  135. ;;
  136. --subdomain)
  137. shift 2
  138. ;;
  139. -c)
  140. [[ "$action" == "unset" ]] || usage
  141. action="_create"
  142. shift 1
  143. ;;
  144. -r)
  145. [[ "$action" == "unset" ]] || usage
  146. action="_read"
  147. shift 1
  148. ;;
  149. -u)
  150. [[ "$action" == "unset" ]] || usage
  151. action="_update"
  152. shift 1
  153. ;;
  154. -d)
  155. [[ "$action" == "unset" ]] || usage
  156. action="_delete"
  157. shift 1
  158. ;;
  159. --)
  160. shift
  161. break
  162. ;;
  163. *)
  164. echoerr "Internal error: unrecognized option: <$1>"
  165. exit 1
  166. ;;
  167. esac
  168. done
  169. [[
  170. "$action" != unset &&
  171. "$port" != unset &&
  172. "$fqdn" != unset &&
  173. "$container" != unset &&
  174. "$shortname" != unset &&
  175. "$data" != unset ]] || usage
  176. . "$MIAOU_BASEDIR/lib/init.sh"
  177. $action