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的更多相关文章

  1. [Python] Create a Django project in Pycharm

    From: http://blog.csdn.net/u013088062/article/details/50158239 From: http://blog.csdn.net/u013088062 ...

  2. Web.config Transformation Syntax for Web Application Project Deployment

    Web.config Transformation Syntax for Web Application Project Deployment Other Versions   Updated: Ma ...

  3. 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 ...

  4. 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 ...

  5. Django project troubleshootings

    1. 当django project文件夹放到cgi-bin目录下面时会出现下面的错误: [Wed Jan 09 01:52:52.611690 2019] [core:notice] [pid 15 ...

  6. django project 的快速构建

    2003年,堪萨斯(Kansas)州 Lawrence 城中的一个 网络开发小组 ——World Online 小组,为了方便制作维护当地的几个新闻站点(一般要求几天或者几小时内被建立),Adrian ...

  7. 18 12 30 新建一个 django project

    1. 新建一个 django project 1 2 django-admin.py startproject project_name 特别是在 windows 上,如果报错,尝试用 django- ...

  8. 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 ...

  9. PyCharm‘s Project Deployment

    当在本地写完项目,部署到服务器上调试的时候,难免会碰到代码的修修改改,但由于项目在服务器上,修改起来相对麻烦.各路大神或许有自己的方法去解决.这篇博客演示利用PyCharm的Deployment功能, ...

随机推荐

  1. linux 实践到的命令 collection

    查看文件夹/文件 大小:du   :(disk usage) 要通过 1024 字节块概述一个目录树及其每个子树的磁盘使用情况,请输入: du -k /home/fran/filename 这在/ho ...

  2. 【spring】- springmvc 工作原理

    原理 本质是将DispatcherServlet及关联的Spring上下文环境的初始化工作织入Servlet的生命周期内,将外部WEB请求转换为Spring Bean能处理的形式,然后将处理后的结果借 ...

  3. 服务器启动完成执行定时任务Timer,TimerTask

    由于项目需求:每隔一段时间就要调外部接口去进行某些操作,于是在网上找了一些资料,用了半天时间弄好了,代码: import java.util.TimerTask; public class Accou ...

  4. 【Cf #290 B】Fox And Jumping(dp,扩展gcd)

    根据裴蜀定理,当且仅当选出来的集合的L[i]的gcd等于1时,才能表示任何数. 考虑普通的dp,dp[i][j]表示前i个数gcd为j的最少花费,j比较大,但状态数不多,拿个map转移就好了. $ \ ...

  5. 第一章:CDib类库的建立

    VC++图像处理程序设计(第1版)    杨淑莹 编著     边奠英 主审 第一章 位图基础 Joanna-In-Hdu&Hust 手工打,印象更深刻 使用工具 VS2010 mfc  整本 ...

  6. D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...

  7. poi导出word表格详解 超详细了

    转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138   一.效果如下 二.js代码 function export_word( ...

  8. Python 爬虫入门(一)

    毕设是做爬虫相关的,本来想的是用java写,也写了几个爬虫,其中一个是爬网易云音乐的用户信息,爬了大概100多万,效果不是太满意.之前听说Python这方面比较强,就想用Python试试,之前也没用过 ...

  9. Git3:Git分支

    目录 一.概念 二.创建与合并分支 1.创建分支原理分析 2.创建分支语法 三.解决冲突 四.分支管理策略 1.保留分支历史 2.分支管理原则 五. bug分支 六.推送和拉取远程分支 一.概念 分支 ...

  10. ios中iframe的scroll滚动事件替代方法

    在公众号的开发中,遇到ios中iframe的scroll滚动事件失效,在此做下记录. 因为接口获取的数据必须放在iframe中展示,滚动到底部按钮变亮,如图: 代码如下: <!DOCTYPE h ...