feat: startup and update script optimization

1. 修复Shellcheck指出的问题,增强脚本稳定性。
2. 对于更新部分:采取先更新主程序和前端,插件和资源包再更新的原则;主要解决如果插件或资源包没有下载成功,主程序就无法更新成功的问题,但是其实资源包和插件是不影响主程序更新的。

Co-Authored-By: DDSDerek <108336573+DDSDerek@users.noreply.github.com>
Co-Authored-By: Summer⛱ <57806936+honue@users.noreply.github.com>
This commit is contained in:
DDSRem 2024-02-20 14:26:59 +08:00
parent f43efab831
commit daa8d80ec9
2 changed files with 55 additions and 52 deletions

View File

@ -1,17 +1,19 @@
#!/bin/bash #!/bin/bash
# shellcheck shell=bash
# shellcheck disable=SC2016
# 使用 `envsubst` 将模板文件中的 ${NGINX_PORT} 替换为实际的环境变量值 # 使用 `envsubst` 将模板文件中的 ${NGINX_PORT} 替换为实际的环境变量值
envsubst '${NGINX_PORT}${PORT}' < /etc/nginx/nginx.template.conf > /etc/nginx/nginx.conf envsubst '${NGINX_PORT}${PORT}' < /etc/nginx/nginx.template.conf > /etc/nginx/nginx.conf
# 自动更新 # 自动更新
cd / cd /
/usr/local/bin/mp_update /usr/local/bin/mp_update
cd /app cd /app || exit
# 更改 moviepilot userid 和 groupid # 更改 moviepilot userid 和 groupid
groupmod -o -g ${PGID} moviepilot groupmod -o -g "${PGID}" moviepilot
usermod -o -u ${PUID} moviepilot usermod -o -u "${PUID}" moviepilot
# 更改文件权限 # 更改文件权限
chown -R moviepilot:moviepilot \ chown -R moviepilot:moviepilot \
${HOME} \ "${HOME}" \
/app \ /app \
/public \ /public \
/config \ /config \
@ -27,6 +29,6 @@ if [ -S "/var/run/docker.sock" ]; then
haproxy -f /app/haproxy.cfg haproxy -f /app/haproxy.cfg
fi fi
# 设置后端服务权限掩码 # 设置后端服务权限掩码
umask ${UMASK} umask "${UMASK}"
# 启动后端服务 # 启动后端服务
exec dumb-init gosu moviepilot:moviepilot python3 app/main.py exec dumb-init gosu moviepilot:moviepilot python3 app/main.py

95
update
View File

@ -1,15 +1,13 @@
#!/bin/bash #!/bin/bash
# shellcheck shell=bash
# 下载及解压 # 下载及解压
download_and_unzip() { download_and_unzip() {
url="$1" url="$1"
target_dir="$2" target_dir="$2"
echo "正在下载 ${url}..." echo "正在下载 ${url}..."
curl ${CURL_OPTIONS} "$url" ${CURL_HEADERS} | busybox unzip -d /tmp - if curl "${CURL_OPTIONS}" "${url}" "${CURL_HEADERS}" | busybox unzip -d /tmp -; then
if [ $? -eq 0 ]; then mv /tmp/MoviePilot-* /tmp/"${target_dir}"
if [ -e /tmp/MoviePilot-* ]; then
mv /tmp/MoviePilot-* /tmp/${target_dir}
fi
else else
return 1 return 1
fi fi
@ -17,59 +15,62 @@ download_and_unzip() {
# 下载程序资源,$1: 后端版本路径 # 下载程序资源,$1: 后端版本路径
install_backend_and_download_resources() { install_backend_and_download_resources() {
download_and_unzip "https://github.com/jxxghp/MoviePilot/archive/refs/${1}" "App" if download_and_unzip "https://github.com/jxxghp/MoviePilot/archive/refs/${1}" "App"; then
if [ $? -eq 0 ]; then
echo "后端程序下载成功" echo "后端程序下载成功"
pip install ${PIP_OPTIONS} --upgrade pip pip install "${PIP_OPTIONS}" --upgrade pip
pip install ${PIP_OPTIONS} -r /tmp/App/requirements.txt if pip install "${PIP_OPTIONS}" -r /tmp/App/requirements.txt; then
if [ $? -eq 0 ]; then
echo "安装依赖成功" echo "安装依赖成功"
download_and_unzip "https://github.com/jxxghp/MoviePilot-Plugins/archive/refs/heads/main.zip" "Plugins" frontend_version=$(curl "${CURL_OPTIONS}" "https://api.github.com/repos/jxxghp/MoviePilot-Frontend/releases/latest" "${CURL_HEADERS}" | jq -r .tag_name)
if [ $? -eq 0 ]; then if [[ "${frontend_version}" == *v* ]]; then
echo "插件下载成功" if download_and_unzip "https://github.com/jxxghp/MoviePilot-Frontend/releases/download/${frontend_version}/dist.zip" "dist"; then
download_and_unzip "https://github.com/jxxghp/MoviePilot-Resources/archive/refs/heads/main.zip" "Resources" echo "前端程序下载成功"
if [ $? -eq 0 ]; then # 清空目录
echo "资源包下载成功" rm -rf /app
frontend_version=$(curl ${CURL_OPTIONS} "https://api.github.com/repos/jxxghp/MoviePilot-Frontend/releases/latest" ${CURL_HEADERS} | jq -r .tag_name) mkdir -p /app
if [[ "${frontend_version}" == *v* ]]; then # 后端程序
download_and_unzip "https://github.com/jxxghp/MoviePilot-Frontend/releases/download/${frontend_version}/dist.zip" "dist" cp -a /tmp/App/* /app/
if [ $? -eq 0 ]; then # 前端程序
echo "前端程序下载成功" rm -rf /public
# 备份插件目录 mkdir -p /public
rm -rf /plugins cp -a /tmp/dist/* /public/
mkdir -p /plugins # 清理临时目录
cp -a /app/app/plugins/* /plugins/ rm -rf /tmp/*
# 不备份__init__.py echo "程序部分更新成功,前端版本:${frontend_version},后端版本:${1}"s
rm -f /plugins/__init__.py echo "开始更新插件..."
# 清空目录 if download_and_unzip "https://github.com/jxxghp/MoviePilot-Plugins/archive/refs/heads/main.zip" "Plugins"; then
rm -rf /app echo "插件下载成功"
mkdir -p /app # 备份插件目录
# 后端程序 rm -rf /plugins
cp -a /tmp/App/* /app/ mkdir -p /plugins
# 恢复插件目录 cp -a /app/app/plugins/* /plugins/
cp -a /plugins/* /app/app/plugins/ # 不备份__init__.py
# 插件仓库 rm -f /plugins/__init__.py
rsync -av --remove-source-files /tmp/Plugins/plugins/* /app/app/plugins/ # 恢复插件目录
cp -a /plugins/* /app/app/plugins/
# 插件仓库
rsync -av --remove-source-files /tmp/Plugins/plugins/* /app/app/plugins/
# 清理临时目录
rm -rf /tmp/*
echo "插件更新成功"
echo "开始更新资源包..."
if download_and_unzip "https://github.com/jxxghp/MoviePilot-Resources/archive/refs/heads/main.zip" "Resources"; then
echo "资源包下载成功"
# 资源包 # 资源包
cp -a /tmp/Resources/resources/* /app/app/helper/ cp -a /tmp/Resources/resources/* /app/app/helper/
# 前端程序
rm -rf /public
mkdir -p /public
cp -a /tmp/dist/* /public/
# 清理临时目录 # 清理临时目录
rm -rf /tmp/* rm -rf /tmp/*
echo "程序更新成功,前端版本:${frontend_version},后端版本:${1}" echo "资源包更新成功"
else else
echo "前端程序下载失败,继续使用旧的程序来启动..." echo "资源包下载失败,继续使用旧的资源包来启动..."
fi fi
else else
echo "前端最新版本号获取失败,继续启动..." echo "插件下载失败,继续使用旧的插件来启动..."
fi fi
else else
echo "资源包下载失败,继续使用旧的程序来启动..." echo "前端程序下载失败,继续使用旧的程序来启动..."
fi fi
else else
echo "插件下载失败,继续使用旧的程序来启动..." echo "前端最新版本号获取失败,继续启动..."
fi fi
else else
echo "安装依赖失败,请重新拉取镜像" echo "安装依赖失败,请重新拉取镜像"
@ -100,9 +101,9 @@ if [[ "${MOVIEPILOT_AUTO_UPDATE}" = "true" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}"
echo "Release 更新模式" echo "Release 更新模式"
old_version=$(cat /app/version.py) old_version=$(cat /app/version.py)
if [[ "${old_version}" == *APP_VERSION* ]]; then if [[ "${old_version}" == *APP_VERSION* ]]; then
current_version=v$(echo ${old_version} | sed -ne "s/APP_VERSION\s=\s'v\(.*\)'/\1/gp") current_version=v$(echo "${old_version}" | sed -ne "s/APP_VERSION\s=\s'v\(.*\)'/\1/gp")
echo "当前版本号:${current_version}" echo "当前版本号:${current_version}"
new_version=$(curl ${CURL_OPTIONS} "https://api.github.com/repos/jxxghp/MoviePilot/releases/latest" ${CURL_HEADERS} | jq -r .tag_name) new_version=$(curl "${CURL_OPTIONS}" "https://api.github.com/repos/jxxghp/MoviePilot/releases/latest" "${CURL_HEADERS}" | jq -r .tag_name)
if [[ "${new_version}" == *v* ]]; then if [[ "${new_version}" == *v* ]]; then
release_version=${new_version} release_version=${new_version}
echo "最新版本号:${release_version}" echo "最新版本号:${release_version}"