feat: better logging output

This commit is contained in:
DDSRem
2024-07-23 14:39:15 +08:00
parent 7459938e92
commit 766640a0a0
+56 -36
View File
@@ -3,12 +3,29 @@
# shellcheck disable=SC2086 # shellcheck disable=SC2086
# shellcheck disable=SC2144 # shellcheck disable=SC2144
Green="\033[32m"
Red="\033[31m"
Yellow='\033[33m'
Font="\033[0m"
INFO="[${Green}INFO${Font}]"
ERROR="[${Red}ERROR${Font}]"
WARN="[${Yellow}WARN${Font}]"
function INFO() {
echo -e "${INFO} ${1}"
}
function ERROR() {
echo -e "${ERROR} ${1}"
}
function WARN() {
echo -e "${WARN} ${1}"
}
# 下载及解压 # 下载及解压
download_and_unzip() { function download_and_unzip() {
url="$1" url="$1"
target_dir="$2" target_dir="$2"
echo "正在下载 ${url}..." INFO "正在下载 ${url}..."
if curl ${CURL_OPTIONS} "${url}" ${CURL_HEADERS} | busybox unzip -d /tmp -; then if curl ${CURL_OPTIONS} "${url}" ${CURL_HEADERS} | busybox unzip -d /tmp - > /dev/null; then
if [ -e /tmp/MoviePilot-* ]; then if [ -e /tmp/MoviePilot-* ]; then
mv /tmp/MoviePilot-* /tmp/"${target_dir}" mv /tmp/MoviePilot-* /tmp/"${target_dir}"
fi fi
@@ -18,25 +35,28 @@ download_and_unzip() {
} }
# 下载程序资源,$1: 后端版本路径 # 下载程序资源,$1: 后端版本路径
install_backend_and_download_resources() { function install_backend_and_download_resources() {
# 清理临时目录,上次安装失败可能有残留 # 清理临时目录,上次安装失败可能有残留
rm -rf /tmp/* rm -rf /tmp/*
if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot/archive/refs/${1}" "App"; then if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot/archive/refs/${1}" "App"; then
echo "后端程序下载成功" INFO "后端程序下载成功"
pip install ${PIP_OPTIONS} --upgrade pip INFO "依赖安装中..."
if pip install ${PIP_OPTIONS} -r /tmp/App/requirements.txt; then pip install ${PIP_OPTIONS} --upgrade pip > /dev/null
echo "安装依赖成功" if pip install ${PIP_OPTIONS} -r /tmp/App/requirements.txt > /dev/null; then
INFO "安装依赖成功"
frontend_version=$(curl ${CURL_OPTIONS} "https://api.github.com/repos/jxxghp/MoviePilot-Frontend/releases/latest" ${CURL_HEADERS} | jq -r .tag_name) frontend_version=$(curl ${CURL_OPTIONS} "https://api.github.com/repos/jxxghp/MoviePilot-Frontend/releases/latest" ${CURL_HEADERS} | jq -r .tag_name)
if [[ "${frontend_version}" == *v* ]]; then if [[ "${frontend_version}" == *v* ]]; then
if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot-Frontend/releases/download/${frontend_version}/dist.zip" "dist"; then if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot-Frontend/releases/download/${frontend_version}/dist.zip" "dist"; then
echo "前端程序下载成功" INFO "前端程序下载成功"
# 提前备份插件目录 # 提前备份插件目录
INFO "备份插件目录中..."
rm -rf /plugins rm -rf /plugins
mkdir -p /plugins mkdir -p /plugins
cp -a /app/app/plugins/* /plugins/ cp -a /app/app/plugins/* /plugins/
# 不备份__init__.py # 不备份__init__.py
rm -f /plugins/__init__.py rm -f /plugins/__init__.py
# 提前备份旧 resources 资源 # 提前备份旧 resources 资源
INFO "备份 resources 资源中..."
rm -rf /resources_bakcup rm -rf /resources_bakcup
mkdir /resources_bakcup mkdir /resources_bakcup
cp -a /app/app/helper/* /resources_bakcup cp -a /app/app/helper/* /resources_bakcup
@@ -51,48 +71,48 @@ install_backend_and_download_resources() {
cp -a /tmp/dist/* /public/ cp -a /tmp/dist/* /public/
# 清理临时目录 # 清理临时目录
rm -rf /tmp/* rm -rf /tmp/*
echo "程序部分更新成功,前端版本:${frontend_version},后端版本:${1}"s INFO "程序部分更新成功,前端版本:${frontend_version},后端版本:${1}"
echo "开始更新插件..." INFO "开始更新插件..."
if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot-Plugins/archive/refs/heads/main.zip" "Plugins"; then if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot-Plugins/archive/refs/heads/main.zip" "Plugins"; then
echo "插件下载成功" INFO "插件下载成功"
# 恢复插件目录 # 恢复插件目录
cp -a /plugins/* /app/app/plugins/ cp -a /plugins/* /app/app/plugins/
# 插件仓库 # 插件仓库
rsync -av --remove-source-files /tmp/Plugins/plugins/* /app/app/plugins/ rsync -av --remove-source-files /tmp/Plugins/plugins/* /app/app/plugins/ > /dev/null
# 提前安装插件依赖 # 提前安装插件依赖
find /app/app/plugins -name requirements.txt -exec pip install ${PIP_OPTIONS} -r {} \; find /app/app/plugins -name requirements.txt -exec pip install ${PIP_OPTIONS} -r {} \; > /dev/null
# 清理临时目录 # 清理临时目录
rm -rf /tmp/* rm -rf /tmp/*
echo "插件更新成功" INFO "插件更新成功"
echo "开始更新资源包..." INFO "开始更新资源包..."
if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot-Resources/archive/refs/heads/main.zip" "Resources"; then if download_and_unzip "${GITHUB_PROXY}https://github.com/jxxghp/MoviePilot-Resources/archive/refs/heads/main.zip" "Resources"; then
echo "资源包下载成功" INFO "资源包下载成功"
# 资源包 # 资源包
cp -a /tmp/Resources/resources/* /app/app/helper/ cp -a /tmp/Resources/resources/* /app/app/helper/
# 清理临时目录 # 清理临时目录
rm -rf /tmp/* rm -rf /tmp/*
echo "资源包更新成功" INFO "资源包更新成功"
else else
cp -a /resources_bakcup/* /app/app/helper/ cp -a /resources_bakcup/* /app/app/helper/
rm -rf /resources_bakcup rm -rf /resources_bakcup
echo "资源包下载失败,继续使用旧的资源包来启动..." WARN "资源包下载失败,继续使用旧的资源包来启动..."
fi fi
else else
cp -a /plugins/* /app/app/plugins/ cp -a /plugins/* /app/app/plugins/
rm -rf /plugins rm -rf /plugins
echo "插件下载失败,继续使用旧的插件来启动..." WARN "插件下载失败,继续使用旧的插件来启动..."
fi fi
else else
echo "前端程序下载失败,继续使用旧的程序来启动..." WARN "前端程序下载失败,继续使用旧的程序来启动..."
fi fi
else else
echo "前端最新版本号获取失败,继续启动..." WARN "前端最新版本号获取失败,继续启动..."
fi fi
else else
echo "安装依赖失败,请重新拉取镜像" ERROR "安装依赖失败,请重新拉取镜像"
fi fi
else else
echo "后端程序下载失败,继续使用旧的程序来启动..." WARN "后端程序下载失败,继续使用旧的程序来启动..."
fi fi
} }
@@ -101,10 +121,10 @@ if [[ "${MOVIEPILOT_AUTO_UPDATE}" = "true" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}"
CURL_OPTIONS="-sL -x ${PROXY_HOST}" CURL_OPTIONS="-sL -x ${PROXY_HOST}"
PIP_OPTIONS="--proxy=${PROXY_HOST}" PIP_OPTIONS="--proxy=${PROXY_HOST}"
GITHUB_PROXY="" GITHUB_PROXY=""
echo "使用代理更新程序" INFO "使用代理更新程序"
else else
CURL_OPTIONS="-sL" CURL_OPTIONS="-sL"
echo "不使用代理更新程序" INFO "不使用代理更新程序"
fi fi
if [ -n "${GITHUB_TOKEN}" ]; then if [ -n "${GITHUB_TOKEN}" ]; then
CURL_HEADERS="--oauth2-bearer ${GITHUB_TOKEN}" CURL_HEADERS="--oauth2-bearer ${GITHUB_TOKEN}"
@@ -112,33 +132,33 @@ if [[ "${MOVIEPILOT_AUTO_UPDATE}" = "true" ]] || [[ "${MOVIEPILOT_AUTO_UPDATE}"
CURL_HEADERS="" CURL_HEADERS=""
fi fi
if [ "${MOVIEPILOT_AUTO_UPDATE}" = "dev" ]; then if [ "${MOVIEPILOT_AUTO_UPDATE}" = "dev" ]; then
echo "Dev 更新模式" INFO "Dev 更新模式"
install_backend_and_download_resources "heads/main.zip" install_backend_and_download_resources "heads/main.zip"
else else
echo "Release 更新模式" INFO "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}" INFO "当前版本号:${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}" INFO "最新版本号:${release_version}"
if [ "${current_version}" != "${release_version}" ]; then if [ "${current_version}" != "${release_version}" ]; then
echo "发现新版本,开始自动升级..." INFO "发现新版本,开始自动升级..."
install_backend_and_download_resources "tags/${release_version}.zip" install_backend_and_download_resources "tags/${release_version}.zip"
else else
echo "未发现新版本,跳过更新步骤..." INFO "未发现新版本,跳过更新步骤..."
fi fi
else else
echo "最新版本号获取失败,继续启动..." WARN "最新版本号获取失败,继续启动..."
fi fi
else else
echo "当前版本号获取失败,继续启动..." WARN "当前版本号获取失败,继续启动..."
fi fi
fi fi
elif [[ "${MOVIEPILOT_AUTO_UPDATE}" = "false" ]]; then elif [[ "${MOVIEPILOT_AUTO_UPDATE}" = "false" ]]; then
echo "程序自动升级已关闭,如需自动升级请在创建容器时设置环境变量:MOVIEPILOT_AUTO_UPDATE=release" INFO "程序自动升级已关闭,如需自动升级请在创建容器时设置环境变量:MOVIEPILOT_AUTO_UPDATE=release"
else else
echo "MOVIEPILOT_AUTO_UPDATE 变量设置错误" INFO "MOVIEPILOT_AUTO_UPDATE 变量设置错误"
fi fi