def script(name=None): project.rsync_project(local_dir='scripts', remote_dir=env.remote_dir + '/scripts', delete=True) with cd(env.remote_dir): remote(f'{env.py_bin} manage.py runscript {name}') remote('rm -rf scripts')
def dep(): ''' 安装系统依赖 ''' py = etc('FABRIC_PYTHON_PATH', default='/root/.pyenv/versions/3.7.4') os.path.exists('requirements.txt') or local( 'pip-compile --output-file=requirements.txt requirements/pre.txt') put('requirements.txt', '/tmp/requirements.txt') remote(f'{py}/bin/pip install -r /tmp/requirements.txt') remote('rm -rf /tmp/requirements.txt')
def mkmg(mode='pre'): ''' 服务器端数据库 migration 操作 ''' PYTHONPATH = f'/home/{env.user}/.pyenv/versions/{env.project}/bin' with cd(env.remote_dir): remote( f'DJANGO_SETTINGS_MODULE=config.settings.{mode} {PYTHONPATH}/python manage.py makemigrations' ) remote( f'DJANGO_SETTINGS_MODULE=config.settings.{mode} {PYTHONPATH}/python manage.py migrate' )
def deploy(): """ 服务器初始化部署 """ # 上传代码 project.rsync_project( remote_dir=env.remote_dir, exclude=env.excludes, local_dir='.', ) # 创建虚拟环境 if int( remote( f" [ -e /home/{env.user}/.pyenv/versions/{env.project} ] && echo 11 || echo 10" )) == 10: remote(f'pyenv virtualenv 3.7.7 {env.project}') # 上传必要文件(.env requirements tests scripts schedule.py) # project.rsync_project(remote_dir=env.remote_dir + '/requirements', local_dir='requirements') # 安装依赖类库 (pip install -r requirements.txt) PYTHONPATH = f'/home/{env.user}/.pyenv/versions/{env.project}/bin' with cd(env.remote_dir): # 创建数据库 待定 remote(f'{PYTHONPATH}/pip install -U pip') remote(f'{PYTHONPATH}/pip install -r requirements/pre.txt') # 导入数据 (manage.py migrate) mkmg(mode='pre') # remote(f'{PYTHONPATH}/python manage.py migrate') # 服务器端测试 remote(f'{PYTHONPATH}/python manage.py test -v2 tests') # 清理项目 clean-release # remote('make clean-release') rest()
def plan(): """crontab 任务部署""" # 上传 schedule.py put('schedule.py', env.remote_dir) # 切换远程目录 with cd(env.remote_dir): # 运行 schedule.py update remote('python schedule.py update') # 删除 schedule.py remote('rm -rf schedule.py')
def migr(): ''' 合并数据文件 ''' with cd(env.remote_dir): remote('''/root/.pyenv/versions/3.6.5/bin/python manage.py migrate''')
def stat(): ''' 更新静态文件 ''' with cd(env.remote_dir): remote('python manage.py collectstatic --noinput')