Compare commits

...

6 Commits

Author SHA1 Message Date
fbc5e6edff fix: 更新 create_ai_projects.sh 脚本以改进服务账号和 API 密钥创建逻辑 2026-01-29 22:33:40 +08:00
4b7e604313 fix: 更新 delete_all_projects.sh 脚本以优化项目获取和展示逻辑 2026-01-29 21:39:08 +08:00
1be07db276 fix: 避免 remote_loader.sh 中重复定义 readonly 变量
## 问题描述

当脚本使用远程加载时,出现警告:
```
/tmp/tmp.xxx: line 10: REMOTE_BASE_URL: readonly variable
```

## 根本原因

`REMOTE_BASE_URL` 被重复定义为 readonly 变量:

1. 调用脚本中定义(如 oci/create_instance.sh:16):
   ```bash
   readonly REMOTE_BASE_URL="..."
   ```

2. remote_loader.sh:10 中再次定义:
   ```bash
   readonly REMOTE_BASE_URL="..."
   ```

Bash 不允许重新赋值 readonly 变量,即使值相同也会产生警告。

## 修复方案

在 remote_loader.sh 中添加检查,只有在变量未定义时才设置:

```bash
# 旧代码
readonly REMOTE_BASE_URL="..."

# 新代码
if [[ -z "${REMOTE_BASE_URL:-}" ]]; then
    readonly REMOTE_BASE_URL="..."
fi
```

## 影响

-  消除警告信息
-  保持向后兼容
-  允许调用者自定义 REMOTE_BASE_URL
-  如果未定义则使用默认值
2025-12-26 15:23:48 +08:00
89f24a7fef fix: 修复所有脚本的 process substitution 兼容性问题
## 问题描述

在使用 `set -u` 严格模式时,`source <(curl ...)` 或 `source <(wget ...)`
的 process substitution 方式会在脚本退出时产生错误:

```
/dev/fd/63: line 1: fifo: unbound variable
```

## 根本原因

Process substitution 创建的临时文件描述符(如 /dev/fd/63)在退出时
与 Bash 的 `set -u` 严格模式存在兼容性问题,导致错误消息。

## 修复方案

将 process substitution 替换为临时文件方案:

**旧方案(有问题):**
```bash
source <(curl -fsSL "$url")
```

**新方案(兼容性好):**
```bash
temp_loader=$(mktemp)
curl -fsSL "$url" -o "$temp_loader"
source "$temp_loader"
rm -f "$temp_loader"
```

## 修改的文件

批量修复了所有 7 个脚本的远程加载逻辑:

- oci/create_instance.sh
- linux/create_raid0_array.sh
- linux/install_oh_my_zsh.sh
- linux/repartition_disks.sh
- gcp/create_ai_projects.sh
- gcp/delete_all_projects.sh
- common/demo_usage.sh

## 优势

-  避免 process substitution 的兼容性问题
-  与 `set -u` 严格模式完全兼容
-  显式的临时文件管理,更易理解
-  确保所有分支都正确清理临时文件
-  保持 curl/wget 双重支持不变
2025-12-26 15:20:51 +08:00
7d04ad7532 fix: 彻底修复 FIFO 清理问题(避免 trap 冲突)
## 问题根源

存在**两个 EXIT trap 冲突**:
1. `oci/create_instance.sh:364` - 清理 FIFO 的 trap
2. `common/remote_loader.sh:23` - 清理临时目录的 trap

在 Bash 中,每次设置新的 trap 会**覆盖**之前的 trap。
当 remote_loader.sh 加载时,它的 `trap cleanup_remote_libs EXIT`
会覆盖 main 函数中设置的 FIFO 清理 trap。

## 错误表现

```
/dev/fd/63: line 1: fifo: unbound variable
```

- FIFO 文件不会被清理(因为 trap 被覆盖)
- Process substitution 退出时产生 trap 交互错误

## 修复方案

**移除 trap,改用显式清理:**

```bash
# 旧方案(有问题)
trap "rm -f \"$fifo\"" EXIT
configure_network "$ocid" "$fifo" &
read subnet_id < "$fifo"

# 新方案(可靠)
configure_network "$ocid" "$fifo" &
local bg_pid=$!           # 保存后台进程 PID
read subnet_id < "$fifo"  # 读取结果
wait "$bg_pid"            # 等待后台进程完成
rm -f "$fifo"             # 手动清理 FIFO
```

## 优势

-  避免 trap 冲突
-  显式的清理流程,更易理解
-  确保后台任务完成后再清理
-  与 remote_loader.sh 的 trap 兼容
2025-12-26 15:14:07 +08:00
b0f1d5d600 fix: 修复 OCI 脚本中的 trap 变量作用域问题
## 问题描述

脚本执行完成后出现错误:
```
/dev/fd/63: line 1: fifo: unbound variable
```

## 根本原因

在 oci/create_instance.sh:364 行使用了:
```bash
trap 'rm -f "$fifo"' EXIT
```

单引号导致 `$fifo` 在 EXIT 时才展开,但此时 `fifo` 是 local 变量,
已超出作用域。由于 `set -u` 严格模式,访问未定义变量会报错。

## 修复方案

将单引号改为双引号:
```bash
trap "rm -f \"$fifo\"" EXIT
```

这样 `$fifo` 在 trap 设置时就会展开为实际的文件路径(如 /tmp/tmp.xxx),
trap 命令会记录具体路径而非变量名,避免作用域问题。

## 影响

-  修复 EXIT trap 的 unbound variable 错误
-  保持功能不变(临时 FIFO 文件仍会正确清理)
-  与 set -euo pipefail 严格模式兼容
2025-12-26 15:09:49 +08:00
9 changed files with 300 additions and 106 deletions

8
.gitignore vendored
View File

@@ -1,4 +1,8 @@
# Claude Code
.mcp.json
# Vibe Coding
.claude/
.oprncode/
llmdoc/
.sisyphus/
.mcp.json

View File

@@ -48,20 +48,31 @@ load_common_libs() {
# 使用远程库
if [[ "$use_remote" == "true" ]]; then
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
local temp_loader
temp_loader=$(mktemp)
if command -v curl &>/dev/null; then
echo "[INFO] 使用 curl 下载远程库..." >&2
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
elif command -v wget &>/dev/null; then
echo "[INFO] 使用 wget 下载远程库..." >&2
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
fi
rm -f "$temp_loader"
echo "[ERROR] 无法加载公共库" >&2
echo "[ERROR] - 本地库不存在" >&2
echo "[ERROR] - 远程下载失败(需要 curl 或 wget" >&2

View File

@@ -6,8 +6,10 @@
# 版本: 1.0.0
# ============================================================================
# 远程仓库 URL 配置
# 远程仓库 URL 配置(如果调用者未定义,则使用默认值)
if [[ -z "${REMOTE_BASE_URL:-}" ]]; then
readonly REMOTE_BASE_URL="${REMOTE_LIB_URL:-https://gitea.bcde.io/wangdefa/tools/raw/branch/main}"
fi
# 临时目录用于存储下载的文件
readonly REMOTE_TMP_DIR="$(mktemp -d)"

View File

@@ -48,20 +48,31 @@ load_common_libs() {
# 使用远程库
if [[ "$use_remote" == "true" ]]; then
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
local temp_loader
temp_loader=$(mktemp)
if command -v curl &>/dev/null; then
echo "[INFO] 使用 curl 下载远程库..." >&2
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
elif command -v wget &>/dev/null; then
echo "[INFO] 使用 wget 下载远程库..." >&2
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
fi
rm -f "$temp_loader"
echo "[ERROR] 无法加载公共库" >&2
echo "[ERROR] - 本地库不存在" >&2
echo "[ERROR] - 远程下载失败(需要 curl 或 wget" >&2
@@ -269,23 +280,24 @@ enable_services() {
}
#
# 创建服务账号
# 创建服务账号(用于 API 密钥身份验证)
#
create_service_account() {
local project_id=$1
local max_retries=$2
local retry_delay=$3
local sa_name="vertex-ai-runner"
local sa_email="${sa_name}@${project_id}.iam.gserviceaccount.com"
log_info "创建服务账号 service-account@$project_id.iam.gserviceaccount.com"
log_info "创建服务账号 $sa_email"
# 检查服务账号是否已存在
if gcloud iam service-accounts describe "service-account@$project_id.iam.gserviceaccount.com" --project="$project_id" &>/dev/null; then
if gcloud iam service-accounts describe "$sa_email" --project="$project_id" &>/dev/null; then
log_warning "服务账号已存在,跳过创建"
return 0
fi
if retry_command "$max_retries" "$retry_delay" gcloud iam service-accounts create service-account \
--display-name="AI Platform Service Account" \
if retry_command "$max_retries" "$retry_delay" gcloud iam service-accounts create "$sa_name" \
--display-name="Vertex AI Runner" \
--project="$project_id"; then
log_success "服务账号创建成功"
return 0
@@ -296,48 +308,124 @@ create_service_account() {
}
#
# 添加 IAM 策略
# 授予服务账号 Vertex AI 角色
#
add_iam_policy() {
grant_vertex_ai_role() {
local project_id=$1
local max_retries=$2
local retry_delay=$3
local sa_email="vertex-ai-runner@${project_id}.iam.gserviceaccount.com"
log_info "授予 aiplatform.serviceAgent 角色"
log_info "授予 Vertex AI Platform Express User 角色"
if retry_command "$max_retries" "$retry_delay" gcloud projects add-iam-policy-binding "$project_id" \
--member="serviceAccount:service-account@$project_id.iam.gserviceaccount.com" \
--role="roles/aiplatform.serviceAgent"; then
log_success "IAM 策略授予成功"
--member="serviceAccount:$sa_email" \
--role="roles/aiplatform.expressUser" \
--condition=None; then
log_success "角色授予成功"
return 0
else
log_error "授予 IAM 策略失败 (已重试)"
log_error "授予角色失败 (已重试)"
return 1
fi
}
#
# 创建服务账号密钥
# 启用 API Keys 服务
#
create_service_account_key() {
enable_api_keys_service() {
local project_id=$1
local max_retries=$2
local retry_delay=$3
log_info "创建服务账号密钥"
log_info "启用 apikeys.googleapis.com 服务"
if retry_command "$max_retries" "$retry_delay" gcloud services enable apikeys.googleapis.com --project="$project_id"; then
log_success "API Keys 服务启用成功"
return 0
else
log_error "启用 apikeys.googleapis.com 服务失败 (已重试)"
return 1
fi
}
# 检查密钥文件是否已存在
if [ -f "pass-$project_id.json" ]; then
log_warning "密钥文件 pass-$project_id.json 已存在,跳过创建"
#
# 创建 Google Cloud API 密钥(绑定服务账号)
#
create_api_key() {
local project_id=$1
local max_retries=$2
local retry_delay=$3
local key_file="apikey-$project_id.txt"
local sa_email="vertex-ai-runner@${project_id}.iam.gserviceaccount.com"
log_info "创建 Google Cloud API 密钥(绑定服务账号 $sa_email"
if [ -f "$key_file" ]; then
log_warning "API 密钥文件 $key_file 已存在,跳过创建"
return 0
fi
if retry_command "$max_retries" "$retry_delay" gcloud iam service-accounts keys create "pass-$project_id.json" \
--iam-account="service-account@$project_id.iam.gserviceaccount.com" \
--project="$project_id"; then
log_success "服务账号密钥创建成功: pass-$project_id.json"
local key_name="vertex-ai-key"
local attempt=1
local create_success=false
while [ $attempt -le "$max_retries" ]; do
log_info "尝试创建 API 密钥 (第 $attempt/$max_retries 次)"
local temp_error
temp_error=$(mktemp)
if gcloud services api-keys create \
--display-name="$key_name" \
--api-target=service=aiplatform.googleapis.com \
--service-account="$sa_email" \
--project="$project_id" 2>"$temp_error"; then
log_success "API 密钥创建命令执行成功"
rm -f "$temp_error"
create_success=true
break
else
log_warning "创建失败 (第 $attempt/$max_retries 次)"
if [ -s "$temp_error" ]; then
log_error "错误: $(cat "$temp_error")"
fi
rm -f "$temp_error"
if [ $attempt -lt "$max_retries" ]; then
log_info "将在 $retry_delay 秒后重试..."
sleep "$retry_delay"
fi
fi
((attempt++))
done
if [ "$create_success" != "true" ]; then
log_error "创建 API 密钥命令执行失败"
return 1
fi
log_info "等待 API 密钥创建完成..."
sleep 5
log_info "查找已创建的 API 密钥..."
local key_resource_name
key_resource_name=$(gcloud services api-keys list --project="$project_id" --filter="displayName=$key_name" --format="value(name)" 2>/dev/null | head -n 1)
if [ -z "$key_resource_name" ]; then
log_error "未找到已创建的 API 密钥"
return 1
fi
log_info "获取 API 密钥字符串: $key_resource_name"
local api_key_value
api_key_value=$(gcloud services api-keys get-key-string "$key_resource_name" --format="value(keyString)" 2>/dev/null)
if [ -n "$api_key_value" ]; then
echo "$api_key_value" > "$key_file"
log_success "API 密钥创建成功: $key_file"
return 0
else
log_error "创建服务账号密钥失败 (已重试)"
log_error "无法获取 API 密钥字符串"
return 1
fi
}
@@ -348,6 +436,7 @@ START_NUM="$DEFAULT_START_NUM"
REPEAT_NUM="$DEFAULT_REPEAT_NUM"
MAX_RETRIES="$DEFAULT_MAX_RETRIES"
RETRY_DELAY="$DEFAULT_RETRY_DELAY"
DEBUG_MODE=false
# 解析命令行参数
while [[ $# -gt 0 ]]; do
@@ -511,17 +600,25 @@ for i in $(seq "$START_NUM" "$END_NUM"); do
fi
if [ "$project_success" = true ]; then
log_debug "步骤5: 添加IAM策略"
if ! add_iam_policy "$PROJECT_ID" "$MAX_RETRIES" "$RETRY_DELAY"; then
log_debug "添加IAM策略失败"
log_debug "步骤5: 授予 Vertex AI 角色"
if ! grant_vertex_ai_role "$PROJECT_ID" "$MAX_RETRIES" "$RETRY_DELAY"; then
log_debug "授予角色失败"
project_success=false
fi
fi
if [ "$project_success" = true ]; then
log_debug "步骤6: 创建服务账号密钥"
if ! create_service_account_key "$PROJECT_ID" "$MAX_RETRIES" "$RETRY_DELAY"; then
log_debug "创建服务账号密钥失败"
log_debug "步骤6: 启用 API Keys 服务"
if ! enable_api_keys_service "$PROJECT_ID" "$MAX_RETRIES" "$RETRY_DELAY"; then
log_debug "启用 API Keys 服务失败"
project_success=false
fi
fi
if [ "$project_success" = true ]; then
log_debug "步骤7: 创建 API 密钥"
if ! create_api_key "$PROJECT_ID" "$MAX_RETRIES" "$RETRY_DELAY"; then
log_debug "创建 API 密钥失败"
project_success=false
fi
fi
@@ -529,10 +626,10 @@ for i in $(seq "$START_NUM" "$END_NUM"); do
# 统计结果
if [ "$project_success" = true ]; then
log_success "项目 $PROJECT_ID 所有操作处理完成"
((successful_projects++))
((successful_projects++)) || true
else
log_warning "项目 $PROJECT_ID 处理过程中出现错误,部分操作可能未完成。"
((failed_projects++))
((failed_projects++)) || true
fi
# 添加延迟,避免 API 限制
@@ -556,8 +653,8 @@ if [ "$successful_projects" -gt 0 ]; then
log_success "成功创建的项目:"
for i in $(seq "$START_NUM" "$END_NUM"); do
PROJECT_ID="${PROJECT_ID_PREFIX}$(printf "%02d" "$i")"
if [ -f "pass-$PROJECT_ID.json" ]; then
log_info "$PROJECT_ID (密钥文件: pass-$PROJECT_ID.json)"
if [ -f "apikey-$PROJECT_ID.txt" ]; then
log_info "$PROJECT_ID (API 密钥文件: apikey-$PROJECT_ID.txt)"
fi
done
fi

View File

@@ -3,7 +3,7 @@
# 文件名: delete_all_projects.sh
# 描述: 删除 GCP 账户下的所有项目(危险操作)
# 作者: Cloud Tools Project
# 版本: 2.1.0(支持远程库加载)
# 版本: 2.1.1(支持远程库加载)
# 警告: 此操作不可逆,所有项目数据将永久丢失
# ============================================================================
@@ -49,20 +49,31 @@ load_common_libs() {
# 使用远程库
if [[ "$use_remote" == "true" ]]; then
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
local temp_loader
temp_loader=$(mktemp)
if command -v curl &>/dev/null; then
echo "[INFO] 使用 curl 下载远程库..." >&2
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
elif command -v wget &>/dev/null; then
echo "[INFO] 使用 wget 下载远程库..." >&2
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
fi
rm -f "$temp_loader"
echo "[ERROR] 无法加载公共库" >&2
echo "[ERROR] - 本地库不存在" >&2
echo "[ERROR] - 远程下载失败(需要 curl 或 wget" >&2
@@ -79,32 +90,39 @@ readonly LOG_FILE="deleted_projects_$(date +%Y%m%d_%H%M%S).log"
log_set_file "$LOG_FILE"
#
# 获取并展示所有项目
# 获取所有项目 ID 列表
# 返回: 项目 ID 列表(每行一个),通过 stdout 返回
# 退出码: 0 成功1 失败
#
get_and_display_projects() {
# 获取所有项目
get_projects() {
log_warning "获取项目列表..."
local projects
projects=$(gcloud projects list --format="value(projectId)")
if [ -z "$projects" ]; then
log_warning "没有找到任何项目"
exit 0
return 0
fi
# 显示所有项目
log_warning "以下项目将被删除:"
echo "$projects" | nl
echo
# 统计项目数量
local project_count
project_count=$(echo "$projects" | wc -l)
log_warning "总计: $project_count 个项目"
echo "$projects"
}
#
# 显示项目列表(带编号)
# 参数: $1 - 项目列表(换行符分隔)
# 输出: 到 stderr
#
display_projects() {
local projects="$1"
local project_count
project_count=$(echo "$projects" | wc -l | tr -d ' ')
log_warning "以下项目将被删除:"
echo "$projects" | nl >&2
echo >&2
log_warning "总计: $project_count 个项目"
}
#
# 删除单个项目
#
@@ -143,13 +161,22 @@ main() {
exit 0
fi
# 获取并展示项目列表
# 获取项目列表
local projects
projects=$(get_and_display_projects)
projects=$(get_projects)
# 检查是否有项目
if [ -z "$projects" ]; then
log_warning "没有找到任何项目,退出"
exit 0
fi
# 显示项目列表
display_projects "$projects"
# 统计项目数量
local project_count
project_count=$(echo "$projects" | wc -l)
project_count=$(echo "$projects" | wc -l | tr -d ' ')
# 第二次确认
log_error "最后确认:您将要删除 $project_count 个项目!"
@@ -169,9 +196,9 @@ main() {
for project_id in $projects; do
if delete_project "$project_id" "$LOG_FILE"; then
((deleted_count++))
((deleted_count++)) || true
else
((failed_count++))
((failed_count++)) || true
fi
# 添加短暂延迟,避免 API 限制

View File

@@ -49,20 +49,31 @@ load_common_libs() {
# 使用远程库
if [[ "$use_remote" == "true" ]]; then
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
local temp_loader
temp_loader=$(mktemp)
if command -v curl &>/dev/null; then
echo "[INFO] 使用 curl 下载远程库..." >&2
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
elif command -v wget &>/dev/null; then
echo "[INFO] 使用 wget 下载远程库..." >&2
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
fi
rm -f "$temp_loader"
echo "[ERROR] 无法加载公共库" >&2
echo "[ERROR] - 本地库不存在" >&2
echo "[ERROR] - 远程下载失败(需要 curl 或 wget" >&2

View File

@@ -48,20 +48,31 @@ load_common_libs() {
# 使用远程库
if [[ "$use_remote" == "true" ]]; then
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
local temp_loader
temp_loader=$(mktemp)
if command -v curl &>/dev/null; then
echo "[INFO] 使用 curl 下载远程库..." >&2
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
elif command -v wget &>/dev/null; then
echo "[INFO] 使用 wget 下载远程库..." >&2
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
fi
rm -f "$temp_loader"
echo "[ERROR] 无法加载公共库" >&2
echo "[ERROR] - 本地库不存在" >&2
echo "[ERROR] - 远程下载失败(需要 curl 或 wget" >&2

View File

@@ -49,20 +49,31 @@ load_common_libs() {
# 使用远程库
if [[ "$use_remote" == "true" ]]; then
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
local temp_loader
temp_loader=$(mktemp)
if command -v curl &>/dev/null; then
echo "[INFO] 使用 curl 下载远程库..." >&2
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
elif command -v wget &>/dev/null; then
echo "[INFO] 使用 wget 下载远程库..." >&2
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
fi
rm -f "$temp_loader"
echo "[ERROR] 无法加载公共库" >&2
echo "[ERROR] - 本地库不存在" >&2
echo "[ERROR] - 远程下载失败(需要 curl 或 wget" >&2

View File

@@ -48,20 +48,31 @@ load_common_libs() {
# 使用远程库
if [[ "$use_remote" == "true" ]]; then
# 下载到临时文件(避免 process substitution 与 set -u 的交互问题)
local temp_loader
temp_loader=$(mktemp)
if command -v curl &>/dev/null; then
echo "[INFO] 使用 curl 下载远程库..." >&2
if curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" -o "$temp_loader" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(curl -fsSL "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
elif command -v wget &>/dev/null; then
echo "[INFO] 使用 wget 下载远程库..." >&2
if wget -qO "$temp_loader" "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null; then
# shellcheck disable=SC1090
if source <(wget -qO- "${REMOTE_BASE_URL}/common/remote_loader.sh" 2>/dev/null); then
if source "$temp_loader"; then
rm -f "$temp_loader"
return 0
fi
fi
fi
rm -f "$temp_loader"
echo "[ERROR] 无法加载公共库" >&2
echo "[ERROR] - 本地库不存在" >&2
echo "[ERROR] - 远程下载失败(需要 curl 或 wget" >&2
@@ -77,7 +88,7 @@ load_common_libs
readonly DEFAULT_NUMBER=1
readonly DEFAULT_SHAPE="VM.Standard.A1.Flex"
readonly DEFAULT_SHAPE_CONFIG="1+6"
readonly DEFAULT_IMAGE_NAME="Canonical-Ubuntu-20.04-aarch64-2025.05.20-0"
readonly DEFAULT_IMAGE_NAME="Canonical-Ubuntu-20.04-aarch64-2025.07.23-0"
readonly DEFAULT_BOOT_VOLUME_SIZE=0
readonly DEFAULT_VPU=120
readonly DEFAULT_DOMAIN=0
@@ -361,9 +372,18 @@ main() {
local fifo
fifo=$(mktemp -u)
mkfifo "$fifo"
trap 'rm -f "$fifo"' EXIT
# 启动后台任务
configure_network "$ocid" "$fifo" &
local bg_pid=$!
# 读取结果
read subnet_id < "$fifo"
# 等待后台任务完成并清理
wait "$bg_pid"
rm -f "$fifo"
log_success "网络配置完成 (子网ID: $subnet_id)"
else
log_info "使用指定的子网ID: $subnet_id"