feat: 在 create_instance.sh 中添加获取租户名称的功能

This commit is contained in:
2026-08-07 10:38:02 +08:00
parent 7c583da851
commit 7eba1eea28
+32 -4
View File
@@ -182,6 +182,34 @@ generate_shape_config() {
esac esac
} }
#
# 获取当前 OCI 配置对应的租户名称
#
get_tenant_name() {
local tenancy_id
local tenant_name
if ! tenancy_id=$(oci iam availability-domain list \
--query 'data[0]."compartment-id"' --raw-output); then
log_error "获取租户 OCID 失败"
return 1
fi
if [[ -z "$tenancy_id" || "$tenancy_id" == "null" ]]; then
log_error "租户 OCID 为空"
return 1
fi
if ! tenant_name=$(oci iam tenancy get --tenancy-id "$tenancy_id" \
--query 'data.name' --raw-output); then
log_error "获取租户名称失败"
return 1
fi
[[ -n "$tenant_name" && "$tenant_name" != "null" ]] || return 1
printf '%s\n' "$tenant_name"
}
# #
# 配置 OCI 网络资源 # 配置 OCI 网络资源
# #
@@ -299,7 +327,7 @@ show_help() {
选项: 选项:
-n, --number <数量> 创建实例数量 (默认: $DEFAULT_NUMBER) -n, --number <数量> 创建实例数量 (默认: $DEFAULT_NUMBER)
--ocid <OCID> 区域ID (自动获取) --ocid <OCID> Compartment OCID (自动获取租户根 OCID)
--shape <形状> 实例规格 (默认: $DEFAULT_SHAPE) --shape <形状> 实例规格 (默认: $DEFAULT_SHAPE)
--shape_config <配置> 资源配置格式: CPU+内存[+基线] (默认: $DEFAULT_SHAPE_CONFIG) --shape_config <配置> 资源配置格式: CPU+内存[+基线] (默认: $DEFAULT_SHAPE_CONFIG)
--subnet_id <子网ID> 指定现有子网ID (自动创建) --subnet_id <子网ID> 指定现有子网ID (自动创建)
@@ -412,9 +440,9 @@ main() {
log_info "正在获取租户名称..." log_info "正在获取租户名称..."
local tenant_name local tenant_name
tenant_name=$(oci iam tenancy get --tenancy-id "$ocid" \ if ! tenant_name=$(get_tenant_name); then
--query 'data.name' --raw-output 2>/dev/null) error_exit "无法获取租户名称"
check_return $? "获取租户名称失败" fi
log_info "租户名称: $tenant_name" log_info "租户名称: $tenant_name"
local tags="{\"RootPassword\":\"$password\",\"TenantName\":\"$tenant_name\"}" local tags="{\"RootPassword\":\"$password\",\"TenantName\":\"$tenant_name\"}"