-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMakefile
More file actions
181 lines (160 loc) · 5.25 KB
/
Copy pathMakefile
File metadata and controls
181 lines (160 loc) · 5.25 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
# Make expects SHELL to be a path (no args). Use bash directly for portability.
SHELL := /bin/bash
.ONESHELL:
.SHELLFLAGS := -euo pipefail -c
.SILENT:
MAKEFLAGS += --no-print-directory
# Defaults (override via environment or `make VAR=value ...`)
LOCAL ?= True
CLUSTER_NAME ?= aiidalab-demo-server-local
NAMESPACE ?= local
RELEASE_NAME ?= aiidalab-demo-server
VALUES_TEMPLATE ?= basehub/values.yaml.j2
VALUES_FILE ?= ${VALUES_TEMPLATE:.j2=}
CHART_LOCK_FILE ?= basehub/Chart.lock
KIND_CONFIG_FILE ?= kind-config.yaml
HELM_DEP_RETRIES ?= 5
help: ## Show available targets
awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z0-9_.-]+:.*##/ {printf "\033[36m%-22s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
generate-values: ## Render basehub/values.yaml from .env + Jinja2 template
if [[ ! -f "$(VALUES_TEMPLATE)" ]]; then
echo "Template not found: $(VALUES_TEMPLATE) (skipping)" >&2
exit 0
fi
if ! command -v jinja2 >/dev/null 2>&1; then
echo "Missing required command: jinja2" >&2
exit 1
fi
if [[ -f .env ]]; then
set -a
source .env
set +a
fi
LOCAL=${LOCAL} jinja2 "$(VALUES_TEMPLATE)" -o "$(VALUES_FILE)"
echo "Wrote $(VALUES_FILE)"
up: _check-for-values ## Create kind cluster (if needed) and deploy helm release
$(MAKE) _local_cmd CMD=up
refresh: _check-for-values ## Re-deploy helm release (fast; no kind create)
$(MAKE) _local_cmd CMD=refresh
restart: ## Restart hub/proxy deployments (no helm upgrade)
$(MAKE) _local_cmd CMD=restart
port-forward: ## Forward http://localhost:8000 -> service/proxy-public
$(MAKE) _local_cmd CMD=port-forward
status: ## Show pods/services in the namespace
$(MAKE) _local_cmd CMD=status
down: ## Uninstall release and delete kind cluster
$(MAKE) _local_cmd CMD=down
clean: ## Remove generated files
rm -f "$(VALUES_FILE)"
rm -f "$(CHART_LOCK_FILE)"
_check-for-values:
if [[ ! -f "$(VALUES_FILE)" ]]; then
echo "Values file not found: $(VALUES_FILE). Run 'make generate-values' first." >&2
exit 1
fi
_local_cmd:
require_cmd() { command -v "$$1" >/dev/null 2>&1 || { echo "Missing required command: $$1" >&2; exit 1; }; }
has_cmd() { command -v "$$1" >/dev/null 2>&1; }
ensure_helm_repos() {
if ! helm repo list 2>/dev/null | awk 'NR>1 {print $$1}' | grep -qx "jupyterhub"; then
helm repo add jupyterhub https://jupyterhub.github.io/helm-chart/ >/dev/null
fi
helm repo update >/dev/null
}
retry() {
local -r attempts="$$1"; shift
local attempt=1
while true; do
if "$$@"; then return 0; fi
if [[ "$$attempt" -ge "$$attempts" ]]; then return 1; fi
local sleep_s=$$((attempt * 5))
echo "Retrying ($$attempt/$$attempts) after $${sleep_s}s: $$*" >&2
sleep "$$sleep_s"
attempt=$$((attempt + 1))
done
}
kind_cluster_exists() { kind get clusters 2>/dev/null | grep -qx "$(CLUSTER_NAME)"; }
deploy_release() {
echo "Deploying helm release '$(RELEASE_NAME)' into namespace '$(NAMESPACE)'"
ensure_helm_repos
retry "$(HELM_DEP_RETRIES)" helm dependency build basehub >/dev/null || true
helm upgrade \
--install \
--cleanup-on-fail \
--create-namespace --namespace="$(NAMESPACE)" \
-f "$(VALUES_FILE)" \
"$(RELEASE_NAME)" basehub
}
wait_ready() {
echo "Waiting for hub and proxy to be ready..."
kubectl -n "$(NAMESPACE)" rollout status deploy/hub --timeout=300s
kubectl -n "$(NAMESPACE)" rollout status deploy/proxy --timeout=300s
}
restart_pods() {
echo "Restarting hub and proxy deployments..."
kubectl -n "$(NAMESPACE)" rollout restart deploy/hub
kubectl -n "$(NAMESPACE)" rollout restart deploy/proxy
wait_ready
}
case "$${CMD:-}" in
up)
require_cmd kubectl
require_cmd helm
if has_cmd kind; then
if ! kind_cluster_exists; then
echo "Creating kind cluster: $(CLUSTER_NAME)"
if [[ -f "$(KIND_CONFIG_FILE)" ]]; then
kind create cluster --name "$(CLUSTER_NAME)" --config "$(KIND_CONFIG_FILE)"
else
kind create cluster --name "$(CLUSTER_NAME)"
fi
else
echo "Using existing kind cluster: $(CLUSTER_NAME)"
fi
else
echo "kind not found; deploying to current kubectl context."
fi
deploy_release
restart_pods
echo
echo "Next: open http://localhost:8000"
echo "- If your kind cluster has port mappings, it should work directly."
echo "- Otherwise, run: make port-forward"
echo "Login with any username and password: demo"
;;
refresh)
require_cmd kubectl
require_cmd helm
deploy_release
restart_pods
;;
restart)
require_cmd kubectl
restart_pods
;;
port-forward)
require_cmd kubectl
echo "Forwarding service/proxy-public to http://localhost:8000 (Ctrl+C to stop)"
kubectl -n "$(NAMESPACE)" port-forward svc/proxy-public 8000:80
;;
status)
require_cmd kubectl
kubectl -n "$(NAMESPACE)" get pods,svc
;;
down)
require_cmd helm
require_cmd kubectl
echo "Uninstalling helm release '$(RELEASE_NAME)' from namespace '$(NAMESPACE)' (if present)"
helm -n "$(NAMESPACE)" uninstall "$(RELEASE_NAME)" 2>/dev/null || true
echo "Deleting namespace '$(NAMESPACE)' (if present)"
kubectl delete namespace "$(NAMESPACE)" 2>/dev/null || true
if has_cmd kind && kind_cluster_exists; then
echo "Deleting kind cluster: $(CLUSTER_NAME)"
kind delete cluster --name "$(CLUSTER_NAME)"
fi
;;
*)
echo "Unknown CMD='$${CMD:-}'. Try: make help" >&2
exit 2 &2>/dev/null
;;
esac