_backend.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/bin/bash
  2. #
  3. # functions for setting up app backend
  4. #######################################
  5. # creates REDIS db using docker
  6. # Arguments:
  7. # None
  8. #######################################
  9. backend_redis_create() {
  10. print_banner
  11. printf "${WHITE} 💻 Criando Redis & Banco Postgres...${GRAY_LIGHT}"
  12. printf "\n\n"
  13. sleep 2
  14. sudo su - root <<EOF
  15. usermod -aG docker deploy
  16. docker run --name redis-${instancia_add} -p ${redis_port}:6379 --restart always --detach redis redis-server --requirepass ${mysql_root_password}
  17. sleep 2
  18. sudo su - postgres
  19. createdb ${instancia_add};
  20. psql
  21. CREATE USER ${instancia_add} SUPERUSER INHERIT CREATEDB CREATEROLE;
  22. ALTER USER ${instancia_add} PASSWORD '${mysql_root_password}';
  23. \q
  24. exit
  25. EOF
  26. sleep 2
  27. }
  28. #######################################
  29. # sets environment variable for backend.
  30. # Arguments:
  31. # None
  32. #######################################
  33. backend_set_env() {
  34. print_banner
  35. printf "${WHITE} 💻 Configurando variáveis de ambiente (backend)...${GRAY_LIGHT}"
  36. printf "\n\n"
  37. sleep 2
  38. # ensure idempotency
  39. backend_url=$(echo "${backend_url/https:\/\/}")
  40. backend_url=${backend_url%%/*}
  41. backend_url=https://$backend_url
  42. # ensure idempotency
  43. frontend_url=$(echo "${frontend_url/https:\/\/}")
  44. frontend_url=${frontend_url%%/*}
  45. frontend_url=https://$frontend_url
  46. sudo su - deploy << EOF
  47. cat <<[-]EOF > /home/deploy/${instancia_add}/backend/.env
  48. NODE_ENV=
  49. BACKEND_URL=${backend_url}
  50. FRONTEND_URL=${frontend_url}
  51. PROXY_PORT=443
  52. PORT=${backend_port}
  53. DB_DIALECT=postgres
  54. DB_HOST=localhost
  55. DB_PORT=5432
  56. DB_USER=${instancia_add}
  57. DB_PASS=${mysql_root_password}
  58. DB_NAME=${instancia_add}
  59. JWT_SECRET=${jwt_secret}
  60. JWT_REFRESH_SECRET=${jwt_refresh_secret}
  61. REDIS_URI=redis://:${mysql_root_password}@127.0.0.1:${redis_port}
  62. REDIS_OPT_LIMITER_MAX=1
  63. REGIS_OPT_LIMITER_DURATION=3000
  64. USER_LIMIT=${max_user}
  65. CONNECTIONS_LIMIT=${max_whats}
  66. CLOSED_SEND_BY_ME=true
  67. [-]EOF
  68. EOF
  69. sleep 2
  70. }
  71. #######################################
  72. # installs node.js dependencies
  73. # Arguments:
  74. # None
  75. #######################################
  76. backend_node_dependencies() {
  77. print_banner
  78. printf "${WHITE} 💻 Instalando dependências do backend...${GRAY_LIGHT}"
  79. printf "\n\n"
  80. sleep 2
  81. sudo su - deploy <<EOF
  82. cd /home/deploy/${instancia_add}/backend
  83. npm install --force
  84. EOF
  85. sleep 2
  86. }
  87. #######################################
  88. # compiles backend code
  89. # Arguments:
  90. # None
  91. #######################################
  92. backend_node_build() {
  93. print_banner
  94. printf "${WHITE} 💻 Compilando o código do backend...${GRAY_LIGHT}"
  95. printf "\n\n"
  96. sleep 2
  97. sudo su - deploy <<EOF
  98. cd /home/deploy/${instancia_add}/backend
  99. npm run build
  100. EOF
  101. sleep 2
  102. }
  103. #######################################
  104. # updates frontend code
  105. # Arguments:
  106. # None
  107. #######################################
  108. backend_update() {
  109. print_banner
  110. printf "${WHITE} 💻 Atualizando o backend...${GRAY_LIGHT}"
  111. printf "\n\n"
  112. sleep 2
  113. sudo su - deploy <<EOF
  114. cd /home/deploy/${empresa_atualizar}
  115. pm2 stop ${empresa_atualizar}-backend
  116. git pull
  117. cd /home/deploy/${empresa_atualizar}/backend
  118. npm install
  119. npm update -f
  120. npm install @types/fs-extra
  121. rm -rf dist
  122. npm run build
  123. npx sequelize db:migrate
  124. npx sequelize db:migrate
  125. npx sequelize db:seed
  126. pm2 start ${empresa_atualizar}-backend
  127. pm2 save
  128. EOF
  129. sleep 2
  130. }
  131. #######################################
  132. # runs db migrate
  133. # Arguments:
  134. # None
  135. #######################################
  136. backend_db_migrate() {
  137. print_banner
  138. printf "${WHITE} 💻 Executando db:migrate...${GRAY_LIGHT}"
  139. printf "\n\n"
  140. sleep 2
  141. sudo su - deploy <<EOF
  142. cd /home/deploy/${instancia_add}/backend
  143. npx sequelize db:migrate
  144. EOF
  145. sleep 2
  146. }
  147. #######################################
  148. # runs db seed
  149. # Arguments:
  150. # None
  151. #######################################
  152. backend_db_seed() {
  153. print_banner
  154. printf "${WHITE} 💻 Executando db:seed...${GRAY_LIGHT}"
  155. printf "\n\n"
  156. sleep 2
  157. sudo su - deploy <<EOF
  158. cd /home/deploy/${instancia_add}/backend
  159. npx sequelize db:seed:all
  160. EOF
  161. sleep 2
  162. }
  163. #######################################
  164. # starts backend using pm2 in
  165. # production mode.
  166. # Arguments:
  167. # None
  168. #######################################
  169. backend_start_pm2() {
  170. print_banner
  171. printf "${WHITE} 💻 Iniciando pm2 (backend)...${GRAY_LIGHT}"
  172. printf "\n\n"
  173. sleep 2
  174. sudo su - deploy <<EOF
  175. cd /home/deploy/${instancia_add}/backend
  176. pm2 start dist/server.js --name ${instancia_add}-backend
  177. EOF
  178. sleep 2
  179. }
  180. #######################################
  181. # updates frontend code
  182. # Arguments:
  183. # None
  184. #######################################
  185. backend_nginx_setup() {
  186. print_banner
  187. printf "${WHITE} 💻 Configurando nginx (backend)...${GRAY_LIGHT}"
  188. printf "\n\n"
  189. sleep 2
  190. backend_hostname=$(echo "${backend_url/https:\/\/}")
  191. sudo su - root << EOF
  192. cat > /etc/nginx/sites-available/${instancia_add}-backend << 'END'
  193. server {
  194. server_name $backend_hostname;
  195. location / {
  196. proxy_pass http://127.0.0.1:${backend_port};
  197. proxy_http_version 1.1;
  198. proxy_set_header Upgrade \$http_upgrade;
  199. proxy_set_header Connection 'upgrade';
  200. proxy_set_header Host \$host;
  201. proxy_set_header X-Real-IP \$remote_addr;
  202. proxy_set_header X-Forwarded-Proto \$scheme;
  203. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  204. proxy_cache_bypass \$http_upgrade;
  205. }
  206. }
  207. END
  208. ln -s /etc/nginx/sites-available/${instancia_add}-backend /etc/nginx/sites-enabled
  209. EOF
  210. sleep 2
  211. }