From 02bd92ff18030aba55ccc59225c96896c02a665c Mon Sep 17 00:00:00 2001 From: xiagw Date: Sat, 15 Jul 2023 19:50:15 +0800 Subject: [PATCH 1/9] auto upgrade with docker --- docker.build.sh | 5 +++-- docker.run.sh | 30 +++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/docker.build.sh b/docker.build.sh index ff2906f44..560916b07 100644 --- a/docker.build.sh +++ b/docker.build.sh @@ -31,8 +31,9 @@ fi apk update apk add --update --no-cache nodejs npm -mv /showdoc_data/html/mock /showdoc_data/ -cd /showdoc_data/mock || exit 1 +showdoc_dir='/showdoc_data' +mv $showdoc_dir/html/mock $showdoc_dir/ +cd $showdoc_dir/mock || exit 1 if [ "$IN_CHINA" = true ]; then npm config set registry https://registry.npmmirror.com/ fi diff --git a/docker.run.sh b/docker.run.sh index 8b6d01049..b0bba0d45 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -3,11 +3,28 @@ set -e ## web site dir -if [ ! -f "/var/www/html/index.php" ]; then - \cp -fr /showdoc_data/html/* /var/www/html/ +showdoc_dir='/showdoc_data' +showdoc_html_dir="$showdoc_dir/html" +web_dir='/var/www/html' +file_ver=$web_dir/.ver +if [ -f "$web_dir/index.php" ]; then + echo "Found $web_dir/index.php, skip copy." +else + echo "Not found $web_dir/index.php, copy..." + rsync -a $showdoc_html_dir/ $web_dir/ +fi +if [ -f $file_ver ]; then + if [[ "$SHOWDOC_DOCKER_VERSION" == "$(cat $file_ver)" ]]; then + echo "Same version, skip upgrade." + else + echo "Upgrade application files..." + rsync -a --exclude='Sqlite/' --exclude='Public/Uploads/' $showdoc_html_dir/ $web_dir/ + fi +else + echo "$SHOWDOC_DOCKER_VERSION" >$file_ver fi ## set file mode -chown -R 1000:1000 /var/www/html/Sqlite /var/www/html/Public/Uploads +chown -R 1000:1000 $web_dir/Sqlite $web_dir/Public/Uploads _kill() { echo "receive SIGTERM, kill $pids" @@ -18,7 +35,7 @@ _kill() { } ## backup sqlite file every day -db_file=/var/www/html/Sqlite/showdoc.db.php +db_file=$web_dir/Sqlite/showdoc.db.php while [ -f $db_file ]; do # backup on 20:01 (UTC) every day if [[ $(date +%H%M) == 2001 ]]; then @@ -33,13 +50,13 @@ pids="$pids $!" ( sleep 3 - cd /showdoc_data/html/server + cd $showdoc_html_dir/server php index.php /api/update/dockerUpdateCode ) ( echo "delay 30s start mock..." sleep 30 - cd /showdoc_data/mock/ + cd $showdoc_dir/mock/ npm run start ) & pids="$pids $!" @@ -51,4 +68,3 @@ pids="$pids $!" trap _kill HUP INT QUIT TERM wait - From 8c0d4df41b42e8168b6aeba5c6fdd8d4fb002147 Mon Sep 17 00:00:00 2001 From: xiagw Date: Sun, 16 Jul 2023 10:25:18 +0800 Subject: [PATCH 2/9] fix file mode --- docker.run.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docker.run.sh b/docker.run.sh index b0bba0d45..63e29d313 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -24,7 +24,13 @@ else echo "$SHOWDOC_DOCKER_VERSION" >$file_ver fi ## set file mode -chown -R 1000:1000 $web_dir/Sqlite $web_dir/Public/Uploads +[[ -d $web_dir/server/Application/Runtime ]] || + mkdir -p $web_dir/server/Application/Runtime +chown -R 1000:1000 \ + $web_dir/Sqlite \ + $web_dir/Public/Uploads \ + $web_dir/install \ + $web_dir/server/Application/Runtime _kill() { echo "receive SIGTERM, kill $pids" From 4d7fda87464518550f6679902a0b833338e0a3c7 Mon Sep 17 00:00:00 2001 From: xiagw Date: Sun, 16 Jul 2023 11:18:47 +0800 Subject: [PATCH 3/9] fix file permission --- docker.run.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docker.run.sh b/docker.run.sh index 63e29d313..b843666c0 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -13,6 +13,7 @@ else echo "Not found $web_dir/index.php, copy..." rsync -a $showdoc_html_dir/ $web_dir/ fi +## upgrade if [ -f $file_ver ]; then if [[ "$SHOWDOC_DOCKER_VERSION" == "$(cat $file_ver)" ]]; then echo "Same version, skip upgrade." @@ -23,7 +24,10 @@ if [ -f $file_ver ]; then else echo "$SHOWDOC_DOCKER_VERSION" >$file_ver fi -## set file mode +## fix file permission +# find $web_dir -type f -exec chmod 644 {} \; +# find $web_dir -type d -exec chmod 755 {} \; +# find $web_dir -type f -iname '*.sh' -exec chmod 755 {} \; [[ -d $web_dir/server/Application/Runtime ]] || mkdir -p $web_dir/server/Application/Runtime chown -R 1000:1000 \ From 8babd6b0afcea7cd8929386432926b0af017dc27 Mon Sep 17 00:00:00 2001 From: xiagw Date: Mon, 17 Jul 2023 10:06:33 +0800 Subject: [PATCH 4/9] add docker-coompose.yml --- docker-compose.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..fb4997a72 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3' + +networks: + showdocnet: + +services: + showdoc: + image: star7th/showdoc + hostname: showdoc + ports: + - 4999:80 + volumes: + - ./showdocdata/html:/var/www/html + restart: always + tty: true + networks: + - showdocnet \ No newline at end of file From 2749d1b933b3ab06b919611b9ad58580ad569547 Mon Sep 17 00:00:00 2001 From: xiagw Date: Mon, 17 Jul 2023 10:49:02 +0800 Subject: [PATCH 5/9] docker compose with build image --- docker-compose.yml | 12 +++++++++++- docker.run.sh | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index fb4997a72..048c53ddc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,17 @@ networks: services: showdoc: - image: star7th/showdoc + ## 官方 image ################################ + # image: star7th/showdoc + ## 官方 image ################################ + ## 源码 build image ################################ + build: + context: ./ + args: + - IN_CHINA=false + ## 国内构建镜像, docker compose build --build-arg IN_CHINA=true + ## 启动, docker compose up -d + ## 源码 build image ################################ hostname: showdoc ports: - 4999:80 diff --git a/docker.run.sh b/docker.run.sh index b843666c0..d81a4deae 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -13,7 +13,7 @@ else echo "Not found $web_dir/index.php, copy..." rsync -a $showdoc_html_dir/ $web_dir/ fi -## upgrade +## upgrade (通过 Dockerfile 的环境变量 变更版本) if [ -f $file_ver ]; then if [[ "$SHOWDOC_DOCKER_VERSION" == "$(cat $file_ver)" ]]; then echo "Same version, skip upgrade." From 2439ff787e40a575acf8315cb3a28b6c9f6c9671 Mon Sep 17 00:00:00 2001 From: xiagw Date: Mon, 17 Jul 2023 12:42:35 +0800 Subject: [PATCH 6/9] fix revert lang if lang=en --- docker.run.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docker.run.sh b/docker.run.sh index d81a4deae..20e4daaa7 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -7,6 +7,9 @@ showdoc_dir='/showdoc_data' showdoc_html_dir="$showdoc_dir/html" web_dir='/var/www/html' file_ver=$web_dir/.ver + +db_file=$web_dir/Sqlite/showdoc.db.php + if [ -f "$web_dir/index.php" ]; then echo "Found $web_dir/index.php, skip copy." else @@ -18,8 +21,14 @@ if [ -f $file_ver ]; then if [[ "$SHOWDOC_DOCKER_VERSION" == "$(cat $file_ver)" ]]; then echo "Same version, skip upgrade." else + echo "Backup db file before upgrade..." + \cp -av $db_file ${db_file}."$(date +%F-%H-%M-%S)".php echo "Upgrade application files..." rsync -a --exclude='Sqlite/' --exclude='Public/Uploads/' $showdoc_html_dir/ $web_dir/ + ## revert lang if lang=en + if grep -q 'lang:.*en' $web_dir/web/index.html; then + sed -i -e "/lang:.*zh-cn.*/s//lang: 'zh-cn'/" $web_dir/web/index.html $web_dir/web_src/index.html + fi fi else echo "$SHOWDOC_DOCKER_VERSION" >$file_ver @@ -45,7 +54,6 @@ _kill() { } ## backup sqlite file every day -db_file=$web_dir/Sqlite/showdoc.db.php while [ -f $db_file ]; do # backup on 20:01 (UTC) every day if [[ $(date +%H%M) == 2001 ]]; then From 63aaebad729f8ac1adf276a1434a2d1ff627f3ff Mon Sep 17 00:00:00 2001 From: xiagw Date: Mon, 17 Jul 2023 12:44:22 +0800 Subject: [PATCH 7/9] add comment for rsync exclude --- docker.run.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/docker.run.sh b/docker.run.sh index 20e4daaa7..35356ba05 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -24,6 +24,7 @@ if [ -f $file_ver ]; then echo "Backup db file before upgrade..." \cp -av $db_file ${db_file}."$(date +%F-%H-%M-%S)".php echo "Upgrade application files..." + ## 此处不同步 db 文件和 upload 文件,自动排除 rsync -a --exclude='Sqlite/' --exclude='Public/Uploads/' $showdoc_html_dir/ $web_dir/ ## revert lang if lang=en if grep -q 'lang:.*en' $web_dir/web/index.html; then From 21058b0f3ddecb7f52a01b79b3679956b882dd07 Mon Sep 17 00:00:00 2001 From: xiagw Date: Mon, 17 Jul 2023 12:57:42 +0800 Subject: [PATCH 8/9] fix old dir of showdoc_data --- docker-compose.yml | 2 ++ docker.run.sh | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 048c53ddc..fa8f4f413 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,6 +20,8 @@ services: ports: - 4999:80 volumes: + ## 兼容历史版本文件 + - /showdoc_data:/showdoc_data_old - ./showdocdata/html:/var/www/html restart: always tty: true diff --git a/docker.run.sh b/docker.run.sh index 35356ba05..1e18f40aa 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -4,6 +4,8 @@ set -e ## web site dir showdoc_dir='/showdoc_data' +showdoc_dir_old='/showdoc_data_old' +showdoc_dir_old_skip='/showdoc_data_old/.skip_old' showdoc_html_dir="$showdoc_dir/html" web_dir='/var/www/html' file_ver=$web_dir/.ver @@ -14,7 +16,14 @@ if [ -f "$web_dir/index.php" ]; then echo "Found $web_dir/index.php, skip copy." else echo "Not found $web_dir/index.php, copy..." - rsync -a $showdoc_html_dir/ $web_dir/ + ## 兼容历史版本 + if [[ -f $showdoc_dir_old/html/index.php && ! -f $showdoc_dir_old_skip ]]; then + echo "Found old version of \"showdoc_data\", copy..." + rsync -a $showdoc_dir_old/html/ $web_dir/ && + touch $showdoc_dir_old_skip + else + rsync -a $showdoc_html_dir/ $web_dir/ + fi fi ## upgrade (通过 Dockerfile 的环境变量 变更版本) if [ -f $file_ver ]; then From 3fcf9caff6bff9a96586f087d9aa16a6a676c5fc Mon Sep 17 00:00:00 2001 From: xiagw Date: Mon, 17 Jul 2023 13:48:03 +0800 Subject: [PATCH 9/9] get ver from composer.json --- docker.run.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docker.run.sh b/docker.run.sh index 1e18f40aa..61479639d 100644 --- a/docker.run.sh +++ b/docker.run.sh @@ -9,14 +9,17 @@ showdoc_dir_old_skip='/showdoc_data_old/.skip_old' showdoc_html_dir="$showdoc_dir/html" web_dir='/var/www/html' file_ver=$web_dir/.ver +# file_ver_json=$web_dir/.json.ver +file_json=$showdoc_html_dir/composer.json db_file=$web_dir/Sqlite/showdoc.db.php +## 首次启动需要 copy to /var/www/html if [ -f "$web_dir/index.php" ]; then echo "Found $web_dir/index.php, skip copy." else echo "Not found $web_dir/index.php, copy..." - ## 兼容历史版本 + ## 兼容历史版本 宿主机 /showdoc_data if [[ -f $showdoc_dir_old/html/index.php && ! -f $showdoc_dir_old_skip ]]; then echo "Found old version of \"showdoc_data\", copy..." rsync -a $showdoc_dir_old/html/ $web_dir/ && @@ -26,8 +29,11 @@ else fi fi ## upgrade (通过 Dockerfile 的环境变量 变更版本) +version_json=$(grep -o '"version":.*"' $file_json | awk '{print $2}') +version_json="${version_json//\"/}" if [ -f $file_ver ]; then - if [[ "$SHOWDOC_DOCKER_VERSION" == "$(cat $file_ver)" ]]; then + # if [[ "$SHOWDOC_DOCKER_VERSION" == "$(cat $file_ver)" ]]; then + if [[ "${version_json}" == "$(cat $file_ver)" ]]; then echo "Same version, skip upgrade." else echo "Backup db file before upgrade..." @@ -41,7 +47,8 @@ if [ -f $file_ver ]; then fi fi else - echo "$SHOWDOC_DOCKER_VERSION" >$file_ver + # echo "$SHOWDOC_DOCKER_VERSION" >$file_ver + echo "$version_json" >$file_ver fi ## fix file permission # find $web_dir -type f -exec chmod 644 {} \;