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.

186 lines
4.4 KiB

9 months ago
9 months ago
9 months ago
9 months ago
2 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 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" -- cat /etc/nginx/sites-enabled/"$longname".conf | grep listen | cut -d ' ' -f2)
  8. [[ "$usedport" == "$port" ]]
  9. }
  10. function check_directory_exists() {
  11. lxc exec "$container" -- test -d /var/www/"$longname"
  12. }
  13. function check_service_running() {
  14. lxc exec "$container" -- bash -c "systemctl is-active --quiet nginx.service"
  15. }
  16. function _read() {
  17. disable_trace
  18. check_database_exists
  19. check_container "$container"
  20. check_port_used
  21. check_directory_exists
  22. check_service_running
  23. enable_trace
  24. return 0
  25. }
  26. function _create() {
  27. PREFIX="recipe:dolibarr:create"
  28. : $PREFIX
  29. echo "create a Dolibarr instance for $shortname"
  30. lxc exec "$container" -- bash <<EOF
  31. set -Eeuo pipefail
  32. echo "install latest release ... "
  33. cd /var/www
  34. PATH="\$PATH:/opt/miaou-bash/tools"
  35. VERSION="\$(wget_semver github Dolibarr/dolibarr)"
  36. if [[ ! -f "dolibarr-\$VERSION.tgz" ]]; then
  37. wget_release github Dolibarr/dolibarr
  38. else
  39. echo "dolibarr version=\$VERSION already downloaded!"
  40. fi
  41. if [[ ! -d "$longname" ]]; then
  42. tar -xzvf "dolibarr-\$VERSION.tgz"
  43. mv dolibarr-\$VERSION $longname
  44. chown www-data:www-data -R $longname
  45. else
  46. echo "$longname already created!"
  47. fi
  48. EOF
  49. echo "generating configuration files from templates... "
  50. mkdir -p "$MIAOU_CONFIGDIR/apps/dolibarr/$shortname"
  51. PHP_VERSION=$(lxc exec "$container" -- dpkg -l php-fpm | grep "^ii" | cut -d ':' -f2 | cut -d '+' -f1)
  52. APP_PORT=$port APP_NAME=$longname PHP_VERSION=$PHP_VERSION tera -e -t "$MIAOU_BASEDIR/templates/apps/dolibarr/host.j2" -o "$MIAOU_CONFIGDIR/apps/dolibarr/$shortname/host.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml" >/dev/null
  53. echo "copying configuration files to container <$container>... "
  54. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/dolibarr/$shortname/host.conf" "$container/etc/nginx/sites-available/$longname.conf"
  55. echo "copying files to container <$container> ... OK"
  56. if ! (db-psql list | grep -q "$longname"); then
  57. echo "create empty database <$longname> ... "
  58. db-psql create "$longname"
  59. echo "create empty database <$longname> ... OK"
  60. else
  61. echo "database already exists!"
  62. fi
  63. echo "enable host config..."
  64. lxc exec "$container" -- bash <<EOF
  65. set -Eeuo pipefail
  66. cd /etc/nginx/sites-enabled
  67. ln -sf ../sites-available/$longname.conf
  68. mkdir -p /var/log/nginx/$longname
  69. nginx -t
  70. systemctl reload nginx
  71. EOF
  72. echo "enable host config... 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:,domain:,subdomain: -- "$@")
  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="dolibarr-$shortname"
  114. shift 2
  115. ;;
  116. --domain)
  117. shift 2
  118. ;;
  119. --subdomain)
  120. shift 2
  121. ;;
  122. -c)
  123. [[ "$action" == "unset" ]] || usage
  124. action="_create"
  125. shift 1
  126. ;;
  127. -r)
  128. [[ "$action" == "unset" ]] || usage
  129. action="_read"
  130. shift 1
  131. ;;
  132. -u)
  133. [[ "$action" == "unset" ]] || usage
  134. action="_update"
  135. shift 1
  136. ;;
  137. -d)
  138. [[ "$action" == "unset" ]] || usage
  139. action="_delete"
  140. shift 1
  141. ;;
  142. --)
  143. shift
  144. break
  145. ;;
  146. *)
  147. echo "Internal error: unrecognized option: <$1>"
  148. exit 1
  149. ;;
  150. esac
  151. done
  152. [[
  153. "$action" != unset &&
  154. "$port" != unset &&
  155. "$container" != unset &&
  156. "$shortname" != unset ]] || usage
  157. . "$MIAOU_BASEDIR/lib/init.sh"
  158. $action