导语:希望通过shell创建wiki里的页面,并把日志文件的内容贴进去。
shell命令实现如下
curl -u xujiamin:password -X POST -H "Content-Type: application/json" -d '{"type":"page","title":"test1","space":{"key":"~xujiamin"},"body":{"storage":{"value":"123","representation":"storage"}}}' http://wiki.deepwise.com/rest/api/content/80073894?expand=body.storage,version,space
Basic后面跟的是base64转码后的username:password
curl --location 'http://wiki.deepwise.com/rest/api/content' --header 'Content-Type: application/json' --header 'Authorization: Basic 123' --data '{
"type": "page",
"status": "current",
"title": "just test confluence api create page6",
"space": {"key": "~xujiamin"},
"ancestors": [{"id": 77015909}],
"body": {"storage": {"value": "<p>New page data for confluence api create page5.</p>","representation": "storage"}}
}
'
更新
put请求
{
"version": {
"number": 2
},
"title": "just test confluence api create page",
"type": "page",
"body": {
"storage": {
"value": "<p>123</p>\n<p>abc</p>",
"representation": "storage"
}
}
}
curl --location --request PUT 'http://wiki.deepwise.com/rest/api/content/80076493' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic eHVqaWFtaW46SGFuZ3pob3UwMEBwYXNz' \
--data '{
"version": {
"number": 3
},
"title": "just test confluence api create page",
"type": "page",
"body": {
"storage": {
"value": "<p>1234</p>\n<p>abcd</p>",
"representation": "storage"
}
}
}'
# 参数传入
curl --location --request PUT 'http://wiki.deepwise.com/rest/api/content/80076493' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic eHVqaWFtaW46SGFuZ3pob3UwMEBwYXNz' \
--data "{
\"version\": {
\"number\": 3
},
\"title\": \"just test confluence api create page\",
\"type\": \"page\",
\"body\": {
\"storage\": {
\"value\": \"$b\",
\"representation\": \"storage\"
}
}
}"
通过脚本把文件123的内容更新到page中。目前version是写死的
#!/bin/bash
# 变量定义
FILE="123"
PAGE_ID="80076493"
AUTH="auth" # 使用实际的 Base64 编码的用户名和密码替换此处的字符串
TITLE="just test confluence api create page"
VERSION=4
# 检查文件是否存在
if [ ! -f "$FILE" ]; then
echo "File $FILE does not exist."
exit 1
fi
# 读取文件内容并处理成 HTML 段落形式
CONTENT=""
while IFS= read -r line; do
if [ -n "$CONTENT" ]; then
CONTENT="$CONTENT\\n"
fi
CONTENT="${CONTENT}<p>${line}</p>"
done < "$FILE"
# 执行 curl 命令
curl --location --request PUT "http://wiki.deepwise.com/rest/api/content/$PAGE_ID" \
--header "Content-Type: application/json" \
--header "Authorization: Basic $AUTH" \
--data "{
\"version\": {
\"number\": $VERSION
},
\"title\": \"${TITLE}\",
\"type\": \"page\",
\"body\": {
\"storage\": {
\"value\": \"$CONTENT\",
\"representation\": \"storage\"
}
}
}"
重复名称就会提示400
贴一下上传安装和质检日志的shell,通过for循环导出日志,并替换掉其中的特殊字符避免提交出现500。并根据关键字对颜色进行调整。
#!/bin/sh
<<'COMMENT'
用于上传验证日志到wiki并钉钉通知我@xujiamin
COMMENT
blue() {
echo -e "\033[34m $(date +%Y%m%d_%H%M%S)>>>>>> $1 \033[0m"
}
green() {
echo -e "\033[32m $(date +%Y%m%d_%H%M%S)>>>>>> $1 \033[0m"
}
red() {
echo -e "\033[31m $(date +%Y%m%d_%H%M%S)>>>>>> $1 \033[0m"
}
yellow() {
echo -e "\033[33m $(date +%Y%m%d_%H%M%S)>>>>>> $1 \033[0m"
}
# $WikiAuth $DingToken $Tag ${Proj_Param} $Step
# 变量定义
FILE="ops_verify_install.log"
# PAGE_ID="80076493"
WikiAuth=$1 # 使用实际的 Base64 编码的用户名和密码替换此处的字符串
DingToken=$2
# TITLE="just test confluence api create page"
# 构建的tag号
Tag=$3
Proj_Param=$4
Step=$5
Title="${Proj_Param}_${MY_NODE_IP}_${Tag}_${Step}"
# 检查文件是否存在
cd /opt
if [ ! -f "$FILE" ]; then
echo "File $FILE does not exist."
exit 1
fi
# 去除转义的字符,不然post可能会500
sed -i 's/\x1B\[[0-9;]*[a-zA-Z]//g' /opt/ops_verify_install.log
sed -i 's/"//g' /opt/ops_verify_install.log
sed -i "s/'//g" /opt/ops_verify_install.log
sed -i '/^$/d' /opt/ops_verify_install.log
sed -i 's#(##g' /opt/ops_verify_install.log
sed -i 's#)##g' /opt/ops_verify_install.log
sed -i 's#{##g' /opt/ops_verify_install.log
sed -i 's#}##g' /opt/ops_verify_install.log
sed -i 's#\\##g' /opt/ops_verify_install.log
sed -i 's/\t/ /g' /opt/ops_verify_install.log
# 读取文件内容并处理成 HTML 段落形式
# CONTENT=""
# while IFS= read -r line; do
# if [ -n "$CONTENT" ]; then
# CONTENT="$CONTENT\\n"
# fi
# CONTENT="${CONTENT}<p>${line}</p>"
# done < "$FILE"
# 增加页面中的颜色
CONTENT=""
while IFS= read -r line; do
if [ -n "$CONTENT" ]; then
CONTENT="$CONTENT\\n"
fi
#if [ "$line" = "*成功*" ]; then
if echo "$line" | grep -q "成功"; then
# Insert a green-colored span tag before this line
#echo "成功匹配成功"
line="<span style=\\\"color: green;\\\">${line}</span>"
#blue "line: $line"
CONTENT="${CONTENT}<p>${line}</p>"
elif echo "$line" | grep -q "失败"; then
#echo "失败匹配成功"
line="<span style=\\\"color: red;\\\">${line}</span>"
#blue "line: $line"
CONTENT="${CONTENT}<p>${line}</p>"
elif echo "$line" | grep -q "job均已apply"; then
#echo "失败匹配成功"
line="<span style=\\\"color: blue;\\\">${line}</span>"
#blue "line: $line"
CONTENT="${CONTENT}<p>${line}</p>"
else
CONTENT="${CONTENT}<p>${line}</p>"
fi
done < "$FILE"
# wiki记录
curl --location 'http://wiki.deepwise.com/rest/api/content' --header 'Content-Type: application/json' --header "Authorization: Basic $WikiAuth" --data "{
\"type\": \"page\",
\"status\": \"current\",
\"title\": \"$Title\",
\"space\": {\"key\": \"~xujiamin\"},
\"ancestors\": [{\"id\": 80076537}],
\"body\": {\"storage\": {\"value\": \"$CONTENT\",\"representation\": \"storage\"}}
}"
# 钉钉通知
curl "https://oapi.dingtalk.com/robot/send?access_token=$DingToken" \
-H 'Content-Type: application/json' \
-d "{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"$Title 创建ok, http://wiki.deepwise.com/display/~xujiamin/${Title} \"
}
}"
https://docs.atlassian.com/ConfluenceServer/rest/7.4.11/#api/content-createContent
https://blog.csdn.net/dongwuming/article/details/120491848