Skip to content

Commit 7ca52a1

Browse files
committed
feat: add local environment
1 parent 751819f commit 7ca52a1

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

Makefile

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.PHONY: all dev clean mongo-shell
2+
3+
all: dev
4+
5+
dev:
6+
@echo "Creating k3d cluster..."
7+
-@k3d cluster create a50passos -p "8080:80@loadbalancer" --registry-create k3d-a50passos-registry:10000 --wait
8+
9+
@echo "Starting local database..."
10+
-@docker run -d --name mongodb -p 27017:27017 mongo
11+
12+
@echo "Creating namespace..."
13+
@kubectl apply -f local/namespace.yml
14+
15+
@echo "Creating secrets..."
16+
-@kubectl create secret generic a50passos-secrets -n a50passos \
17+
--from-literal=DATABASE=mongodb://host.docker.internal:27017/a50passos \
18+
--from-literal=SESSION_SECRET=SECRET
19+
20+
@echo "Building website image..."
21+
@docker build --pull --no-cache -t fraguinha/a50passos.com:latest src/website
22+
23+
@echo "Building assets image..."
24+
@docker build --pull --no-cache -t fraguinha/a50passos.com-assets:latest -f src/website/Dockerfile.assets .
25+
26+
@echo "Tagging website image..."
27+
@docker tag fraguinha/a50passos.com localhost:10000/fraguinha/a50passos.com:latest
28+
29+
@echo "Tagging assets image..."
30+
@docker tag fraguinha/a50passos.com-assets localhost:10000/fraguinha/a50passos.com-assets:latest
31+
32+
@echo "Pushing website image..."
33+
@docker push localhost:10000/fraguinha/a50passos.com:latest
34+
35+
@echo "Pushing assets image..."
36+
@docker push localhost:10000/fraguinha/a50passos.com-assets:latest
37+
38+
@echo "Capturing website image digest..."; \
39+
WEBSITE_SHA=$$(docker inspect --format='{{index .RepoDigests 0}}' fraguinha/a50passos.com:latest | cut -d'@' -f2); \
40+
echo "Website SHA: $$WEBSITE_SHA"; \
41+
\
42+
echo "Capturing assets image digest..."; \
43+
ASSETS_SHA=$$(docker inspect --format='{{index .RepoDigests 0}}' fraguinha/a50passos.com-assets:latest | cut -d'@' -f2); \
44+
echo "Assets SHA: $$ASSETS_SHA"; \
45+
\
46+
echo "Patching kubernetes manifests with SHA digests..."; \
47+
sed -i '' "s|a50passos.com:latest|a50passos.com:latest@$$WEBSITE_SHA|g" local/website.yml; \
48+
sed -i '' "s|a50passos.com-assets:latest|a50passos.com-assets:latest@$$ASSETS_SHA|g" local/website.yml; \
49+
\
50+
echo "Applying kubernetes manifests..."; \
51+
kubectl apply -k local/
52+
53+
@echo "Rolling out deployments..."
54+
@kubectl rollout restart deployment/website -n a50passos
55+
56+
@echo "Waiting for deployments to be successful..."
57+
@kubectl rollout status deployment/website -n a50passos
58+
59+
@echo "Restoring kubernetes manifests..."
60+
@sed -i '' "s|@sha256:.*||g" local/website.yml
61+
62+
@echo "You can now access the application at http://localhost:8080"
63+
64+
clean:
65+
@echo "Cleaning docker resources..."
66+
@docker stop $$(docker ps -aq)
67+
@docker container prune -f
68+
@docker image prune -f
69+
@docker volume prune -af
70+
@docker network prune -f
71+
72+
mongo-shell:
73+
@echo "Connecting to MongoDB shell..."
74+
@docker exec -it mongodb mongosh

local/ingress.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: a50passos-ingress
5+
namespace: a50passos
6+
annotations:
7+
kubernetes.io/ingress.class: traefik
8+
spec:
9+
rules:
10+
- http:
11+
paths:
12+
- path: /
13+
pathType: Prefix
14+
backend:
15+
service:
16+
name: website
17+
port:
18+
number: 8080

local/kustomization.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- namespace.yml
5+
- website.yml
6+
- ingress.yml

local/namespace.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: a50passos

local/website.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: website
5+
namespace: a50passos
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: website
11+
template:
12+
metadata:
13+
labels:
14+
app: website
15+
spec:
16+
initContainers:
17+
- name: create-uploads-dir
18+
image: busybox
19+
command: ['sh', '-c', 'mkdir -p /app/dist/public/images/uploads && chmod -R 755 /app/dist/public/images/uploads']
20+
volumeMounts:
21+
- name: images
22+
mountPath: /app/dist/public/images
23+
- name: copy-image-assets
24+
image: k3d-a50passos-registry:10000/fraguinha/a50passos.com-assets:latest
25+
imagePullPolicy: Always
26+
command: ['sh', '-c', 'cp -r /assets/* /app/dist/public/images/']
27+
volumeMounts:
28+
- name: images
29+
mountPath: /app/dist/public/images
30+
containers:
31+
- name: website
32+
image: k3d-a50passos-registry:10000/fraguinha/a50passos.com:latest
33+
imagePullPolicy: Always
34+
ports:
35+
- containerPort: 8080
36+
env:
37+
- name: PORT
38+
value: "8080"
39+
- name: NODE_ENV
40+
value: "development"
41+
- name: APPNAME
42+
value: "a50passos"
43+
- name: DATABASE
44+
valueFrom:
45+
secretKeyRef:
46+
name: a50passos-secrets
47+
key: DATABASE
48+
- name: SESSION_SECRET
49+
valueFrom:
50+
secretKeyRef:
51+
name: a50passos-secrets
52+
key: SESSION_SECRET
53+
volumeMounts:
54+
- name: images
55+
mountPath: /app/dist/public/images
56+
volumes:
57+
- name: images
58+
persistentVolumeClaim:
59+
claimName: a50passos-images-pvc
60+
---
61+
apiVersion: v1
62+
kind: Service
63+
metadata:
64+
name: website
65+
namespace: a50passos
66+
spec:
67+
selector:
68+
app: website
69+
ports:
70+
- port: 8080
71+
targetPort: 8080
72+
---
73+
apiVersion: v1
74+
kind: PersistentVolumeClaim
75+
metadata:
76+
name: a50passos-images-pvc
77+
namespace: a50passos
78+
spec:
79+
accessModes:
80+
- ReadWriteOnce
81+
resources:
82+
requests:
83+
storage: 1Gi

src/website/src/lib/setup/database.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import mongoose from 'mongoose'
2+
import Meta from '../../models/meta-model.js'
3+
4+
const seedDatabase = async () => {
5+
try {
6+
const count = await Meta.countDocuments()
7+
if (count === 0) {
8+
await Meta.create({ managed: 0 })
9+
console.log('Database seeded with initial metadata')
10+
}
11+
} catch (err) {
12+
console.error('Error seeding database:', err)
13+
}
14+
}
215

316
const connect = (database: string) => {
417
mongoose.set('strictQuery', false)
@@ -9,6 +22,7 @@ const connect = (database: string) => {
922
const db = mongoose.connection
1023
db.on('open', () => {
1124
console.log('Connected to database')
25+
seedDatabase()
1226
})
1327
}
1428

0 commit comments

Comments
 (0)