Skip to content

Commit

Permalink
Refactor aliyun scripts to improve error handling and logging. Update…
Browse files Browse the repository at this point in the history
…d command execution logic to use variable assignments for exit codes, enhancing readability and maintainability. Modified README to reflect changes in script usage from 'aliyun2.sh' to 'main.sh' for consistency across commands. Removed unnecessary debug output in oss.sh. These changes improve the overall clarity and reliability of the scripts.
  • Loading branch information
xiagw committed Dec 28, 2024
1 parent 40e6e30 commit 7716673
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 45 deletions.
46 changes: 23 additions & 23 deletions lib/aliyun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,63 +50,63 @@

1. 支持多配置文件管理:
```bash
./aliyun2.sh config create <配置名> <AccessKey> <SecretKey> [RegionId]
./aliyun2.sh config list
./aliyun2.sh config update <配置名> <AccessKey> <SecretKey> [RegionId]
./aliyun2.sh config delete <配置名>
./main.sh config create <配置名> <AccessKey> <SecretKey> [RegionId]
./main.sh config list
./main.sh config update <配置名> <AccessKey> <SecretKey> [RegionId]
./main.sh config delete <配置名>
```

## 使用方法

基本用法:
```bash
./aliyun2.sh [--profile <配置名>] [--region <地域>] <服务> <操作> [参数...]
./main.sh [--profile <配置名>] [--region <地域>] <服务> <操作> [参数...]
```

常用示例:

1. ECS实例管理:
```bash
# 列出所有ECS实例
./aliyun2.sh ecs list
./main.sh ecs list

# 创建ECS实例(交互式)
./aliyun2.sh ecs create [实例名称]
./main.sh ecs create [实例名称]

# 启动/停止实例
./aliyun2.sh ecs start <实例ID>
./aliyun2.sh ecs stop <实例ID>
./main.sh ecs start <实例ID>
./main.sh ecs stop <实例ID>

# SSH密钥对管理
./aliyun2.sh ecs key-list
./aliyun2.sh ecs key-create <密钥名称>
./aliyun2.sh ecs key-import <密钥名称> github:<用户名>
./main.sh ecs key-list
./main.sh ecs key-create <密钥名称>
./main.sh ecs key-import <密钥名称> github:<用户名>
```

2. 网络资源管理:
```bash
# VPC操作
./aliyun2.sh vpc list
./aliyun2.sh vpc create <名称>
./main.sh vpc list
./main.sh vpc create <名称>

# EIP操作
./aliyun2.sh eip list
./aliyun2.sh eip create <带宽>
./main.sh eip list
./main.sh eip create <带宽>
```

3. 费用查询:
```bash
# 查询账户余额
./aliyun2.sh balance list
./main.sh balance list

# 查询每日费用
./aliyun2.sh cost daily [YYYY-MM-DD]
./main.sh cost daily [YYYY-MM-DD]
```

4. 查看所有资源:
```bash
# 列出所有服务的资源
./aliyun2.sh list-all
./main.sh list-all
```

## 输出格式
Expand All @@ -118,8 +118,8 @@

示例:
```bash
./aliyun2.sh ecs list json
./aliyun2.sh ecs list tsv
./main.sh ecs list json
./main.sh ecs list tsv
```

## 日志记录
Expand All @@ -137,5 +137,5 @@

每个服务都有详细的帮助信息,可通过以下方式查看:
```bash
./aliyun2.sh <服务> help
```
./main.sh <服务> help
```
9 changes: 6 additions & 3 deletions lib/aliyun/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ create_profile() {
--access-key-id "$access_key_id" \
--access-key-secret "$access_key_secret"

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "配置文件已创建。"
else
echo "错误:创建配置文件失败。" >&2
Expand All @@ -58,7 +59,8 @@ update_profile() {
--access-key-id "$access_key_id" \
--access-key-secret "$access_key_secret"

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "配置文件已更新。"
else
echo "错误:更新配置文件失败。" >&2
Expand All @@ -71,7 +73,8 @@ delete_profile() {

aliyun configure delete --profile "$name"

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "配置文件已删除。"
else
echo "错误:删除配置文件失败。" >&2
Expand Down
1 change: 0 additions & 1 deletion lib/aliyun/oss.sh
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ oss_bind_domain() {
echo "成功获取 CNAME 令牌:$token"

echo "正在自动添加 TXT 记录..."
set -x
dns_create "${domain#*.}" "${domain%%.*}" "TXT" "$token"

echo "请等待 DNS 记录生效,这可能需要几分钟时间..."
Expand Down
48 changes: 30 additions & 18 deletions lib/aliyun/vpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ vpc_delete() {
# 首先检查 VPC 是否存在
local vpc_info
vpc_info=$(aliyun --profile "${profile:-}" vpc DescribeVpcs --VpcId "$vpc_id" --RegionId "$region")
if [ $? -ne 0 ] || [ "$(echo "$vpc_info" | jq '.Vpcs.Vpc | length')" -eq 0 ]; then
ret=$?
if [ $ret -ne 0 ] || [ "$(echo "$vpc_info" | jq '.Vpcs.Vpc | length')" -eq 0 ]; then
echo "错误:VPC $vpc_id 不存在或无法访问。" >&2
return 1
fi
Expand Down Expand Up @@ -302,7 +303,8 @@ get_vpc_id() {
vpc_vswitch_list() {
local vpc_id
vpc_id=$(get_vpc_id "$1")
if [ $? -ne 0 ]; then
ret=$?
if [ $ret -ne 0 ]; then
return 1
fi

Expand Down Expand Up @@ -403,7 +405,8 @@ vpc_vswitch_create() {
local vpc_id
if [ -z "$1" ] || [ "$1" = "--auto" ]; then
vpc_id=$(get_vpc_id)
if [ $? -ne 0 ]; then
ret=$?
if [ $ret -ne 0 ]; then
return 1
fi
shift
Expand Down Expand Up @@ -435,7 +438,8 @@ vpc_vswitch_create() {
if [ -z "$zone" ]; then
echo "未指定可用区,正在获取可用区列表..."
zone=$(select_zone)
if [ $? -ne 0 ]; then
ret=$?
if [ $ret -ne 0 ]; then
return 1
fi
fi
Expand All @@ -454,7 +458,8 @@ vpc_vswitch_create() {
--VSwitchName "$name" \
--CidrBlock "$cidr")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "交换机创建成功:"
echo "$result" | jq '.'
else
Expand All @@ -479,7 +484,8 @@ vpc_vswitch_update() {
--VSwitchId "$vswitch_id" \
--VSwitchName "$new_name")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "交换机更新成功:"
echo "$result" | jq '.'
else
Expand Down Expand Up @@ -508,9 +514,9 @@ vpc_vswitch_delete() {
echo "删除交换机:"
local result
result=$(aliyun --profile "${profile:-}" vpc DeleteVSwitch --VSwitchId "$vswitch_id" --RegionId "$region")
local status=$?
ret=$?

if [ $status -eq 0 ]; then
if [ $ret -eq 0 ]; then
echo "交换机删除成功。"
log_delete_operation "${profile:-}" "$region" "vpc" "$vswitch_id" "交换机" "成功"
else
Expand All @@ -526,7 +532,8 @@ vpc_vswitch_delete() {
vpc_sg_list() {
local vpc_id
vpc_id=$(get_vpc_id "$1")
if [ $? -ne 0 ]; then
ret=$?
if [ $ret -ne 0 ]; then
return 1
fi

Expand Down Expand Up @@ -568,7 +575,8 @@ vpc_sg_list() {
vpc_sg_create() {
local vpc_id
vpc_id=$(get_vpc_id "$1")
if [ $? -ne 0 ]; then
ret=$?
if [ $ret -ne 0 ]; then
return 1
fi

Expand All @@ -581,7 +589,8 @@ vpc_sg_create() {
--SecurityGroupName "$name" \
--Description "$description")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "安全组创建成功:"
echo "$result" | jq '.'
else
Expand All @@ -601,7 +610,8 @@ vpc_sg_update() {
--SecurityGroupName "$new_name" \
--Description "$new_description")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "安全组更新成功:"
echo "$result" | jq '.'
else
Expand All @@ -624,9 +634,9 @@ vpc_sg_delete() {
echo "删除安全组:"
local result
result=$(aliyun --profile "${profile:-}" ecs DeleteSecurityGroup --SecurityGroupId "$sg_id" --RegionId "$region")
local status=$?
ret=$?

if [ $status -eq 0 ]; then
if [ $ret -eq 0 ]; then
echo "安全组删除成功。"
log_delete_operation "${profile:-}" "$region" "vpc" "$sg_id" "安全组" "成功"
else
Expand Down Expand Up @@ -677,7 +687,8 @@ vpc_sg_rule_add() {
--Priority 1 \
--Description "$description")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "安全组规则添加成功:"
echo "$result" | jq '.'
else
Expand All @@ -701,7 +712,8 @@ vpc_sg_rule_update() {
--Policy accept \
--Priority 1)

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "安全组规则更新成功:"
echo "$result" | jq '.'
else
Expand Down Expand Up @@ -748,9 +760,9 @@ vpc_sg_rule_delete() {
--SecurityGroupId "$sg_id" \
--SecurityGroupRuleId.1 "$rule_id")
fi
local status=$?
ret=$?

if [ $status -eq 0 ]; then
if [ $ret -eq 0 ]; then
echo "安全组规则删除成功。"
log_delete_operation "${profile:-}" "$region" "vpc" "$rule_id" "安全组规则" "成功"
else
Expand Down

0 comments on commit 7716673

Please sign in to comment.