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.3 KiB

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_DIR/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 onto 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 over 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: -- "$@")
  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. -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