Skip to content

Commit

Permalink
chore: add update.py can merge app.json when update
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVon2021 committed Sep 20, 2024
1 parent 7aaf2eb commit dab00be
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
7 changes: 6 additions & 1 deletion package/kvmd-web/install-kvmd-web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
89 changes: 87 additions & 2 deletions script/install_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
import re
import argparse
import json
import copy

gArgs = None
def doArgParse():
Expand All @@ -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")
Expand Down Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion script/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v1.4.5",
"version": "v1.4.6",
"md5value": "",
"oled":{
"oled_enable": 1,
Expand Down
4 changes: 3 additions & 1 deletion script/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit dab00be

Please sign in to comment.