Skip to content

Commit

Permalink
fix aliyun
Browse files Browse the repository at this point in the history
  • Loading branch information
xiagw committed Nov 20, 2024
1 parent b98163d commit 9b63a16
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 74 deletions.
31 changes: 18 additions & 13 deletions lib/aliyun/ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ ecs_list() {

# 获取 EIP 列表
eip_result=$(aliyun --profile "${profile:-}" vpc DescribeEipAddresses --RegionId "${region:-}")

if [ $? -ne 0 ]; then
return_code=$?
if [ $return_code -ne 0 ]; then
echo "错误:无法获取 ECS 实例列表。请检查您的凭证和权限。" >&2
return 1
fi
Expand Down Expand Up @@ -166,7 +166,8 @@ ecs_create() {
local vswitch_id
local vswitch_list
vswitch_list=$(vpc_vswitch_list "$vpc_id" json)
if [ $? -ne 0 ] || [ -z "$vswitch_list" ]; then
return_code=$?
if [ $return_code -ne 0 ] || [ -z "$vswitch_list" ]; then
echo "错误:在选定的 VPC 中没有找到交换机,请先创建交换机。"
return 1
fi
Expand Down Expand Up @@ -272,7 +273,8 @@ ecs_create() {
echo "正在创建 ECS 实例..."
local result
result=$(eval "$create_command")
if [ $? -eq 0 ]; then
return_code=$?
if [ $return_code -eq 0 ]; then
echo "$result" | jq '.'
echo "ECS 实例创建并启动成功。"

Expand Down Expand Up @@ -379,8 +381,8 @@ ecs_key_create() {
echo "创建 SSH 密钥对:"
local result
result=$(aliyun --profile "${profile:-}" ecs CreateKeyPair --RegionId "$region" --KeyPairName "$key_name")

if [ $? -eq 0 ]; then
return_code=$?
if [ $return_code -eq 0 ]; then
echo "SSH 密钥对创建成功:"
echo "$result" | jq '.'
echo "请保存私钥内容,它只会显示一次!"
Expand Down Expand Up @@ -414,8 +416,8 @@ ecs_key_import() {

local result
result=$(aliyun --profile "${profile:-}" ecs ImportKeyPair --RegionId "$region" --KeyPairName "$key_name" --PublicKeyBody "$public_key")

if [ $? -eq 0 ]; then
return_code=$?
if [ $return_code -eq 0 ]; then
echo "SSH 密钥对导入成功:"
echo "$result" | jq '.'
else
Expand All @@ -439,9 +441,9 @@ ecs_key_delete() {
echo "删除 SSH 密钥对:"
local result
result=$(aliyun --profile "${profile:-}" ecs DeleteKeyPairs --RegionId "$region" --KeyPairNames "['$key_name']")
local status=$?
return_code=$?

if [ $status -eq 0 ]; then
if [ $return_code -eq 0 ]; then
echo "SSH 密钥对删除成功。"
log_delete_operation "$profile" "$region" "ecs" "$key_name" "SSH密钥对" "成功"
else
Expand All @@ -468,7 +470,8 @@ get_supported_disk_categories() {
--DestinationResource SystemDisk \
--InstanceType "${instance_type:-}")

if [ "$?" -ne 0 ]; then
return_code=$?
if [ $return_code -ne 0 ]; then
echo "错误:调用 DescribeAvailableResource API 失败。" >&2
echo "$result" >&2
return 1
Expand Down Expand Up @@ -508,7 +511,8 @@ ecs_start() {
--RegionId "$region" \
--InstanceId "$instance_id")

if [ $? -eq 0 ]; then
return_code=$?
if [ $return_code -eq 0 ]; then
echo "ECS 实例启动命令已发送。"
echo "$result" | jq '.'

Expand Down Expand Up @@ -550,7 +554,8 @@ ecs_stop() {
--StoppedMode StopCharging \
--ForceStop false)

if [ $? -eq 0 ]; then
return_code=$?
if [ $return_code -eq 0 ]; then
echo "ECS 实例停止命令已发送。"
echo "$result" | jq '.'

Expand Down
77 changes: 34 additions & 43 deletions lib/aliyun/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ show_help() {
echo "可用服务:"
echo " list-all - 列出所有服务的资源"
echo " ecs - 弹性计算服务"
echo " dns - 域名解析服务"
echo " oss - 对象存储服务"
echo " domain - 域名服务"
echo " dns - 域名解析服务"
echo " cdn - 内容分发网络"
echo " lbs - 负载均衡服务"
echo " rds - 关系型数据库服务"
echo " kvstore - 键值存储服务(Redis)"
echo " vpc - 专有网络"
echo " nat - NAT网关"
echo " eip - 弹性公网IP"
echo " config - 配置管理"
echo " cost - 费用查询"
echo " cas - 证书服务"
echo " ram - 访问控制"
echo " nas - 文件存储"
echo " ack - 容器服务 Kubernetes 版"
echo " config - 配置管理"
echo " balance - 账户余额查询"
echo " cost - 费用查询"
echo
echo "每个服务的具体操作和参数,请使用 '$0 <服务>' 查看"
echo
Expand Down Expand Up @@ -122,19 +122,19 @@ validate_params() {
ecs)
case "$operation" in
list) [[ ${#params[@]} -ge 0 ]] || {
echo "错误:参数错误。用法:$0 ecs list [region]" >&2
echo "错误:参数错误。用法:$0 ecs list" >&2
return 1
} ;;
create) [[ ${#params[@]} -ge 3 ]] || {
echo "错误:缺少参数。用法:$0 ecs create <名称> <类型> <镜像ID> [region]" >&2
echo "错误:缺少参数。用法:$0 ecs create <名称> <类型> <镜像ID>" >&2
return 1
} ;;
update) [[ ${#params[@]} -ge 2 ]] || {
echo "错误:缺少参数。用法:$0 ecs update <实例ID> <新名称> [region]" >&2
echo "错误:缺少参数。用法:$0 ecs update <实例ID> <新名称>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 ecs delete <实例ID> [region]" >&2
echo "错误:缺少参数。用法:$0 ecs delete <实例ID>" >&2
return 1
} ;;
*)
Expand Down Expand Up @@ -182,15 +182,15 @@ validate_params() {
oss)
case "$operation" in
list) [[ ${#params[@]} -ge 0 ]] || {
echo "错误:参数错误。用法:$0 oss list [region]" >&2
echo "错误:参数错误。用法:$0 oss list" >&2
return 1
} ;;
create) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 oss create <存储桶名称> [region]" >&2
echo "错误:缺少参数。用法:$0 oss create <存储桶名称>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 oss delete <存储桶名称> [region]" >&2
echo "错误:缺少参数。用法:$0 oss delete <存储桶名称>" >&2
return 1
} ;;
*)
Expand Down Expand Up @@ -237,11 +237,11 @@ validate_params() {
local lb_type=${params[0]}
case "$lb_type" in
slb) [[ ${#params[@]} -ge 4 ]] || {
echo "错误:缺少参数。用法:$0 lbs create slb <名称> <规格> <付费类型> [地域]" >&2
echo "错误:缺少参数。用法:$0 lbs create slb <名称> <规格> <付费类型>" >&2
return 1
} ;;
nlb | alb) [[ ${#params[@]} -ge 4 ]] || {
echo "错误:缺少参数。用法:$0 lbs create $lb_type <名称> <VPC-ID> <交换机ID> [地域]" >&2
echo "错误:缺少参数。用法:$0 lbs create $lb_type <名称> <VPC-ID> <交换机ID>" >&2
return 1
} ;;
*)
Expand All @@ -251,11 +251,11 @@ validate_params() {
esac
;;
update) [[ ${#params[@]} -ge 3 ]] || {
echo "错误:缺少参数。用法:$0 lbs update <type> <实例ID> <新名称> [地域]" >&2
echo "错误:缺少参数。用法:$0 lbs update <type> <实例ID> <新名称>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 2 ]] || {
echo "错误:缺少参数。用法:$0 lbs delete <type> <实例ID> [地域]" >&2
echo "错误:缺少参数。用法:$0 lbs delete <type> <实例ID>" >&2
return 1
} ;;
*)
Expand All @@ -267,19 +267,19 @@ validate_params() {
rds)
case "$operation" in
list) [[ ${#params[@]} -ge 0 ]] || {
echo "错误:参数错误。用法:$0 rds list [region]" >&2
echo "错误:参数错误。用法:$0 rds list" >&2
return 1
} ;;
create) [[ ${#params[@]} -ge 4 ]] || {
echo "错误:缺少参数。用法:$0 rds create <名称> <引擎> <版本> <规格> [地域]" >&2
echo "错误:缺少参数。用法:$0 rds create <名称> <引擎> <版本> <规格>" >&2
return 1
} ;;
update) [[ ${#params[@]} -ge 2 ]] || {
echo "错误:缺少参数。用法:$0 rds update <实例ID> <新名称> [地域]" >&2
echo "错误:缺少参数。用法:$0 rds update <实例ID> <新名称>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 rds delete <实例ID> [地域]" >&2
echo "错误:缺少参数。用法:$0 rds delete <实例ID>" >&2
return 1
} ;;
*)
Expand All @@ -291,19 +291,19 @@ validate_params() {
nlb)
case "$operation" in
list) [[ ${#params[@]} -ge 0 ]] || {
echo "错误:参数错误。用法:$0 nlb list [region]" >&2
echo "错误:参数错误。用法:$0 nlb list" >&2
return 1
} ;;
create) [[ ${#params[@]} -ge 4 ]] || {
echo "错误:缺少参数。用法:$0 nlb create <名称> <VPC-ID> <交换机ID> [地域]" >&2
echo "错误:缺少参数。用法:$0 nlb create <名称> <VPC-ID> <交换机ID>" >&2
return 1
} ;;
update) [[ ${#params[@]} -ge 2 ]] || {
echo "错误:缺少参数。用法:$0 nlb update <实例ID> <新名称> [地域]" >&2
echo "错误:缺少参数。用法:$0 nlb update <实例ID> <新名称>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 nlb delete <实例ID> [地域]" >&2
echo "错误:缺少参数。用法:$0 nlb delete <实例ID>" >&2
return 1
} ;;
*)
Expand All @@ -315,19 +315,19 @@ validate_params() {
alb)
case "$operation" in
list) [[ ${#params[@]} -ge 0 ]] || {
echo "错误:参数错误。用法:$0 alb list [region]" >&2
echo "错误:参数错误。用法:$0 alb list" >&2
return 1
} ;;
create) [[ ${#params[@]} -ge 4 ]] || {
echo "错误:缺少参数。用法:$0 alb create <名称> <VPC-ID> <交换机ID> [地域]" >&2
echo "错误:缺少参数。用法:$0 alb create <名称> <VPC-ID> <交换机ID>" >&2
return 1
} ;;
update) [[ ${#params[@]} -ge 2 ]] || {
echo "错误:缺少参数。用法:$0 alb update <实例ID> <新名称> [地域]" >&2
echo "错误:缺少参数。用法:$0 alb update <实例ID> <新名称>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 alb delete <实例ID> [地域]" >&2
echo "错误:缺少参数。用法:$0 alb delete <实例ID>" >&2
return 1
} ;;
*)
Expand All @@ -339,19 +339,19 @@ validate_params() {
eip)
case "$operation" in
list) [[ ${#params[@]} -ge 0 ]] || {
echo "错误:参数错误。用法:$0 eip list [region]" >&2
echo "错误:参数错误。用法:$0 eip list" >&2
return 1
} ;;
create) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 eip create <带宽> [region]" >&2
echo "错误:缺少参数。用法:$0 eip create <带宽>" >&2
return 1
} ;;
update) [[ ${#params[@]} -ge 2 ]] || {
echo "错误:缺少参数。用法:$0 eip update <EIP-ID> <新带宽> [region]" >&2
echo "错误:缺少参数。用法:$0 eip update <EIP-ID> <新带宽>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 eip delete <EIP-ID> [region]" >&2
echo "错误:缺少参数。用法:$0 eip delete <EIP-ID>" >&2
return 1
} ;;
*)
Expand All @@ -363,19 +363,19 @@ validate_params() {
kvstore)
case "$operation" in
list) [[ ${#params[@]} -ge 0 ]] || {
echo "错误:参数错误。用法:$0 kvstore list [region]" >&2
echo "错误:参数错误。用法:$0 kvstore list" >&2
return 1
} ;;
create) [[ ${#params[@]} -ge 3 ]] || {
echo "错误:缺少参数。用法:$0 kvstore create <名称> <实例类型> <容量> [region]" >&2
echo "错误:缺少参数。用法:$0 kvstore create <名称> <实例类型> <容量>" >&2
return 1
} ;;
update) [[ ${#params[@]} -ge 2 ]] || {
echo "错误:缺少参数。用法:$0 kvstore update <实例ID> <新名称> [region]" >&2
echo "错误:缺少参数。用法:$0 kvstore update <实例ID> <新名称>" >&2
return 1
} ;;
delete) [[ ${#params[@]} -ge 1 ]] || {
echo "错误:缺少参数。用法:$0 kvstore delete <实例ID> [region]" >&2
echo "错误:缺少参数。用法:$0 kvstore delete <实例ID>" >&2
return 1
} ;;
*)
Expand Down Expand Up @@ -477,15 +477,8 @@ get_credentials() {
]
}' >"$config_file"
fi

# 输出凭证信息
# echo "$access_key_id"
# echo "$access_key_secret"
# echo "$region"
}

# 在文件末尾添加以下函数

create_profile() {
local name=$1
local access_key_id=$2
Expand Down Expand Up @@ -584,8 +577,6 @@ query_account_balance() {
log_result "${profile:-}" "${region:-}" "account" "balance" "$result" "$format"
}

# 在文件末尾添加以下函数

show_balance_help() {
echo "账户余额操作:"
echo " list [format] - 查询账户余额,format 可选 human/json/tsv"
Expand Down
36 changes: 18 additions & 18 deletions lib/aliyun/vpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

show_vpc_help() {
echo "VPC 操作:"
echo " all [region] - 列出所有 VPC 相关资源"
echo " list [region] - 列出 VPC"
echo " all - 列出所有 VPC 相关资源"
echo " list - 列出 VPC"
echo " create [名称] [网段] [disable_ipv6] - 创建 VPC (自动生成名称,默认网段: 192.168.0.0/16)"
echo " update <VPC-ID> <新名称> [region] - 更新 VPC"
echo " delete <VPC-ID> [region] - 删除 VPC"
echo " vswitch-list <VPC-ID> [region] - 列出交换机"
echo " update <VPC-ID> <新名称> - 更新 VPC"
echo " delete <VPC-ID> - 删除 VPC"
echo " vswitch-list <VPC-ID> - 列出交换机"
echo " vswitch-create [VPC-ID] [名称] [网段] [可用区] - 创建交换机"
echo " vswitch-update <交换机ID> <新名称> [region] - 更新交换机"
echo " vswitch-delete <交换机ID> [region] - 删除交换机"
echo " sg-list <VPC-ID> [region] - 列出安全组"
echo " sg-create <VPC-ID> <名称> <描述> [region] - 创建安全组"
echo " sg-update <安全组ID> <新名称> <新描述> [region] - 更新安全组"
echo " sg-delete <安全组ID> [region] - 删除安全组"
echo " sg-rule-list <安全组ID> [region] - 列出安全组规则"
echo " sg-rule-add <安全组ID> <协议> <端口范围> <源IP> <描述> [region] - 添加安全组规则"
echo " sg-rule-update <安全组规则ID> <协议> <端口范围> <源IP> [region] - 更新安全组规则"
echo " vswitch-update <交换机ID> <新名称> - 更新交换机"
echo " vswitch-delete <交换机ID> - 删除交换机"
echo " sg-list <VPC-ID> - 列出安全组"
echo " sg-create <VPC-ID> <名称> <描述> - 创建安全组"
echo " sg-update <安全组ID> <新名称> <新描述> - 更新安全组"
echo " sg-delete <安全组ID> - 删除安全组"
echo " sg-rule-list <安全组ID> - 列出安全组规则"
echo " sg-rule-add <安全组ID> <协议> <端口范围> <源IP> <描述> - 添加安全组规则"
echo " sg-rule-update <安全组规则ID> <协议> <端口范围> <源IP> - 更新安全组规则"
echo " sg-rule-delete <规则ID> <安全组ID> [方向] - 删除安全组规则"
echo " ipv6gw-list <VPC-ID> [region] - 列出 IPv6 网关"
echo " ipv6gw-create <VPC-ID> <名称> [规格] [region] - 创建 IPv6 网关"
echo " ipv6gw-update <IPv6网关ID> <新名称> [新规格] [region] - 更新 IPv6 网关"
echo " ipv6gw-delete <IPv6网关ID> [region] - 删除 IPv6 网关"
echo " ipv6gw-list <VPC-ID> - 列出 IPv6 网关"
echo " ipv6gw-create <VPC-ID> <名称> [规格] - 创建 IPv6 网关"
echo " ipv6gw-update <IPv6网关ID> <新名称> [新规格] - 更新 IPv6 网关"
echo " ipv6gw-delete <IPv6网关ID> - 删除 IPv6 网关"
echo
echo "示例:"
echo " $0 vpc list"
Expand Down

0 comments on commit 9b63a16

Please sign in to comment.