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.

163 lines
4.2 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. #!/bin/bash
  2. function check_service_running() {
  3. lxc exec "$container" -- bash -c "docker ps --format '{{.Names}},{{.Ports}}' | grep $1 | grep $2"
  4. }
  5. function _read() {
  6. disable_trace
  7. check_db_postgres_exists "$longname"
  8. check_container "$container"
  9. check_service_running "$longname" "$port"
  10. enable_trace
  11. return 0
  12. }
  13. function _create() {
  14. echo "creating discourse instance for <$shortname> ... "
  15. echo "initialize discourse $shortname $longname ... OK"
  16. admin_username=$(load_yaml_from_expanded services[\""$domain"\"][\""$subdomain"\"].data.admin.username)
  17. admin_email=$(load_yaml_from_expanded services[\""$domain"\"][\""$subdomain"\"].data.admin.email)
  18. admin_password=$(load_yaml_from_expanded services[\""$domain"\"][\""$subdomain"\"].data.admin.password)
  19. redis_password=$(load_yaml_from_expanded credential.redis)
  20. mkdir -p "$MIAOU_CONFIGDIR/apps/discourse"
  21. APP_REDIS_PASSWORD=$redis_password APP_DOMAIN=$domain APP_SUBDOMAIN=$subdomain APP_FQDN=$fqdn APP_PORT=$port APP_NAME=$longname tera -e --env-key env -t "$MIAOU_BASEDIR/templates/apps/discourse/forum.yml.j2" -o "$MIAOU_CONFIGDIR/apps/discourse/$longname.yml" "$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  22. echo "creating templates ... OK"
  23. echo "copying files to container <$container> ... "
  24. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/discourse/$longname.yml" "$container/var/discourse/containers/$longname.yml"
  25. echo "copying files to container <$container> ... OK"
  26. if ! (db-psql list | grep -q "$longname"); then
  27. echo "create empty database <$longname> ... "
  28. db-psql create "$longname"
  29. db-psql use "$longname" "CREATE EXTENSION IF NOT EXISTS hstore"
  30. db-psql use "$longname" "CREATE EXTENSION IF NOT EXISTS pg_trgm"
  31. echo "create empty database <$longname> ... OK"
  32. else
  33. echo "database already exists!"
  34. fi
  35. echo "build docker container $longname ..."
  36. lxc exec "$container" -- bash <<EOF
  37. set -Eeuo pipefail
  38. cd /var/discourse
  39. ./launcher rebuild $longname
  40. command='u=User.create_with(email: "$admin_email", password: "$admin_password").find_or_initialize_by(username: "$admin_username"); u.save; u.activate'
  41. ./launcher run $longname "rails runner '\$command'"
  42. EOF
  43. echo "initialize discourse $longname ... OK"
  44. }
  45. function _update() {
  46. echo "update"
  47. }
  48. function _delete() {
  49. echo "delete"
  50. }
  51. function usage() {
  52. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  53. exit 2
  54. }
  55. ### MAIN
  56. # init_strict
  57. source "$(dirname "$0")/../base/common.sh"
  58. COMMAND_NAME=$(basename "$0")
  59. # read the options
  60. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn:,domain:,subdomain:,data: -- "$@")
  61. # shellcheck disable=SC2181
  62. [[ "$?" -eq 0 ]] || usage
  63. eval set -- "$TEMP"
  64. action="unset"
  65. port="unset"
  66. container="unset"
  67. shortname="unset"
  68. longname="unset"
  69. fqdn="unset"
  70. domain="unset"
  71. subdomain="unset"
  72. # extract options and their arguments into variables.
  73. while true; do
  74. case "$1" in
  75. --port)
  76. port=$2
  77. shift 2
  78. ;;
  79. --fqdn)
  80. fqdn=$2
  81. shift 2
  82. ;;
  83. --container)
  84. container=$2
  85. shift 2
  86. ;;
  87. --name)
  88. shortname=$2
  89. longname="discourse-$shortname"
  90. shift 2
  91. ;;
  92. --domain)
  93. domain=$2
  94. shift 2
  95. ;;
  96. --subdomain)
  97. subdomain=$2
  98. shift 2
  99. ;;
  100. --data)
  101. # don't care
  102. shift 2
  103. ;;
  104. -c)
  105. [[ "$action" == "unset" ]] || usage
  106. action="_create"
  107. shift 1
  108. ;;
  109. -r)
  110. [[ "$action" == "unset" ]] || usage
  111. action="_read"
  112. shift 1
  113. ;;
  114. -u)
  115. [[ "$action" == "unset" ]] || usage
  116. action="_update"
  117. shift 1
  118. ;;
  119. -d)
  120. [[ "$action" == "unset" ]] || usage
  121. action="_delete"
  122. shift 1
  123. ;;
  124. --)
  125. shift
  126. break
  127. ;;
  128. *)
  129. echoerr "Internal error: unrecognized option: <$1>"
  130. exit 1
  131. ;;
  132. esac
  133. done
  134. [[
  135. "$action" != unset &&
  136. "$port" != unset &&
  137. "$container" != unset &&
  138. "$fqdn" != unset &&
  139. "$domain" != unset &&
  140. "$subdomain" != unset &&
  141. "$shortname" != unset ]] || usage
  142. . "$MIAOU_BASEDIR/lib/init.sh"
  143. $action