diff --git a/3.3.sh b/3.3.sh index 2dc40a8..36799db 100755 --- a/3.3.sh +++ b/3.3.sh @@ -5,31 +5,68 @@ # 强制设置字符集为 UTF-8,防止 OpenStack 客户端因为读取到中文/全角标点而崩溃报错 ascii codec export LANG=en_US.UTF-8 export LC_ALL=en_US.UTF-8 -# 颜色定义 +# 学生可见输出:每组一行,失败时列出原因;仅统计实际检查结果。 RED='\033[0;31m' GREEN='\033[0;32m' -YELLOW='\033[0;33m' CYAN='\033[0;36m' -NC='\033[0m' # 恢复默认颜色 - +NC='\033[0m' PASS_CNT=0 FAIL_CNT=0 +SECTION_TITLE='' +SECTION_PASS=0 +SECTION_FAIL=0 +SECTION_DETAILS=() -pass() { echo -e "${GREEN}[✔ 通过] $1${NC}"; ((PASS_CNT++)); } -fail() { echo -e "${RED}[✘ 失败] $1${NC}"; ((FAIL_CNT++)); } -info() { echo -e "${CYAN}[➜] $1${NC}"; } +pass() { PASS_CNT=$((PASS_CNT+1)); } +fail() { + FAIL_CNT=$((FAIL_CNT+1)) + if [ -n "$SECTION_TITLE" ]; then + SECTION_DETAILS+=("$1") + else + printf '%b[✘ 失败] %s%b\n' "$RED" "$1" "$NC" + fi +} +section_summary() { + [ -n "$SECTION_TITLE" ] || return 0 + local passed=$((PASS_CNT-SECTION_PASS)) failed=$((FAIL_CNT-SECTION_FAIL)) message + # 没有实际检查结果的分组不显示,也不记为通过。 + if [ "$failed" -gt 0 ]; then + printf '%b[✘ 失败] %s:通过 %s,失败 %s%b\n' "$RED" "$SECTION_TITLE" "$passed" "$failed" "$NC" + for message in "${SECTION_DETAILS[@]}"; do + printf '%b · %s%b\n' "$RED" "$message" "$NC" + done + elif [ "$passed" -gt 0 ]; then + printf '%b[✔ 通过] %s(%s 项)%b\n' "$GREEN" "$SECTION_TITLE" "$passed" "$NC" + fi +} +info() { + section_summary + SECTION_TITLE=$1 + SECTION_PASS=$PASS_CNT + SECTION_FAIL=$FAIL_CNT + SECTION_DETAILS=() +} +finish() { + section_summary + printf '\n检查完毕:通过 %b%s%b 项,失败 %b%s%b 项。\n' \ + "$GREEN" "$PASS_CNT" "$NC" "$RED" "$FAIL_CNT" "$NC" + if [ "$FAIL_CNT" -gt 0 ]; then + printf '%b请按失败提示修正后重试。%b\n' "$RED" "$NC" + exit 1 + fi + printf '%b本次检查项全部通过。%b\n' "$GREEN" "$NC" + exit 0 +} -echo -e "${YELLOW}====================================================${NC}" -echo -e "${YELLOW} OpenStack Glance 镜像服务部署自测脚本 ${NC}" -echo -e "${YELLOW}====================================================${NC}" +printf '%bOpenStack Glance 镜像服务自测%b\n' "$CYAN" "$NC" if [ "$EUID" -ne 0 ]; then - echo -e "${RED}请使用 root 权限执行此脚本!${NC}" + fail "请使用 root 权限执行此脚本" exit 1 fi # 1. 检查数据库 -info "1. 检查 MariaDB 数据库创建与授权" +info "MariaDB 数据库创建与授权" if mysql -uglance -pglance -e "SHOW DATABASES;" 2>/dev/null | grep -qw "glance"; then pass "glance 数据库已创建且用户授权正确" @@ -43,7 +80,7 @@ else fi # 2. 检查软件包 -info "2. 检查依赖软件包安装" +info "依赖软件包安装" if rpm -q openstack-glance &>/dev/null; then pass "相关软件包 (openstack-glance) 已安装" else @@ -61,16 +98,15 @@ export OS_IDENTITY_API_VERSION=3 export NO_PROXY=localhost,127.0.0.1,controller # 验证 Keystone 连通性 +info "Keystone 认证与 Glance 注册" +AUTH_OK=1 if ! openstack token issue >/dev/null 2>&1; then - fail "当前无法与 Keystone 认证服务通信,请检查上节内容是否运行正常!" - echo -e "${RED}后续关于 Keystone 的依赖测试将被跳过!${NC}" - FAIL_CNT=$((FAIL_CNT+7)) - goto_end=1 + fail "Keystone 认证失败,请检查服务及管理员凭证" + AUTH_OK=0 fi -if [ -z "$goto_end" ]; then +if [ "$AUTH_OK" -eq 1 ]; then # 3. 检查 Glance 凭证注册 - info "3. 检查 KeyStone 中的 Glance 账号与权限注册" if openstack user show glance >/dev/null 2>&1; then pass "用户: glance 已在 KeyStone 中创建"; else fail "用户: glance 未在 KeyStone 中找到"; fi if openstack role assignment list --user glance --project service --role admin -f value --names 2>/dev/null | grep -q "glance"; then @@ -82,44 +118,48 @@ if [ -z "$goto_end" ]; then if openstack service show image >/dev/null 2>&1; then pass "服务实体: image(glance) 已创建"; else fail "服务实体: image(glance) 未创建"; fi # 4. 检查端点 Endpoint 注册 - info "4. 检查 Endpoint API 端点注册" - endpoints=$(openstack endpoint list --service image -f value -c Interface) + info "Endpoint API 端点注册" + endpoints=$(openstack endpoint list --service image -f value -c Interface 2>/dev/null) if echo "$endpoints" | grep -qw "public" && echo "$endpoints" | grep -qw "internal" && echo "$endpoints" | grep -qw "admin"; then pass "镜像服务的 public, internal, admin 端点已全部注册成功" else fail "镜像服务的端点注册不全,请检查 openstack endpoint list" fi - # 5. 检查 Glance 配置文件 - info "5. 检查 Glance 服务配置文件" - if grep -q "^connection.*=.*glance:glance@controller/glance" /etc/glance/glance-api.conf 2>/dev/null; then - pass "glance-api.conf 数据库配置正确" - else - fail "glance-api.conf 数据库连接 [database] 未配置或错误" - fi +fi - if grep -q "^auth_url.*=.*http://controller:5000" /etc/glance/glance-api.conf 2>/dev/null; then - pass "glance-api.conf 身份认证 [keystone_authtoken] 配置正确" - else - fail "glance-api.conf 身份认证 [keystone_authtoken] 配置有误" - fi +# 本地配置和进程检查不依赖 Keystone 认证。 +# 5. 检查 Glance 配置文件 +info "Glance 服务配置文件" +if grep -q "^connection.*=.*glance:glance@controller/glance" /etc/glance/glance-api.conf 2>/dev/null; then + pass "glance-api.conf 数据库配置正确" +else + fail "glance-api.conf 数据库连接 [database] 未配置或错误" +fi - if grep -q "^filesystem_store_datadir.*=.*?/var/lib/glance/images/" /etc/glance/glance-api.conf 2>/dev/null || grep -q "^stores.*=.*file" /etc/glance/glance-api.conf; then - pass "glance-api.conf 本地存储目录 [glance_store] 配置完成" - else - fail "glance-api.conf 存储方式 [glance_store] 配置有误" - fi +if grep -q "^auth_url.*=.*http://controller:5000" /etc/glance/glance-api.conf 2>/dev/null; then + pass "glance-api.conf 身份认证 [keystone_authtoken] 配置正确" +else + fail "glance-api.conf 身份认证 [keystone_authtoken] 配置有误" +fi - # 6. 检查系统服务进程 - info "6. 检查 Glance 服务启动状态" - if systemctl is-active openstack-glance-api &>/dev/null; then pass "openstack-glance-api 服务正在运行"; else fail "openstack-glance-api 服务未运行 (手动补全系统命令了吗?)"; fi - if systemctl is-enabled openstack-glance-api &>/dev/null; then pass "openstack-glance-api 服务已设为开机自启"; else fail "openstack-glance-api 服务未设置开启自启"; fi +if grep -q "^filesystem_store_datadir.*=.*?/var/lib/glance/images/" /etc/glance/glance-api.conf 2>/dev/null || grep -q "^stores.*=.*file" /etc/glance/glance-api.conf 2>/dev/null; then + pass "glance-api.conf 本地存储目录 [glance_store] 配置完成" +else + fail "glance-api.conf 存储方式 [glance_store] 配置有误" +fi - if systemctl is-active openstack-glance-registry &>/dev/null; then pass "openstack-glance-registry 服务正在运行"; else fail "openstack-glance-registry 服务未运行"; fi - if systemctl is-enabled openstack-glance-registry &>/dev/null; then pass "openstack-glance-registry 服务已设为开机自启"; else fail "openstack-glance-registry 未设置开机自启"; fi +# 6. 检查系统服务进程 +info "Glance 服务启动状态" +if systemctl is-active openstack-glance-api &>/dev/null; then pass "openstack-glance-api 服务正在运行"; else fail "openstack-glance-api 服务未运行"; fi +if systemctl is-enabled openstack-glance-api &>/dev/null; then pass "openstack-glance-api 服务已设为开机自启"; else fail "openstack-glance-api 服务未设置开启自启"; fi +if systemctl is-active openstack-glance-registry &>/dev/null; then pass "openstack-glance-registry 服务正在运行"; else fail "openstack-glance-registry 服务未运行"; fi +if systemctl is-enabled openstack-glance-registry &>/dev/null; then pass "openstack-glance-registry 服务已设为开机自启"; else fail "openstack-glance-registry 未设置开机自启"; fi + +if [ "$AUTH_OK" -eq 1 ]; then # 7. 检查镜像上传测试结果 - info "7. 检查 CirrOS 镜像上传测试结果" + info "CirrOS 镜像上传测试结果" img_status=$(openstack image show cirros -f value -c status 2>/dev/null) if [ "$img_status" == "active" ]; then pass "测试镜像 cirros 已成功上传,且目前处于 active 状态!" @@ -128,12 +168,4 @@ if [ -z "$goto_end" ]; then fi fi -echo "" -echo -e "${YELLOW}====================================================${NC}" -echo -e "检查完毕!通过项: ${GREEN}${PASS_CNT}${NC},失败项: ${RED}${FAIL_CNT}${NC}" -if [ ${FAIL_CNT} -eq 0 ]; then - echo -e "${GREEN}完美通过!Glance 镜像服务部署成功,已具备为虚拟机分发操作系统镜像的能力。${NC}" -else - echo -e "${RED}你有 ${FAIL_CNT} 处配置未达标,请对比报错提示与实验手册,仔细排查修改后再运行!${NC}" -fi -echo -e "${YELLOW}====================================================${NC}" +finish