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.

208 lines
5.3 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 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 to 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 to 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:,domain:,subdomain: -- "$@")
  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. --domain)
  133. shift 2
  134. ;;
  135. --subdomain)
  136. shift 2
  137. ;;
  138. -c)
  139. [[ "$action" == "unset" ]] || usage
  140. action="_create"
  141. shift 1
  142. ;;
  143. -r)
  144. [[ "$action" == "unset" ]] || usage
  145. action="_read"
  146. shift 1
  147. ;;
  148. -u)
  149. [[ "$action" == "unset" ]] || usage
  150. action="_update"
  151. shift 1
  152. ;;
  153. -d)
  154. [[ "$action" == "unset" ]] || usage
  155. action="_delete"
  156. shift 1
  157. ;;
  158. --)
  159. shift
  160. break
  161. ;;
  162. *)
  163. echoerr "Internal error: unrecognized option: <$1>"
  164. exit 1
  165. ;;
  166. esac
  167. done
  168. . "$MIAOU_BASEDIR/lib/init.sh"
  169. [[
  170. "$action" != unset &&
  171. "$port" != unset &&
  172. "$container" != unset &&
  173. "$shortname" != unset ]] || usage
  174. $action