145 lines
3.4 KiB
Bash
Executable File
145 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Get project ID
|
|
PROJECT=$(gcloud config get-value project)
|
|
|
|
# Reconnect to cluster in case connection timed out
|
|
echo "🔄 Refreshing connection to GKE cluster..."
|
|
gcloud container clusters get-credentials sk1-cluster --zone us-central1-a --project ${PROJECT}
|
|
|
|
# Create a temporary file with our complete backend app
|
|
cat > /tmp/complete-backend.js << 'EOF'
|
|
const express = require('express');
|
|
const cors = require('cors');
|
|
const mongoose = require('mongoose');
|
|
const app = express();
|
|
const port = process.env.PORT || 4000;
|
|
const host = process.env.HOST || 'localhost';
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
// Root route
|
|
app.get('/', (req, res) => {
|
|
res.json({ message: 'Welcome to the API' });
|
|
});
|
|
|
|
// Health check endpoint
|
|
app.get('/api', (req, res) => {
|
|
res.json({ status: 'ok', message: 'API is healthy' });
|
|
});
|
|
|
|
// Health check endpoint
|
|
app.get('/api/health', (req, res) => {
|
|
res.json({ status: 'ok' });
|
|
});
|
|
|
|
// MongoDB connection
|
|
const MONGO_URI = process.env.MONGO_URI;
|
|
if (MONGO_URI) {
|
|
mongoose.connect(MONGO_URI)
|
|
.then(() => console.log('✅ MongoDB connected'))
|
|
.catch(err => console.error('MongoDB connection error:', err));
|
|
}
|
|
|
|
// Start server
|
|
app.listen(port, host, () => {
|
|
console.log(`🚀 Server running on http://${host}:${port}`);
|
|
});
|
|
EOF
|
|
|
|
# Create a Dockerfile for our custom backend
|
|
cat > /tmp/Dockerfile.backend << 'EOF'
|
|
FROM node:20-alpine
|
|
WORKDIR /app
|
|
COPY package.json .
|
|
RUN npm install express cors mongoose
|
|
COPY . .
|
|
EXPOSE 4000
|
|
CMD ["node", "index.js"]
|
|
EOF
|
|
|
|
# Create a directory for our custom backend
|
|
mkdir -p /tmp/custom-backend
|
|
cp /tmp/complete-backend.js /tmp/custom-backend/index.js
|
|
cp /tmp/Dockerfile.backend /tmp/custom-backend/Dockerfile
|
|
|
|
# Create a basic package.json
|
|
cat > /tmp/custom-backend/package.json << 'EOF'
|
|
{
|
|
"name": "backend",
|
|
"version": "1.0.0",
|
|
"description": "Custom backend for sk1",
|
|
"main": "index.js",
|
|
"dependencies": {
|
|
"cors": "^2.8.5",
|
|
"express": "^4.18.2",
|
|
"mongoose": "^7.0.3"
|
|
}
|
|
}
|
|
EOF
|
|
|
|
echo "🏗️ Building custom backend image..."
|
|
cd /tmp/custom-backend
|
|
gcloud builds submit --tag "gcr.io/${PROJECT}/backend-fixed:latest" .
|
|
cd -
|
|
|
|
# Create a new deployment using our fixed image
|
|
cat > /tmp/fixed-backend-deployment.yaml << EOF
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: backend-fixed
|
|
namespace: sk1
|
|
spec:
|
|
replicas: 1
|
|
selector:
|
|
matchLabels:
|
|
app: backend-fixed
|
|
template:
|
|
metadata:
|
|
labels:
|
|
app: backend-fixed
|
|
spec:
|
|
containers:
|
|
- name: backend
|
|
image: gcr.io/${PROJECT}/backend-fixed:latest
|
|
env:
|
|
- name: PORT
|
|
value: "4000"
|
|
- name: HOST
|
|
value: "0.0.0.0"
|
|
- name: MONGO_URI
|
|
valueFrom:
|
|
secretKeyRef:
|
|
name: mongodb-secret
|
|
key: MONGO_URI
|
|
ports:
|
|
- containerPort: 4000
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: backend-service
|
|
namespace: sk1
|
|
spec:
|
|
selector:
|
|
app: backend-fixed
|
|
ports:
|
|
- port: 4000
|
|
targetPort: 4000
|
|
EOF
|
|
|
|
echo "📦 Deploying fixed backend..."
|
|
kubectl apply -f /tmp/fixed-backend-deployment.yaml
|
|
|
|
echo "⏱️ Waiting for deployment to be available..."
|
|
kubectl rollout status deployment/backend-fixed -n sk1
|
|
|
|
echo "🔍 Checking for running pods..."
|
|
kubectl get pods -n sk1 -l app=backend-fixed
|
|
|
|
echo "✅ Deployment complete. Testing backend health..."
|
|
kubectl exec -it netshoot -n sk1 -- curl -s http://backend-service:4000/api | grep status |