From 1b58ee96671b0be86e67182da6fcea0735a3dcb7 Mon Sep 17 00:00:00 2001 From: xiagw Date: Sun, 27 Oct 2024 14:51:48 +0800 Subject: [PATCH] update common.sh and include --- .gitignore | 1 + bin/aliyun.sh | 2 +- bin/backup_encrypt_notify.sh | 7 +- bin/common_functions.sh | 84 ---------------------- {bin => conf}/backup_encrypt_notify.sh.env | 2 +- lib/{ => aliyun}/workorder.py | 0 lib/common.sh | 81 +++++++++++++++++++++ {bin => lib}/element.py | 0 8 files changed, 88 insertions(+), 89 deletions(-) delete mode 100644 bin/common_functions.sh rename {bin => conf}/backup_encrypt_notify.sh.env (98%) rename lib/{ => aliyun}/workorder.py (100%) rename {bin => lib}/element.py (100%) diff --git a/.gitignore b/.gitignore index ed0091d1..7399291d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ ## vim swap .swp *.log +*.env ## gitlab runner exclude builds/ diff --git a/bin/aliyun.sh b/bin/aliyun.sh index 6b30191b..f630072a 100755 --- a/bin/aliyun.sh +++ b/bin/aliyun.sh @@ -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 diff --git a/bin/backup_encrypt_notify.sh b/bin/backup_encrypt_notify.sh index 6cd67de8..5e76685c 100644 --- a/bin/backup_encrypt_notify.sh +++ b/bin/backup_encrypt_notify.sh @@ -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 @@ -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 @@ -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" "$@" ## 检查必要的命令 diff --git a/bin/common_functions.sh b/bin/common_functions.sh deleted file mode 100644 index db625b5c..00000000 --- a/bin/common_functions.sh +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env bash - -# 定义日志级别常量 -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" -} - -# ... 可以继续添加其他通用函数 ... diff --git a/bin/backup_encrypt_notify.sh.env b/conf/backup_encrypt_notify.sh.env similarity index 98% rename from bin/backup_encrypt_notify.sh.env rename to conf/backup_encrypt_notify.sh.env index 512eee76..0cb49094 100644 --- a/bin/backup_encrypt_notify.sh.env +++ b/conf/backup_encrypt_notify.sh.env @@ -5,7 +5,7 @@ LOG_LEVEL=INFO # DEBUG=false ## 备份路径 -PATHS=("/tmp/test") +PATHS=("/home/test") # 备份主机 # HOSTS=("host1.example.com" "host2.example.com") diff --git a/lib/workorder.py b/lib/aliyun/workorder.py similarity index 100% rename from lib/workorder.py rename to lib/aliyun/workorder.py diff --git a/lib/common.sh b/lib/common.sh index c7986348..bd1c1cd9 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -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" diff --git a/bin/element.py b/lib/element.py similarity index 100% rename from bin/element.py rename to lib/element.py