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
# 显示Spug标志
function spug_banner() {
cat << "EOF"
echo " ";
echo " #### ##### # # #### ";
echo "# # # # # # #";
echo " #### # # # # # ";
echo " # ##### # # # ###";
echo "# # # # # # #";
echo " #### # #### #### ";
echo " ";
#### ##### # # ####
# # # # # # #
#### # # # # #
# ##### # # # ###
# # # # # # #
#### # #### ####
EOF
}
# 系统依赖安装
function init_system_lib() {
source /etc/os-release
local packages
case $ID in
centos|fedora|rhel)
echo "开始安装/更新可能缺少的依赖: git mariadb-server mariadb-devel python3-devel gcc openldap-devel redis nginx supervisor python36"
yum install -y epel-release
yum install -y 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 $packages
sed -i 's/ default_server//g' /etc/nginx/nginx.conf
MYSQL_CONF=/etc/my.cnf.d/spug.cnf
SUPERVISOR_CONF=/etc/supervisord.d/spug.ini
REDIS_SRV=redis
SUPERVISOR_SRV=supervisord
MYSQL_CONF="/etc/my.cnf.d/spug.cnf"
SUPERVISOR_CONF="/etc/supervisord.d/spug.ini"
REDIS_SRV="redis"
SUPERVISOR_SRV="supervisord"
;;
debian|ubuntu|devuan)
echo "开始安装/更新可能缺少的依赖: git mariadb-server libmariadbd-dev python3-venv libsasl2-dev libldap2-dev redis-server nginx supervisor"
apt update
apt install -y git mariadb-server libmariadbd-dev python3-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 install -y $packages
rm -f /etc/nginx/sites-enabled/default
MYSQL_CONF=/etc/mysql/conf.d/spug.cnf
SUPERVISOR_CONF=/etc/supervisor/conf.d/spug.conf
REDIS_SRV=redis-server
SUPERVISOR_SRV=supervisor
MYSQL_CONF="/etc/mysql/conf.d/spug.cnf"
SUPERVISOR_CONF="/etc/supervisor/conf.d/spug.conf"
REDIS_SRV="redis-server"
SUPERVISOR_SRV="supervisor"
;;
*)
echo "不支持的操作系统: $ID"
exit 1
;;
esac
}
# 安装Spug
function install_spug() {
echo "开始安装Spug..."
mkdir -p /data
@ -62,22 +61,20 @@ function install_spug() {
python3 -m venv venv
source venv/bin/activate
pip install wheel -i https://pypi.doubanio.com/simple/
pip install gunicorn mysqlclient -i https://pypi.doubanio.com/simple/
pip install -r requirements.txt -i https://pypi.doubanio.com/simple/
pip install -i https://pypi.doubanio.com/simple/ wheel gunicorn mysqlclient -r requirements.txt
}
# 配置Spug
function setup_conf() {
echo "开始配置Spug配置..."
# mysql conf
# MySQL 配置
cat << EOF > $MYSQL_CONF
[mysqld]
bind-address=127.0.0.1
EOF
# spug conf
# Spug 配置
cat << EOF > spug/overrides.py
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']
@ -98,6 +95,7 @@ DATABASES = {
}
EOF
# Supervisor 配置
cat << EOF > $SUPERVISOR_CONF
[program:spug-api]
command = bash /data/spug/spug_api/tools/start-api.sh
@ -130,6 +128,7 @@ stdout_logfile = /data/spug/spug_api/logs/scheduler.log
redirect_stderr = true
EOF
# Nginx 配置
cat << EOF > /etc/nginx/conf.d/spug.conf
server {
listen 80 default_server;
@ -155,35 +154,23 @@ server {
}
EOF
systemctl enable --now mariadb $REDIS_SRV nginx $SUPERVISOR_SRV
systemctl start mariadb
systemctl enable mariadb
mysql -e "create database spug default character set utf8mb4 collate utf8mb4_unicode_ci;"
mysql -e "grant all on spug.* to spug@127.0.0.1 identified by 'spug.dev'"
mysql -e "flush privileges"
mysql -e "CREATE DATABASE spug DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -e "GRANT ALL ON spug.* TO spug@127.0.0.1 IDENTIFIED BY 'spug.dev'; 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
init_system_lib
install_spug
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 "默认管理员账户admin 密码spug.dev"
echo "默认数据库用户spug 密码spug.dev"
echo "管理员账户admin 密码spug.dev"
echo "数据库用户spug 密码spug.dev"