Skip to content

Commit

Permalink
feature: P-Agent 安装 IPv6 场景适配 (closed TencentBlueKing#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Nov 29, 2022
1 parent 98b4c5c commit 13eba6a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 38 deletions.
5 changes: 1 addition & 4 deletions apps/backend/agent/solution_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,7 @@ def get_run_cmd_base_params(self) -> typing.List[str]:
# 代理机器配置
f"-HPP '{settings.BK_NODEMAN_NGINX_PROXY_PASS_PORT}'",
# 代理机器主机信息
f"-I {self.gse_servers_info['jump_server'].inner_ip}",
f"-I6 {self.gse_servers_info['jump_server'].inner_ipv6}"
if self.gse_servers_info["jump_server"].inner_ipv6
else "",
f"-I {self.gse_servers_info['jump_server'].inner_ip or self.gse_servers_info['jump_server'].inner_ipv6}",
]

# 通道特殊配置
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/components/collections/agent_new/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def execute_job_commands(self, sub_inst_id, installation_tool: InstallationTools
)
self.log_info(
sub_inst_ids=sub_inst_id,
log_content=_("已选择 {inner_ip} 作为本次安装的跳板机").format(inner_ip=jump_server.inner_ip),
log_content=_("已选择 {inner_ip} 作为本次安装的跳板机").format(inner_ip=jump_server.inner_ip or jump_server.inner_ipv6),
)
path = os.path.join(settings.BK_SCRIPTS_PATH, constants.SetupScriptFileName.SETUP_PAGENT_PY.value)
with open(path, encoding="utf-8") as fh:
Expand Down Expand Up @@ -453,8 +453,8 @@ def execute_job_commands(self, sub_inst_id, installation_tool: InstallationTools
"2. Proxy是否已正确完成所有安装步骤且状态正常。 \n"
"3. 点击上面链接跳转到作业平台查看任务执行情况。\n"
).format(
host_inner_ip=host.inner_ip,
jump_server_ip=jump_server.inner_ip,
host_inner_ip=host.inner_ip or host.inner_ipv6,
jump_server_ip=jump_server.inner_ip or host.inner_ipv6,
download_port=settings.BK_NODEMAN_NGINX_DOWNLOAD_PORT,
proxy_pass_port=settings.BK_NODEMAN_NGINX_PROXY_PASS_PORT,
),
Expand Down
2 changes: 1 addition & 1 deletion apps/node_man/serializers/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def validate(self, attrs):
attrs["bk_host_ids"] = bk_host_ids
attrs["bk_biz_scope"] = bk_biz_scope

if attrs["job_type"] not in ["REINSTALL_PROXY", "UPGRADE_PROXY"]:
if attrs["job_type"] not in ["REINSTALL_PROXY", "UPGRADE_PROXY", "UNINSTALL_PROXY"]:
return attrs

# 如果开启业务灰度,安装新版本 Agent
Expand Down
3 changes: 3 additions & 0 deletions dev_log/2.2.32/crayon_202211292126.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
feature:
- "P-Agent 安装 IPv6 场景适配 (closed #1290)"
48 changes: 31 additions & 17 deletions script_tools/agent_tools/agent2/setup_agent.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rem DEFAULT DEFINITION
set PKG_NAME=
set CLOUD_ID=
set UNINSTALL=1
set UNREGISTER_AGENT_ID=1
set BACKUP_CONFIG_FILE=procinfo.json

:CheckOpts
Expand Down Expand Up @@ -41,6 +42,7 @@ if "%1" EQU "-K" (set TRACKER_PORT=%~2) && shift && shift && goto CheckOpts
if "%1" EQU "-U" (set INSTALL_USER=%~2) && shift && shift && goto CheckOpts
if "%1" EQU "-P" (set INSTALL_PASSWORD=%~2) && shift && shift && goto CheckOpts
if "%1" EQU "-R" (set UNINSTALL=0) && shift && shift && goto CheckOpts
if "%1" EQU "-F" (set UNREGISTER_AGENT_ID=0) && shift && goto CheckOpts
if "%1" NEQ "" echo Invalid option: "%1" && goto :EOF && exit /B 1
rem if "%1" EQU "-R" goto remove_agent_pro

Expand Down Expand Up @@ -187,7 +189,7 @@ goto :EOF
ping -n 1 127.0.0.1 >nul 2>&1
for %%a in (%CLUSTER_PORT%,%DATA_PORT%) do (
ping -n 1 127.0.0.1 >nul 2>&1
netstat -an | findstr /r /C:":%%a *ESTABLISHED"
netstat -an | findstr /r /C:":%%a *ESTABLISHED"
if !errorlevel! equ 0 (
for /F "tokens=*" %%i in ('netstat -an ^| findstr /r /C:":%%a *ESTABLISHED"') do (set conninfo=%%i)
call :print_check_deploy_result INFO check_deploy_result - "%%a connect to gse server success" 1>nul 2>&1
Expand Down Expand Up @@ -219,7 +221,7 @@ goto :EOF
call :multi_report_step_status
) else (
call :print WARN check_env - "connect to upstream cluster server: %%p %CLUSTER_PORT% failed"
set network_not_reachable=%network_not_reachable% %%p to %CLUSTER_PORT%
set network_not_reachable=%network_not_reachable% %%p to %CLUSTER_PORT%
call :multi_report_step_status
)
)
Expand All @@ -228,7 +230,7 @@ goto :EOF
if "!network_not_reachable!" == "" (
echo "reachable"
) else (
call :print FAIL check_env FAILED "%network_not_reachable% is not reachable"
call :print FAIL check_env FAILED "%network_not_reachable% is not reachable"
call :multi_report_step_status
exit /b 1
)
Expand All @@ -247,7 +249,7 @@ goto :EOF
call :multi_report_step_status
) else (
call :print WARN check_env - "connect to upstream data server: %%p %DATA_PORT% failed"
set data_network_not_reachable=%data_network_not_reachable% %%p to %DATA_PORT%
set data_network_not_reachable=%data_network_not_reachable% %%p to %DATA_PORT%
call :multi_report_step_status
)
)
Expand All @@ -256,7 +258,7 @@ goto :EOF
if "!data_network_not_reachable!" == "" (
echo "reachable"
) else (
call :print FAIL check_env FAILED "%data_network_not_reachable% is not reachable"
call :print FAIL check_env FAILED "%data_network_not_reachable% is not reachable"
call :multi_report_step_status
exit /b 1
)
Expand All @@ -276,7 +278,7 @@ goto :EOF
call :multi_report_step_status
) else (
call :print WARN check_env - "connect to upstream file server: %%p %%a failed"
set file_network_not_reachable=%file_network_not_reachable% %%p to %%a
set file_network_not_reachable=%file_network_not_reachable% %%p to %%a
call :multi_report_step_status
)
)
Expand All @@ -286,7 +288,7 @@ goto :EOF
if "!file_network_not_reachable!" == "" (
echo "reachable"
) else (
call :print FAIL check_env FAILED "%file_network_not_reachable% is not reachable"
call :print FAIL check_env FAILED "%file_network_not_reachable% is not reachable"
call :multi_report_step_status
exit /b 1
)
Expand All @@ -312,7 +314,7 @@ goto :EOF
call :multi_report_step_status
) else (
call :print WARN check_env - "gse server %%p %CLUSTER_PORT% can not reachable"
set network_not_reachable=%network_not_reachable% %%p to %CLUSTER_PORT%
set network_not_reachable=%network_not_reachable% %%p to %CLUSTER_PORT%
call :multi_report_step_status
)
)
Expand All @@ -321,7 +323,7 @@ goto :EOF
if "!network_not_reachable!" == "" (
echo "reachable"
) else (
call :print FAIL check_env FAILED "%network_not_reachable% is not reachable"
call :print FAIL check_env FAILED "%network_not_reachable% is not reachable"
call :multi_report_step_status
exit /b 1
)
Expand All @@ -339,7 +341,7 @@ goto :EOF
call :multi_report_step_status
) else (
call :print WARN check_env - "gse server %%p %DATA_PORT% can not reachable"
set network_not_reachable=%network_not_reachable% %%p to %DATA_PORT%
set network_not_reachable=%network_not_reachable% %%p to %DATA_PORT%
call :multi_report_step_status
)
)
Expand All @@ -348,7 +350,7 @@ goto :EOF
if "!network_not_reachable!" == "" (
echo "reachable"
) else (
call :print FAIL check_env FAILED "%network_not_reachable% is not reachable"
call :print FAIL check_env FAILED "%network_not_reachable% is not reachable"
call :multi_report_step_status
exit /b 1
)
Expand All @@ -366,7 +368,7 @@ goto :EOF
call :print INFO check_env - "gse server %%p %%a is reachable"
call :multi_report_step_status
) else (
set network_not_reachable=%network_not_reachable% %%p to %%a
set network_not_reachable=%network_not_reachable% %%p to %%a
call :print INFO check_env - "gse server %%p %%a can not reachable"
call :multi_report_step_status
)
Expand Down Expand Up @@ -1071,7 +1073,7 @@ goto :EOF
goto :EOF

:check_uninstall_result

set service=1
sc query gse_agent_daemon_%service_id% | findstr /C:"does not exist" 1>nul 2>&1
if %errorlevel% equ 0 (
Expand Down Expand Up @@ -1103,6 +1105,11 @@ goto :EOF
call :multi_report_step_status
exit /b 1
)

if %UNREGISTER_AGENT_ID% EQU 0 (
call :unregister_agent_id SKIP
)

%gse_winagent_home%\\agent\\bin\\gse_agent.exe --agent-id -f %GSE_AGENT_ETC_DIR%\\gse_agent.conf
if %errorlevel% EQU 0 (
call :re_register_agent_id
Expand Down Expand Up @@ -1214,6 +1221,7 @@ goto :EOF
goto :EOF

:unregister_agent_id
(set skip=%1)
if NOT EXIST %gse_winagent_home%\\agent\\bin\\gse_agent.exe (
call :print FAIL unregister_agent_id FAILED "%gse_winagent_home%\\agent\\bin\\gse_agent.exe not exists"
call :multi_report_step_status
Expand All @@ -1232,12 +1240,17 @@ goto :EOF
%gse_winagent_home%\\agent\\bin\\gse_agent.exe --unregister
)
if %errorlevel% EQU 0 (
call :print INFO register_agent_id - "unregister agent id success"
call :print INFO unregister_agent_id - "unregister agent id success"
call :multi_report_step_status
) else (
call :print FAIL register_agent_id FAILED "unregister agent id failed"
call :multi_report_step_status
exit /b 1
if "%skip%" == "SKIP": (
call :print WARN unregister_agent_id - "unregister agent id failed, but skip it"
call :multi_report_step_status
) else (
call :print FAIL unregister_agent_id FAILED "unregister agent id failed"
call :multi_report_step_status
exit /b 1
)
)
goto :EOF

Expand Down Expand Up @@ -1350,6 +1363,7 @@ goto :EOF
echo -O CLUSTER_PORT
echo -E FILE_SVR_PORT
echo -R UNINSTALL_AGENT
echo -F UNREGISTER_AGENT_ID [optional]
goto :EOF

:EOF
18 changes: 16 additions & 2 deletions script_tools/agent_tools/agent2/setup_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ register_agent_id () {
fail register_agent_id FAILED "gse_agent file not exists in $AGENT_SETUP_PATH/bin"
fi


if [[ "${UNREGISTER_AGENT_ID}" == "TRUE" ]]; then
log register_agent_id - "trying to unregister agent id"
unregister_agent_id SKIP
fi

log register_agent_id - "trying to register agent id"
if [ -f "${GSE_AGENT_CONFIG_PATH}" ]; then
registe_result=$($AGENT_SETUP_PATH/bin/gse_agent -f "${GSE_AGENT_CONFIG_PATH}" --register 2>&1)
Expand All @@ -346,6 +352,7 @@ register_agent_id () {
}

unregister_agent_id () {
local skip="$1"
log unregister_agent_id - "trying to unregister agent id"
if [ -f "$AGENT_SETUP_PATH/bin/gse_agent" ]; then
if [ -f "${GSE_AGENT_CONFIG_PATH}" ]; then
Expand All @@ -357,7 +364,11 @@ unregister_agent_id () {
if [[ $? -eq 0 ]]; then
log unregister_agent_id SUCCESS "unregister agent id succeed"
else
fail unregister_agent_id FAILED "unregister agent id failed, error: ${unregister_agent_id_result}"
if [[ "${skip}" == "SKIP" ]]; then
warn unregister_agent_id - "unregister agent id failed, but skip it. error: ${unregister_agent_id_result}"
else
fail unregister_agent_id FAILED "unregister agent id failed, error: ${unregister_agent_id_result}"
fi
fi
else
log unregister_agent_id FAILED "gse_agent file not exists in $AGENT_SETUP_PATH/bin"
Expand Down Expand Up @@ -756,6 +767,7 @@ _help () {
echo " -S BT_PORT_START"
echo " -Z BT_PORT_END"
echo " -K TRACKER_PORT"
echo " -F UNREGISTER_AGENT_ID [optional]"

exit 0
}
Expand Down Expand Up @@ -783,6 +795,7 @@ AGENT_SETUP_PATH="/usr/local/gse/${NODE_TYPE}"
CURR_PID=$$
OVERIDE=false
REMOVE=false
UNREGISTER_AGENT_ID=false
CALLBACK_URL=
AGENT_PID=
DEBUG=
Expand All @@ -792,7 +805,7 @@ LOG_RPT_CNT=0
BULK_LOG_SIZE=3

# main program
while getopts n:t:I:i:l:s:uc:r:x:p:e:a:k:N:v:oT:RDO:E:A:V:B:S:Z:K: arg; do
while getopts n:t:I:i:l:s:uc:r:x:p:e:a:k:N:v:oT:RDO:E:A:V:B:S:Z:K:F arg; do
case $arg in
n) NAME="$OPTARG" ;;
t) VERSION="$OPTARG" ;;
Expand Down Expand Up @@ -821,6 +834,7 @@ while getopts n:t:I:i:l:s:uc:r:x:p:e:a:k:N:v:oT:RDO:E:A:V:B:S:Z:K: arg; do
S) BT_PORT_START=$OPTARG ;;
Z) BT_PORT_END=$OPTARG ;;
K) TRACKER_PORT=$OPTARG ;;
F) UNREGISTER_AGENT_ID=TRUE ;;
*) _help ;;
esac
done
Expand Down
2 changes: 1 addition & 1 deletion script_tools/agent_tools/agent2/setup_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ is_port_connected_by_pid () {
sleep 1
stat -L -c %i /proc/"$pid"/fd/* 2>/dev/null \
| grep -qwFf - \
<( awk -v p="$port" 'BEGIN{ check=sprintf(":%04X01$", p)} $3$4 ~ check {print $10}' /proc/net/tcp) \
<( awk -v p="$port" 'BEGIN{ check=sprintf(":%04X01$", p)} $3$4 ~ check {print $10}' /proc/net/tcp*) \
&& return 0
done
return 1
Expand Down
29 changes: 27 additions & 2 deletions script_tools/setup_pagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@

import argparse
import base64
import ipaddress
import json
import os
import re
import socket
import sys
import time
import traceback
from functools import partial
from io import StringIO
from pathlib import Path
from subprocess import Popen
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional

PRIVATE_KEY_MERGED_TEXT = """
%%PRIVATE_KEY_MERGED_TEXT%%
Expand Down Expand Up @@ -45,6 +47,29 @@
JOB_PRIVATE_KEY_RE = re.compile(r"^(-{5}BEGIN .*? PRIVATE KEY-{5})(.*?)(-{5}END .*? PRIVATE KEY-{5}.?)$")


def is_ip(ip: str, _version: Optional[int] = None) -> bool:
"""
判断是否为合法 IP
:param ip:
:param _version: 是否为合法版本,缺省表示 both
:return:
"""
try:
ip_address = ipaddress.ip_address(ip)
except ValueError:
return False
if _version is None:
return True
return ip_address.version == _version


# 判断是否为合法 IPv6
is_v6 = partial(is_ip, _version=6)

# 判断是否为合法 IPv4
is_v4 = partial(is_ip, _version=4)


def arg_parser() -> argparse.ArgumentParser:
"""Commandline argument parser"""
parser = argparse.ArgumentParser(description="p-agent setup scripts")
Expand Down Expand Up @@ -277,7 +302,7 @@ def execute_shell_solution(


def is_port_listen(ip: str, port: int) -> bool:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket((socket.AF_INET, socket.AF_INET6)[is_v6(ip)], socket.SOCK_STREAM)
r = s.connect_ex((ip, port))

if r == 0:
Expand Down
Loading

0 comments on commit 13eba6a

Please sign in to comment.