core: 网页日志自动刷新并滚动到最底部

This commit is contained in:
bin456789 2023-07-09 00:04:18 +08:00
parent 9efe2552e3
commit 7443b18467
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B

View File

@ -5,15 +5,6 @@
# 命令出错终止运行,将进入到登录界面,防止失联 # 命令出错终止运行,将进入到登录界面,防止失联
set -eE set -eE
# 显示输出到前台
# 似乎script更优雅但 alpine 不带 script 命令
# script -f /dev/tty0
if [ -e /dev/ttyS0 ]; then
exec > >(tee /dev/tty0 /dev/ttyS0 /reinstall.log) 2>&1
else
exec > >(tee /dev/tty0 /reinstall.log) 2>&1
fi
trap 'error line $LINENO return $?' ERR trap 'error line $LINENO return $?' ERR
catch() { catch() {
@ -91,35 +82,71 @@ setup_nginx() {
server { server {
listen 80 default_server; listen 80 default_server;
listen [::]:80 default_server; listen [::]:80 default_server;
location = / { location = / {
root /; root /;
try_files /reinstall.log /reinstall.log; try_files /reinstall.html /reinstall.html;
types { # types {
text/plain log; # text/plain log;
} # }
} }
} }
EOF EOF
# rc-service nginx start # rc-service nginx start
nginx nginx
} }
setup_lighttpd() { setup_lighttpd() {
apk add lighttpd apk add lighttpd
ln -sf /reinstall.log /var/www/localhost/htdocs/index.html ln -sf /reinstall.html /var/www/localhost/htdocs/index.html
rc-service lighttpd start rc-service lighttpd start
} }
# 提取 finalos/extra 到变量
for prefix in finalos extra; do setup_tty_and_log() {
while read -r line; do cat <<EOF >/reinstall.html
if [ -n "$line" ]; then <!DOCTYPE html>
key=$(echo $line | cut -d= -f1) <html lang="en">
value=$(echo $line | cut -d= -f2-)
eval "$key='$value'" <head>
fi <meta http-equiv="refresh" content="2">
done <<EOF </head>
<body>
<script>
window.onload = function() {
// history.scrollRestoration = "manual";
window.scrollTo(0, document.body.scrollHeight);
}
</script>
<pre>
EOF
# 显示输出到前台
# 似乎script更优雅但 alpine 不带 script 命令
# script -f /dev/tty0
if [ -e /dev/ttyS0 ]; then
exec > >(tee -a /dev/tty0 /dev/ttyS0 /reinstall.html) 2>&1
else
exec > >(tee -a /dev/tty0 /reinstall.html) 2>&1
fi
}
extract_env_from_cmdline() {
# 提取 finalos/extra 到变量
for prefix in finalos extra; do
while read -r line; do
if [ -n "$line" ]; then
key=$(echo $line | cut -d= -f1)
value=$(echo $line | cut -d= -f2-)
eval "$key='$value'"
fi
done <<EOF
$(xargs -n1 </proc/cmdline | grep "^$prefix" | sed "s/^$prefix\.//") $(xargs -n1 </proc/cmdline | grep "^$prefix" | sed "s/^$prefix\.//")
EOF EOF
done done
}
setup_tty_and_log
extract_env_from_cmdline
# 安装 nginx目标系统是 alpine 除外 # 安装 nginx目标系统是 alpine 除外
# shellcheck disable=SC2154 # shellcheck disable=SC2154