-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
195 lines (178 loc) · 6.99 KB
/
Copy pathMakefile
File metadata and controls
195 lines (178 loc) · 6.99 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
# OpenCode Dev Environment Makefile
NS ?= default
APP ?= opencode-dev
PORT ?= 4096
REGISTRY := $(shell grep '^registry:' config.yaml 2>/dev/null | awk '{print $$2}')
REGISTRY_USER := $(shell grep '^registry_user:' config.yaml 2>/dev/null | awk '{print $$2}')
REGISTRY_PASS := $(shell grep '^registry_pass:' config.yaml 2>/dev/null | awk '{print $$2}')
IMAGE := $(shell grep '^image:' config.yaml 2>/dev/null | awk '{print $$2}')
KUBECONFIG_EXISTS := $(shell [ -f kubeconfig.yaml ] && echo "yes" || echo "no")
REGISTRIES_CONF := config/registries.conf
K8S_POD_CONFIG := config/k8s-pod.yaml
K8S_DEPLOY_CONFIG := config/k8s-deploy.yaml
DOCKERFILE_TEMPLATE := config/Dockerfile.template
.PHONY: all
all: help
# =======================
# 检查配置
# =======================
check-config:
@if [ -z "$(IMAGE)" ]; then echo "Error: image not configured"; exit 1; fi
@if [ -z "$(REGISTRY)" ] || [ -z "$(REGISTRY_USER)" ] || [ -z "$(REGISTRY_PASS)" ]; then echo "Error: registry auth not configured"; exit 1; fi
@echo "Config check passed"
check-preinstall:
@echo "=== Checking GitHub proxy ==="
@(which jq >/dev/null 2>&1 || (echo "Error: jq not installed" && exit 1))
@echo "Check passed"
# =======================
# 检查镜像源配置
# =======================
check-registries:
@echo "=== Checking buildah registries config ==="
@if [ ! -f "$(REGISTRIES_CONF)" ]; then \
echo "Error: $(REGISTRIES_CONF) not found"; \
exit 1; \
fi
@# 复制配置到系统目录
@cp $(REGISTRIES_CONF) /etc/containers/registries.conf 2>/dev/null || true
@# 验证配置是否生效(检查是否有 docker.io 的镜像源重定向)
@if ! grep -q "registry.cdn.w7.cc\|daocloud\|nju.edu.cn" /etc/containers/registries.conf 2>/dev/null; then \
echo "Warning: No Chinese mirror configured"; \
fi
@echo "Registries config check passed"
# =======================
# 复制 OpenCode skills
# =======================
copy-skills:
@if [ -d ".opencode/skills" ]; then \
mkdir -p preinstall/.config/opencode && \
cp -r .opencode/skills preinstall/.config/opencode/ 2>/dev/null || true; \
fi
# =======================
# 生成 Dockerfile
# =======================
prepare-dockefile: check-config check-preinstall check-registries copy-skills
@echo "=== Generating Dockerfile ==="
@bash scripts/generate-dockefile.sh preinstall/preinstall.json $(DOCKERFILE_TEMPLATE) Dockerfile
# =======================
# 本地构建
# =======================
build-local: check-config prepare-dockefile
@echo "=== Build Image (Local) ==="
@echo "Image: $(IMAGE)"
@(which buildah >/dev/null 2>&1 || (echo "Error: buildah not installed" && exit 1))
@buildah login --username $(REGISTRY_USER) --password $(REGISTRY_PASS) $(REGISTRY) 2>/dev/null || true
@buildah bud --squash -f Dockerfile -t $(IMAGE) --pull .
@buildah push $(IMAGE)
@echo ""
@echo "========================================"
@echo "Build and push successful!"
@echo "Image: $(IMAGE)"
@echo "========================================"
# =======================
# K8s 构建
# =======================
build-k8s: check-config prepare-dockefile
@echo "=== Build Image (K8s) ==="
@echo "Image: $(IMAGE)"
@export KUBECONFIG=$$(pwd)/kubeconfig.yaml
@# 删除旧 Pod
@kubectl delete pod $(APP)-build -n $(NS) --ignore-not-found=true 2>/dev/null || true
@sleep 2
@# 应用 Pod 配置(替换变量)
@APP=$(APP) NS=$(NS) envsubst < $(K8S_POD_CONFIG) | kubectl apply -n $(NS) -f -
@echo "Waiting for pod..."
@kubectl wait --for=condition=Ready pod/$(APP)-build -n $(NS) --timeout=120s || { kubectl describe pod $(APP)-build -n $(NS); exit 1; }
@echo "Copying files..."
@kubectl cp Dockerfile $(APP)-build:/workspace/Dockerfile -n $(NS)
@kubectl cp preinstall $(APP)-build:/workspace/ -n $(NS)
@kubectl cp scripts $(APP)-build:/workspace/ -n $(NS)
@echo "Creating registries.conf..."
@kubectl exec $(APP)-build -n $(NS) -- mkdir -p /etc/containers
@kubectl cp $(REGISTRIES_CONF) $(APP)-build:/etc/containers/registries.conf -n $(NS)
@echo "Logging in to registry..."
@kubectl exec $(APP)-build -n $(NS) -- buildah login --username $(REGISTRY_USER) --password $(REGISTRY_PASS) $(REGISTRY)
@echo "Building..."
@kubectl exec $(APP)-build -n $(NS) -- buildah bud --squash --registries-conf /etc/containers/registries.conf --file /workspace/Dockerfile --tag $(IMAGE) --pull /workspace
@echo "Pushing..."
@kubectl exec $(APP)-build -n $(NS) -- buildah push $(IMAGE)
@echo ""
@echo "========================================"
@echo "Build and push successful!"
@echo "Image: $(IMAGE)"
@echo "========================================"
# =======================
# 构建镜像(自动选择模式)
# =======================
.PHONY: build
build: check-config
@if [ "$(KUBECONFIG_EXISTS)" = "yes" ]; then \
$(MAKE) build-k8s; \
else \
$(MAKE) build-local; \
fi
# =======================
# 部署应用
# =======================
.PHONY: deploy
deploy: check-config
@echo "=== Deploy $(APP) ==="
@echo "Image: $(IMAGE), Port: $(PORT)"
@# 创建 namespace
@kubectl create namespace $(NS) --dry-run=client -o yaml | kubectl apply -f - 2>/dev/null || true
@# 应用部署配置(替换变量)
@APP=$(APP) IMAGE=$(IMAGE) PORT=$(PORT) NS=$(NS) envsubst < $(K8S_DEPLOY_CONFIG) | kubectl apply -n $(NS) -f -
@kubectl rollout status deployment/$(APP) -n $(NS) --timeout=120s
@kubectl get pods,svc -n $(NS) -l app=$(APP)
@echo "Service: $(APP).$(NS):$(PORT)"
# =======================
# 查看日志
# =======================
.PHONY: logs
logs:
@kubectl logs -n $(NS) -l app=$(APP) -f
.PHONY: logs-build
logs-build:
@kubectl logs -n $(NS) $(APP)-build -f
# =======================
# 进入容器
# =======================
.PHONY: exec
exec:
@POD=$$(kubectl get pods -n $(NS) -l app=$(APP) -o jsonpath='{.items[0].metadata.name}'); \
if [ -z "$$POD" ]; then echo "No pod found"; exit 1; fi; \
kubectl exec -it -n $(NS) $$POD -- /bin/bash
# =======================
# 清理资源
# =======================
.PHONY: clean
clean:
@echo "=== Clean Resources ==="
@kubectl delete deployment $(APP) -n $(NS) --ignore-not-found=true 2>/dev/null || true
@kubectl delete svc $(APP) -n $(NS) --ignore-not-found=true 2>/dev/null || true
@kubectl delete pod $(APP)-build -n $(NS) --ignore-not-found=true 2>/dev/null || true
# =======================
# 显示帮助
# =======================
.PHONY: help
help:
@echo "OpenCode Dev Environment - Makefile"
@echo ""
@echo "Usage: make <target>"
@echo ""
@echo "Targets:"
@echo " build Build Docker image (local or K8s)"
@echo " build-local Build using local buildah"
@echo " build-k8s Build using K8s Pod"
@echo " deploy Deploy to K8s"
@echo " logs View logs"
@echo " logs-build View build logs"
@echo " exec Exec into pod"
@echo " clean Clean resources"
@echo " help Show help"
@echo ""
@echo "Configuration files:"
@echo " config.yaml - Registry and image config"
@echo " config/registries.conf - Buildah mirror config"
@echo " config/k8s-pod.yaml - K8s Build Pod template"
@echo " config/k8s-deploy.yaml - K8s Deploy template"