Skip to content

Commit

Permalink
Refactor ack.sh script to improve error handling and command executio…
Browse files Browse the repository at this point in the history
…n logic; replaced direct exit code checks with variable assignments for better readability and maintainability. Enhanced lock file management in ack_auto_scale function to handle expired locks more effectively.
  • Loading branch information
xiagw committed Dec 12, 2024
1 parent b4a1375 commit 3c27377
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/aliyun/ack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ ack_create() {
# 获取VPC信息
local vpc_id
vpc_id=$(get_vpc_id)
if [ $? -ne 0 ]; then
return 1
ret=$?
if [ $ret -ne 0 ]; then
return $ret
fi

# 获取交换机信息
Expand Down Expand Up @@ -178,7 +179,8 @@ ack_create() {
--is-enterprise-security-group true \
--cloud-monitor-flags 1)

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "ACK 集群创建请求已提交:"
echo "$result" | jq '.'

Expand All @@ -187,7 +189,7 @@ ack_create() {
cluster_id=$(echo "$result" | jq -r '.ClusterId')

echo "等待集群创建完成..."
local max_wait_time=1800 # 30分钟
local max_wait_time=1800 # 30分钟
local start_time
start_time=$(date +%s)

Expand Down Expand Up @@ -283,7 +285,8 @@ ack_update() {
--ClusterId "$cluster_id" \
--name "$new_name")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "集群更新成功:"
echo "$result" | jq '.'
else
Expand All @@ -305,7 +308,8 @@ ack_detail() {
local result
result=$(aliyun --profile "${profile:-}" cs DescribeClusterDetail --ClusterId "$cluster_id")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "$result" | jq '.'
else
echo "错误:无法获取集群详情。"
Expand Down Expand Up @@ -368,7 +372,8 @@ ack_node_add() {
--ClusterId "$cluster_id" \
--count "$count")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "节点添加请求已提交:"
echo "$result" | jq '.'
else
Expand Down Expand Up @@ -402,7 +407,8 @@ ack_node_remove() {
--nodes "[$node_id]" \
--release-node true)

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "节点移除请求已提交:"
echo "$result" | jq '.'
else
Expand All @@ -427,7 +433,8 @@ ack_get_kubeconfig() {
--ClusterId "$cluster_id" \
--PrivateIpAddress "$private")

if [ $? -eq 0 ]; then
ret=$?
if [ $ret -eq 0 ]; then
echo "$result" | jq -r '.config'
else
echo "错误:无法获取 kubeconfig。"
Expand Down Expand Up @@ -465,8 +472,14 @@ ack_auto_scale() {

# 检查锁文件
if [[ -f $lock_file ]]; then
echo "另一个扩缩容进程正在运行..." >&2
return 1
# 检查锁文件是否过期(超过冷却时间)
if [[ $(stat -c %Y "$lock_file") -gt $(date -d "$COOLDOWN_MINUTES minutes ago" +%s) ]]; then
echo "另一个扩缩容进程正在运行..." >&2
return 1
else
# 删除过期的锁文件
rm -f "$lock_file"
fi
fi

# 计算阈值
Expand Down

0 comments on commit 3c27377

Please sign in to comment.