This repository was archived by the owner on Feb 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
251 lines (213 loc) · 6.67 KB
/
deploy.sh
File metadata and controls
251 lines (213 loc) · 6.67 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
#!/bin/bash
# Learner Graph RAG System Deployment Script
# Usage: ./deploy.sh [environment] [action]
# Example: ./deploy.sh production deploy
set -e
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
K8S_DIR="${SCRIPT_DIR}/k8s"
NAMESPACE="learner-graph"
APP_NAME="learner-graph-rag-system"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check prerequisites
check_prerequisites() {
log_info "Checking prerequisites..."
# Check kubectl
if ! command -v kubectl &> /dev/null; then
log_error "kubectl is not installed. Please install kubectl first."
exit 1
fi
# Check kustomize
if ! command -v kustomize &> /dev/null; then
log_warning "kustomize is not installed. Using kubectl kustomize instead."
fi
# Check Docker
if ! command -v docker &> /dev/null; then
log_error "Docker is not installed. Please install Docker first."
exit 1
fi
# Check cluster connectivity
if ! kubectl cluster-info &> /dev/null; then
log_error "Cannot connect to Kubernetes cluster. Please check your kubeconfig."
exit 1
fi
log_success "Prerequisites check passed"
}
# Build Docker image
build_image() {
local tag=${1:-latest}
log_info "Building Docker image with tag: $tag"
docker build -t learner-graph:$tag .
# If using a registry, push the image
if [[ -n "$DOCKER_REGISTRY" ]]; then
docker tag learner-graph:$tag $DOCKER_REGISTRY/learner-graph:$tag
docker push $DOCKER_REGISTRY/learner-graph:$tag
log_success "Image pushed to registry: $DOCKER_REGISTRY/learner-graph:$tag"
fi
log_success "Docker image built successfully"
}
# Deploy to Kubernetes
deploy() {
local environment=${1:-development}
log_info "Deploying to $environment environment..."
# Create namespace if it doesn't exist
kubectl create namespace $NAMESPACE --dry-run=client -o yaml | kubectl apply -f -
# Apply configurations
if command -v kustomize &> /dev/null; then
cd $K8S_DIR
kustomize build . | kubectl apply -f -
else
kubectl apply -k $K8S_DIR
fi
# Wait for rollout
log_info "Waiting for deployment rollout..."
kubectl rollout status deployment/learner-graph-app -n $NAMESPACE --timeout=300s
# Check service status
kubectl get services -n $NAMESPACE
kubectl get pods -n $NAMESPACE
log_success "Deployment completed successfully"
}
# Rollback deployment
rollback() {
log_info "Rolling back deployment..."
kubectl rollout undo deployment/learner-graph-app -n $NAMESPACE
kubectl rollout status deployment/learner-graph-app -n $NAMESPACE
log_success "Rollback completed"
}
# Scale deployment
scale() {
local replicas=${1:-3}
log_info "Scaling deployment to $replicas replicas..."
kubectl scale deployment/learner-graph-app --replicas=$replicas -n $NAMESPACE
kubectl rollout status deployment/learner-graph-app -n $NAMESPACE
log_success "Scaling completed"
}
# Get logs
logs() {
local lines=${1:-100}
log_info "Fetching logs (last $lines lines)..."
kubectl logs -f deployment/learner-graph-app -n $NAMESPACE --tail=$lines
}
# Health check
health_check() {
log_info "Performing health check..."
# Get service URL
SERVICE_URL=$(kubectl get service learner-graph-service -n $NAMESPACE -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
if [[ -z "$SERVICE_URL" ]]; then
SERVICE_URL=$(kubectl get service learner-graph-service -n $NAMESPACE -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
fi
if [[ -z "$SERVICE_URL" ]]; then
log_warning "LoadBalancer not ready, using port-forward for health check"
kubectl port-forward service/learner-graph-service 8080:80 -n $NAMESPACE &
PF_PID=$!
sleep 5
SERVICE_URL="localhost:8080"
fi
# Perform health check
if curl -f "http://$SERVICE_URL/health" &> /dev/null; then
log_success "Health check passed - service is running"
else
log_error "Health check failed - service may not be ready"
exit 1
fi
# Cleanup port-forward if used
if [[ -n "$PF_PID" ]]; then
kill $PF_PID 2>/dev/null || true
fi
}
# Delete deployment
delete() {
log_warning "This will delete the entire deployment. Are you sure? (y/N)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
log_info "Deleting deployment..."
kubectl delete -k $K8S_DIR
log_success "Deployment deleted"
else
log_info "Deletion cancelled"
fi
}
# Show status
status() {
log_info "Deployment status:"
echo
kubectl get all -n $NAMESPACE
echo
log_info "Recent events:"
kubectl get events -n $NAMESPACE --sort-by='.lastTimestamp' | tail -10
}
# Main script logic
main() {
local environment=${1:-development}
local action=${2:-deploy}
case $action in
"build")
check_prerequisites
build_image $3
;;
"deploy")
check_prerequisites
build_image latest
deploy $environment
health_check
;;
"rollback")
rollback
;;
"scale")
scale $3
;;
"logs")
logs $3
;;
"health")
health_check
;;
"status")
status
;;
"delete")
delete
;;
*)
echo "Usage: $0 [environment] [action] [options]"
echo
echo "Environments: development, staging, production"
echo "Actions:"
echo " build [tag] - Build Docker image"
echo " deploy - Deploy to Kubernetes"
echo " rollback - Rollback deployment"
echo " scale [replicas] - Scale deployment"
echo " logs [lines] - Show application logs"
echo " health - Perform health check"
echo " status - Show deployment status"
echo " delete - Delete deployment"
echo
echo "Examples:"
echo " $0 production deploy"
echo " $0 development scale 5"
echo " $0 staging logs 200"
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"