-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·215 lines (186 loc) · 7.78 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·215 lines (186 loc) · 7.78 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
#!/bin/bash
# ChemGraph Kubernetes Deployment Script
# Deploys both Streamlit UI and MCP server
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored messages
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if kubectl is installed
if ! command -v kubectl &> /dev/null; then
print_error "kubectl is not installed. Please install it first."
exit 1
fi
# Default namespace (set early so the connection check can use it)
NAMESPACE="${NAMESPACE:-chemgraph}"
# Check kubectl connection using the target namespace
# (cluster-info requires kube-system access which RBAC-limited users may not have)
if ! kubectl get namespace "$NAMESPACE" &> /dev/null; then
print_error "Cannot access namespace '$NAMESPACE'. Check your kubeconfig and permissions."
exit 1
fi
print_info "Connected to Kubernetes cluster (namespace: $NAMESPACE)"
# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Image tag override (defaults to whatever is in the YAML manifests)
IMAGE_TAG="${IMAGE_TAG:-}"
IMAGE_REPO="ghcr.io/argonne-lcf/chemgraph"
# Parse command line arguments
ACTION="${1:-deploy}"
TARGET="${2:-}"
# Helper: wait for LoadBalancer IP and print access URL
wait_for_lb() {
local svc_name="$1"
local port="$2"
local label="$3"
print_info "Waiting for $label LoadBalancer IP..."
local external_ip=""
for i in {1..30}; do
external_ip=$(kubectl get svc "$svc_name" -n "$NAMESPACE" \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null || echo "")
if [ -n "$external_ip" ]; then
print_info "$label is available at: http://$external_ip:$port"
return
fi
sleep 5
done
print_warn "$label LoadBalancer IP not assigned yet."
print_info " Check: kubectl get svc $svc_name -n $NAMESPACE"
print_info " Or use: $0 port-forward ${svc_name#chemgraph-}"
}
case "$ACTION" in
deploy)
print_info "Deploying ChemGraph (Streamlit + MCP) to namespace: $NAMESPACE"
# Check if secrets file exists
if [ ! -f "$SCRIPT_DIR/secrets.yaml" ]; then
print_warn "secrets.yaml not found. Creating from template..."
cp "$SCRIPT_DIR/secrets.yaml.template" "$SCRIPT_DIR/secrets.yaml"
print_warn "Please edit k8s/secrets.yaml with your API keys before proceeding."
print_warn "Press Enter to continue after editing secrets.yaml, or Ctrl+C to cancel..."
read -r
fi
# Apply secrets (shared by both services)
print_info "Creating secrets..."
kubectl apply -f "$SCRIPT_DIR/secrets.yaml" -n "$NAMESPACE"
# Deploy Streamlit
print_info "Creating Streamlit deployment..."
kubectl apply -f "$SCRIPT_DIR/deployment.yaml" -n "$NAMESPACE"
kubectl apply -f "$SCRIPT_DIR/service.yaml" -n "$NAMESPACE"
# Deploy MCP
print_info "Creating MCP deployment..."
kubectl apply -f "$SCRIPT_DIR/mcp-deployment.yaml" -n "$NAMESPACE"
kubectl apply -f "$SCRIPT_DIR/mcp-service.yaml" -n "$NAMESPACE"
# Override image tag if IMAGE_TAG is set
if [ -n "$IMAGE_TAG" ]; then
print_info "Overriding image tag to: $IMAGE_REPO:$IMAGE_TAG"
kubectl set image deployment/chemgraph-streamlit streamlit="$IMAGE_REPO:$IMAGE_TAG" -n "$NAMESPACE"
kubectl set image deployment/chemgraph-mcp mcp="$IMAGE_REPO:$IMAGE_TAG" -n "$NAMESPACE"
fi
# Wait for both rollouts
print_info "Waiting for deployments to be ready..."
kubectl rollout status deployment/chemgraph-streamlit -n "$NAMESPACE" --timeout=5m
kubectl rollout status deployment/chemgraph-mcp -n "$NAMESPACE" --timeout=5m
print_info "Deployment successful!"
echo ""
# Show service info
print_info "Service information:"
kubectl get svc -l app=chemgraph -n "$NAMESPACE"
echo ""
# Wait for LoadBalancer IPs
wait_for_lb "chemgraph-streamlit" 8501 "Streamlit"
wait_for_lb "chemgraph-mcp" 9003 "MCP server"
;;
status)
print_info "Checking deployment status in namespace: $NAMESPACE"
echo ""
print_info "Pods:"
kubectl get pods -l app=chemgraph -n "$NAMESPACE"
echo ""
print_info "Services:"
kubectl get svc -l app=chemgraph -n "$NAMESPACE"
echo ""
print_info "Deployments:"
kubectl get deployment -l app=chemgraph -n "$NAMESPACE"
;;
logs)
case "$TARGET" in
streamlit)
print_info "Fetching Streamlit logs..."
kubectl logs -l app=chemgraph,component=streamlit -n "$NAMESPACE" --tail=100 -f
;;
mcp)
print_info "Fetching MCP server logs..."
kubectl logs -l app=chemgraph,component=mcp -n "$NAMESPACE" --tail=100 -f
;;
*)
print_info "Fetching logs from all ChemGraph pods..."
kubectl logs -l app=chemgraph -n "$NAMESPACE" --tail=100 -f --prefix
;;
esac
;;
delete)
print_warn "This will delete all ChemGraph deployments from namespace: $NAMESPACE"
read -p "Are you sure? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_info "Deleting resources..."
kubectl delete -f "$SCRIPT_DIR/mcp-deployment.yaml" -n "$NAMESPACE" || true
kubectl delete -f "$SCRIPT_DIR/mcp-service.yaml" -n "$NAMESPACE" || true
kubectl delete -f "$SCRIPT_DIR/deployment.yaml" -n "$NAMESPACE" || true
kubectl delete -f "$SCRIPT_DIR/service.yaml" -n "$NAMESPACE" || true
print_info "Keeping secrets (delete manually if needed)"
print_info "Cleanup complete!"
else
print_info "Deletion cancelled"
fi
;;
port-forward)
case "$TARGET" in
streamlit)
print_info "Port forwarding Streamlit to localhost:8501"
kubectl port-forward svc/chemgraph-streamlit 8501:8501 -n "$NAMESPACE"
;;
mcp)
print_info "Port forwarding MCP server to localhost:9003"
kubectl port-forward svc/chemgraph-mcp 9003:9003 -n "$NAMESPACE"
;;
*)
print_info "Port forwarding both services (Ctrl+C to stop)..."
print_info " Streamlit: http://localhost:8501"
print_info " MCP: http://localhost:9003/mcp/"
kubectl port-forward svc/chemgraph-streamlit 8501:8501 -n "$NAMESPACE" &
PF_PID_STREAMLIT=$!
kubectl port-forward svc/chemgraph-mcp 9003:9003 -n "$NAMESPACE" &
PF_PID_MCP=$!
trap "kill $PF_PID_STREAMLIT $PF_PID_MCP 2>/dev/null; exit" INT TERM
wait
;;
esac
;;
*)
echo "Usage: $0 {deploy|status|logs|delete|port-forward} [target]"
echo ""
echo "Commands:"
echo " deploy - Deploy Streamlit and MCP server to Kubernetes"
echo " status - Check deployment status"
echo " logs [streamlit|mcp] - View logs (default: all pods)"
echo " delete - Remove all ChemGraph deployments"
echo " port-forward [streamlit|mcp] - Forward ports to localhost (default: both)"
echo ""
echo "Environment variables:"
echo " NAMESPACE - Kubernetes namespace (default: chemgraph)"
echo " IMAGE_TAG - Override image tag (e.g. dev, sha-abc123, v1.0.0)"
exit 1
;;
esac