-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart6-deployment.ps1
More file actions
970 lines (813 loc) · 24.8 KB
/
part6-deployment.ps1
File metadata and controls
970 lines (813 loc) · 24.8 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# ALGODON Part 6: Deployment & CI/CD
# This script creates Docker, Kubernetes, and CI/CD configurations
Write-Host "🚀 ALGODON Part 6: Deployment & CI/CD" -ForegroundColor Cyan
Set-Location "ALGODON"
# Generate Docker Compose file
$dockerCompose = @'
version: '3.8'
services:
web:
build:
context: ./web
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- NODE_ENV=development
- ORACLE_CONNECTION_STRING=oracle://algodon:password@oracle-db:1521/XE
- REDIS_URL=redis://redis:6379
- NEXT_PUBLIC_APP_URL=http://localhost:3000
depends_on:
- oracle-db
- redis
volumes:
- ./web:/app
- /app/node_modules
- /app/.next
networks:
- algodon-network
oracle-db:
image: container-registry.oracle.com/database/express:21.3.0-xe
ports:
- "1521:1521"
- "5500:5500"
environment:
- ORACLE_PWD=password
- ORACLE_CHARACTERSET=AL32UTF8
volumes:
- oracle-data:/opt/oracle/oradata
networks:
- algodon-network
healthcheck:
test: ["CMD", "sqlplus", "-L", "algodon/password@localhost:1521/XE", "-e", "SELECT 1 FROM DUAL"]
interval: 30s
timeout: 10s
retries: 5
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis-data:/data
networks:
- algodon-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
postgres:
image: postgres:15-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_USER=algodon
- POSTGRES_PASSWORD=password
- POSTGRES_DB=algodon_analytics
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- algodon-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U algodon"]
interval: 10s
timeout: 5s
retries: 5
minio:
image: minio/minio:latest
ports:
- "9000:9000"
- "9001:9001"
environment:
- MINIO_ROOT_USER=minioadmin
- MINIO_ROOT_PASSWORD=minioadmin
volumes:
- minio-data:/data
command: server /data --console-address ":9001"
networks:
- algodon-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
volumes:
oracle-data:
redis-data:
postgres-data:
minio-data:
networks:
algodon-network:
driver: bridge
'@
$dockerCompose | Out-File -FilePath "deployment/docker/docker-compose.yml" -Encoding UTF8
Write-Host "✅ Created Docker Compose file" -ForegroundColor Green
# Generate web Dockerfile
$webDockerfile = @'
FROM node:20-alpine AS base
# Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY web/package.json web/package-lock.json* ./
RUN npm ci
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY web .
ENV NEXT_TELEMETRY_DISABLED 1
RUN npm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
CMD ["node", "server.js"]
'@
$webDockerfile | Out-File -FilePath "web/Dockerfile" -Encoding UTF8
# Generate Kubernetes deployment
$k8sDeployment = @'
apiVersion: apps/v1
kind: Deployment
metadata:
name: algodon-web
namespace: algodon
labels:
app: algodon-web
spec:
replicas: 3
selector:
matchLabels:
app: algodon-web
template:
metadata:
labels:
app: algodon-web
spec:
containers:
- name: web
image: ghcr.io/algodon/web:latest
ports:
- containerPort: 3000
env:
- name: NODE_ENV
value: "production"
- name: ORACLE_CONNECTION_STRING
valueFrom:
secretKeyRef:
name: algodon-secrets
key: oracle-connection-string
- name: REDIS_URL
valueFrom:
secretKeyRef:
name: algodon-secrets
key: redis-url
- name: STRIPE_SECRET_KEY
valueFrom:
secretKeyRef:
name: algodon-secrets
key: stripe-secret-key
- name: OPENAI_API_KEY
valueFrom:
secretKeyRef:
name: algodon-secrets
key: openai-api-key
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "1Gi"
cpu: "500m"
livenessProbe:
httpGet:
path: /api/health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /api/health
port: 3000
initialDelaySeconds: 5
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: algodon-web
namespace: algodon
spec:
selector:
app: algodon-web
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: algodon-ingress
namespace: algodon
annotations:
cert-manager.io/cluster-issuer: "letsencrypt-prod"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
ingressClassName: nginx
tls:
- hosts:
- algodon.app
- www.algodon.app
secretName: algodon-tls
rules:
- host: algodon.app
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: algodon-web
port:
number: 80
- host: www.algodon.app
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: algodon-web
port:
number: 80
'@
$k8sDeployment | Out-File -FilePath "deployment/kubernetes/web-deployment.yaml" -Encoding UTF8
Write-Host "✅ Created Kubernetes deployment" -ForegroundColor Green
# Generate GitHub Actions workflow
$githubActions = @'
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: web/package-lock.json
- name: Install dependencies
run: |
cd web
npm ci
- name: Run linter
run: |
cd web
npm run lint
- name: Run type check
run: |
cd web
npm run type-check
- name: Run tests
run: |
cd web
npm test -- --coverage
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./web/coverage/lcov.info
build:
needs: test
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/web
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ./web
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy-staging:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop'
environment:
name: staging
url: https://staging.algodon.app
steps:
- uses: actions/checkout@v4
- name: Configure kubectl
uses: azure/setup-kubectl@v3
- name: Set up Kubeconfig
run: |
echo "${{ secrets.KUBECONFIG_STAGING }}" | base64 -d > kubeconfig
export KUBECONFIG=./kubeconfig
- name: Deploy to staging
run: |
export KUBECONFIG=./kubeconfig
kubectl set image deployment/algodon-web web=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/web:develop-${{ github.sha }} -n algodon
kubectl rollout status deployment/algodon-web -n algodon
- name: Run smoke tests
run: |
sleep 30
curl -f https://staging.algodon.app/api/health || exit 1
deploy-production:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
environment:
name: production
url: https://algodon.app
steps:
- uses: actions/checkout@v4
- name: Configure kubectl
uses: azure/setup-kubectl@v3
- name: Set up Kubeconfig
run: |
echo "${{ secrets.KUBECONFIG_PRODUCTION }}" | base64 -d > kubeconfig
export KUBECONFIG=./kubeconfig
- name: Deploy to production (canary)
run: |
export KUBECONFIG=./kubeconfig
# Deploy to 10% of traffic first
kubectl set image deployment/algodon-web web=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/web:main-${{ github.sha }} -n algodon
kubectl rollout status deployment/algodon-web -n algodon --timeout=5m
- name: Run smoke tests
run: |
sleep 60
curl -f https://algodon.app/api/health || exit 1
- name: Full rollout
if: success()
run: |
export KUBECONFIG=./kubeconfig
# Scale to 100% if canary successful
kubectl scale deployment/algodon-web --replicas=5 -n algodon
kubectl rollout status deployment/algodon-web -n algodon
'@
$githubActions | Out-File -FilePath "deployment/ci-cd/.github/workflows/ci-cd.yml" -Encoding UTF8
Write-Host "✅ Created GitHub Actions workflow" -ForegroundColor Green
# Generate health check API
$healthCheckApi = @'
import { NextRequest, NextResponse } from 'next/server';
import { getDbConnection } from '@/lib/db/oracle';
import { createClient } from 'redis';
export async function GET(req: NextRequest) {
const checks: Record<string, string> = {};
// Database check
try {
const connection = await getDbConnection();
await connection.execute('SELECT 1 FROM DUAL');
await connection.close();
checks.database = 'healthy';
} catch (error) {
checks.database = 'unhealthy';
}
// Redis check
try {
const redis = createClient({ url: process.env.REDIS_URL });
await redis.connect();
await redis.ping();
await redis.quit();
checks.redis = 'healthy';
} catch (error) {
checks.redis = 'unhealthy';
}
const allHealthy = Object.values(checks).every((status) => status === 'healthy');
return NextResponse.json(
{
status: allHealthy ? 'healthy' : 'degraded',
checks,
timestamp: new Date().toISOString(),
},
{ status: allHealthy ? 200 : 503 }
);
}
'@
$healthCheckApi | Out-File -FilePath "web/app/api/health/route.ts" -Encoding UTF8
Write-Host "✅ Created health check API" -ForegroundColor Green
# Generate deployment script
$deployScript = @'
# ALGODON Deployment Script
# Usage: .\deploy.ps1 [local|staging|production]
param(
[Parameter(Mandatory=$true)]
[ValidateSet("local", "staging", "production")]
[string]$Environment
)
Write-Host "🚀 ALGODON Deployment - $Environment" -ForegroundColor Cyan
# Check prerequisites
$prerequisites = @{
"Docker" = "docker"
"Node.js" = "node"
"npm" = "npm"
}
foreach ($prereq in $prerequisites.GetEnumerator()) {
$command = Get-Command $prereq.Value -ErrorAction SilentlyContinue
if (-not $command) {
Write-Host "❌ $($prereq.Key) is not installed. Please install it first." -ForegroundColor Red
exit 1
}
Write-Host "✅ $($prereq.Key) found: $($command.Version)" -ForegroundColor Green
}
switch ($Environment) {
"local" {
Write-Host "`n📦 Starting local development environment..." -ForegroundColor Yellow
# Check if .env file exists
if (-not (Test-Path "web\.env")) {
Write-Host "⚠️ .env file not found. Creating from .env.example..." -ForegroundColor Yellow
Copy-Item "web\.env.example" "web\.env"
Write-Host "⚠️ Please update web\.env with your API keys!" -ForegroundColor Yellow
}
# Start Docker Compose
Write-Host "`n🐳 Starting Docker containers..." -ForegroundColor Yellow
Set-Location "deployment\docker"
docker-compose up -d
Write-Host "`n⏳ Waiting for services to be ready..." -ForegroundColor Yellow
Start-Sleep -Seconds 30
# Check health
$maxRetries = 10
$retryCount = 0
$healthy = $false
while ($retryCount -lt $maxRetries -and -not $healthy) {
try {
$response = Invoke-WebRequest -Uri "http://localhost:3000/api/health" -UseBasicParsing -ErrorAction Stop
if ($response.StatusCode -eq 200) {
$healthy = $true
}
} catch {
$retryCount++
Write-Host " Waiting for services... ($retryCount/$maxRetries)" -ForegroundColor Gray
Start-Sleep -Seconds 5
}
}
if ($healthy) {
Write-Host "`n✅ Local environment is ready!" -ForegroundColor Green
Write-Host "`n📍 Services:" -ForegroundColor Cyan
Write-Host " Web: http://localhost:3000" -ForegroundColor Yellow
Write-Host " Oracle DB: localhost:1521" -ForegroundColor Yellow
Write-Host " Redis: localhost:6379" -ForegroundColor Yellow
Write-Host " MinIO: http://localhost:9001" -ForegroundColor Yellow
Write-Host "`n💡 To start the Next.js dev server:" -ForegroundColor Cyan
Write-Host " cd web && npm run dev" -ForegroundColor White
} else {
Write-Host "`n❌ Services failed to start. Check logs with:" -ForegroundColor Red
Write-Host " docker-compose logs" -ForegroundColor White
exit 1
}
Set-Location "../.."
}
"staging" {
Write-Host "`n📦 Deploying to staging..." -ForegroundColor Yellow
# Check kubectl
$kubectl = Get-Command kubectl -ErrorAction SilentlyContinue
if (-not $kubectl) {
Write-Host "❌ kubectl is not installed. Please install it first." -ForegroundColor Red
exit 1
}
# Build and push image
Write-Host "`n🔨 Building Docker image..." -ForegroundColor Yellow
Set-Location "web"
docker build -t ghcr.io/algodon/web:staging .
docker push ghcr.io/algodon/web:staging
Set-Location ".."
# Deploy to Kubernetes
Write-Host "`n🚀 Deploying to Kubernetes..." -ForegroundColor Yellow
kubectl set image deployment/algodon-web web=ghcr.io/algodon/web:staging -n algodon
kubectl rollout status deployment/algodon-web -n algodon
Write-Host "`n✅ Staging deployment complete!" -ForegroundColor Green
Write-Host " URL: https://staging.algodon.app" -ForegroundColor Yellow
}
"production" {
Write-Host "`n⚠️ PRODUCTION DEPLOYMENT" -ForegroundColor Red
$confirm = Read-Host "Type 'DEPLOY' to confirm production deployment"
if ($confirm -ne "DEPLOY") {
Write-Host "Deployment cancelled." -ForegroundColor Yellow
exit 0
}
# Check kubectl
$kubectl = Get-Command kubectl -ErrorAction SilentlyContinue
if (-not $kubectl) {
Write-Host "❌ kubectl is not installed. Please install it first." -ForegroundColor Red
exit 1
}
# Build and push image
Write-Host "`n🔨 Building Docker image..." -ForegroundColor Yellow
Set-Location "web"
$tag = "ghcr.io/algodon/web:prod-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
docker build -t $tag .
docker push $tag
Set-Location ".."
# Deploy to Kubernetes (canary)
Write-Host "`n🚀 Deploying to production (canary 10%)..." -ForegroundColor Yellow
kubectl set image deployment/algodon-web web=$tag -n algodon
kubectl rollout status deployment/algodon-web -n algodon --timeout=5m
# Smoke tests
Write-Host "`n🧪 Running smoke tests..." -ForegroundColor Yellow
Start-Sleep -Seconds 30
try {
$response = Invoke-WebRequest -Uri "https://algodon.app/api/health" -UseBasicParsing
if ($response.StatusCode -eq 200) {
Write-Host "✅ Smoke tests passed!" -ForegroundColor Green
# Full rollout
Write-Host "`n🚀 Scaling to 100%..." -ForegroundColor Yellow
kubectl scale deployment/algodon-web --replicas=5 -n algodon
kubectl rollout status deployment/algodon-web -n algodon
}
} catch {
Write-Host "❌ Smoke tests failed! Rolling back..." -ForegroundColor Red
kubectl rollout undo deployment/algodon-web -n algodon
exit 1
}
Write-Host "`n✅ Production deployment complete!" -ForegroundColor Green
Write-Host " URL: https://algodon.app" -ForegroundColor Yellow
}
}
Write-Host "`n✅ Deployment script complete!" -ForegroundColor Green
'@
$deployScript | Out-File -FilePath "deployment/scripts/deploy.ps1" -Encoding UTF8
Write-Host "✅ Created deployment script" -ForegroundColor Green
# Generate Terraform configuration
$terraformMain = @'
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "algodon-terraform-state"
key = "terraform.tfstate"
region = "us-east-1"
}
}
provider "aws" {
region = var.aws_region
}
# VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "algodon-vpc"
}
}
# Internet Gateway
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "algodon-igw"
}
}
# Public Subnets
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = "10.0.${count.index + 1}.0/24"
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
tags = {
Name = "algodon-public-${count.index + 1}"
}
}
# EKS Cluster
resource "aws_eks_cluster" "main" {
name = "algodon-cluster"
role_arn = aws_iam_role.eks_cluster.arn
version = "1.28"
vpc_config {
subnet_ids = aws_subnet.public[*].id
}
depends_on = [
aws_iam_role_policy_attachment.eks_cluster_policy,
]
tags = {
Name = "algodon-eks"
}
}
# RDS Oracle Instance
resource "aws_db_instance" "oracle" {
identifier = "algodon-oracle"
engine = "oracle-se2"
engine_version = "21.0.0.0"
instance_class = "db.t3.medium"
allocated_storage = 100
storage_type = "gp3"
storage_encrypted = true
db_name = "XE"
username = "algodon"
password = var.db_password
vpc_security_group_ids = [aws_security_group.rds.id]
db_subnet_group_name = aws_db_subnet_group.main.name
backup_retention_period = 7
backup_window = "03:00-04:00"
tags = {
Name = "algodon-oracle"
}
}
# ElastiCache Redis
resource "aws_elasticache_cluster" "redis" {
cluster_id = "algodon-redis"
engine = "redis"
node_type = "cache.t3.micro"
num_cache_nodes = 1
parameter_group_name = "default.redis7"
port = 6379
subnet_group_name = aws_elasticache_subnet_group.main.name
security_group_ids = [aws_security_group.redis.id]
tags = {
Name = "algodon-redis"
}
}
# S3 Bucket for storage
resource "aws_s3_bucket" "storage" {
bucket = "algodon-storage-${random_id.bucket_suffix.hex}"
tags = {
Name = "algodon-storage"
}
}
resource "aws_s3_bucket_versioning" "storage" {
bucket = aws_s3_bucket.storage.id
versioning_configuration {
status = "Enabled"
}
}
# CloudFront Distribution
resource "aws_cloudfront_distribution" "cdn" {
origin {
domain_name = aws_s3_bucket.storage.bucket_regional_domain_name
origin_id = "S3-${aws_s3_bucket.storage.id}"
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.main.cloudfront_access_identity_path
}
}
enabled = true
is_ipv6_enabled = true
default_root_object = "index.html"
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-${aws_s3_bucket.storage.id}"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
tags = {
Name = "algodon-cdn"
}
}
variable "aws_region" {
description = "AWS region"
default = "us-east-1"
}
variable "db_password" {
description = "Oracle database password"
type = string
sensitive = true
}
data "aws_availability_zones" "available" {
state = "available"
}
resource "random_id" "bucket_suffix" {
byte_length = 4
}
'@
$terraformMain | Out-File -FilePath "deployment/terraform/main.tf" -Encoding UTF8
Write-Host "✅ Created Terraform configuration" -ForegroundColor Green
# Generate README for deployment
$deploymentReadme = @'
# ALGODON Deployment Guide
## Quick Start
### Local Development
```powershell
.\deployment\scripts\deploy.ps1 local
```
This will:
- Start Docker Compose with all services (Oracle, Redis, PostgreSQL, MinIO)
- Wait for services to be healthy
- Provide URLs for accessing services
Then start the Next.js dev server:
```bash
cd web
npm install
npm run dev
```
### Staging Deployment
```powershell
.\deployment\scripts\deploy.ps1 staging
```
### Production Deployment
```powershell
.\deployment\scripts\deploy.ps1 production
```
## Prerequisites
- Docker Desktop
- Node.js 20+
- kubectl (for staging/production)
- AWS CLI (for Terraform)
## Environment Variables
Copy `web/.env.example` to `web/.env` and fill in:
- Database connection strings
- API keys (Stripe, OpenAI, etc.)
- AWS credentials
## Database Setup
1. Run Oracle schema migration:
```sql
@database/oracle/schemas/01_schema.sql
```
2. Seed initial data:
```sql
@database/oracle/seeds/01_seed.sql
```
## Monitoring
- Health check: `http://localhost:3000/api/health`
- Prometheus: `http://localhost:9090`
- Grafana: `http://localhost:3001`
## Troubleshooting
### Services not starting
```bash
docker-compose logs
```
### Database connection issues
```bash
docker-compose exec oracle-db sqlplus algodon/password@localhost:1521/XE
```
### Reset everything
```bash
docker-compose down -v
docker-compose up -d
```
'@
$deploymentReadme | Out-File -FilePath "deployment/README.md" -Encoding UTF8
Write-Host "✅ Created deployment README" -ForegroundColor Green
Write-Host "`n✅ Part 6: Deployment & CI/CD Complete!" -ForegroundColor Green
Write-Host "`n🎉 All 6 parts complete! ALGODON is ready to deploy!" -ForegroundColor Cyan
Write-Host "`nNext steps:" -ForegroundColor Yellow
Write-Host "1. Run all PowerShell scripts in order (part1 through part6)" -ForegroundColor White
Write-Host "2. Install dependencies: cd web && npm install" -ForegroundColor White
Write-Host "3. Set up environment variables: cp web/.env.example web/.env" -ForegroundColor White
Write-Host "4. Start local environment: .\deployment\scripts\deploy.ps1 local" -ForegroundColor White
Write-Host "5. Start dev server: cd web && npm run dev" -ForegroundColor White