Created
May 14, 2024 07:20
-
-
Save TBXark/f5b1123f362245b6ad975c70c1099128 to your computer and use it in GitHub Desktop.
Update PVE UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import datetime | |
backup_folder = "/root/backup" | |
def read_file(file_path): | |
try: | |
with open(file_path, 'r') as file: | |
return file.read() | |
except Exception as e: | |
print(f"读取文件 {file_path} 时出错: {e}") | |
return None | |
def write_file(file_path, content): | |
try: | |
with open(file_path, 'w') as file: | |
file.write(content) | |
except Exception as e: | |
print(f"写入文件 {file_path} 时出错: {e}") | |
def backup_file(file_path): | |
try: | |
content = read_file(file_path) | |
if content is not None: | |
if not os.path.exists(backup_folder): | |
os.makedirs(backup_folder) | |
base_name = os.path.basename(file_path) | |
name, ext = os.path.splitext(base_name) | |
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S") | |
backup_file_path = os.path.join(backup_folder, f"{name}_{timestamp}{ext}") | |
write_file(backup_file_path, content) | |
print(f"备份文件 {file_path} 到 {backup_file_path}") | |
except Exception as e: | |
print(f"备份文件 {file_path} 时出错: {e}") | |
def modify_file(file_path, finder, updater): | |
content = read_file(file_path) | |
if content is None: | |
return | |
if finder(content): | |
newContent = updater(content) | |
if content == newContent: | |
print(f"{file_path} 内容没有变化") | |
return | |
write_file(file_path, newContent) | |
print(f"{file_path} 修改成功") | |
else: | |
print(f"{file_path} 已经修改过了") | |
# 修改 /usr/share/perl5/PVE/API2/Nodes.pm | |
def updateNodesPm(): | |
pmPath = "/usr/share/perl5/PVE/API2/Nodes.pm" | |
searchContent = " $res->{pveversion} = PVE::pvecfg::package() . \"/\" .\n PVE::pvecfg::version_text();" | |
newContent = " $res->{thermalstate} = `sensors`;" | |
finder = lambda content: content.find("thermalstate") == -1 | |
updater = lambda content: content.replace(searchContent, searchContent + "\n" + newContent) | |
backup_file(pmPath) | |
modify_file(pmPath, finder, updater) | |
# 修改 /usr/share/pve-manager/js/pvemanagerlib.js | |
def updatePvemanagerlib(): | |
managerPath = "/usr/share/pve-manager/js/pvemanagerlib.js" | |
searchContent = "\t xtype: 'pmxNodeInfoRepoStatus',\n\t itemId: 'repositoryStatus',\n\t product: 'Proxmox VE',\n\t repoLink: `#${repoLink}`," | |
replaceContent = "\t itemId: 'thermal',\n\t colspan: 2,\n\t printBar: false,\n\t title: gettext('CPU Thermal State'),\n\t textField: 'thermalstate',\n\t renderer:function(value){\n\t const c0 = value.match(/Core 0.*?\\+([\\d\\.]+)Â/)[1];\n\t const c1 = value.match(/Core 1.*?\\+([\\d\\.]+)Â/)[1];\n\t const c2 = value.match(/Core 2.*?\\+([\\d\\.]+)Â/)[1];\n\t const c3 = value.match(/Core 3.*?\\+([\\d\\.]+)Â/)[1];\n\t return `Core 0: ${c0} ℃ | Core 1: ${c1} ℃ | Core 2: ${c2} ℃ | Core 3: ${c3} ℃`\n\t }" | |
finder = lambda content: content.find("thermalstate") == -1 | |
updater = lambda content: content.replace(searchContent, replaceContent) | |
backup_file(managerPath) | |
modify_file(managerPath, finder, updater) | |
# 修改 /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js | |
def updateProxmoxlib(): | |
libPath = "/usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js" | |
searchContent = "\tProxmox.Utils.API2Request(\n\t {\n\t\turl: '/nodes/localhost/subscription',\n\t\tmethod: 'GET',\n\t\tfailure: function(response, opts) {\n\t\t Ext.Msg.alert(gettext('Error'), response.htmlStatus);\n\t\t},\n\t\tsuccess: function(response, opts) {" | |
newContent = "\t\t\nreturn;//BlockTheSubscription" | |
finder = lambda content: content.find("BlockTheSubscription") == -1 | |
updater = lambda content: content.replace(searchContent, searchContent + newContent) | |
backup_file(libPath) | |
modify_file(libPath, finder, updater) | |
if __name__ == "__main__": | |
updateNodesPm() | |
updatePvemanagerlib() | |
updateProxmoxlib() | |
print("修改完成") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment