代码拉取完成,页面将自动刷新
#!/bin/bash
#
# 要求:
# CentOS 7+
# Python 3.6+ (系统自带即可)
#
# 安装目录: /opt/idcops/
# 默认端口号: 18113 (gunicorn),参数:SrvPort
# idcops 版本: develop 或 master,参数:VERSION
WorkDir=/opt
[ -d ${WorkDir} ]||mkdir -p ${WorkDir}
VERSION=master
SrvAddr=0.0.0.0
SrvPort=18113
# gunicorn logfile and pid file
ProjDir=${WorkDir}/idcops
LogFile=${ProjDir}/logs/idcops.log
PidFile=${ProjDir}/run/idcops.pid
# Install system dependent packages
OS=$(cat /etc/os-release |grep -w '^ID'|awk -F= '{print $2}'|sed 's/\"//g')
case $OS in
debian|ubuntu)
apt update
apt install -y gcc python3 python3-dev python3-venv libjpeg-dev openssl git default-libmysqlclient-dev pkg-config redis-server
;;
centos|fedora|rhel)
yum install -y gcc python3-devel openssl git
;;
alpine)
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev \
tiff-dev tk-dev tcl-dev harfbuzz-dev fribidi-dev jpeg g++ openssl \
gcc python3 python3-dev git
;;
*)
echo "unknow os ${OS}, exit!"
exit 1
;;
esac
# 检查 Python 和 Python 版本
PYTHON="${PYTHON:-python3}"
which $PYTHON
if [ $? -ne 0 ];then
echo "Need install python3 version."
exit 1
else
# Check Python version
PY_VERSION=$(${PYTHON} -V 2>&1 | awk '{print $NF}' | awk -F'.' '{print $1, $2}')
read -r major minor <<<$(echo $PY_VERSION)
if [ ${major} -lt 3 ] || [ ${minor} -gt 10 ];then
echo "Python version unsupported, require python version 3.6 <= 3.10"
exit 1
fi
fi
# 下载项目放到 /opt/ 目录下,最终项目目录为: /opt/idcops/
cd ${WorkDir}
git clone -b ${VERSION} https://gitee.com/decbe/idcops.git
cd ${ProjDir}
# Check install.lock file exists
if [ -f 'install.lock' ];then
echo "install.lock is already exists."
exit 1
fi
# Check db.sqlite3 file exists
if [ -f 'db.sqlite3' ];then
COMMAND="\cp -raf db.sqlite3 db.sqlite3.save-$(date +'%F')"
echo "db.sqlite3 is already exists, will backup to db.sqlite3.save-$(date +'%F') ."
eval $COMMAND
fi
VIRTUALENV="${ProjDir}/venv"
# Remove the existing virtual environment (if any)
if [ -d "$VIRTUALENV" ]; then
COMMAND="\rm -rf ${VIRTUALENV} db.sqlite3"
echo "Removing old virtual environment..."
eval $COMMAND
else
WARN_MISSING_VENV=1
fi
# Create a new virtual environment
COMMAND="$PYTHON -m venv ${VIRTUALENV}"
echo "Creating a new virtual environment at ${VIRTUALENV}..."
eval $COMMAND || {
echo "--------------------------------------------------------------------"
echo "ERROR: Failed to create the virtual environment. Check that you have"
echo "the required system packages installed and the following path is"
echo "writable: ${VIRTUALENV}"
echo "--------------------------------------------------------------------"
exit 1
}
# Activate the virtual environment
. ${VIRTUALENV}/bin/activate
# Install necessary system packages
COMMAND="pip install wheel -i https://mirrors.aliyun.com/pypi/simple"
echo "Installing Python system packages ($COMMAND)..."
eval $COMMAND || exit 1
# Install requirement packages
COMMAND="pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple"
echo "Installing Python requirement packages ($COMMAND)..."
eval $COMMAND || exit 1
# COMMAND="pip install -r requirements-prod.txt -i https://mirrors.aliyun.com/pypi/simple"
# echo "Installing Python requirement prod packages ($COMMAND)..."
# eval $COMMAND || exit 1
# default .env settings
cp config/.env.dev config/.env
mkdir -p /opt/idcops/data/
# settings databases
# Use sqlite3 by default
# migrate
python manage.py makemigrations
python manage.py migrate --no-input
# collect static files
python manage.py collectstatic --no-input
# create django super user
UserName=admin
UserEmail=admin@idcops.cn
UserPass=$(openssl rand -base64 12)
ImportUser="from django.contrib.auth import get_user_model; User = get_user_model();"
DeleteUser="User.objects.filter(username='${UserName}').delete();"
CreateUser="User.objects.create_superuser('${UserName}', '${UserEmail}', '${UserPass}')"
echo "${ImportUser} ${DeleteUser} ${CreateUser}" | python manage.py shell
echo -e "用户名:${UserName}\n用户密码:${UserPass}"
echo -e "账户密码可以查看 install.log 文件"
echo -e "SECRET_KEY: ${SECRET_KEY}\n" > install.log
echo -e "Server: http://${SrvAddr}:${SrvPort}/\nUsername: ${UserName}\nPassword: ${UserPass}\nEmail: ${UserEmail}" >> install.log
echo -e "Server: http://${SrvAddr}:${SrvPort}/\nUsername: ${UserName}\nPassword: ${UserPass}\nEmail: ${UserEmail}"
touch install.lock
RUN_SERVER="nohup ${VIRTUALENV}/bin/gunicorn --workers 3 \
--bind ${SrvAddr}:${SrvPort} \
--pid ${PidFile} \
--access-logfile ${LogFile} \
--pythonpath ${ProjDir} \
--reload \
proj.wsgi:application > /dev/null 2>&1 &"
eval ${RUN_SERVER}
echo '#!/bin/sh' > ${ProjDir}/scripts/start.sh
echo "${RUN_SERVER}" >> ${ProjDir}/scripts/start.sh
echo '#!/bin/sh' > ${ProjDir}/scripts/stop.sh
echo "kill \`cat ${PidFile}\`" >> ${ProjDir}/scripts/stop.sh
chmod +x ${ProjDir}/scripts/start.sh ${ProjDir}/scripts/stop.sh
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。