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.

202 lines
5.2 KiB

8 months ago
  1. #!/bin/bash
  2. function check_database_exists() {
  3. db-maria list | grep -q "$longname"
  4. }
  5. function check_port_used() {
  6. # shellcheck disable=SC2034
  7. usedport=$(lxc exec "$container" -- bash -c "grep listen /etc/nginx/sites-enabled/$longname.conf | cut -d' ' -f6")
  8. [[ "$usedport" == "$port" ]]
  9. }
  10. function check_service_running() {
  11. lxc exec "$container" -- bash -c "systemctl is-active --quiet nginx.service"
  12. }
  13. function _read() {
  14. disable_trace
  15. check_database_exists
  16. check_container "$container"
  17. check_port_used
  18. check_service_running
  19. enable_trace
  20. return 0
  21. }
  22. function _create() {
  23. echo "creating wordpress instance for <$shortname> ... "
  24. mkdir -p "$MIAOU_CONFIGDIR/apps/wordpress"
  25. APP_PORT=$port APP_NAME=$shortname tera -e --env-key env -t "$MIAOU_BASEDIR/templates/apps/wordpress/wp-host.j2" -o "$MIAOU_CONFIGDIR/apps/wordpress/$longname.conf" "$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  26. echo "creating templates ... OK"
  27. echo "copying files over container <$container> ... "
  28. lxc file push --uid 0 --gid 0 "$MIAOU_CONFIGDIR/apps/wordpress/$longname.conf" "$container/etc/nginx/sites-available/$longname.conf"
  29. echo "copying files over container <$container> ... OK"
  30. if ! (db-maria list | grep -q "$longname"); then
  31. echo "create empty database <$longname> ... "
  32. db-maria create "$longname"
  33. echo "create empty database <$longname> ... OK"
  34. else
  35. echo "database already exists!"
  36. fi
  37. echo "initialize wordpress $shortname $longname ..."
  38. lxc exec "$container" -- bash <<EOF
  39. set -Eeuo pipefail
  40. if [[ ! -d /var/www/wordpress/$shortname ]]; then
  41. echo "installing new instance of wordpress into /var/www/wordpress/$shortname"
  42. mkdir -p /tmp/$shortname
  43. tar -xzf /var/www/wordpress-latest.tgz -C /tmp/$shortname
  44. mv /tmp/$shortname/wordpress /var/www/wordpress/$shortname
  45. else
  46. echo "instance of wordpress /var/www/wordpress/$shortname already defined!"
  47. fi
  48. if [[ ! -f /var/www/wordpress/$shortname/wp-config.php ]]; then
  49. echo "create wp-config.php"
  50. cat << 'EOT2' > /var/www/wordpress/$shortname/wp-config.php
  51. <?php
  52. define( 'DB_NAME', '$longname' );
  53. define( 'DB_USER', '$longname' );
  54. define( 'DB_PASSWORD', '$longname' );
  55. define( 'DB_HOST', 'ct1.lxd' );
  56. define( 'DB_CHARSET', 'utf8' );
  57. define( 'DB_COLLATE', '' );
  58. define( 'AUTH_KEY', '$(genpasswd 20)' );
  59. define( 'SECURE_AUTH_KEY', '$(genpasswd 20)' );
  60. define( 'LOGGED_IN_KEY', '$(genpasswd 20)' );
  61. define( 'NONCE_KEY', '$(genpasswd 20)' );
  62. define( 'AUTH_SALT', '$(genpasswd 20)' );
  63. define( 'SECURE_AUTH_SALT', '$(genpasswd 20)' );
  64. define( 'LOGGED_IN_SALT', '$(genpasswd 20)' );
  65. define( 'NONCE_SALT', '$(genpasswd 20)' );
  66. \$table_prefix = 'wp_';
  67. define( 'WP_DEBUG', false );
  68. /* Add any custom values between this line and the "stop editing" line. */
  69. \$_SERVER['HTTPS'] = 'on';
  70. /* That's all, stop editing! Happy publishing. */
  71. /** Absolute path to the WordPress directory. */
  72. if ( ! defined( 'ABSPATH' ) ) {
  73. define( 'ABSPATH', __DIR__ . '/' );
  74. }
  75. /** Sets up WordPress vars and included files. */
  76. require_once ABSPATH . 'wp-settings.php';
  77. EOT2
  78. else
  79. echo "wp-config.php already defined!"
  80. fi
  81. chown -R www-data:www-data /var/www/wordpress/$shortname
  82. echo "enabling wordpress host into nginx"
  83. mkdir -p /var/log/nginx/$shortname
  84. ln -sf "/etc/nginx/sites-available/$longname.conf" "/etc/nginx/sites-enabled/$longname.conf"
  85. echo "reloading nginx"
  86. nginx -tq && systemctl reload nginx
  87. EOF
  88. echo "initialize wordpress $shortname $longname ... OK"
  89. }
  90. function _update() {
  91. echo "update"
  92. }
  93. function _delete() {
  94. echo "delete"
  95. }
  96. function usage() {
  97. echo "Usage: $COMMAND_NAME -c|r|u|d --port PORT --container CONTAINER --name NAME"
  98. exit 2
  99. }
  100. ### MAIN
  101. # init_strict
  102. COMMAND_NAME=$(basename "$0")
  103. # read the options
  104. TEMP=$(getopt -n "$COMMAND_NAME" -o crud --long port:,container:,name:,fqdn: -- "$@")
  105. # shellcheck disable=SC2181
  106. [[ "$?" -eq 0 ]] || usage
  107. eval set -- "$TEMP"
  108. action="unset"
  109. port="unset"
  110. container="unset"
  111. shortname="unset"
  112. longname="unset"
  113. # extract options and their arguments into variables.
  114. while true; do
  115. case "$1" in
  116. --port)
  117. port=$2
  118. shift 2
  119. ;;
  120. --fqdn)
  121. shift 2
  122. ;;
  123. --container)
  124. container=$2
  125. shift 2
  126. ;;
  127. --name)
  128. shortname=$2
  129. longname="wp-$shortname"
  130. shift 2
  131. ;;
  132. -c)
  133. [[ "$action" == "unset" ]] || usage
  134. action="_create"
  135. shift 1
  136. ;;
  137. -r)
  138. [[ "$action" == "unset" ]] || usage
  139. action="_read"
  140. shift 1
  141. ;;
  142. -u)
  143. [[ "$action" == "unset" ]] || usage
  144. action="_update"
  145. shift 1
  146. ;;
  147. -d)
  148. [[ "$action" == "unset" ]] || usage
  149. action="_delete"
  150. shift 1
  151. ;;
  152. --)
  153. shift
  154. break
  155. ;;
  156. *)
  157. echo "Internal error!"
  158. exit 1
  159. ;;
  160. esac
  161. done
  162. . "$MIAOU_BASEDIR/lib/init.sh"
  163. [[
  164. "$action" != unset &&
  165. "$port" != unset &&
  166. "$container" != unset &&
  167. "$shortname" != unset ]] || usage
  168. $action