style[install.sh]: Code format optimization

拆分模块,提升代码可读性; 提取了公共变量与依赖列表; 标准化日志文件路径和错误处理。
pull/700/head
Harilou 2024-12-10 17:43:47 +08:00 committed by GitHub
parent e9c161bcbe
commit 1710be0803
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 71 additions and 84 deletions

View File

@ -5,52 +5,51 @@
set -e set -e
# 显示Spug标志
function spug_banner() { function spug_banner() {
cat << "EOF"
echo " "; #### ##### # # ####
echo " #### ##### # # #### "; # # # # # # #
echo "# # # # # # #"; #### # # # # #
echo " #### # # # # # "; # ##### # # # ###
echo " # ##### # # # ###"; # # # # # # #
echo "# # # # # # #"; #### # #### ####
echo " #### # #### #### ";
echo " ";
EOF
} }
# 系统依赖安装
function init_system_lib() { function init_system_lib() {
source /etc/os-release source /etc/os-release
local packages
case $ID in case $ID in
centos|fedora|rhel) centos|fedora|rhel)
echo "开始安装/更新可能缺少的依赖: git mariadb-server mariadb-devel python3-devel gcc openldap-devel redis nginx supervisor python36" packages="git mariadb-server mariadb-devel python3-devel gcc openldap-devel redis nginx supervisor python36"
yum install -y epel-release yum install -y epel-release $packages
yum install -y git mariadb-server mariadb-devel python3-devel gcc openldap-devel redis nginx supervisor python36
sed -i 's/ default_server//g' /etc/nginx/nginx.conf sed -i 's/ default_server//g' /etc/nginx/nginx.conf
MYSQL_CONF=/etc/my.cnf.d/spug.cnf MYSQL_CONF="/etc/my.cnf.d/spug.cnf"
SUPERVISOR_CONF=/etc/supervisord.d/spug.ini SUPERVISOR_CONF="/etc/supervisord.d/spug.ini"
REDIS_SRV=redis REDIS_SRV="redis"
SUPERVISOR_SRV=supervisord SUPERVISOR_SRV="supervisord"
;; ;;
debian|ubuntu|devuan) debian|ubuntu|devuan)
echo "开始安装/更新可能缺少的依赖: git mariadb-server libmariadbd-dev python3-venv libsasl2-dev libldap2-dev redis-server nginx supervisor" packages="git mariadb-server libmariadbd-dev python3-dev python3-venv libsasl2-dev libldap2-dev redis-server nginx supervisor"
apt update apt update && apt install -y $packages
apt install -y git mariadb-server libmariadbd-dev python3-dev python3-venv libsasl2-dev libldap2-dev redis-server nginx supervisor
rm -f /etc/nginx/sites-enabled/default rm -f /etc/nginx/sites-enabled/default
MYSQL_CONF=/etc/mysql/conf.d/spug.cnf MYSQL_CONF="/etc/mysql/conf.d/spug.cnf"
SUPERVISOR_CONF=/etc/supervisor/conf.d/spug.conf SUPERVISOR_CONF="/etc/supervisor/conf.d/spug.conf"
REDIS_SRV=redis-server REDIS_SRV="redis-server"
SUPERVISOR_SRV=supervisor SUPERVISOR_SRV="supervisor"
;; ;;
*) *)
echo "不支持的操作系统: $ID"
exit 1 exit 1
;; ;;
esac esac
} }
# 安装Spug
function install_spug() { function install_spug() {
echo "开始安装Spug..." echo "开始安装Spug..."
mkdir -p /data mkdir -p /data
@ -62,23 +61,21 @@ function install_spug() {
python3 -m venv venv python3 -m venv venv
source venv/bin/activate source venv/bin/activate
pip install wheel -i https://pypi.doubanio.com/simple/ pip install -i https://pypi.doubanio.com/simple/ wheel gunicorn mysqlclient -r requirements.txt
pip install gunicorn mysqlclient -i https://pypi.doubanio.com/simple/
pip install -r requirements.txt -i https://pypi.doubanio.com/simple/
} }
# 配置Spug
function setup_conf() { function setup_conf() {
echo "开始配置Spug配置..." echo "开始配置Spug配置..."
# mysql conf
cat << EOF > $MYSQL_CONF # MySQL 配置
cat << EOF > $MYSQL_CONF
[mysqld] [mysqld]
bind-address=127.0.0.1 bind-address=127.0.0.1
EOF EOF
# spug conf # Spug 配置
cat << EOF > spug/overrides.py cat << EOF > spug/overrides.py
DEBUG = False DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1'] ALLOWED_HOSTS = ['127.0.0.1']
@ -98,7 +95,8 @@ DATABASES = {
} }
EOF EOF
cat << EOF > $SUPERVISOR_CONF # Supervisor 配置
cat << EOF > $SUPERVISOR_CONF
[program:spug-api] [program:spug-api]
command = bash /data/spug/spug_api/tools/start-api.sh command = bash /data/spug/spug_api/tools/start-api.sh
autostart = true autostart = true
@ -130,7 +128,8 @@ stdout_logfile = /data/spug/spug_api/logs/scheduler.log
redirect_stderr = true redirect_stderr = true
EOF EOF
cat << EOF > /etc/nginx/conf.d/spug.conf # Nginx 配置
cat << EOF > /etc/nginx/conf.d/spug.conf
server { server {
listen 80 default_server; listen 80 default_server;
root /data/spug/spug_web/build/; root /data/spug/spug_web/build/;
@ -155,35 +154,23 @@ server {
} }
EOF EOF
systemctl enable --now mariadb $REDIS_SRV nginx $SUPERVISOR_SRV
systemctl start mariadb mysql -e "CREATE DATABASE spug DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
systemctl enable mariadb mysql -e "GRANT ALL ON spug.* TO spug@127.0.0.1 IDENTIFIED BY 'spug.dev'; FLUSH PRIVILEGES;"
mysql -e "create database spug default character set utf8mb4 collate utf8mb4_unicode_ci;" python manage.py initdb
mysql -e "grant all on spug.* to spug@127.0.0.1 identified by 'spug.dev'" python manage.py useradd -u admin -p spug.dev -s -n 管理员
mysql -e "flush privileges"
python manage.py initdb
python manage.py useradd -u admin -p spug.dev -s -n 管理员
systemctl enable nginx
systemctl enable $REDIS_SRV
systemctl enable $SUPERVISOR_SRV
systemctl restart nginx
systemctl start $REDIS_SRV
systemctl restart $SUPERVISOR_SRV
systemctl restart $SUPERVISOR_SRV nginx
} }
spug_banner spug_banner
init_system_lib init_system_lib
install_spug install_spug
setup_conf setup_conf
echo -e "\n\n\033[33m安全警告默认的数据库和Redis服务并不安全请确保其仅监听在127.0.0.1,推荐参考官网文档自行加固安全配置\033[0m" echo -e "\n\033[33m安全警告请根据文档加强数据库和Redis的安全性\033[0m"
echo -e "\033[32m安装成功\033[0m" echo -e "\033[32m安装成功\033[0m"
echo "默认管理员账户admin 密码spug.dev" echo "管理员账户admin 密码spug.dev"
echo "默认数据库用户spug 密码spug.dev" echo "数据库用户spug 密码spug.dev"