@@ -175,6 +175,35 @@ login_admin() {
175175 return 1
176176}
177177
178+ # #######################################
179+ # 获取 Higress Gateway 公网 IP 地址
180+ # #######################################
181+ get_higress_gateway_address () {
182+ log " 获取 Higress Gateway 公网 IP 地址..." >&2
183+
184+ local gateway_ip=" "
185+
186+ # 尝试多个公网 IP 检测服务
187+ for service in " ifconfig.me" " icanhazip.com" " ipecho.net/plain" " api.ipify.org" ; do
188+ gateway_ip=$( curl -s --connect-timeout 3 --max-time 5 " http://${service} " 2> /dev/null | tr -d ' [:space:]' )
189+
190+ # 验证是否为有效的 IPv4 地址(排除内网地址)
191+ if [[ " $gateway_ip " =~ ^[0-9]{1,3}\. [0-9]{1,3}\. [0-9]{1,3}\. [0-9]{1,3}$ ]]; then
192+ if [[ ! " $gateway_ip " =~ ^10\. ]] && \
193+ [[ ! " $gateway_ip " =~ ^172\. (1[6-9]| 2[0-9]| 3[0-1])\. ]] && \
194+ [[ ! " $gateway_ip " =~ ^192\. 168\. ]] && \
195+ [[ ! " $gateway_ip " =~ ^127\. ]]; then
196+ log " 检测到公网 IP: ${gateway_ip} " >&2
197+ echo " ${gateway_ip} "
198+ return 0
199+ fi
200+ fi
201+ done
202+
203+ err " 无法获取公网 IP 地址"
204+ return 1
205+ }
206+
178207# #######################################
179208# 获取或创建 Gateway ID
180209# #######################################
@@ -189,8 +218,32 @@ get_or_create_gateway() {
189218 return 0
190219 fi
191220
192- # 尝试创建(Docker 环境使用 higress:8001)
193- local body=" {\" gatewayName\" :\" ${gateway_name} \" ,\" gatewayType\" :\" HIGRESS\" ,\" higressConfig\" :{\" address\" :\" http://higress:8001\" ,\" username\" :\" admin\" ,\" password\" :\" ${HIGRESS_PASSWORD} \" }}"
221+ # 获取 Higress Gateway 公网 IP
222+ local gateway_ip
223+ gateway_ip=$( get_higress_gateway_address)
224+ if [[ -z " $gateway_ip " ]]; then
225+ err " 无法获取 Higress Gateway 公网 IP,无法创建网关"
226+ return 1
227+ fi
228+
229+ # 使用 jq 构建 JSON 请求体
230+ local body=$( jq -n \
231+ --arg gatewayName " $gateway_name " \
232+ --arg gatewayType " HIGRESS" \
233+ --arg address " http://higress:8001" \
234+ --arg username " admin" \
235+ --arg password " $HIGRESS_PASSWORD " \
236+ --arg gatewayAddress " http://$gateway_ip :8082" \
237+ ' {
238+ gatewayName: $gatewayName,
239+ gatewayType: $gatewayType,
240+ higressConfig: {
241+ address: $address,
242+ username: $username,
243+ password: $password,
244+ gatewayAddress: $gatewayAddress
245+ }
246+ }' )
194247
195248 call_api " 插入网关" " POST" " /api/v1/gateways" " $body " > /dev/null 2>&1 || true
196249
@@ -385,9 +438,20 @@ publish_to_portal() {
385438 local portal_id=" $2 "
386439 local mcp_name=" $3 "
387440
388- if call_api " 发布到门户" " POST" " /api/v1/products/${product_id} /publications/${portal_id} " " " ; then
389- log " [${mcp_name} ] 发布到门户成功"
390- return 0
441+ # 构建请求体
442+ local body=" {\" portalId\" :\" ${portal_id} \" }"
443+
444+ if call_api " 发布到门户" " POST" " /api/v1/products/${product_id} /publications" " $body " ; then
445+ if [[ " $API_HTTP_CODE " =~ ^2[0-9]{2}$ ]]; then
446+ log " [${mcp_name} ] 发布到门户成功"
447+ return 0
448+ elif [[ " $API_HTTP_CODE " == " 409" ]]; then
449+ log " [${mcp_name} ] 产品已发布到门户(跳过)"
450+ return 0
451+ else
452+ log " [${mcp_name} ] 发布到门户失败(HTTP ${API_HTTP_CODE} )"
453+ return 0 # 允许失败,继续执行
454+ fi
391455 else
392456 log " [${mcp_name} ] 发布到门户失败(可能已发布)"
393457 return 0 # 允许失败,继续执行
0 commit comments