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

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