-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2006 from xiagw/merge-to-one-sh
merge build.sh to run.sh
- Loading branch information
Showing
6 changed files
with
171 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ web_src/*.ntvs* | |
web_src/*.njsproj | ||
web_src/*.sln | ||
|
||
showdocdata/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,170 @@ | ||
#!/usr/bin/env bash | ||
|
||
## 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 | ||
# 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/ && | ||
touch $showdoc_dir_old_skip | ||
else | ||
rsync -a $showdoc_html_dir/ $web_dir/ | ||
_docker_build() { | ||
set -xe | ||
rm -rf /app | ||
ln -sf $web_dir /app | ||
## php setting | ||
( | ||
echo "upload_max_filesize=5120M;" | ||
echo "post_max_size=5120M;" | ||
) >>/opt/docker/etc/php/php.ini | ||
( | ||
echo "client_max_body_size 5120m;" | ||
) >/opt/docker/etc/nginx/vhost.common.d/10-general.conf | ||
|
||
## fix nginx warning | ||
sed -i -e '1 s/^/#/' /opt/docker/etc/nginx/vhost.ssl.conf | ||
sed -i -e '/443\ default_server/s//443\ default_server\ ssl/' /opt/docker/etc/nginx/vhost.conf | ||
|
||
## disable service | ||
mv /opt/docker/etc/supervisor.d/cron.conf{,.bak} | ||
mv /opt/docker/etc/supervisor.d/dnsmasq.conf{,.bak} | ||
# mv /opt/docker/etc/supervisor.d/nginx.conf{,.bak} | ||
# mv /opt/docker/etc/supervisor.d/php-fpm.conf{,.bak} | ||
mv /opt/docker/etc/supervisor.d/postfix.conf{,.bak} | ||
mv /opt/docker/etc/supervisor.d/ssh.conf{,.bak} | ||
mv /opt/docker/etc/supervisor.d/syslog.conf{,.bak} | ||
|
||
## mirror in china | ||
if [ "$IN_CHINA" = true ] && [ -f /etc/apk/repositories ]; then | ||
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories | ||
fi | ||
fi | ||
## upgrade (通过 Dockerfile 的环境变量 变更版本) | ||
## upgrade (通过 composer.json "version" 变更版本) | ||
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 [[ "${version_json}" == "$(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..." | ||
## 此处不同步 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 | ||
sed -i -e "/lang:.*zh-cn.*/s//lang: 'zh-cn'/" $web_dir/web/index.html $web_dir/web_src/index.html | ||
fi | ||
apk update | ||
apk add --update --no-cache nodejs npm | ||
|
||
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 | ||
else | ||
# echo "$SHOWDOC_DOCKER_VERSION" >$file_ver | ||
echo "$version_json" >$file_ver | ||
fi | ||
## 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 \ | ||
$web_dir/Sqlite \ | ||
$web_dir/Public/Uploads \ | ||
$web_dir/install \ | ||
$web_dir/server/Application/Runtime | ||
## fix old warn | ||
rm -f package-lock.json | ||
|
||
npm install | ||
} | ||
|
||
_kill() { | ||
echo "receive SIGTERM, kill $pids" | ||
for pid in $pids; do | ||
echo "receive SIGTERM, kill ${pids[*]}" | ||
for pid in "${pids[@]}"; do | ||
kill "$pid" | ||
wait "$pid" | ||
done | ||
} | ||
|
||
## backup sqlite file every day | ||
while [ -f $db_file ]; do | ||
# backup on 20:01 (UTC) every day | ||
if [[ $(date +%H%M) == 2001 ]]; then | ||
\cp $db_file ${db_file}."$(date +%F-%H-%M-%S)".php | ||
## remove old files (15 days ago) | ||
find ${db_file}.* -type f -ctime +15 -print0 | | ||
xargs -t -0 rm -f >/dev/null | ||
sleep 2 | ||
_docker_run() { | ||
pids=() | ||
## 首次启动需要 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/ && | ||
touch $showdoc_dir_old_skip | ||
else | ||
rsync -a $showdoc_dir_html/ $web_dir/ | ||
fi | ||
fi | ||
## upgrade (通过 Dockerfile 的环境变量 变更版本) | ||
## upgrade (通过 composer.json "version" 变更版本) | ||
ver_file=$web_dir/.ver | ||
# ver_file_json=$web_dir/.json.ver | ||
json_file=$showdoc_dir_html/composer.json | ||
version_json=$(grep -o '"version":.*"' $json_file | awk '{print $2}') | ||
version_json="${version_json//\"/}" | ||
if [ -f $ver_file ]; then | ||
# if [[ "$SHOWDOC_DOCKER_VERSION" == "$(cat $ver_file)" ]]; then | ||
if [[ "${version_json}" == "$(cat $ver_file)" ]]; then | ||
echo "Same version, skip upgrade." | ||
else | ||
echo "Backup db file before upgrade..." | ||
\cp -av $db_file ${db_file}."$backup_time".php | ||
echo "Upgrade application files..." | ||
## 此处不同步 db 文件和 upload 文件,自动排除 | ||
rsync -a --exclude='Sqlite/' --exclude='Public/Uploads/' $showdoc_dir_html/ $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" >$ver_file | ||
echo "$version_json" >$ver_file | ||
fi | ||
## 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 {} \; | ||
runtime_dir="$web_dir/server/Application/Runtime" | ||
[[ -d $runtime_dir ]] || mkdir -p $runtime_dir | ||
chown -R 1000:1000 \ | ||
$web_dir/Sqlite \ | ||
$web_dir/Public/Uploads \ | ||
$web_dir/install \ | ||
$runtime_dir | ||
|
||
## backup sqlite file every day | ||
while [ -f $db_file ]; do | ||
# backup on 20:01 (UTC) every day | ||
if [[ $(date +%H%M) == 2001 ]]; then | ||
\cp $db_file ${db_file}."$backup_time".php | ||
## remove old files (15 days ago) | ||
find ${db_file}.* -type f -ctime +15 -print0 | | ||
xargs -t -0 rm -f >/dev/null | ||
sleep 2 | ||
fi | ||
sleep 58 | ||
done & | ||
pids+=("$!") | ||
|
||
( | ||
sleep 3 | ||
cd $showdoc_dir_html/server || exit 1 | ||
php index.php /api/update/dockerUpdateCode | ||
) | ||
( | ||
echo "delay 30s start mock..." | ||
sleep 30 | ||
cd $showdoc_dir/mock/ || exit 1 | ||
npm run start | ||
) & | ||
pids+=("$!") | ||
|
||
supervisord -c /opt/docker/etc/supervisor.conf & | ||
pids+=("$!") | ||
|
||
## 识别中断信号,停止进程 | ||
trap _kill HUP INT QUIT TERM | ||
|
||
wait | ||
} | ||
|
||
main() { | ||
showdoc_dir='/showdoc_data' | ||
showdoc_dir_old='/showdoc_data_old' | ||
showdoc_dir_old_skip='/showdoc_data_old/.skip_old' | ||
showdoc_dir_html="$showdoc_dir/html" | ||
## web site dir | ||
web_dir='/var/www/html' | ||
|
||
db_file=$web_dir/Sqlite/showdoc.db.php | ||
if [[ "${IN_CHINA}" == true ]]; then | ||
backup_time="$(TZ='Asia/Shanghai' date +%F-%H-%M-%S)" | ||
else | ||
backup_time="$(date +%F-%H-%M-%S)" | ||
fi | ||
sleep 58 | ||
done & | ||
pids="$pids $!" | ||
|
||
( | ||
sleep 3 | ||
cd $showdoc_html_dir/server || exit 1 | ||
php index.php /api/update/dockerUpdateCode | ||
) | ||
( | ||
echo "delay 30s start mock..." | ||
sleep 30 | ||
cd $showdoc_dir/mock/ || exit 1 | ||
npm run start | ||
) & | ||
pids="$pids $!" | ||
|
||
supervisord -c /opt/docker/etc/supervisor.conf & | ||
pids="$pids $!" | ||
|
||
## 识别中断信号,停止进程 | ||
trap _kill HUP INT QUIT TERM | ||
|
||
wait | ||
|
||
case $1 in | ||
-b | --build) | ||
_docker_build | ||
;; | ||
*) | ||
_docker_run | ||
;; | ||
esac | ||
} | ||
|
||
main "$@" |