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