forked from TencentBlueKing/bk-install
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinitdata.sh
More file actions
187 lines (172 loc) · 8.69 KB
/
initdata.sh
File metadata and controls
187 lines (172 loc) · 8.69 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
#!/usr/bin/env bash
# vim:ft=sh sts=4 ts=4 sw=4 expandtab nu ai
# set -euo pipefail
source "${CTRL_DIR}"/load_env.sh
source "${CTRL_DIR}"/functions
source "${CTRL_DIR}"/tools.sh
set -e
_initdata_mysql () {
# 如果存在mysql(paas)则paas 只授权对应需要改独立mysql的用户登录, 否则为默认mysql
source <(/opt/py36/bin/python ${CTRL_DIR}/qq.py -s -P ${CTRL_DIR}/bin/default/port.yaml)
local grant_user_list=${_projects["mysql"]}
# 第一步 对应模块授权对应的mysql 如果没有独有mysql则授权 default mysql
for ip in "${BK_MYSQL_IP[@]}"; do
for project in ${grant_user_list[@]}; do
target=BK_MYSQL_${project^^}_IP_COMMA
if [[ $project == 'monitorv3' ]]; then
arraypaas=BK_MONITOR_MYSQL_PASSWORD
elif [[ $project == 'job' ]]; then
arraypaas=BK_JOB_MANAGE_MYSQL_PASSWORD
elif [[ $project == 'log' ]]; then
arraypaas=BK_BKLOG_MYSQL_PASSWORD
else
arraypaas=BK_${project^^}_MYSQL_PASSWORD
fi
mysql_user=${_project_name["mysql,${project}"]}
if ! [[ -z ${!target} ]]; then
login_path="${project}-root"
emphasize "grant mysql privileges for ${mysql_user} by ${ip} login_path: ${login_path}"
"${CTRL_DIR}"/pcmd.sh -H "${ip}" "${CTRL_DIR}/bin/grant_mysql_priv.sh -n ${login_path} -u ${mysql_user} -p '${!arraypaas}' -H ${ALL_IP_COMMA}"
"${CTRL_DIR}"/pcmd.sh -H "${ip}" "${CTRL_DIR}/bin/grant_mysql_priv.sh -n ${login_path} -u root -p '${BK_MYSQL_ADMIN_PASSWORD}' -H ${ALL_IP_COMMA}"
fi
if ! grep "${ip}" "${CTRL_DIR}"/bin/02-dynamic/hosts.env | grep "BK_MYSQL_.*_IP_COMMA" >/dev/null; then
login_path="default-root"
emphasize "grant mysql privileges for ${mysql_user} by ${ip} login_path: ${login_path}"
"${CTRL_DIR}"/pcmd.sh -H "${ip}" "${CTRL_DIR}/bin/grant_mysql_priv.sh -n ${login_path} -u ${mysql_user} -p '${!arraypaas}' -H ${ALL_IP_COMMA}"
"${CTRL_DIR}"/pcmd.sh -H "${ip}" "${CTRL_DIR}/bin/grant_mysql_priv.sh -n ${login_path} -u root -p '${BK_MYSQL_ADMIN_PASSWORD}' -H ${ALL_IP_COMMA}"
fi
done
done
# 如果存在mysql(paas), 授权给所有模块访问
if ! [[ -z "${BK_MYSQL_PAAS_IP_COMMA}" ]]; then
for project in ${grant_user_list[@]}; do
mysql_user=${_project_name["mysql,${project}"]}
if [[ $project == 'monitorv3' ]]; then
arraypaas=BK_MONITOR_MYSQL_PASSWORD
elif [[ $project == 'job' ]]; then
arraypaas=BK_JOB_MANAGE_MYSQL_PASSWORD
else
arraypaas=BK_${project^^}_MYSQL_PASSWORD
fi
login_path="paas-root"
"${CTRL_DIR}"/pcmd.sh -H "${BK_MYSQL_PAAS_IP_COMMA}" "${CTRL_DIR}/bin/grant_mysql_priv.sh -n '${login_path}' -u '${mysql_user}' -p '${!arraypaas}' -H '${ALL_IP_COMMA}'"
"${CTRL_DIR}"/pcmd.sh -H "${BK_MYSQL_PAAS_IP_COMMA}" "${CTRL_DIR}/bin/grant_mysql_priv.sh -n '${login_path}' -u root -p '${BK_MYSQL_ADMIN_PASSWORD}' -H '${ALL_IP_COMMA}'"
done
fi
}
_initdata_nodeman () {
local ip=$BK_NODEMAN_IP0
emphasize "copy file to nginx"
"${CTRL_DIR}"/pcmd.sh -H "$ip" "workon bknodeman-nodeman;export BK_FILE_PATH=${INSTALL_PATH}/bknodeman/cert/saas_priv.txt; runuser -u blueking ./bin/manage.sh copy_file_to_nginx"
emphasize "init gse plugins"
gse_agent_pkg=$(_find_latest_gse_agent)
emphasize "pack gse client with plugin"
bash "${CTRL_DIR}"/bin/pack_gse_client_with_plugin.sh \
-f "${BK_PKG_SRC_PATH}/${gse_agent_pkg}" -p "${BK_PKG_SRC_PATH}/gse_plugins" -e "${CTRL_DIR}/bin/04-final/gse.env" -c "${INSTALL_PATH}"/cert
emphasize "sync gse agent to host: nodeman"
chown -R blueking.blueking /tmp/gse/
rsync -v /tmp/gse/*.tgz "$ip:${INSTALL_PATH}/public/bknodeman/download/"
emphasize "sync gse_plugins to host: nodeman"
chown -R blueking.blueking "${BK_PKG_SRC_PATH}"/gse_plugins/
rsync -v "${BK_PKG_SRC_PATH}"/gse_plugins/*.tgz "$ip":"${INSTALL_PATH}"/bknodeman/nodeman/official_plugin/
emphasize "init official plugins on host: nodeman"
"${CTRL_DIR}"/pcmd.sh -H "$ip" "workon bknodeman-nodeman;export BK_FILE_PATH=${INSTALL_PATH}/bknodeman/cert/saas_priv.txt; rm -fv ./official_plugin/pluginscripts-*.tgz; ./bin/manage.sh init_official_plugins"
# 同步python包到download目录
emphasize "sync py36.tgz to nodeman host"
rsync -avz "${BK_PKG_SRC_PATH}"/python/py36.tgz "$ip":"${INSTALL_PATH}"/public/bknodeman/download/
${CTRL_DIR}/pcmd.sh -m "nodeman" "chown blueking.blueking -R ${INSTALL_PATH}/public/bknodeman/"
}
_initdata_cmdb () {
local tmpfile
tmpfile=$(mktemp /tmp/init_cmdb.XXXXXXXXX)
trap 'rm -f $tmpfile' EXIT
emphasize "migrate mongodb"
curl -s -X POST \
-H 'Content-Type:application/json' \
-H 'BK_USER:migrate' \
-H 'HTTP_BLUEKING_SUPPLIER_ID:0' \
"http://cmdb-admin.service.consul:$BK_CMDB_ADMIN_PORT/migrate/v3/migrate/enterprise/0" > "${tmpfile}" 2>&1
jq . "${tmpfile}"
if ! [[ $(jq .result "${tmpfile}") =~ 'true' ]]; then
err "cmdb migrate mongodb failed!"
else
emphasize "cmdb migrate mongodb success!"
fi
emphasize "Registration authority model for cmdb"
curl -s -X POST \
-H 'Content-Type:application/json' \
-H 'BK_USER:migrate' \
-H 'HTTP_BLUEKING_SUPPLIER_ID:0' \
--data '{"host": "http://cmdb-auth.service.consul:'$BK_CMDB_AUTH_PORT'"}' "http://cmdb-admin.service.consul:$BK_CMDB_ADMIN_PORT/migrate/v3/authcenter/init" > "${tmpfile}" 2>&1
jq . "${tmpfile}"
if ! [[ $(jq .result "${tmpfile}") =~ 'true' ]]; then
err "cmdb registration authority model failed"
else
emphasize "cmdb registration authority model success"
fi
emphasize "Registration gse dataid"
curl -s -X POST \
-H 'Content-Type:application/json' \
-H 'BK_USER:migrate' \
-H 'HTTP_BLUEKING_SUPPLIER_ID:0' \
"http://cmdb-admin.service.consul:$BK_CMDB_ADMIN_PORT/migrate/v3/migrate/old/dataid" > "${tmpfile}" 2>&1
jq . "${tmpfile}"
if ! [[ $(jq .result "${tmpfile}") =~ 'true' ]]; then
err "cmdb registration gse dataid failed"
else
emphasize "cmdb registration gse dataid success"
fi
}
_initdata_paas () {
curl --connect-timeout 10 \
-H 'Content-Type:application/x-www-form-urlencoded' \
-X POST \
-d "mq_ip=rabbitmq.service.consul&username=$BK_RABBITMQ_ADMIN_USER&password=$BK_RABBITMQ_ADMIN_PASSWORD" \
"http://$BK_PAAS_PRIVATE_ADDR/v1/rabbitmq/init/" || return 1
# 初始化saas环境变量
add_saas_environment
}
_initdata_topo () {
local ip mod
mod="$1"
if [[ -z "${mod}" || "${mod}" -ne 1 ]]; then
# 创建服务模板
/opt/py36/bin/python "${CTRL_DIR}"/bin/create_blueking_set.py -c "${BK_PAAS_APP_CODE}" -t "${BK_PAAS_APP_SECRET}" --tpl "${CTRL_DIR}"/bin/default/blueking_service_module.tpl --create-service
# 创建集群模板
"${CTRL_DIR}"/bin/create_blueking_topo_template.sh "${CTRL_DIR}"/bin/default/blueking_topo_module.tpl
# 创建蓝鲸集群
/opt/py36/bin/python "${CTRL_DIR}"/bin/create_blueking_set.py -c "${BK_PAAS_APP_CODE}" -t "${BK_PAAS_APP_SECRET}" --create-set
fi
# 转移主机到模块
for ip in "${ALL_IP[@]}"; do
tmpfile=$(mktemp /tmp/init_topo.XXXXXXXXX)
"${CTRL_DIR}"/pcmd.sh -H "${ip}" "cat ${BK_HOME}/.installed_module" > "${tmpfile}"
/opt/py36/bin/python "${CTRL_DIR}"/bin/create_blueking_set.py -c "${BK_PAAS_APP_CODE}" -t "${BK_PAAS_APP_SECRET}" --create-proc-instance -f "${tmpfile}" && rm -f "${tmpfile}" || return 1
done
}
_initdata_es7 () {
# 监控禁止产生以 write 开头的index
source <(/opt/py36/bin/python ${CTRL_DIR}/qq.py -s -P ${CTRL_DIR}/bin/default/port.yaml)
rest_port=$(awk -F ',' '{print $1}' <<<"${_project_port["es7,default"]}")
host="${_project_consul["es7,default"]}"
data=$(cat <<EOF
{
"persistent": {
"action.auto_create_index": "-write_*,*"
}
}
EOF
)
wait_ns_alive "${host}".service.consul || fail "es7 启动失败"
resp=$(curl -s -u elastic:"${BK_ES7_ADMIN_PASSWORD}" http://"${host}".service.consul:"${rest_port}"/_cluster/health |jq .status)
if [[ $resp == '"green"' ]]; then
curl -X PUT -w "\n" -s -u elastic:"${BK_ES7_ADMIN_PASSWORD}" "${host}.service.consul:${rest_port}/_cluster/settings" -H 'Content-Type: application/json' --data "${data}"
else
echo "es7 集群状态异常, msg -> ${resp}"
return 1
fi
}
_initdata_lesscode () {
cost_time_attention
"${CTRL_DIR}"/pcmd.sh -m lesscode "cd '${INSTALL_PATH}'/lesscode && npm run build"
}