增加自动创建logs和output目录
This commit is contained in:
@@ -12,10 +12,9 @@ Vmware 虚拟机自动化程序:自动化导出虚拟机和快照信息,自
|
|||||||
- [x] 最后删除 15 天前的 snapshot,并同时记录删除的 snapshot 日志信息
|
- [x] 最后删除 15 天前的 snapshot,并同时记录删除的 snapshot 日志信息
|
||||||
- [x] 设置计划任务,每周六(或 每15 天)执行一次
|
- [x] 设置计划任务,每周六(或 每15 天)执行一次
|
||||||
- [ ] 增加排除不能删除的 snapshots 信息,用红色底填充标识
|
- [ ] 增加排除不能删除的 snapshots 信息,用红色底填充标识
|
||||||
- [ ] Outlook 邮箱发送统计超过 15 天的 snapshots 信息(即要删除的快照列表)
|
- [ ] 删除快照前/后发送邮件通知
|
||||||
- [ ] 需要控制每台 vCenter 不可以同时删除超过 4 个快照
|
- [ ] 多线程删除(控制每台 vCenter 不可以同时删除超过 4 个快照)
|
||||||
- [ ] 删除前后发送邮件通知
|
- [ ] 使用数据库保存管理节点账号密码(加密)
|
||||||
- [ ] 多线程删除
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -446,7 +445,7 @@ WORKDIR /removeweeklysnapshot/
|
|||||||
# 复制代码
|
# 复制代码
|
||||||
COPY ./ /removeweeklysnapshot/
|
COPY ./ /removeweeklysnapshot/
|
||||||
# 安装必要的软件和 python 库
|
# 安装必要的软件和 python 库
|
||||||
RUN apt-get update && apt-get install procps tzdata -y && pip install -r requirements.txt && chmod +x /removeweeklysnapshot/main.py && mkdir output
|
RUN apt-get update && apt-get install procps tzdata -y && pip install -r requirements.txt && chmod +x /removeweeklysnapshot/main.py
|
||||||
# 容器内执行启动程序
|
# 容器内执行启动程序
|
||||||
CMD ["python3", "/removeweeklysnapshot/main.py"]
|
CMD ["python3", "/removeweeklysnapshot/main.py"]
|
||||||
EOF
|
EOF
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ management_nodes:
|
|||||||
|
|
||||||
# 全局策略配置
|
# 全局策略配置
|
||||||
global:
|
global:
|
||||||
disable_ssl_verify: true
|
disable_ssl_verify: true # ESXi连接特殊配置(禁用SSL验证,ESXi默认自签证书)
|
||||||
snapshot_retention_days: 15 # 可选,默认值 15 天
|
snapshot_retention_days: 15 # 可选,默认值 15 天
|
||||||
# excel_output_path: ./vm_snapshots_report.xlsx # 可选,使用默认值,如:/logs/2026-02-20_14-00-21-VMsSnapShots_report.xlsx
|
# excel_output_path: ./vm_snapshots_report.xlsx # 可选,使用默认值,如:/logs/2026-02-20_14-00-21-VMsSnapShots_report.xlsx
|
||||||
# 'excel_output_path',: ./vm_snapshots_report.xlsx # 可选,使用默认值,如:/logs/2026-02-20_14-00-21-VMsSnapShots_report.xlsx
|
# yaml_output_path: ./yaml_snapshots_report.yaml # 可选
|
||||||
# ESXi连接特殊配置(禁用SSL验证,ESXi默认自签证书)
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import yaml, os
|
import os, yaml
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from utils.logger import logger
|
from utils.logger import logger
|
||||||
|
|
||||||
@@ -15,10 +15,8 @@ def load_config():
|
|||||||
# vCenter/ESXi节点列表
|
# vCenter/ESXi节点列表
|
||||||
"MANAGEMENT_NODES": raw_config.get('management_nodes', []),
|
"MANAGEMENT_NODES": raw_config.get('management_nodes', []),
|
||||||
"SNAPSHOT_RETENTION_DAYS": int(global_config.get('snapshot_retention_days', 15)),
|
"SNAPSHOT_RETENTION_DAYS": int(global_config.get('snapshot_retention_days', 15)),
|
||||||
# "EXCEL_OUTPUT_PATH": global_config.get('excel_output_path', f'/logs/{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}-VMsSnapShots_report.xlsx'),
|
"EXCEL_OUTPUT_PATH": global_config.get('excel_output_path', os.path.join(DATA_DIR, f'vm_snapshots_report-{datetime.now().strftime('%Y-%m-%d')}.xlsx')),
|
||||||
# "LOG_FILE_PATH": global_config.get('log_file_path', f'/logs/{datetime.now().strftime('%Y%m%d_%H%M%S')}-VMsSnapShots_cleanup.logs'), "EXCEL_OUTPUT_PATH": global_config.get('excel_output_path', f'/logs/{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}-VMsSnapShots_report.xlsx'),
|
"YAML_OUTPUT_PATH": global_config.get('yaml_output_path', os.path.join(DATA_DIR, f'old_snapshots-{datetime.now().strftime('%Y-%m-%d')}.yaml')),
|
||||||
"EXCEL_OUTPUT_PATH": global_config.get('excel_output_path', f'D:\\PycharmProjects\\RemoveWeeklySnapshot\\output\\vm_snapshots_report-{datetime.now().strftime('%Y-%m-%d')}.xlsx'),
|
|
||||||
"YAML_OUTPUT_PATH": global_config.get('yaml_output_path', f'D:\\PycharmProjects\\RemoveWeeklySnapshot\\output\\old_snapshots-{datetime.now().strftime('%Y-%m-%d')}.yaml'),
|
|
||||||
"DISABLE_SSL_VERIFY": global_config.get('disable_ssl_verify', True),
|
"DISABLE_SSL_VERIFY": global_config.get('disable_ssl_verify', True),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +40,11 @@ def load_config():
|
|||||||
logger.error(f"❌ 加载配置失败: {str(e)}")
|
logger.error(f"❌ 加载配置失败: {str(e)}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取项目根目录
|
||||||
|
DATA_DIR = os.path.join(BASE_DIR, 'output') # 获取导出数据的文件根目录
|
||||||
|
os.makedirs(DATA_DIR, exist_ok=True) # 自动创建目录
|
||||||
|
|
||||||
# 配置文件路径
|
# 配置文件路径
|
||||||
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config.yaml')
|
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config.yaml')
|
||||||
# 加载配置并导出全局变量
|
# 加载配置并导出全局变量
|
||||||
@@ -50,7 +53,6 @@ MANAGEMENT_NODES = config["MANAGEMENT_NODES"]
|
|||||||
SNAPSHOT_RETENTION_DAYS = config["SNAPSHOT_RETENTION_DAYS"]
|
SNAPSHOT_RETENTION_DAYS = config["SNAPSHOT_RETENTION_DAYS"]
|
||||||
EXCEL_OUTPUT_PATH = config["EXCEL_OUTPUT_PATH"]
|
EXCEL_OUTPUT_PATH = config["EXCEL_OUTPUT_PATH"]
|
||||||
YAML_OUTPUT_PATH = config["YAML_OUTPUT_PATH"]
|
YAML_OUTPUT_PATH = config["YAML_OUTPUT_PATH"]
|
||||||
#LOG_FILE_PATH = config["LOG_FILE_PATH"]
|
|
||||||
DISABLE_SSL_VERIFY = config["DISABLE_SSL_VERIFY"]
|
DISABLE_SSL_VERIFY = config["DISABLE_SSL_VERIFY"]
|
||||||
|
|
||||||
|
|
||||||
@@ -59,8 +61,6 @@ if __name__ == "__main__":
|
|||||||
print("\n【全局配置】")
|
print("\n【全局配置】")
|
||||||
print(f" 快照保留天数: {config['SNAPSHOT_RETENTION_DAYS']}")
|
print(f" 快照保留天数: {config['SNAPSHOT_RETENTION_DAYS']}")
|
||||||
print(f" Excel输出路径: {EXCEL_OUTPUT_PATH}")
|
print(f" Excel输出路径: {EXCEL_OUTPUT_PATH}")
|
||||||
#print(f" 日志文件路径: {LOG_FILE_PATH}")
|
|
||||||
print(f" 禁用SSL验证: {config['DISABLE_SSL_VERIFY']}")
|
|
||||||
|
|
||||||
# 打印管理节点
|
# 打印管理节点
|
||||||
nodes = config['MANAGEMENT_NODES']
|
nodes = config['MANAGEMENT_NODES']
|
||||||
@@ -68,10 +68,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
for i, node in enumerate(nodes, 1):
|
for i, node in enumerate(nodes, 1):
|
||||||
print(f"\n 节点[{i}]:")
|
print(f"\n 节点[{i}]:")
|
||||||
print(f" 类型: {node.get('type')}")
|
|
||||||
print(f" 名称: {node.get('name')}")
|
|
||||||
print(f" 地址: {node.get('host')}")
|
print(f" 地址: {node.get('host')}")
|
||||||
print(f" 用户: {node.get('user')}")
|
print(f" 用户: {node.get('user')}")
|
||||||
print(f" 密码: {'*' * len(node.get('password', ''))}")
|
print(f" 密码: {'*' * len(node.get('password', ''))}")
|
||||||
# print(f" 密码: {node.get('password', '')}") # 直接打印出密码
|
# print(f" 密码: {node.get('password', '')}") # 直接打印出密码
|
||||||
print(f" 最大并发删除: {node.get('max_delete_concurrent')}")
|
|
||||||
@@ -1,9 +1,4 @@
|
|||||||
import os, logging
|
import os, datetime, logging
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
# 获取项目根目录(假设 logger.py 在 utils/ 目录下)
|
|
||||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
||||||
LOG_DIR = os.path.join(BASE_DIR, 'logs')
|
|
||||||
|
|
||||||
|
|
||||||
def get_logger():
|
def get_logger():
|
||||||
@@ -19,13 +14,11 @@ def get_logger():
|
|||||||
# 定义日志格式
|
# 定义日志格式
|
||||||
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
# 自动创建日志目录
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取项目根目录
|
||||||
os.makedirs(LOG_DIR, exist_ok=True)
|
LOG_DIR = os.path.join(BASE_DIR, 'logs') # 获取日志文件根目录
|
||||||
# 日志文件路径
|
os.makedirs(LOG_DIR, exist_ok=True) # 自动创建目录
|
||||||
log_file = os.path.join(
|
|
||||||
LOG_DIR,
|
log_file = os.path.join(LOG_DIR, f'{datetime.now().strftime("%Y-%m-%d")}-removeweeklysnapshot.log') # 日志文件路径
|
||||||
f'{datetime.now().strftime("%Y-%m-%d")}-vm_snapshot_cleanup.log'
|
|
||||||
)
|
|
||||||
|
|
||||||
# 文件处理器(写入日志文件)
|
# 文件处理器(写入日志文件)
|
||||||
file_handler = logging.FileHandler(log_file, encoding='utf-8')
|
file_handler = logging.FileHandler(log_file, encoding='utf-8')
|
||||||
|
|||||||
Reference in New Issue
Block a user