Skip to content

Commit

Permalink
update common.sh and include
Browse files Browse the repository at this point in the history
  • Loading branch information
xiagw committed Oct 27, 2024
1 parent fb4eb29 commit 1b58ee9
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## vim swap
.swp
*.log
*.env

## gitlab runner exclude
builds/
Expand Down
2 changes: 1 addition & 1 deletion bin/aliyun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ _add_workorder() {
# python3 -m pip list | grep 'alibabacloud-workorder' || python3 -m pip install alibabacloud_workorder20210610==1.0.0
# python3 -m pip list | grep 'alibabacloud_tea_console' || python3 -m pip install alibabacloud_tea_console
## 列出产品列表 (没有 aliyun cli 可用,使用 python sdk)
call_python_file="$g_me_path/aliyun.workorder.py"
call_python_file="$g_me_path/../lib/aliyun/workorder.py"
saved_json="$g_me_data_path/aliyun.product.list.json"
command -v fzf >/dev/null 2>&1 || sudo apt install -y fzf

Expand Down
7 changes: 4 additions & 3 deletions bin/backup_encrypt_notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ main() {

# 导入通用函数
# shellcheck disable=SC1091
source "$(dirname "$0")/common_functions.sh"
source "$(dirname "$0")/../lib/common.sh"

local me_name
local me_path
Expand All @@ -413,8 +413,8 @@ main() {

me_name="$(basename "$0")"
me_path="$(dirname "$(readlink -f "$0")")"
me_env="${me_path}/${me_name}.env"
me_log="${me_path}/${me_name}.log"
me_env="${me_path}/../data/${me_name}.env"
me_log="${me_path}/../data/${me_name}.log"

# 初始化 CURRENT_LOG_LEVEL
export CURRENT_LOG_LEVEL=$LOG_LEVEL_INFO
Expand All @@ -427,6 +427,7 @@ main() {
_log $LOG_LEVEL_INFO "Backup start"

# Load configuration
[ -f "${me_env}" ] || cp "${me_path}/../conf/${me_name}.env" "${me_env}"
_load_config "$me_env" "$@"

## 检查必要的命令
Expand Down
84 changes: 0 additions & 84 deletions bin/common_functions.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LOG_LEVEL=INFO
# DEBUG=false

## 备份路径
PATHS=("/tmp/test")
PATHS=("/home/test")

# 备份主机
# HOSTS=("host1.example.com" "host2.example.com")
Expand Down
File renamed without changes.
81 changes: 81 additions & 0 deletions lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,87 @@
# Use gdate if available, otherwise fallback to date command
cmd_date="$(command -v gdate || command -v date)"

# 定义日志级别常量
readonly LOG_LEVEL_ERROR=0
readonly LOG_LEVEL_WARNING=1
readonly LOG_LEVEL_INFO=2
readonly LOG_LEVEL_SUCCESS=3
readonly LOG_LEVEL_FILE=4

# 定义颜色代码
readonly COLOR_RED='\033[0;31m'
readonly COLOR_YELLOW='\033[0;33m'
readonly COLOR_GREEN='\033[0;32m'
readonly COLOR_RESET='\033[0m'

_log() {
local level=$1
shift
local message="$*"
local level_name
local color

case $level in
"$LOG_LEVEL_ERROR")
level_name="ERROR"
color="$COLOR_RED"
;;
"$LOG_LEVEL_WARNING")
level_name="WARNING"
color="$COLOR_YELLOW"
;;
"$LOG_LEVEL_INFO")
level_name="INFO"
color="$COLOR_RESET"
;;
"$LOG_LEVEL_SUCCESS")
level_name="SUCCESS"
color="$COLOR_GREEN"
;;
"$LOG_LEVEL_FILE")
level_name="FILE"
color="$COLOR_RESET"
;;
*)
level_name="UNKNOWN"
color="$COLOR_RESET"
;;
esac

if [[ $CURRENT_LOG_LEVEL -ge $level ]]; then
if [[ $level -eq $LOG_LEVEL_FILE ]]; then
# 输出到日志文件,不使用颜色
echo "[${level_name}] $(date '+%Y-%m-%d %H:%M:%S') - $message" >>"$LOG_FILE"
else
# 输出到终端,使用颜色
echo -e "${color}[${level_name}] $(date '+%Y-%m-%d %H:%M:%S')${COLOR_RESET} - $message" >&2
fi
fi
}

_check_commands() {
# 检查必要的命令是否可用
for cmd in "$@"; do
if ! command -v "$cmd" &>/dev/null; then
_log $LOG_LEVEL_ERROR "$cmd command not found. Please install $cmd."
return 1
fi
done
}

_check_disk_space() {
local required_space_gb=$1
# 检查磁盘空间
local available_space
available_space=$(df -k . | awk 'NR==2 {print $4}')
local required_space=$((required_space_gb * 1024 * 1024)) # 转换为 KB
if [[ $available_space -lt $required_space ]]; then
_log $LOG_LEVEL_ERROR "Not enough disk space. Required: ${required_space_gb}GB, Available: $((available_space / 1024 / 1024))GB"
return 1
fi
_log $LOG_LEVEL_INFO "Sufficient disk space available. Required: ${required_space_gb}GB, Available: $((available_space / 1024 / 1024))GB"
}

_msg() {
local color_on color_off='\033[0m'
local time_hms="$((SECONDS / 3600))h$(((SECONDS / 60) % 60))m$((SECONDS % 60))s"
Expand Down
File renamed without changes.

0 comments on commit 1b58ee9

Please sign in to comment.