websoft9/docker/apphub/script/migration.sh

51 lines
1.2 KiB
Bash
Raw Normal View History

2023-10-27 17:14:27 +08:00
#!/bin/bash
2023-10-27 14:22:53 +08:00
echo "start to migrate config.ini"
2023-10-27 15:17:22 +08:00
migrate_ini() {
# Define file paths, use template ini and syn exsit items from target ini
2023-10-27 17:14:27 +08:00
export target_ini="$1"
export template_ini="$2"
2023-10-27 15:17:22 +08:00
2023-10-27 17:14:27 +08:00
python3 - <<EOF
2023-10-27 14:22:53 +08:00
import configparser
import os
import sys
2023-10-27 17:14:27 +08:00
target_ini = os.environ['target_ini']
template_ini = os.environ['template_ini']
2023-10-27 15:17:22 +08:00
# Create two config parsers
target_parser = configparser.ConfigParser()
template_parser = configparser.ConfigParser()
2023-10-27 14:22:53 +08:00
try:
2023-10-27 17:14:27 +08:00
2023-10-27 15:17:22 +08:00
target_parser.read(target_ini)
template_parser.read(template_ini)
2023-10-27 14:22:53 +08:00
except configparser.MissingSectionHeaderError:
print("Error: The provided files are not valid INI files.")
sys.exit(1)
2023-10-27 16:31:14 +08:00
# use target_parser to override template_parser
for section in target_parser.sections():
if template_parser.has_section(section):
for key, value in target_parser.items(section):
if template_parser.has_option(section, key):
template_parser.set(section, key, value)
2023-10-27 15:17:22 +08:00
2023-10-27 17:14:27 +08:00
2023-10-27 15:17:22 +08:00
with open(target_ini, 'w') as f:
template_parser.write(f)
2023-10-27 14:22:53 +08:00
EOF
}
2023-10-27 16:31:14 +08:00
2023-10-27 17:14:27 +08:00
migrate_ini "/websoft9/apphub/src/config/config.ini" "/websoft9/config/config.ini"
2023-10-27 14:22:53 +08:00
if [ $? -eq 0 ]; then
echo "Success to update config.ini"
else
echo "Fail to update config.ini, skip it"
fi