前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >fabric自动发布django博客系统

fabric自动发布django博客系统

作者头像
以谁为师
发布2019-05-31 20:41:34
4730
发布2019-05-31 20:41:34
举报
文章被收录于专栏:小网管的运维之路

安装模块

代码语言:javascript
复制
pip -V
# 查看是不是pip3

pip install fabric3
# 安装模块

fabric 命令列表

fab list

代码语言:javascript
复制
fab ping     #测试主机ping外网...
fab upload #上传代码到git
fab deploy #部署到服务器

vi fabfile.py

代码语言:javascript
复制
from fabric.api import env,run,cd,local,task,abort
from fabric.colors import green,yellow,red
from fabric.context_managers import settings,hide
from fabric.contrib.console import confirm 

import datetime
nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

env.user = 'root'
env.port = '55555'
env.hosts = 'ops.attacker.club' #主机地址
env.key_filename = ['~/.ssh/id_rsa'] #本地秘钥



def get_git_status():
    git_status_result = local("git status", capture=True)
    #if "无文件要提交,干净的工作区" not in git_status_result:
    if "modified"  in git_status_result:
        print (red("当前分支还有文件没有提交"))
        print (git_status_result)
        abort("已经终止")

def local_unit_test():
    with settings(warn_only=True):
        test_result = local("python3 manage.py test")
        if test_result.failed:
            print (test_result)
            if not confirm(red("单元测试失败,是否继续?")):
                abort("已经终止")


def download_code():
    run("git checkout .")
    run("git pull")
    print(green("\n[%s] 完成代码下载" % env.hosts))

def app ():
    run("pip3 install -i http://mirrors.aliyun.com/pypi/simple   --trusted-host mirrors.aliyun.com  -r  requirements.txt")
    run("python3 manage.py collectstatic --noinput &&python3 manage.py migrate")
    run('''sed -i "/ALLOWED_HOSTS/c ALLOWED_HOSTS= \['127.0.0.1','.attacker.club'\]" mysite/settings.py ''' )
    run("sed -i 's#/data/LJblog#/www/django/blog#' mysite/settings.py")
    run("sed -i 's/DEBUG = True/DEBUG = False/' mysite/settings.py")
    run("/usr/bin/supervisord -c /etc/supervisor/supervisord.conf")
    print(green("\n[%s] app完成部署" % env.hosts))



@task
def upload():
    with settings(hide('running','stderr'), warn_only=True):
        local("git add .")
        local("git commit -m '%s 提交' " % (nowTime))
        local("git push")
        print(green("\n\n 完成代码上传"))


@task
def deploy():
    with settings(hide('running','stderr'), warn_only=True):
        with cd("/www/django/blog"):
            get_git_status()
            local_unit_test()
            run ("pkill sup")
            download_code()
            app()
            print(yellow("\n\n[%s] 部署完毕 !!!" % env.hosts))



@task
def ping ():
    with settings(hide('running','stderr'),warn_only=True):
        run("ping -c 1 114.114.114.114")

sed替换settings.py里面 ALLOWED_HOSTS允许地址、DEBUG模式、static_root静态文件路径

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年4月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装模块
  • fabric 命令列表
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档