diff --git a/.github/workflows/xiaoya_cron.yml b/.github/workflows/xiaoya_cron.yml new file mode 100644 index 000000000..d4c1401f6 --- /dev/null +++ b/.github/workflows/xiaoya_cron.yml @@ -0,0 +1,65 @@ +name: xiaoya cron + +on: + workflow_dispatch: + push: + branches: + - master + paths: + - "cron/**" + - ".github/workflows/xiaoya_cron.yml" + +jobs: + build: + runs-on: ubuntu-latest + name: Build Docker Image + steps: + - + name: Checkout + uses: actions/checkout@v4 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ secrets.DOCKER_USERNAME }}/xiaoya-cron + tags: | + type=raw,value=latest + + - + name: Set Up QEMU + uses: docker/setup-qemu-action@v3 + + - + name: Set Up Buildx + uses: docker/setup-buildx-action@v3 + + - + name: Login DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - + name: Build Image + uses: docker/build-push-action@v5 + with: + context: ./cron + file: cron/Dockerfile + platforms: | + linux/amd64 + linux/arm64/v8 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - + name: Docker Hub Description + uses: peter-evans/dockerhub-description@v4 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + repository: ${{ secrets.DOCKER_USERNAME }}/xiaoya-cron + short-description: 小雅Alist的相关周边 + readme-filepath: ./README.md \ No newline at end of file diff --git a/cron/Dockerfile b/cron/Dockerfile new file mode 100644 index 000000000..026e29d42 --- /dev/null +++ b/cron/Dockerfile @@ -0,0 +1,17 @@ +FROM alpine:3.19 + +RUN set -ex && \ + apk add --no-cache \ + jq \ + bash \ + curl \ + docker \ + s6-overlay \ + sqlite && \ + rm -rf \ + /var/cache/apk/* \ + /tmp/* + +COPY --chmod=755 ./rootfs / + +ENTRYPOINT [ "/init" ] diff --git a/cron/rootfs/app/sync_emby_config.sh b/cron/rootfs/app/sync_emby_config.sh new file mode 100644 index 000000000..bb1d1c398 --- /dev/null +++ b/cron/rootfs/app/sync_emby_config.sh @@ -0,0 +1,92 @@ +#!/usr/bin/bash + +if [ "$1" ]; then + EMBY_NAME=$1 +else + EMBY_NAME=emby +fi + +if [ "$2" ]; then + RESILIO_NAME=$2 +else + RESILIO_NAME=resilio +fi + +EMBY_URL=$(cat /etc/xiaoya/emby_server.txt) + +media_lib=/media +if [ ! -d "$media_lib/config_sync" ]; then + mkdir "$media_lib/config_sync" +fi + +echo "Emby 和 Resilio关闭中 ...." +docker stop "${EMBY_NAME}" +docker stop "${RESILIO_NAME}" + +echo "检查同步数据库完整性..." +sleep 4 +if sqlite3 $media_lib/config_sync/data/library.db ".tables" | grep Chapters3 > /dev/null ; then + curl -s "${EMBY_URL}/Users?api_key=e825ed6f7f8f44ffa0563cddaddce14d" > /tmp/emby.response + echo -e "\033[32m同步数据库数据完整\033[0m" + sqlite3 $media_lib/config/data/library.db ".dump UserDatas" > /tmp/emby_user.sql + sqlite3 $media_lib/config/data/library.db ".dump ItemExtradata" > /tmp/emby_library_mediaconfig.sql + rm "$media_lib/config/data/library.db*" + cp "$media_lib/config_sync/data/library.db*" "$media_lib/config/data/" + sqlite3 $media_lib/config/data/library.db "DROP TABLE IF EXISTS UserDatas;" + sqlite3 $media_lib/config/data/library.db ".read /tmp/emby_user.sql" + sqlite3 $media_lib/config/data/library.db "DROP TABLE IF EXISTS ItemExtradata;" + sqlite3 $media_lib/config/data/library.db ".read /tmp/emby_library_mediaconfig.sql" + echo "保存用户信息完成" + cp -rf "$media_lib/config_sync/cache/*" "$media_lib/config/cache/" + cp -rf "$media_lib/config_sync/metadata/*" "$media_lib/config/metadata/" + chmod -R 777 \ + "$media_lib/config/data" \ + "$media_lib/config/cache" \ + "$media_lib/config/metadata" + echo "复制 config_sync 至 config 完成" + echo "Emby 和 Resilio 重启中 ...." + docker start "${EMBY_NAME}" + docker start "${RESILIO_NAME}" +else + echo -e "\033[35m同步数据库不完整,跳过复制...\033[0m" + echo "Emby 和 Resilio 重启中 ...." + docker start "${EMBY_NAME}" + docker start "${RESILIO_NAME}" + exit 0 +fi + +start_time=$(date +%s) +CONTAINER_NAME=${EMBY_NAME} +TARGET_LOG_LINE_SUCCESS="All entry points have started" +while true; do + line=$(docker logs "$CONTAINER_NAME" 2>&1| tail -n 10) + echo "$line" + if [[ "$line" == *"$TARGET_LOG_LINE_SUCCESS"* ]]; then + break + fi + current_time=$(date +%s) + elapsed_time=$((current_time - start_time)) + if [ "$elapsed_time" -gt 300 ]; then + echo "Emby未正常启动超时 5分钟,终止执行更新用户Policy" + exit + fi + sleep 3 +done + +USER_COUNT=$(jq '.[].Name' /tmp/emby.response |wc -l) +for ((i = 0; i < USER_COUNT; i++)) +do + if [[ "$USER_COUNT" -gt 9 ]]; then + exit + fi + read -r id <<< "$(jq -r ".[$i].Id" /tmp/emby.response)" + read -r name <<< "$(jq -r ".[$i].Name" /tmp/emby.response)" + read -r policy <<< "$(jq -r ".[$i].Policy | to_entries | from_entries | tojson" /tmp/emby.response)" + USER_URL_2="${EMBY_URL}/Users/$id/Policy?api_key=e825ed6f7f8f44ffa0563cddaddce14d" + status_code=$(curl -s -w "%{http_code}" -H "Content-Type: application/json" -X POST -d "$policy" "$USER_URL_2") + if [ "$status_code" == "204" ]; then + echo "成功更新 $name 用户Policy" + else + echo "返回错误代码 $status_code" + fi +done diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/run b/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/run new file mode 100644 index 000000000..6414bac79 --- /dev/null +++ b/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/run @@ -0,0 +1,16 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +cd /app || exit + +crontab -r + +echo "${CRON} bash /app/sync_emby_config.sh ${EMBY_NAME} ${RESILIO_NAME} >> /config/cron.log 2>&1" > /app/cronjob.list +echo "bash /app/sync_emby_config.sh ${EMBY_NAME} ${RESILIO_NAME}" > /app/command.sh + +if crontab /app/cronjob.list; then + echo "定时任务添加成功" +else + echo "定时任务添加失败" + exit 1 +fi \ No newline at end of file diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/type b/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/type new file mode 100644 index 000000000..3d92b15f2 --- /dev/null +++ b/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/type @@ -0,0 +1 @@ +oneshot \ No newline at end of file diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/up b/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/up new file mode 100644 index 000000000..4567b3925 --- /dev/null +++ b/cron/rootfs/etc/s6-overlay/s6-rc.d/init-config/up @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/init-config/run \ No newline at end of file diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/dependencies.d/init-config b/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/dependencies.d/init-config new file mode 100644 index 000000000..e69de29bb diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/run b/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/run new file mode 100644 index 000000000..508e300d0 --- /dev/null +++ b/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/run @@ -0,0 +1,5 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +exec \ + s6-setuidgid root crond -f \ No newline at end of file diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/type b/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/type new file mode 100644 index 000000000..1780f9f44 --- /dev/null +++ b/cron/rootfs/etc/s6-overlay/s6-rc.d/svc-crond/type @@ -0,0 +1 @@ +longrun \ No newline at end of file diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/init-config b/cron/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/init-config new file mode 100644 index 000000000..e69de29bb diff --git a/cron/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/svc-crond b/cron/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/svc-crond new file mode 100644 index 000000000..e69de29bb