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.

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