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.

186 lines
4.3 KiB

  1. #!/bin/bash
  2. set -e
  3. PROJECT=$1
  4. if [[ -z "${PROJECT}" ]]; then
  5. echo 'Usage: ./create-stack-project PROJECT [SEAFILE_DUMPS_LIB_UUID]';
  6. exit 1;
  7. fi
  8. test -f .env || {
  9. echo 'No env';
  10. exit 1;
  11. }
  12. echo
  13. source .env
  14. P_UID=1000
  15. P_GID=1000
  16. mkdir /opt/stacks/${PROJECT}
  17. cat << EOF > /opt/stacks/${PROJECT}/compose.yaml
  18. name: ${PROJECT}
  19. services:
  20. nginx:
  21. extends:
  22. file: /opt/structure/docker-compose.base.yml
  23. service: nginx
  24. php:
  25. extends:
  26. file: /opt/structure/docker-compose.base.yml
  27. service: php
  28. build:
  29. args:
  30. - PHP_VERSION=8.1
  31. workspace:
  32. extends:
  33. file: /opt/structure/docker-compose.base.yml
  34. service: workspace
  35. build:
  36. args:
  37. - PROJECT=\${PROJECT}
  38. - PHP_VERSION=8.1
  39. expose:
  40. - 22
  41. working_dir: /var/www/${PROJECT}
  42. volumes:
  43. - /opt/projects/${PROJECT}/profiler:/tmp/profiler
  44. - /opt/projects/${PROJECT}/www:/var/www
  45. mariadb:
  46. extends:
  47. file: /opt/structure/docker-compose.base.yml
  48. service: mariadb
  49. redis:
  50. extends:
  51. file: /opt/structure/docker-compose.base.yml
  52. service: redis
  53. seafile:
  54. extends:
  55. file: /opt/structure/docker-compose.base.yml
  56. service: seafile
  57. secrets:
  58. user_authorized_keys:
  59. file: /opt/projects/\${PROJECT}/ssh/authorized_keys
  60. user_ssh_key:
  61. file: /opt/projects/\${PROJECT}/ssh/id_ed25519
  62. user_ssh_key_pub:
  63. file: /opt/projects/\${PROJECT}/ssh/id_ed25519.pub
  64. composer_auth:
  65. file: /opt/projects/\${PROJECT}/config/composer/auth.json
  66. networks:
  67. dockge_default:
  68. external: true
  69. structure:
  70. volumes:
  71. seafile:
  72. EOF
  73. SEAFILE_USER="${MACHINE}-workspace-${PROJECT}@dimti.ru"
  74. SEAFILE_PASS=$(pwgen -s 20 1)
  75. SEAFILE_DUMPS_LIB=$2
  76. echo "Seafile credentionals for create new account:"
  77. echo "SEAFILE_USER=${SEAFILE_USER}"
  78. echo "SEAFILE_PASS=${SEAFILE_PASS}"
  79. cat << EOF > /opt/stacks/${PROJECT}/.env
  80. PROJECT=${PROJECT}
  81. MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
  82. SEAFILE_USER=${SEAFILE_USER}
  83. SEAFILE_PASS=${SEAFILE_PASS}
  84. SEAFILE_DUMPS_LIB=${SEAFILE_DUMPS_LIB}
  85. EOF
  86. chown -R $_PUID:$P_GID /opt/stacks/${PROJECT}
  87. PROJECT_DIR=/opt/projects/${PROJECT}
  88. CONFIG_DIR=${PROJECT_DIR}/config
  89. HOSTFILES_DIR=${PROJECT_DIR}/hostfiles
  90. LOGS_DIR=${PROJECT_DIR}/logs
  91. MARIADB_DIR=${PROJECT_DIR}/mariadb
  92. PROFILER_DIR=${PROJECT_DIR}/profiler
  93. SSH_DIR=${PROJECT_DIR}/ssh
  94. WWW_DIR=${PROJECT_DIR}/www
  95. mkdir -p ${CONFIG_DIR}/composer
  96. mkdir -p ${CONFIG_DIR}/mariadb
  97. mkdir -p ${CONFIG_DIR}/nginx/conf.d
  98. mkdir -p ${CONFIG_DIR}/nginx/includes.d
  99. mkdir -p ${CONFIG_DIR}/nginx/ssl.d
  100. mkdir -p ${CONFIG_DIR}/redis
  101. cat << EOF > ${CONFIG_DIR}/composer/auth.json
  102. {
  103. "github-oauth": {
  104. "github.com": ""
  105. }
  106. }
  107. EOF
  108. cat << EOF > ${CONFIG_DIR}/mariadb/90-mysqld.cnf
  109. [mysqld]
  110. EOF
  111. cat << EOF > ${CONFIG_DIR}/nginx/conf.d/vhosts.conf
  112. upstream php {
  113. server php:9000;
  114. }
  115. map \$http_host \$root {
  116. ${PROJECT}.local.wpstudio.ru /var/www/${PROJECT};
  117. }
  118. server {
  119. listen 80 default;
  120. root \$root;
  121. include includes.d/octobercms.conf;
  122. include includes.d/staticfiles.conf;
  123. client_max_body_size 300M;
  124. large_client_header_buffers 4 32k;
  125. location ~ ^/index.php {
  126. fastcgi_pass php;
  127. include fastcgi_params;
  128. fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  129. fastcgi_param SERVER_NAME \$host;
  130. }
  131. }
  132. EOF
  133. cp /opt/stacks/structure/config/nginx/includes.d/octobercms.conf ${CONFIG_DIR}/nginx/includes.d/
  134. cp /opt/stacks/structure/config/nginx/includes.d/staticfiles.conf ${CONFIG_DIR}/nginx/includes.d/
  135. cat << EOF > ${CONFIG_DIR}/redis/redis-local.conf
  136. port 6379
  137. tcp-backlog 128
  138. protected-mode no
  139. stop-writes-on-bgsave-error no
  140. databases 4
  141. always-show-logo no
  142. syslog-enabled yes
  143. pidfile /var/run/redis_6379.pid
  144. loglevel notice
  145. logfile ""
  146. EOF
  147. chown $P_UID:$P_GID -R ${CONFIG_DIR}
  148. mkdir ${HOSTFILES_DIR}
  149. chown $P_UID:$P_GID ${HOSTFILES_DIR}
  150. mkdir -p ${LOGS_DIR}/mariadb
  151. mkdir -p ${LOGS_DIR}/nginx
  152. mkdir -p ${LOGS_DIR}/php-fpm
  153. mkdir ${MARIADB_DIR}
  154. mkdir ${PROFILER_DIR}
  155. mkdir ${SSH_DIR}
  156. touch ${SSH_DIR}/authorized_keys
  157. ssh-keygen -N "" -t ed25519 -f ${SSH_DIR}/id_ed25519 -C workspace-${PROJECT}@${MACHINE} 2>&1 > /dev/null
  158. echo "SSH Key:"
  159. cat ${SSH_DIR}/id_ed25519.pub
  160. ssh-keyscan -H github.com > ${SSH_DIR}/known_hosts 2>/dev/null
  161. chown $P_UID:$P_GID -R ${SSH_DIR}
  162. mkdir -p ${WWW_DIR}/${PROJECT}
  163. chown -R $P_UID:$P_GID ${WWW_DIR}