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.

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