JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
修复patch_hanlder.sh的问题
jiangchenglin20fb7001565e
diff --git a/deploy/enterprise/patch_handler.sh b/deploy/enterprise/patch_handler.shindex e10d499b0f..2bbbab8a49 100755--- a/deploy/enterprise/patch_handler.sh+++ b/deploy/enterprise/patch_handler.sh@@ -1,27 +1,100 @@#!/usr/bin/env bashset -euo >/dev/null 2>&1+# 轮询 NodePort 健康端点直到 200,确认服务真正可达。死等:起不来就一直等。+# 原因: Deployment ready(readinessProbe 通过) ≠ NodePort 可达——kube-proxy 的+# iptables DNAT 规则传播有 1~2s 滞后。下发前必须确认走 NodePort 真能打通,+# 否则请求会打到尚未就绪的端口(连接被拒 / 502),却因脚本只看 curl 退出码+# 而被当成成功,导致配置/模板永远没下发、agentserver 起不来。+# 用法: wait_http_ready <host> <port> <health_path> <module>+wait_http_ready() {+ local host="$1"+ local port="$2"+ local path="$3"+ local module="${4:-service}"+ local elapsed=0+ local code="000"+ [ -n "${port}" ] || error "${module} NodePort is empty; cannot check readiness"+ info "Waiting for ${module} ready: http://${host}:${port}${path} (forever)"+ while true; do+ code=$(curl -s --max-time 3 -o /dev/null -w "%{http_code}" "http://${host}:${port}${path}" 2>/dev/null) || code="000"+ if [ "${code}" = "200" ]; then+ success "${module} ready (http=200 after ${elapsed}s)"+ return+ fi+ sleep 1+ elapsed=$((elapsed + 1))+ done+}++# POST JSON 并校验: HTTP 必须 2xx; module=runtime 时再校验响应体 .ok==true+# (只有 agent-runtime 的 config_sync 响应带顶层 .ok;gateway 的 model/agent/+# instance 接口无此字段,只卡 2xx)。+# 修掉"只看 curl 传输退出码"的坑——rc=0 只代表拿到字节,不代表业务成功+# (例如 404/502/503/409 都可能 rc=0)。+# 用法: post_and_validate <url> <data(@file 或 json 串)> <module>+post_and_validate() {+ local url="$1"+ local data="$2"+ local module="${3:-}"+ local resp_file="$(mktemp)"+ local code=$(curl -s --max-time 20 -o "${resp_file}" -w "%{http_code}" \+ -X POST "${url}" -H "Content-Type: application/json" -d "${data}" 2>/dev/null) || code="000"+ local body="$(cat "${resp_file}" 2>/dev/null || true)"+ rm -f "${resp_file}"+ if ! [[ "${code}" =~ ^2[0-9][0-9]$ ]]; then+ error "POST ${url} failed: http=${code} body=${body}"+ fi+ if [ "${module}" = "runtime" ]; then+ local ok="$(printf '%s' "${body}" | jq -r '.ok // empty' 2>/dev/null || true)"+ [ "${ok}" = "true" ] || error "POST ${url} failed: ok!=true body=${body}"+ fi+ success "POST ${url} ok (http=${code})"+}+render_patch_file() {- local namespace="${DEPLOY_VARS["NAMESPACE"]}"local json_template="${CONFIG["AS_JSON_TEMPLATE_FILE"]}"local json_file="${CONFIG["AS_JSON_FILE"]}"- local env_template="${CONFIG["AS_ENV_TEMPLATE_FILE"]}"- local env_file="${CONFIG["AS_ENV_FILE"]}"- local envfile_name="${DEPLOY_VARS["AGENT_SERVER_ENV_CM_NAME"]}"info "Rendering AgentServer templates"render_config_template "${json_template}" "${json_file}" "DEPLOY_VARS"- render_config_template "${env_template}" "${env_file}" "DEPLOY_VARS"if ! jq . "${json_file}" >/dev/null 2>&1; thenerror "AgentServer JSON rendering failed, invalid JSON format: ${json_file}"fi- # product 模式:镜像内置代码,不需要 hostPath 挂载 / root 权限- # 三段式契约下:删 hostPath volumes + 主容器 securityContext + 引用已删卷的 volumeMounts+ # 计算要剔除的 hostPath 卷(+ 引用它们的 volumeMounts);product 模式额外删主容器 securityContext:+ # 规则1(两模式通用):三个代码目录变量(CLAW_CODE_PATH/RUNTIME_CODE_PATH/CORE_CODE_PATH)任一为空,+ # 引用该变量的 hostPath 路径会变坏、不能挂载,一并剔除。映射(按 hostPath path 引用):+ # CLAW_CODE_PATH -> hp-code, hp-jiuwenbox+ # RUNTIME_CODE_PATH -> hp-rt-foundation, hp-rt-management+ # CORE_CODE_PATH -> hp-openjiuwen+ # 规则2(仅 product):agentserver 镜像内置代码,4 个 agentserver 代码挂载,1 个 jiuwenbox代码挂载+ # (hp-code/hp-rt-foundation/hp-rt-management/hp-openjiuwen)无论变量是否为空都删;+ # hp-cgroup(sidecar 系统路径)始终保留。+ local drop_names=()+ if [[ -z "${DEPLOY_VARS["CLAW_CODE_PATH"]:-}" ]]; then+ drop_names+=(hp-code hp-jiuwenbox)+ fi+ if [[ -z "${DEPLOY_VARS["RUNTIME_CODE_PATH"]:-}" ]]; then+ drop_names+=(hp-rt-foundation hp-rt-management)+ fi+ if [[ -z "${DEPLOY_VARS["CORE_CODE_PATH"]:-}" ]]; then+ drop_names+=(hp-openjiuwen)+ fiif [[ "${DEPLOY_VARS["MODE"]}" == "product" ]]; then- jq '.rawdata.templates[].volumes |= map(select(has("hostPath") | not))- | .rawdata.containers |= map(if .container_id == "c-agentserver" then del(.securityContext) else . end)' \+ drop_names+=(hp-code hp-rt-foundation hp-rt-management hp-openjiuwen hp-jiuwenbox)+ fi++ if [ "${#drop_names[@]}" -gt 0 ]; then+ local jq_drop sec_filter+ jq_drop="$(printf '%s\n' "${drop_names[@]}" | jq -R . | jq -s .)"+ sec_filter=""+ if [[ "${DEPLOY_VARS["MODE"]}" == "product" ]]; then+ sec_filter=' | .rawdata.containers |= map(if .container_id == "c-agentserver" then del(.securityContext) else . end)'+ fi+ jq --argjson drop "${jq_drop}" \+ ".rawdata.templates[].volumes |= map(select(.name as \$n | \$drop | map(. == \$n) | any | not))${sec_filter}" \"${json_file}" > "${json_file}.tmp"jq '(.rawdata.templates[0].volumes | map(.name)) as $keep| .rawdata.containers[].volumeMounts |= map(select(.name as $n | $keep | map(. == $n) | any))' \@@ -29,18 +102,6 @@ render_patch_file() {rm -f "${json_file}.tmp"fi- # 移除所有注释行、过滤空值行 KEY=、按变量名排序- # 注意:不能 sort > 同一个文件,shell 会在管道启动前就截断输出文件,- # 导致左侧 grep 读到空。先写临时文件再 mv 覆盖。- grep -v '^[[:space:]]*#' "${env_file}" \- | grep '=' \- | awk -F'=' '$2 != ""' \- | sort > "${env_file}.tmp" && mv -f "${env_file}.tmp" "${env_file}"-- kubectl create configmap -n "${namespace}" "${envfile_name}" \- --from-env-file="${env_file}" \- --dry-run=client -o yaml \- | yq eval 'del(.metadata.creationTimestamp)' > "${CONFIG["AS_ENV_YAML_FILE"]}"success "AgentServer configuration rendered"}@@ -51,25 +112,14 @@ install_agentserver_patch() {local runtime_port="${DEPLOY_VARS["AGENT_RUNTIME_NODE_PORT"]}"local host="${DEPLOY_VARS["CURRENT_NODE_IP"]:-127.0.0.1}"- local yaml_file="${CONFIG["AS_ENV_YAML_FILE"]}"local json_file="${CONFIG["AS_JSON_FILE"]}"- ensure_secret_configmap- exec_cmd kubectl apply -f "${yaml_file}"-- # 下发agentserver服务配置+ # 下发agentserver服务配置:先确认 runtime NodePort 真正可达(等 kube-proxy+ # 规则生效),再推 config_sync,并校验 HTTP 2xx + ok==true(不再只看 curl 退出码)。+ wait_http_ready "${host}" "${runtime_port}" "/healthz" "agent-runtime NodePort"info "Pushing AgentServer template"- set +e- local response=$(curl -s --max-time 20 -X POST "http://${host}:${runtime_port}/api/session/config_sync" \- -H "Content-Type: application/json" \- -d @"${json_file}" 2>&1)- local rc=$?- set -e-- if [ ${rc} -ne 0 ]; then- error "Send AgentServer config failed (curl rc=${rc}): ${response}"- fi- success "AgentServer template pushed."+ post_and_validate "http://${host}:${runtime_port}/api/session/config_sync" \+ "@${json_file}" runtime}install_model_patch() {@@ -83,12 +133,14 @@ install_model_patch() {local agent_template_id="${AGENT_TEMPLATE_ID:-default-agent}"local model_template_id="default-model"+ # 下发前确认 gateway config NodePort 真正可达(等 kube-proxy 规则生效)。+ # gateway readinessProbe 即 /api/v1/ready on config-http(8775),与下发同端口。+ wait_http_ready "${host}" "${gw_node_port}" "/api/v1/ready" "gateway NodePort"+# 1. 下发 model_template(数据一:模型凭据)info "Pushing model template (template_id=${model_template_id})"- set +e- local response=$(curl -s --max-time 20 -X POST "http://${host}:${gw_node_port}/api/v1/model-templates" \- -H "Content-Type: application/json" \- -d "{+ post_and_validate "http://${host}:${gw_node_port}/api/v1/model-templates" \+ "{\"template_id\":\"${model_template_id}\",\"template_name\":\"${DEPLOY_VARS["MODEL_NAME"]} default\",\"description\":\"auto-pushed\",@@ -103,52 +155,23 @@ install_model_patch() {\"enable_function_calling\":true,\"verify_ssl\":false,\"enabled\":true- }" 2>&1)- local rc=$?- set -e- if [ ${rc} -ne 0 ]; then- error "Push model template failed (curl rc=${rc}): ${response}"- fi- success "Model template pushed (template_id=${model_template_id})"+ }" gateway# 2. 下发 agent_template(数据二:template_ref 指向 model_template)info "Pushing agent template (template_id=${agent_template_id}, ref→${model_template_id})"- set +e- response=$(curl -s --max-time 20 -X POST "http://${host}:${gw_node_port}/api/v1/agent-templates" \- -H "Content-Type: application/json" \- -d "{\"template_id\":\"${agent_template_id}\",\"template_name\":\"default agent\",\"template_ref\":{\"default_model\":[\"${model_template_id}\"]},\"enabled\":true}" 2>&1)- rc=$?- set -e- if [ ${rc} -ne 0 ]; then- error "Push agent template failed (curl rc=${rc}): ${response}"- fi- success "Agent template pushed (template_id=${agent_template_id})"+ post_and_validate "http://${host}:${gw_node_port}/api/v1/agent-templates" \+ "{\"template_id\":\"${agent_template_id}\",\"template_name\":\"default agent\",\"template_ref\":{\"default_model\":[\"${model_template_id}\"]},\"enabled\":true}" \+ gateway# 3. 下发 instance_agent_resource(数据三:resource_id=bot_id, ref_template_id 指向 agent_template)info "Pushing instance agent resource (resource_id=${bot_id}, ref→${agent_template_id})"- set +e- response=$(curl -s --max-time 20 -X POST "http://${host}:${gw_node_port}/api/v1/instance-agent-resources" \- -H "Content-Type: application/json" \- -d "{\"resource_id\":\"${bot_id}\",\"ref_template_id\":\"${agent_template_id}\",\"resource_name\":\"default agent\",\"match_expr\":[],\"enabled\":true}" 2>&1)- rc=$?- set -e- if [ ${rc} -ne 0 ]; then- error "Push instance agent resource failed (curl rc=${rc}): ${response}"- fi- success "Instance agent resource pushed (resource_id=${bot_id})"+ post_and_validate "http://${host}:${gw_node_port}/api/v1/instance-agent-resources" \+ "{\"resource_id\":\"${bot_id}\",\"ref_template_id\":\"${agent_template_id}\",\"resource_name\":\"default agent\",\"match_expr\":[],\"enabled\":true}" \+ gateway}install_patch(){install_agentserver_patchinstall_model_patch-}--uninstall_patch() {- if [[ "${DEPLOY_VARS["RENDER_ONLY"]}" == "true" || "${DEPLOY_VARS["APPLY_PATCH"]}" == "false" ]]; then- return- fi-- local yaml_file="${CONFIG["AS_ENV_YAML_FILE"]}"- exec_cmd kubectl delete -f "${yaml_file}" --ignore-not-found=true}\ No newline at end of filediff --git a/deploy/enterprise/runtime_handler.sh b/deploy/enterprise/runtime_handler.shindex fa15fd3dea..e14b10adcb 100755--- a/deploy/enterprise/runtime_handler.sh+++ b/deploy/enterprise/runtime_handler.sh@@ -1,6 +1,41 @@#!/usr/bin/env bashset -euo >/dev/null 2>&1+render_agentserver_env_configmap() {+ local namespace="${DEPLOY_VARS["NAMESPACE"]}"+ local env_template="${CONFIG["AS_ENV_TEMPLATE_FILE"]}"+ local env_file="${CONFIG["AS_ENV_FILE"]}"+ local envfile_name="${DEPLOY_VARS["AGENT_SERVER_ENV_CM_NAME"]}"+ local yaml_file="${CONFIG["AS_ENV_YAML_FILE"]}"++ render_config_template "${env_template}" "${env_file}" "DEPLOY_VARS"++ # 移除所有注释行、过滤空值行 KEY=、按变量名排序+ # 注意:不能 sort > 同一个文件,shell 会在管道启动前就截断输出文件,+ # 导致左侧 grep 读到空。先写临时文件再 mv 覆盖。+ grep -v '^[[:space:]]*#' "${env_file}" \+ | grep '=' \+ | awk -F'=' '$2 != ""' \+ | sort > "${env_file}.tmp" && mv -f "${env_file}.tmp" "${env_file}"++ kubectl create configmap -n "${namespace}" "${envfile_name}" \+ --from-env-file="${env_file}" \+ --dry-run=client -o yaml \+ | yq eval 'del(.metadata.creationTimestamp)' > "${yaml_file}"+ success "AgentServer env ConfigMap rendered: ${yaml_file}"+}++create_agentserver_env_configmap() {+ local yaml_file="${CONFIG["AS_ENV_YAML_FILE"]}"+ ensure_secret_configmap+ exec_cmd kubectl apply -f "${yaml_file}"+}++delete_agentserver_env_configmap() {+ local yaml_file="${CONFIG["AS_ENV_YAML_FILE"]}"+ exec_cmd kubectl delete -f "${yaml_file}" --ignore-not-found=true+}+gen_runtime_file() {local template_file="${CONFIG["RUNTIME_TEMPLATE_FILE"]}"local file="${CONFIG["RUNTIME_FILE"]}"@@ -39,6 +74,7 @@ render_runtime_files() {ensure_available_port "AGENT_RUNTIME_NODE_PORT"gen_runtime_filerender_patch_file+ render_agentserver_env_configmap}deploy_runtime() {@@ -49,6 +85,7 @@ deploy_runtime() {ensure_secret_configmapexec_cmd kubectl apply -f ${file}wait_k8s_resource_ready "deployment" "${name}" "${namespace}"+ create_agentserver_env_configmapinstall_patch}@@ -79,5 +116,5 @@ uninstall_runtime() {exec_cmd kubectl delete -f "${file}" --ignore-not-found=trueuninstall_secret_configmapensure_redis_down- uninstall_patch+ delete_agentserver_env_configmap}