Prepare tasks for django project deployment.md
As we know, there are some boring tasks while deploy Django project, like create db, do migrations and so on, here I wrote a shell script to auto do these things
#!/bin/bash
# The shell is used to do some prepare tasks for deploying TMS, including:
# (1) create new user to run TMS;
# (2) create database and add privileges;
# (3) extract software to the new user's home directionay;
# (4) modify the system database related configuration;
# (5) create necessary tables for TMS;
# (6) add administartor for TMS;
# (7) run MQ as a daemon;
# (8) deploy TMS with nginx.
# Note: please exec this sh as root.
PACKAGE='tms-1.0.tar.gz'
echo "The processing will spend several mins, please waiting patiently..."
echo "(1) create new user to run TMS"
USER='tmsx'
ACCOUNT_NUM=`cat /etc/passwd | grep "$USER" |wc -l`
if [ $ACCOUNT_NUM -eq 0 ] && [ ! -d "/home/$USER" ]
then
echo "Please enter user password, default is '123456'"
read USER_PASSWORD
[ "${USER_PASSWORD}" == "" ] && USER_PASSWORD='123456'
useradd $USER -p `openssl passwd -1 ${USER_PASSWORD}` -d /home/$USER -m -c "termianl manage system default user"
echo "$USER ALL=NOPASSWD:ALL" >> /etc/sudoers
if [ $? -eq 0 ]
then
su $USER -c 'echo "" | ssh-keygen -t rsa'
fi
fi
echo ""
echo "(2) create database and add privileges, "
echo "Please enter database host, default is 'localhost'"
read HOST
[ "$HOST" == "" ] && HOST='localhost'
echo "Please enter the database root user password"
read ROOT_PASSWORD
[ "$ROOT_PASSWORD" == "" ] && echo "Need root password" && exit 1
DBNAME='tmsx'
MYSQL_CMD="mysql -h${HOST} -uroot -p${ROOT_PASSWORD}"
CREATE_DB_SQL="CREATE DATABASE IF NOT EXISTS ${DBNAME} CHARACTER SET utf8;"
echo ${CREATE_DB_SQL} | ${MYSQL_CMD}
if [ $? -ne 0 ]
then
echo "create database failed..."
exit 1
fi
GRANT_PRIVILEGES_SQL="GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${USER}'@'%' IDENTIFIED BY '${USER_PASSWORD}';"
echo ${GRANT_PRIVILEGES_SQL} | ${MYSQL_CMD}
if [ $? -ne 0 ]
then
echo "grant failed..."
exit 1
fi
echo ""
echo "(3) extract software to the new user's home directionay"
if [ -d /home/$USER ]
then
cp $PACKAGE /home/$USER
cd /home/$USER
tar xf $PACKAGE
else
echo "no dir /home/$USER"
exit 1
fi
echo ""
echo "(4)modify the system database related configuration"
TARGET=`basename $PACKAGE .tar.gz`
DB_SETTING="
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '${HOST}',
'NAME': '${DBNAME}',
'USER': '${USER}',
'PASSWORD': '${USER_PASSWORD}',
}
}"
if [ -d /home/$USER/$TARGET ] && [ -f /home/$USER/$TARGET/tms/settings.py ]
then
echo $DB_SETTING >> /home/$USER/$TARGET/tms/settings.py
else
echo "modify the system database related configuration"
exit 1
fi
echo ""
echo "(5) create necessary tables for TMS"
if [ -d /home/$USER/$TARGET ]
then
cd /home/$USER/$TARGET
python manage.py makemigrations tmsApp && python manage.py migrate
fi
echo ""
if [ $? -ne 0 ]
then
echo "create tables for TMS failed..."
exit 1
fi
echo "(6) add administartor for TMS"
if [ -f /home/$USER/$TARGET/manage.py ]
then
cd /home/$USER/$TARGET
python manage.py createsuperuser
fi
echo ""
echo "(7) run MQ as a daemon"
MQ_SETTING="
description \"TMS MQ daemon\"
author \"<Andy Wu>\"
start on startup
stop on shutdown
respawn
respawn limit 20 5
pre-start script
end script
script
python /home/$USER/$TARGET/manage.py runMSGServer
end script
post-start script
end script"
DAEMON_CFG_FILE='/etc/init/tms-mq.conf'
if [ -f $DAEMON_CFG_FILE ]
then
status=`service tms-mq status`
ret=`echo $status | grep 'running' | wc -l`
if [ "$ret" != "0" ]; then
echo >&1 "ERROR: tms-mq daemon already run."
exit 1
fi
fi
echo "$MQ_SETTING" > $DAEMON_CFG_FILE
status=`service tms-mq status`
ret=`echo $status | grep 'waiting' | wc -l`
if [ "$ret" = "0" ]; then
echo >&1 "ERROR: Could not add tms-mq daemon"
exit 1
fi
service tms-mq start
sleep 1
status=`service tms-mq status`
ret=`echo $status | grep 'running' | wc -l`
if [ "$ret" = "0" ]; then
echo >&1 "ERROR: Could not start tms-mq daemon"
exit 1
fi
echo ""
echo "(8) deploy TMS with nginx"
# install nginx if need
if grep "Ubuntu" /etc/issue
then
if ! dpkg -l | grep "nginx"
then
apt-get install python-dev nginx || exit 1
fi
fi
if grep "CentOS" /etc/issue
then
if ! yum list nginx
then
yum install epel-release
yum install python-devel nginx || exit 1
fi
fi
if ! pip list | grep supervisor
then
pip install supervisor || exit 1
fi
if ! pip list | grep uwsgi
then
pip install uwsgi || exit 1
fi
echo_supervisord_conf > /etc/supervisord.conf
SUPERVISORD="
[program:tms]\n
command=uwsgi --ini /home/$USER/$TARGET/uwsgi.ini\n
directory=/home/$USER/$TARGET\n
startsecs=0\n
stopwaitsecs=0\n
autostart=true\n
autorestart=true\n"
echo $SUPERVISORD >> /etc/supervisord.conf
supervisord -c /etc/supervisord.conf
supervisorctl -c /etc/supervisord.conf restart tms
if [ ! -f /home/$USER/$TARGET/reload ]
then
echo > /home/$USER/$TARGET/reload
fi
UWSGI="
[uwsgi]\n
socket = 127.0.0.1:8077\n
chdir = /home/$USER/$TARGET\n
wsgi-file = tms/wsgi.py\n
touch-reload=/home/$USER/$TARGET/reload\n
processes = 2\n
threads = 4\n
chmod-socket = 664\n
chown-socket=$USER:www-data\n"
echo -e $UWSGI > /home/$USER/$TARGET/uwsgi.ini
#restart supervisor
supervisorctl -c /etc/supervisord.conf restart tms
#config nginx
TMS_CONF="
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location /static {
alias /home/$USER/$TARGET/static;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8077;
}
}"
echo -e $TMS_CONF > /etc/nginx/sites-available/tms.conf
ln -s /etc/nginx/sites-available/tms.conf /etc/nginx/sites-enabled/tms.conf
service nginx reload
echo "Congratulations!"
Prepare tasks for django project deployment.md的更多相关文章
- [Python] Create a Django project in Pycharm
From: http://blog.csdn.net/u013088062/article/details/50158239 From: http://blog.csdn.net/u013088062 ...
- Web.config Transformation Syntax for Web Application Project Deployment
Web.config Transformation Syntax for Web Application Project Deployment Other Versions Updated: Ma ...
- Start Your Django Project in Nginx with uWsgi
Step 0:Install A,B,C,blabla needed This can be seen in my another article in the blog.click here(una ...
- Apache:To Config The Vhost of Django Project
It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...
- Django project troubleshootings
1. 当django project文件夹放到cgi-bin目录下面时会出现下面的错误: [Wed Jan 09 01:52:52.611690 2019] [core:notice] [pid 15 ...
- django project 的快速构建
2003年,堪萨斯(Kansas)州 Lawrence 城中的一个 网络开发小组 ——World Online 小组,为了方便制作维护当地的几个新闻站点(一般要求几天或者几小时内被建立),Adrian ...
- 18 12 30 新建一个 django project
1. 新建一个 django project 1 2 django-admin.py startproject project_name 特别是在 windows 上,如果报错,尝试用 django- ...
- Django project structure: how does static folder, STATIC_URL, STATIC_ROOT work
So I've been messing up with Django(1.6+) project setting for quite sometime, this is what i finally ...
- PyCharm‘s Project Deployment
当在本地写完项目,部署到服务器上调试的时候,难免会碰到代码的修修改改,但由于项目在服务器上,修改起来相对麻烦.各路大神或许有自己的方法去解决.这篇博客演示利用PyCharm的Deployment功能, ...
随机推荐
- 使用jQuery在javascript中自定义事件
js中的自定义事件有attachEvent,addEventListener等等好多种,往往受困于浏览器兼容,而且代码写起来也相当麻烦.jQuery为我们解决了这个问题,几行代码就可以很好的实现事件的 ...
- 初探Java 9 的的模块化
Java 9中最重要的功能,毫无疑问就是模块化(Module),它将自己长期依赖JRE的结构,转变成以Module为基础的组件,当然这在使用Java 9 开发也和以前有着很大的不同. Java8或更加 ...
- 美团codeM之美团代金券
前天做了下美团的一个codeM比赛的资格赛,遇到一个题目挺有意思的,所以现在做一下总结. 题目描述 美团的每一个用户都有一个用户代金券的消费记录日志,每位用户都能购买若干种代金券,但是每一种代金券最多 ...
- BZOJ5389 比例查询 【离线】
题目链接 BZOJ5389 题解 太\(sb\)了,这种题都想不出来 发现复杂度允许\(n\sqrt{n}\),我们可以对于每个位置\(\sqrt{n}\)枚举约数,然后维护比例的最晚出现的位置,维护 ...
- 解题:POI 2009 Fire Extinguishers
题面 洛谷数据非常水,建议去bzoj 我第一眼一看这不是那个POI2011的升级版吗(明明这个是2009年的,应该说那个是这个的弱化版,果然思想差不多. 因为$k$很小,可以考虑每个间隔距离来转移.我 ...
- ab输出信息解释以及Failed requests原因分析
ab是apache自带的压力测试工具.ab进行的一切测试本质上是基于HTTP的.下面是对ab输出项信息的解释和出现Failed requests原因分析.测试实例:1. ab输出信息说明: 1 2 ...
- 读Bayes' Theorem
Bayes' Theorem定理的原理说明,三个简单的例子来说明用法及一些练习. Bayes' Theorem就是概率问题,论文相对比较好理解,也不必做什么笔记.
- Python【经典类与新式类】
经典类多继承的调用方法顺序是:深度优先查询,如下图: 新式类多继承的调用方法顺序是:广度优先查询,如下图: 可以使用下面的代码进行实验验证: #经典类class A: def __init__ ...
- Date时间格式比较大小
方法一: 两个Date类型的变量可以通过compareTo方法来比较.此方法的描述是这样的:如果参数 Date 等于此 Date,则返回值 0:如果此 Date 在 Date 参数之前,则返回小于 0 ...
- IOS数组的排序和筛选
1.排序 [self.tableItems sortUsingComparator:^NSComparisonResult(GPBTeacherBrief* obj1, GPBTeacherBrief ...