From dab00be6c821e2db6bd36aa419969154161c6672 Mon Sep 17 00:00:00 2001 From: ThomasVon2021 <1767893964@qq.com> Date: Fri, 20 Sep 2024 20:27:40 +0800 Subject: [PATCH] chore: add update.py can merge app.json when update --- package/kvmd-web/install-kvmd-web.sh | 7 ++- script/install_release.py | 89 +++++++++++++++++++++++++++- script/package.json | 2 +- script/update.py | 4 +- 4 files changed, 97 insertions(+), 5 deletions(-) diff --git a/package/kvmd-web/install-kvmd-web.sh b/package/kvmd-web/install-kvmd-web.sh index 4c02fd3..13f9d87 100755 --- a/package/kvmd-web/install-kvmd-web.sh +++ b/package/kvmd-web/install-kvmd-web.sh @@ -13,7 +13,12 @@ if [ -n "${pid}" ]; then kill -9 "${pid}" fi -cp -R release /mnt/exec +if [ -f "/mnt/exec/release/config/user.json" ]; then + rsync -av --exclude 'config/user.json' release /mnt/exec/ +else + cp -R release /mnt/exec +fi + chmod 777 -R /mnt/exec/release cp release/lib/janus/* /opt/janus/etc/janus diff --git a/script/install_release.py b/script/install_release.py index e7e2cc6..87f879f 100755 --- a/script/install_release.py +++ b/script/install_release.py @@ -5,6 +5,8 @@ import os import re import argparse +import json +import copy gArgs = None def doArgParse(): @@ -25,7 +27,88 @@ def execute_cmd(cmd,sh_path): except subprocess.CalledProcessError as e: out_bytes = e.output.decode() code = e.returncode - print("error code:",code, out_bytes) + # print("error code:",code, out_bytes) + +def update_config(local_file, new_file): + """Update the local configuration file by merging the new config file into it. + + Args: + local_file (str): Path to the local configuration file. + new_file (str): Path to the new configuration file. + """ + def merge_dicts(local_dict, new_dict): + """Recursively merge two dictionaries. + + This function will update the local_dict with values from new_dict. + If a value in new_dict is a dictionary and exists in local_dict, + it will recursively merge the nested dictionaries. Otherwise, + it will overwrite or add the value from new_dict. + """ + for key, value in new_dict.items(): + if isinstance(value, dict) and key in local_dict: + # If the value is a dictionary and the key exists in local_dict, recursively merge + merge_dicts(local_dict[key], value) + else: + # Otherwise, add or overwrite the value from new_dict + local_dict[key] = value + + # Check if the local file exists + if os.path.exists(local_file): + # Read the local configuration file + with open(local_file, 'r', encoding='utf-8') as f: + local_config = json.load(f) + else: + local_config = {} + + # Read the new configuration file + with open(new_file, 'r', encoding='utf-8') as f: + new_config = json.load(f) + + # Merge the local and new configurations + merge_dicts(local_config, new_config) + + # Save the merged configuration back to the local file + with open(local_file, 'w', encoding='utf-8') as f: + json.dump(local_config, f, indent=4, ensure_ascii=False) + + print(f"Successfully updated {local_file}.") + +def merge_and_save_config(existing_config_path, new_config_path): + """ + 读取两个配置文件,递归合并后将合并结果保存到新配置文件路径。 + + :param existing_config_path: 现有配置文件路径 + :param new_config_path: 新配置文件路径 + """ + def merge_config(existing_config, new_config): + """ + 递归合并两个配置字典,保留现有配置中的键值,新配置中同样的键不会更新现有键。 + """ + merged = copy.deepcopy(existing_config) + + for key, value in new_config.items(): + if key not in merged: + merged[key] = value + elif isinstance(value, dict) and isinstance(merged[key], dict): + merged[key] = merge_config(merged[key], value) + + return merged + + # 读取配置文件 + with open(existing_config_path, 'r') as file: + existing_config = json.load(file) + + with open(new_config_path, 'r') as file: + new_config = json.load(file) + + # 合并配置 + merged_config = merge_config(existing_config, new_config) + + # 保存合并后的配置 + with open(new_config_path, 'w') as file: + json.dump(merged_config, file, indent=4) + + print(f"The configuration file has been successfully merged and saved to {new_config_path}") def main(): print("start install") @@ -55,13 +138,15 @@ def main(): # install all software if os.path.exists(gArgs.releasepath): - cmd = "systemctl disable kvmd-janus && systemctl disable kvmd-hid && systemctl disable kvmd-main \ + cmd = "cp -R /mnt/exec/release/config /tmp && systemctl disable kvmd-janus && systemctl disable kvmd-hid && systemctl disable kvmd-main \ && systemctl disable kvmd-video && bash install-kvmd-web.sh && cp package.json /usr/bin/blikvm/package.json" subprocess.check_output(cmd, shell = True, cwd=gArgs.releasepath ) print('install alpha version successful, start to resatrt service, need 60s...') cmd = "systemctl daemon-reload && systemctl restart kvmd-web" subprocess.check_output(cmd, shell = True, cwd=gArgs.releasepath ) print('restart kvmd-web successful') + merge_and_save_config('/tmp/config/app.json', '/mnt/exec/release/config/app.json') + else: print(gArgs.releasepath, ' not exit') diff --git a/script/package.json b/script/package.json index d619829..07c5872 100755 --- a/script/package.json +++ b/script/package.json @@ -1,5 +1,5 @@ { - "version": "v1.4.5", + "version": "v1.4.6", "md5value": "", "oled":{ "oled_enable": 1, diff --git a/script/update.py b/script/update.py index 04c1f0d..b08b648 100755 --- a/script/update.py +++ b/script/update.py @@ -116,7 +116,9 @@ def version_to_tuple(version): version_numbers = re.findall(r'\d+', version) return tuple(map(int, version_numbers)) + def main(): + print("Welcome to use the upgrade script. Please confirm that you have used git related commands before upgrading the script to ensure that update.by is in the latest state.") board_type = get_board_type() print("Board type:",board_type) global update_result @@ -200,7 +202,7 @@ def main(): output = subprocess.check_output(cmd, shell = True, cwd=install_path) print(output) update_result = True - print("All configurations have been reset to default. If you have changed the web or SSH password, you will need to update the configuration again. Config path is /mnt/exec/release/config/app.json", flush=True) + print("If you cannot log in with your original password, it may be due to a version upgrade and reset configuration to default. If you have changed the web or SSH password, you will need to update the configuration again. Config path is /mnt/exec/release/config/app.json", flush=True) print("Upgrade successful!", flush=True) else: print("There is no latest stable version available.")