Files
wecom_it_smart_desk/ops-tools/fix_redis_env.py

43 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""修复 .env 文件中的 Redis URL 特殊字符问题"""
import os
import sys
# 从参数获取目标服务器路径
if len(sys.argv) > 1:
env_path = sys.argv[1]
else:
print("Usage: python fix_redis_env.py <path_to_env_file>")
sys.exit(1)
# 备份
backup_path = "/tmp/" + os.path.basename(env_path) + ".bak"
os.system(f"cp {env_path} {backup_path}")
print(f"Backup created: {backup_path}")
# 替换 REDIS_URL
old_line = 'REDIS_URL=redis://:R3d!s@2026#Secure@redis:6379/0'
new_line = 'REDIS_URL="redis://:R3d!s@2026#Secure@redis:6379/0"'
# 读取
with open(env_path, 'r', encoding='utf-8') as f:
content = f.read()
if old_line in content:
content = content.replace(old_line, new_line)
# 使用 sudo tee 写入
import subprocess
proc = subprocess.Popen(['sudo', 'tee', env_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.stdin.write(content.encode('utf-8'))
proc.stdin.close()
proc.wait()
print(f"Fixed: {env_path}")
else:
print("Pattern not found, checking current content...")
for line in content.split('\n'):
if 'REDIS_URL' in line:
print(f" {line}")
print("Done!")