264 lines
12 KiB
Bash
264 lines
12 KiB
Bash
|
|
#!/bin/bash
|
|||
|
|
|
|||
|
|
config_ota_server() {
|
|||
|
|
local user="$1"
|
|||
|
|
local product_dir="$2"
|
|||
|
|
local platform_dir="$3"
|
|||
|
|
local os_name="$4"
|
|||
|
|
local target_dir="/opt/ota_server"
|
|||
|
|
local service_name="ota_server"
|
|||
|
|
|
|||
|
|
if [ -z "$product_dir" ] || [ -z "$platform_dir" ]; then
|
|||
|
|
echo "ERROR: 必须提供 product 和 platform 目录路径作为参数"
|
|||
|
|
return 1
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
echo "INFO: 开始配置 ota_server..."
|
|||
|
|
echo "INFO: 源目录 - product: $product_dir, platform: $platform_dir"
|
|||
|
|
echo "INFO: 目标目录: $target_dir"
|
|||
|
|
echo "INFO: 目标用户: $user"
|
|||
|
|
|
|||
|
|
# 查找 ota_server 可执行文件
|
|||
|
|
local ota_server_executable=""
|
|||
|
|
local source_dirs=("$product_dir" "$platform_dir")
|
|||
|
|
|
|||
|
|
for source_dir in "${source_dirs[@]}"; do
|
|||
|
|
if [ -f "$source_dir/ota_server" ]; then
|
|||
|
|
ota_server_executable="$source_dir/ota_server"
|
|||
|
|
break
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
|
|||
|
|
if [ -z "$ota_server_executable" ]; then
|
|||
|
|
echo "ERROR: 在所有源目录中均未找到 ota_server 可执行文件"
|
|||
|
|
return 1
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
echo "INFO: 找到 ota_server: $ota_server_executable"
|
|||
|
|
|
|||
|
|
# 检查 ota_server 是否以系统服务方式运行
|
|||
|
|
if systemctl is-active --quiet "$service_name"; then
|
|||
|
|
echo "INFO: ota_server 服务正在运行,检查文件更新..."
|
|||
|
|
|
|||
|
|
# 检查是否有文件需要更新
|
|||
|
|
local need_update=false
|
|||
|
|
|
|||
|
|
# 检查 ota_server 可执行文件是否有更新
|
|||
|
|
# local target_server="$target_dir/ota_server"
|
|||
|
|
# if [ -f "$target_server" ]; then
|
|||
|
|
# source_size=$(stat -c%s "$ota_server_executable" 2>/dev/null || echo "0")
|
|||
|
|
# target_size=$(stat -c%s "$target_server" 2>/dev/null || echo "0")
|
|||
|
|
# source_hash=$(md5sum "$ota_server_executable" 2>/dev/null | cut -d' ' -f1)
|
|||
|
|
# target_hash=$(md5sum "$target_server" 2>/dev/null | cut -d' ' -f1)
|
|||
|
|
|
|||
|
|
# if [ "$source_size" != "$target_size" ] && [ "$source_hash" != "$target_hash" ]; then
|
|||
|
|
# echo "INFO: 检测到 ota_server 程序更新"
|
|||
|
|
# echo " Source - Size: $source_size, Hash: $source_hash"
|
|||
|
|
# echo " Target - Size: $target_size, Hash: $target_hash"
|
|||
|
|
# need_update=true
|
|||
|
|
# fi
|
|||
|
|
# else
|
|||
|
|
# echo "INFO: 目标目录中不存在 ota_server,需要安装"
|
|||
|
|
# need_update=true
|
|||
|
|
# fi
|
|||
|
|
|
|||
|
|
# 检查依赖库是否有更新(只检查源目录下的库)
|
|||
|
|
if [ "$need_update" = false ]; then
|
|||
|
|
# 使用 ldd 查找 ota_server 的依赖库
|
|||
|
|
local libs=$(ldd "$ota_server_executable" 2>/dev/null | grep "=>" | awk '{print $3}' | grep -v "not found")
|
|||
|
|
|
|||
|
|
for lib in $libs; do
|
|||
|
|
if [ -n "$lib" ] && [ -f "$lib" ]; then
|
|||
|
|
# 只处理位于两个源目录下的库,不处理系统库
|
|||
|
|
if [[ "$lib" == "$product_dir"* ]] || [[ "$lib" == "$platform_dir"* ]]; then
|
|||
|
|
lib_name=$(basename "$lib")
|
|||
|
|
# 使用通配符匹配库文件的所有相关文件
|
|||
|
|
lib_base=$(echo "$lib_name" | sed 's/\.so\..*$/.so*/' | sed 's/\.so$/.so*/')
|
|||
|
|
lib_dir=$(dirname "$lib")
|
|||
|
|
|
|||
|
|
# 查找所有相关的库文件
|
|||
|
|
for lib_file in "$lib_dir"/$lib_base; do
|
|||
|
|
if [ -f "$lib_file" ]; then
|
|||
|
|
target_lib="$target_dir/$(basename "$lib_file")"
|
|||
|
|
|
|||
|
|
# 检查库文件是否在目标目录中
|
|||
|
|
if [ -f "$target_lib" ]; then
|
|||
|
|
source_size=$(stat -c%s "$lib_file" 2>/dev/null || echo "0")
|
|||
|
|
target_size=$(stat -c%s "$target_lib" 2>/dev/null || echo "0")
|
|||
|
|
source_hash=$(md5sum "$lib_file" 2>/dev/null | cut -d' ' -f1)
|
|||
|
|
target_hash=$(md5sum "$target_lib" 2>/dev/null | cut -d' ' -f1)
|
|||
|
|
|
|||
|
|
if [ "$source_size" != "$target_size" ] && [ "$source_hash" != "$target_hash" ]; then
|
|||
|
|
echo "INFO: 检测到依赖库更新: $(basename "$lib_file")"
|
|||
|
|
need_update=true
|
|||
|
|
break 2
|
|||
|
|
fi
|
|||
|
|
else
|
|||
|
|
echo "INFO: 检测到新依赖库: $(basename "$lib_file")"
|
|||
|
|
need_update=true
|
|||
|
|
break 2
|
|||
|
|
fi
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
fi
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
if [ "$need_update" = true ]; then
|
|||
|
|
# 拷贝依赖库(只拷贝源目录下的库)
|
|||
|
|
echo "INFO: 拷贝依赖库到 $target_dir"
|
|||
|
|
local libs=$(ldd "$ota_server_executable" 2>/dev/null | grep "=>" | awk '{print $3}' | grep -v "not found")
|
|||
|
|
for lib in $libs; do
|
|||
|
|
if [ -n "$lib" ] && [ -f "$lib" ]; then
|
|||
|
|
# 只拷贝位于两个源目录下的库,不拷贝系统库
|
|||
|
|
if [[ "$lib" == "$product_dir"* ]] || [[ "$lib" == "$platform_dir"* ]]; then
|
|||
|
|
lib_name=$(basename "$lib")
|
|||
|
|
# 使用通配符匹配库文件的所有相关文件
|
|||
|
|
lib_base=$(echo "$lib_name" | sed 's/\.so\..*$/.so*/' | sed 's/\.so$/.so*/')
|
|||
|
|
lib_dir=$(dirname "$lib")
|
|||
|
|
|
|||
|
|
# 拷贝所有相关的库文件
|
|||
|
|
for lib_file in "$lib_dir"/$lib_base; do
|
|||
|
|
if [ -f "$lib_file" ]; then
|
|||
|
|
# echo "INFO: 拷贝依赖库: $(basename "$lib_file")"
|
|||
|
|
cp "$lib_file" "$target_dir/"
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
fi
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
|
|||
|
|
# 设置目录和文件的所有权
|
|||
|
|
# echo "INFO: 设置目录和文件的所有权为: $user"
|
|||
|
|
# sudo chown -R "$user":root "$target_dir"
|
|||
|
|
# sudo chmod 777 "$target_dir/"*
|
|||
|
|
|
|||
|
|
# echo "INFO: 服务已更新,需手动重启"
|
|||
|
|
|
|||
|
|
# echo "INFO: 启动 ota_server 服务..."
|
|||
|
|
# systemctl start "$service_name"
|
|||
|
|
|
|||
|
|
# if systemctl is-active --quiet "$service_name"; then
|
|||
|
|
# echo "INFO: ota_server 服务更新并启动成功"
|
|||
|
|
# else
|
|||
|
|
# echo "ERROR: ota_server 服务启动失败"
|
|||
|
|
# return 1
|
|||
|
|
# fi
|
|||
|
|
else
|
|||
|
|
echo "INFO: ota_server 及其依赖库无变化,无需更新"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
else
|
|||
|
|
echo "INFO: ota_server 服务未运行,进行安装或更新..."
|
|||
|
|
|
|||
|
|
# 创建目标目录
|
|||
|
|
mkdir -p "$target_dir"
|
|||
|
|
|
|||
|
|
# 拷贝 ota_server 可执行文件
|
|||
|
|
echo "INFO: 拷贝 ota_server 可执行文件到 $target_dir"
|
|||
|
|
cp "$ota_server_executable" "$target_dir/"
|
|||
|
|
chmod +x "$target_dir/ota_server"
|
|||
|
|
|
|||
|
|
# 查找并拷贝 updater_linux.sh 脚本和 ota_server.yaml
|
|||
|
|
local update_script_source=""
|
|||
|
|
local ota_server_yaml=""
|
|||
|
|
for source_dir in "${source_dirs[@]}"; do
|
|||
|
|
if [ -f "$source_dir/updater_linux.sh" ]; then
|
|||
|
|
update_script_source="$source_dir/updater_linux.sh"
|
|||
|
|
fi
|
|||
|
|
if [ -f "$source_dir/ota_server.yaml" ] && [ ! -f "$target_dir/ota_server.yaml" ]; then
|
|||
|
|
ota_server_yaml="$source_dir/ota_server.yaml"
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
|
|||
|
|
if [ -n "$update_script_source" ] && [ -f "$update_script_source" ]; then
|
|||
|
|
echo "INFO: 拷贝 updater_linux.sh 脚本到 $target_dir"
|
|||
|
|
cp "$update_script_source" "$target_dir/"
|
|||
|
|
chmod +x "$target_dir/updater_linux.sh"
|
|||
|
|
else
|
|||
|
|
echo "WARNING: 在所有源目录中均未找到 updater_linux.sh 脚本"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
if [ -n "$ota_server_yaml" ] && [ -f "$ota_server_yaml" ]; then
|
|||
|
|
echo "INFO: 拷贝 ota_server.yaml 脚本到 $target_dir"
|
|||
|
|
cp "$ota_server_yaml" "$target_dir/"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
# 拷贝依赖库(只拷贝源目录下的库)
|
|||
|
|
echo "INFO: 拷贝依赖库到 $target_dir"
|
|||
|
|
local libs=$(ldd "$ota_server_executable" 2>/dev/null | grep "=>" | awk '{print $3}' | grep -v "not found")
|
|||
|
|
for lib in $libs; do
|
|||
|
|
if [ -n "$lib" ] && [ -f "$lib" ]; then
|
|||
|
|
# 只拷贝位于两个源目录下的库,不拷贝系统库
|
|||
|
|
if [[ "$lib" == "$product_dir"* ]] || [[ "$lib" == "$platform_dir"* ]]; then
|
|||
|
|
lib_name=$(basename "$lib")
|
|||
|
|
# 使用通配符匹配库文件的所有相关文件
|
|||
|
|
lib_base=$(echo "$lib_name" | sed 's/\.so\..*$/.so*/' | sed 's/\.so$/.so*/')
|
|||
|
|
lib_dir=$(dirname "$lib")
|
|||
|
|
|
|||
|
|
# 拷贝所有相关的库文件
|
|||
|
|
for lib_file in "$lib_dir"/$lib_base; do
|
|||
|
|
if [ -f "$lib_file" ]; then
|
|||
|
|
# echo "INFO: 拷贝依赖库: $(basename "$lib_file")"
|
|||
|
|
cp "$lib_file" "$target_dir/"
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
fi
|
|||
|
|
fi
|
|||
|
|
done
|
|||
|
|
|
|||
|
|
# 设置目录和文件的所有权
|
|||
|
|
echo "INFO: 设置目录和文件的所有权为: $user"
|
|||
|
|
sudo chown -R "$user":root "$target_dir"
|
|||
|
|
sudo chmod 777 "$target_dir/"*
|
|||
|
|
|
|||
|
|
# 检查是否有 ota_server 可执行文件
|
|||
|
|
if [ -f "$target_dir/ota_server" ] && [ -x "$target_dir/ota_server" ]; then
|
|||
|
|
echo "INFO: 运行 ota_server 进行服务注册..."
|
|||
|
|
echo "INFO: user="$user", os="$os_name""
|
|||
|
|
|
|||
|
|
# 运行 ota_server(首次运行会自动注册服务)
|
|||
|
|
sudo "$target_dir"/ota_server --user "$user" --os "$os_name" > /dev/null 2>&1
|
|||
|
|
|
|||
|
|
sleep 2
|
|||
|
|
|
|||
|
|
# 检查服务是否成功注册
|
|||
|
|
# if systemctl is-active --quiet "$service_name"; then
|
|||
|
|
# echo "INFO: ota_server 服务注册并启动成功"
|
|||
|
|
# # 杀死临时进程(服务已经在后台运行)
|
|||
|
|
# kill $ota_pid 2>/dev/null || true
|
|||
|
|
# else
|
|||
|
|
# echo "WARNING: ota_server 可能未成功注册为系统服务"
|
|||
|
|
# # 杀死临时进程
|
|||
|
|
# kill $ota_pid 2>/dev/null || true
|
|||
|
|
|
|||
|
|
# # 尝试手动启动服务
|
|||
|
|
# if systemctl start "$service_name" 2>/dev/null; then
|
|||
|
|
# echo "INFO: ota_server 服务手动启动成功"
|
|||
|
|
# else
|
|||
|
|
# echo "ERROR: ota_server 服务启动失败"
|
|||
|
|
# return 1
|
|||
|
|
# fi
|
|||
|
|
# fi
|
|||
|
|
else
|
|||
|
|
echo "ERROR: 在目标目录中找不到 ota_server 可执行文件"
|
|||
|
|
return 1
|
|||
|
|
fi
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
return 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 如果脚本被直接执行,则显示用法
|
|||
|
|
# if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|||
|
|
# if [ $# -eq 3 ]; then
|
|||
|
|
# # 如果提供了3个参数,直接执行配置
|
|||
|
|
# config_ota_server "$1" "$2" "$3"
|
|||
|
|
# else
|
|||
|
|
# echo "Usage: $0 <user> <product_dir> <platform_dir>"
|
|||
|
|
# echo "Example:"
|
|||
|
|
# echo " $0 admin /opt/EnergyHub/product/oe2203_debug /opt/EnergyHub/platform/oe2203_debug"
|
|||
|
|
# exit 1
|
|||
|
|
# fi
|
|||
|
|
# fi
|