fabric 部署 python web 应用的基本config

来源:互联网 发布:获取windows 7密钥 编辑:程序博客网 时间:2024/05/17 07:28
"""Fabric commands to setup and run serverCopy fabhosts-example.py to fabhosts.py and add host configurations foryour deployment."""from fabric.api import cd, env, lcd, put, prompt, local, sudo, prefixfrom fabric.contrib.files import existsfrom fabric.contrib.console import confirmtry:    from fabhosts import prodexcept ImportError:    pass# configlocal_app_dir = '.'local_flask_dir = local_app_dir + '/baxter'local_config_dir = local_app_dir + '/server-config'remote_app_dir = '/home/www/baxter-flask'remote_git_dir = '/home/git'remote_flask_dir = remote_app_dir + '/baxter'remote_nginx_dir = '/etc/nginx/sites-enabled'remote_supervisor_dir = '/etc/supervisor/conf.d'# tasksdef install_requirements():    """    Install required system wide packages.    Includes exporting some info so GDAL can be used to build in pip    http://gis.stackexchange.com/questions/28966/python-gdal-package-missing-header-file-when-installing-via-pip    """    sudo('apt-get update')    sudo('apt-get upgrade')    sudo('apt-get install -y python python-dev python-pip python-virtualenv')    sudo('apt-get install -y nginx')    sudo('apt-get install -y postgresql-9.4 postgresql-9.4-postgis-2.1')    sudo('apt-get install -y postgresql-server-dev-9.4')    sudo('apt-get install -y libpq-dev')    sudo('apt-get install -y git')    sudo('apt-get install -y gdal-bin python-gdal libgdal-dev')    sudo('apt-get install -y supervisor')    sudo('apt-get install -y redis-server')    sudo('apt-get install -y libxslt1.dev')    # for pillow    sudo('apt-get install -y libjpeg8-dev libjpeg-dev libfreetype6-dev')    sudo('apt-get install -y zlib1g-dev libpng12-dev')    # So that GDAL can be built    sudo('export CPLUS_INCLUDE_PATH=/usr/include/gdal')    sudo('export C_INCLUDE_PATH=/usr/include/gdal')def create_www_user():    """    Create a user for running it    """    password = prompt('Password for baxter-www user?')    sudo('adduser --quiet baxter-www')    sudo('chpasswd '.format(password))    # not finisheddef setup_database():    """    Setup postgres database with postgis extension    """    with cd('/'):        sudo('su postgres')        # not finisheddef install_flask():    """    1. Create project directories    2. Create and activeate a virtualenv    3. Copy Flask files to remote host    """    if exists(remote_app_dir) is False:        sudo('mkdir ' + remote_app_dir)    if exists(remote_flask_dir) is False:        sudo('mkdir' + remote_flask_dir)    with lcd(local_app_dir):        with cd(remote_app_dir):            sudo('virtualenv ../env')            sudo('source ../env/bin/activate')            put('*', './', use_sudo=True)            sudo('pip install -r requirements.txt')            sudo('pip install PIL --allow-external PIL --allow-unverified PIL')def configure_nginx():    """    1. Remove default nginx config file    2. Create new config file    3. Setup new symbolic link    4. Copy local config to remote config    5. Restart nginx    """    sudo('service nginx start')    if exists('/etc/nginx/sites-enabled/default'):        sudo('rm /etc/nginx/sites-enabled/default')    if exists('/etc/nginx/sites-available/baxter-flask') is False:        sudo('touch /etc/nginx/sites-available/baxter-flask')        sudo('ln -s /etc/nginx/sites-available/baxter-flask' +             ' /etc/nginx/sites-enabled/baxter-flask')    with lcd(local_config_dir):        with cd(remote_nginx_dir):            put('./baxter-flask', './', use_sudo=True)    sudo('service nginx restart')def configure_supervisor():    """    1. Create new supervisor config file    2. Copy local config to remote config    3. Register new command    """    if exists('/etc/supervisor/conf.d/baxter_flask.conf') is False:        with lcd(local_config_dir):            with cd(remote_supervisor_dir):                put('baxter_flask.conf', './', use_sudo=True)                sudo('supervisorctl reread')                sudo('supervisorctl update')def configure_git():    """    1. Setup bare Git repo    2. Create post-recieve hook    """    if exists(remote_git_dir) is False:        sudo('mkdir ' + remote_git_dir)        with cd(remote_git_dir):            sudo('mkdir baxter-flask.git')            with cd('baxter-flask.git'):                sudo('git init --bare')                with lcd(local_config_dir):                    with cd('hooks'):                        put('./post-receive', './', use_sudo=True)                        sudo('chmod +x post-receive')def configure_gunicorn():    """    1. Setup gunicorn starter file    """    if exists('/home/www/gunicorn-start-baxter') is False:        with cd(remote_app_dir):            with lcd(local_config_dir):                put('./gunicorn-start-baxter', './', use_sudo=True)                sudo('chmod +x gunicorn-start-baxter')def configure_ufw():    """    1. Allow ssh, http, https, and postgres connections through ufw    2. Enable ufw    """    sudo('ufw allow ssh')    sudo('ufw allow www')    sudo('ufw allow https')    sudo('ufw allow postgres')    sudo('ufw enable')def create():    """    Setup a brand new server    """    install_requirements()    install_flask()    configure_gunicorn()    configure_nginx()    configure_supervisor()    configure_git()def run_app():    """    Run the app    """    with cd(remote_flask_dir):        sudo('supervisorctl start baxter_flask')def create_roles():    """    Create roles for users    """    with cd(remote_app_dir):        with prefix('source /home/www/baxter-flask/server-config/host-export'):            with prefix('source /home/www/env/bin/activate'):                sudo('python manage.py user create_role -n admin')                sudo('python manage.py user create_role -n user')                sudo('python manage.py user create_role -n contributor')def deploy():    """    1. Commit and push new flask files to production    2. Restart gunicorn via supervisor on production    """    with lcd(local_app_dir):        local('git add -A')        commit_message = prompt('Commit message?')        local('git commit -am "{0}"'.format(commit_message))        local('git push production master')        sudo('supervisorctl restart baxter_flask')def rollback():    """    1. Quick rollback in case of error    2. Restart gunicorn via supervisor    """    with lcd(local_app_dir):        local('git revert master --no-edit')        local('git push production master')        sudo('supervisorctl restart baxter_flask')def status():    """    Is the app live?    """    sudo('supervisorctl status')    sudo('service nginx status')    sudo('service postgresql status')    sudo('service ufw status')    sudo('service redis-server status')def add_baxter_user():    """    Add a user to the website    """    with cd(remote_app_dir):        with prefix('source /home/www/baxter-flask/server-config/host-export'):            with prefix('source /home/www/env/bin/activate'):                email = prompt('Email address:')                password = prompt('Password:')                admin = confirm('Is {0} an admin?'.format(email), default=False)                sudo('python manage.py user create_user -e {0} -p {1} -a y'.format(email, password))                if admin:                    sudo('python manage.py user add_role -u {0} -r admin'.format(email))

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 咸鱼上买的东西有问题怎么办 咸鱼买到的东西有问题怎么办 天猫超市买的东西有问题怎么办 满白天的宝宝睡觉总哭怎么办 京东退货显示在售后检测要怎么办 装修公司定金交了公司关门怎么办 背冷、腰泠、屁股冷、大腿冷怎么办 买家退款把卖家地址写错了怎么办 退款编号写错了提交了怎么办 京东购物收到货第二天搞活动怎么办 玩王者荣耀来电话断网怎么办 苹果7p玩王者荣耀卡怎么办 手机丢了隐私空间里面的照片怎么办 微信内存满了打不开了怎么办 u盘内存满了打不开了怎么办 清算组出的报告书无法清算怎么办 手机停机了收不到快递取件码怎么办 买东西货到了不小心确认收货怎么办 不小心用了蚂蚁花呗怎么办 q微店忘记密码怎么办微忘记密 在优酷买了开通会员不想续费怎么办 微店怎么卖吃的要认证怎么办 在天猫买东西遇到不讲理卖家怎么办 天猫退货收到退款了卖家拒绝怎么办 申请退款保证金不够的情况下怎么办 发奖品把钱骗走了应该怎么办? 淘宝东西没到自动确认了收货怎么办 淘宝买的东西收不到货怎么办 资料如实填了 怎么办不下来借款啊 如果淘宝店家跑了没收到货怎么办 淘宝店家退完款把货又发了怎么办 淘宝收到货有问题客服不回复怎么办 手机从美国寄回中国被海关扣怎么办 摩托车新买的后悔了想卖怎么办? 淘宝单号输错了怎么办运费险 在淘宝卖东西快递单号输错了怎么办 淘宝买东西签收后发现坏了怎么办 摩托车购车发票写了别人名字怎么办 新领的发票跟电脑对不上号怎么办 淘宝未发货退款卖家拒绝怎么办 在超市买东西把票丢了不让出怎么办