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.

create.sh 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #! /bin/sh
  2. #
  3. if [ ! -f "${PWD}/.env" ]; then
  4. echo "missing env file in ${PWD}"
  5. exit 1
  6. fi
  7. set -a
  8. . "${PWD}/.env"
  9. . "${PWD}/env_files/certs.env"
  10. . "${PWD}/env_files/project.env"
  11. set +a
  12. mkdir -p "/tmp/${PREFIX}"
  13. echo "Setting up docker volumes"
  14. docker volume create "${PREFIX}-db"
  15. docker volume create "${PREFIX}-root"
  16. docker volume create "${PREFIX}-traefik-certs"
  17. docker volume create "${PREFIX}-traefik-dynamic"
  18. docker volume create "${PREFIX}-traefik-logs"
  19. docker volume create "${PREFIX}-traefik-static"
  20. docker volume create "${PREFIX}-webroot"
  21. docker volume create wp-cli-cache
  22. docker volume create composer-cache
  23. echo "Copying SSL certificates to traefik volume"
  24. if [ ! -f "${SSL_CRT_LOCATION}/${SSL_CRT_NAME}" ] || [ ! -f "${SSL_KEY_LOCATION}/${SSL_KEY_NAME}" ]; then
  25. echo "Missing SSL key or cert file"
  26. exit 1
  27. fi
  28. docker run \
  29. --rm \
  30. --volume "${PREFIX}-traefik-certs":/certs \
  31. --volume "${SSL_CRT_LOCATION}":/source \
  32. ubuntu \
  33. cp "/source/${SSL_CRT_NAME}" /certs
  34. docker run \
  35. --rm \
  36. --volume "${PREFIX}-traefik-certs":/certs \
  37. --volume "${SSL_KEY_LOCATION}":/source \
  38. ubuntu \
  39. cp "/source/${SSL_KEY_NAME}" /certs
  40. echo "Generating traefik configuration files (ssl.yml and middlewares.yml)"
  41. cat << EOF > /tmp/${PREFIX}/ssl.yml
  42. ---
  43. tls:
  44. stores:
  45. default:
  46. defaultCertificate:
  47. certFile: /certs/${SSL_CRT_NAME}
  48. keyFile: /certs/${SSL_KEY_NAME}
  49. EOF
  50. cat << EOF > /tmp/${PREFIX}/middlewares.yml
  51. ---
  52. http:
  53. middlewares:
  54. https-redirect:
  55. redirectscheme:
  56. scheme: https
  57. permanent: true
  58. EOF
  59. docker run \
  60. --rm \
  61. --volume "/tmp/${PREFIX}":/source \
  62. --volume "${PREFIX}-traefik-dynamic":/destination \
  63. ubuntu \
  64. cp /source/ssl.yml /source/middlewares.yml /destination
  65. echo "Generating traefik static configuration"
  66. cat << EOF > /tmp/${PREFIX}/static.yml
  67. ---
  68. api:
  69. dashboard: true
  70. entrypoints:
  71. http:
  72. address: :80
  73. https:
  74. address: :443
  75. log:
  76. filepath: /logs/traefik.log
  77. level: debug
  78. providers:
  79. docker:
  80. exposedbydefault: false
  81. file:
  82. directory: /etc/traefik/dynamic
  83. watch: true
  84. EOF
  85. docker run \
  86. --rm \
  87. --volume "/tmp/${PREFIX}":/source \
  88. --volume "${PREFIX}-traefik-static":/destination \
  89. ubuntu \
  90. cp /source/static.yml /destination/traefik.yml
  91. docker compose up -d app
  92. while ! docker ps -q -f name="${PREFIX}-app"; do
  93. echo "Waiting for the web container to be up and running..."
  94. sleep 1
  95. done
  96. docker compose up -d db
  97. while ! docker ps -q -f name="${PREFIX}-db"; do
  98. echo "Waiting for the db container to be up and running..."
  99. sleep 1
  100. done
  101. while ! docker exec "${PREFIX}-app" /bin/sh -c "mysqladmin ping -h ${PREFIX}-db -P 3306 --protocol=tcp -u user -puser --silent"; do
  102. echo "Waiting for the mysql server in the db container to be up and running and reachable from the app container..."
  103. sleep 1
  104. done
  105. if [ "${PROJECT_TYPE}" = "laravel" ]; then
  106. echo "Installing laravel"
  107. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "composer create-project --prefer-dist laravel/laravel /var/www/html '${LAREVEL_VERSION}'"
  108. fi
  109. if [ "${PROJECT_TYPE}" = "livewire" ]; then
  110. echo "Installing livewire"
  111. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "composer create-project --prefer-dist laravel/laravel /var/www/html '${LARAVEL_VERSION}'"
  112. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "composer require livewire/livewire ${LIVEWIRE_VERSION}"
  113. fi
  114. if [ "${PROJECT_TYPE}" = "wintercms" ]; then
  115. echo "Installing wintercms"
  116. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "composer create-project wintercms/winter /var/www/html"
  117. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "echo ASSET_URL=https://${APP_URL} >> /var/www/html/.env"
  118. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "echo DB_DATABASE=${DB_NAME} >> /var/www/html/.env"
  119. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "echo DB_USERNAME=${DB_USER} >> /var/www/html/.env"
  120. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "echo DB_PASSWORD=${DB_USER_PASSWORD} >> /var/www/html/.env"
  121. docker exec --user www-data --workdir "/var/www/html" "${PREFIX}-app" /bin/sh -c "echo DB_HOST=${PREFIX}-db >> /var/www/html/.env"
  122. docker exec --user root "${PREFIX}-app" /bin/sh -c "sed -i 's|/var/www/html/public|/var/www/html|g' /etc/apache2/sites-available/000-default.conf"
  123. docker restart "${PREFIX}-app"
  124. docker exec --user www-data --workdir "/var/www/html" -it "${PREFIX}-app" /bin/sh -c "php artisan winter:install"
  125. fi
  126. if [ "${PROJECT_TYPE}" = "laravel" ] || [ "${PROJECT_TYPE}" = "livewire" ] || [ "${PROJECT_TYPE}" = "php" ]; then
  127. echo "Generating vscode launch.json file"
  128. cat << EOF > /tmp/${PREFIX}/launch.json
  129. {
  130. "version": "0.2.0",
  131. "configurations": [
  132. {
  133. "hostname": "${TRAEFIK_LISTENING_IP}",
  134. "name": "Listen for Xdebug (${TRAEFIK_LISTENING_IP})",
  135. "pathMappings": {
  136. "/var/www/html/": "\${workspaceRoot}/",
  137. },
  138. "port": 9003,
  139. "request": "launch",
  140. "type": "php"
  141. }
  142. ]
  143. }
  144. EOF
  145. docker run \
  146. --rm \
  147. --user www-data \
  148. --volume "${PREFIX}-app":/destination \
  149. ubuntu \
  150. mkdir --parents /destination/.vscode
  151. docker run \
  152. --rm \
  153. --user www-data \
  154. --volume "${PREFIX}-app":/destination \
  155. ubuntu \
  156. cp /source/launch.json /destination/.vscode
  157. docker run \
  158. --rm \
  159. --user root \
  160. --volume "${PREFIX}-app":/destination \
  161. ubuntu \
  162. chown www-data:www-data /destination/.vscode -r
  163. fi
  164. if [ "${PROJECT_TYPE}" = "wordpress" ]; then
  165. echo "Installing wppb-cli"
  166. composer global require tmeister/wppb-cli
  167. echo "Downloading WordPress core"
  168. docker exec --user www-data "${PREFIX}-app" /bin/sh -c "
  169. wp core download \
  170. --locale=${WP_LOCALE} \
  171. --path=/var/www/html \
  172. --version=${WP_VERSION}"
  173. echo "Creating WordPress config"
  174. docker exec --user www-data "${PREFIX}-app" /bin/sh -c '
  175. wp config create \
  176. --dbhost='"${PREFIX}-db"' \
  177. --dbname='"${DB_NAME}"' \
  178. --dbpass='"${DB_USER_PASSWORD}"' \
  179. --dbuser='"${DB_USER}"' \
  180. --force \
  181. --path=/var/www/html \
  182. --skip-check \
  183. --extra-php <<EXTRA-PHP
  184. if (isset(\$_SERVER["HTTP_X_FORWARDED_PROTO"]) && \$_SERVER["HTTP_X_FORWARDED_PROTO"] == "https") \$_SERVER["HTTPS"]="on";
  185. EXTRA-PHP
  186. '
  187. echo "Installing WordPress core"
  188. docker exec --user www-data "${PREFIX}-app" /bin/sh -c "
  189. wp core install \
  190. --admin_email=no@mail.com \
  191. --admin_password=${WP_ADMIN_PASSWORD} \
  192. --admin_user=${WP_ADMIN_USERNAME} \
  193. --path=/var/www/html \
  194. --skip-email \
  195. --title=${PREFIX} \
  196. --url=${WP_DEFAULT_PROTOCOL}://${APP_URL}"
  197. echo "Installing WordPress "${WP_THEME}" theme"
  198. docker exec --user www-data "${PREFIX}-app" /bin/sh -c "
  199. wp theme install ${WP_THEME} \
  200. --activate \
  201. --path=/var/www/html"
  202. fi
  203. docker compose up -d adminer
  204. docker compose up -d app
  205. docker compose up -d traefik