-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·301 lines (239 loc) · 6.69 KB
/
deploy.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/bash
# Colores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
# Función de logging
log() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
error() {
echo -e "${RED}[ERROR] $1${NC}"
exit 1
}
success() {
echo -e "${GREEN}[SUCCESS] $1${NC}"
}
# Verificar dependencias
check_dependencies() {
log "Verificando dependencias..."
command -v node >/dev/null 2>&1 || error "Node.js no está instalado"
command -v npm >/dev/null 2>&1 || error "npm no está instalado"
command -v docker >/dev/null 2>&1 || error "Docker no está instalado"
command -v docker-compose >/dev/null 2>&1 || error "Docker Compose no está instalado"
success "Todas las dependencias están instaladas"
}
# Crear estructura de directorios
create_directory_structure() {
log "Creando estructura de directorios..."
mkdir -p .github/workflows
mkdir -p scripts/{backup,maintenance,deployment}
mkdir -p config/{nginx,security}
mkdir -p docker
success "Estructura de directorios creada"
}
# Crear archivos de configuración
create_config_files() {
log "Creando archivos de configuración..."
# Crear .env.development
cat > .env.development << 'EOL'
NODE_ENV=development
API_URL=http://localhost:3000
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/netfixhub_dev
JWT_SECRET=dev_secret_key_change_in_production
STRIPE_API_KEY=sk_test_your_stripe_test_key
STRIPE_WEBHOOK_SECRET=whsec_your_stripe_webhook_secret
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_your_stripe_publishable_key
EOL
# Crear .env.production
cat > .env.production << 'EOL'
NODE_ENV=production
API_URL=https://api.netfixhub.com
DATABASE_URL=postgresql://user:password@production-host:5432/netfixhub_prod
JWT_SECRET=your_secure_production_jwt_secret
STRIPE_API_KEY=sk_live_your_stripe_live_key
STRIPE_WEBHOOK_SECRET=whsec_your_stripe_webhook_secret
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_your_stripe_publishable_key
CLOUDFLARE_API_TOKEN=your_cloudflare_api_token
CLOUDFLARE_ZONE_ID=your_cloudflare_zone_id
CLOUDFLARE_ACCOUNT_ID=your_cloudflare_account_id
EOL
# Crear docker-compose.yml
cat > docker-compose.yml << 'EOL'
version: '3.8'
services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- API_URL=http://backend:8000
depends_on:
- backend
backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/netfixhub
depends_on:
- db
db:
image: postgres:15-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_DB=netfixhub
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
EOL
# Crear GitHub Actions workflow
cat > .github/workflows/main.yml << 'EOL'
name: CI/CD Pipeline
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: netfixhub_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Test and Deploy
run: |
# Tus pasos de test
echo "Running tests..."
# Si los tests pasan, desplegar
if [ $? -eq 0 ]; then
echo "Tests passed, deploying..."
else
echo "Tests failed!"
exit 1
fi
EOL
success "Archivos de configuración creados"
}
# Inicializar proyecto
initialize_project() {
log "Inicializando proyecto..."
# Instalar dependencias del frontend
cd frontend
npm install
cd ..
# Instalar dependencias del backend
cd backend
pip install -r requirements.txt
cd ..
success "Proyecto inicializado"
}
# Configurar base de datos
setup_database() {
log "Configurando base de datos..."
docker-compose up -d db
sleep 5 # Esperar a que la base de datos esté lista
cd backend
python manage.py migrate
python scripts/create_superuser.py
cd ..
success "Base de datos configurada"
}
# Desplegar aplicación
deploy_application() {
log "Desplegando aplicación..."
# Construir frontend
cd frontend
npm run build
cd ..
# Construir y desplegar con Docker Compose
docker-compose up -d --build
success "Aplicación desplegada"
}
# Configurar Cloudflare
setup_cloudflare() {
log "Configurando Cloudflare..."
if [ -z "$CLOUDFLARE_API_TOKEN" ]; then
error "CLOUDFLARE_API_TOKEN no está configurado"
fi
# Desplegar a Cloudflare Pages
wrangler deploy
success "Cloudflare configurado"
}
# Función principal
main() {
log "Iniciando despliegue de CyberAegis..."
check_dependencies
create_directory_structure
create_config_files
initialize_project
setup_database
deploy_application
setup_cloudflare
success "¡Despliegue completado exitosamente!"
log "La aplicación está disponible en: https://cyberaegis.hapariciop.uk"
log "Panel de administración: https://cyberaegis.hapariciop.uk/admin"
}
# Ejecutar el script
main
# Crear script de mantenimiento
cat > scripts/maintenance/update.sh << 'EOL'
#!/bin/bash
# Actualizar dependencias
cd frontend && npm update
cd ../backend && pip install -r requirements.txt --upgrade
# Realizar backup
./scripts/backup/backup.sh
# Actualizar certificados SSL
./scripts/maintenance/update_ssl_certs.sh
# Reiniciar servicios
docker-compose restart
EOL
chmod +x scripts/maintenance/update.sh
# Crear script de backup
cat > scripts/backup/backup.sh << 'EOL'
#!/bin/bash
BACKUP_DIR="/backups/netfixhub"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# Backup de la base de datos
docker-compose exec db pg_dump -U postgres netfixhub > "$BACKUP_DIR/db_backup_$TIMESTAMP.sql"
# Comprimir backup
tar -czf "$BACKUP_DIR/backup_$TIMESTAMP.tar.gz" "$BACKUP_DIR/db_backup_$TIMESTAMP.sql"
# Limpiar backups antiguos
find $BACKUP_DIR -type f -mtime +7 -delete
EOL
chmod +x scripts/backup/backup.sh
# Crear script de monitoreo
cat > scripts/maintenance/monitor.sh << 'EOL'
#!/bin/bash
# Verificar servicios
docker-compose ps
# Verificar uso de recursos
docker stats --no-stream
# Verificar logs
docker-compose logs --tail=100
EOL
chmod +x scripts/maintenance/monitor.sh