Skip to content

Instantly share code, notes, and snippets.

@f213
Created January 6, 2019 10:11
Show Gist options
  • Save f213/7bc283b67ebd34f54fb436973ac48f4f to your computer and use it in GitHub Desktop.
Save f213/7bc283b67ebd34f54fb436973ac48f4f to your computer and use it in GitHub Desktop.
Fabfile example
from fabric.api import env, run as fabric_run
from fabric.contrib.project import rsync_project
env.app_path = '/home/backend'
env.user = 'circle'
env.use_ssh_config = True
env.disable_knodwn_hosts = True
env.colorize_errors = True
def run(cmd, *args, **kwargs):
fabric_run('cd %s && %s' % (env.app_path, cmd), *args, **kwargs)
def run_in_venv(cmd):
run('cd %s && . venv/bin/activate && %s' % (env.app_path, cmd))
def manage_py(*args):
run_in_venv('cd src && ./manage.py %s' % ' '.join(args))
def upload():
return rsync_project(
local_dir='src/',
remote_dir=env.app_path + '/src/',
delete=True,
exclude=[
'.git',
'.env',
'__pycache__',
'*.pyc',
'celerybeat-schedule',
'celerybeat.pid',
],
extra_opts='--links --omit-dir-times',
)
def deploy():
upload()
run_in_venv('pip install -r src/requirements.txt') # install new requirements
manage_py('migrate', '--noinput')
manage_py('compilemessages')
manage_py('remove_stale_contenttypes', '--noinput')
manage_py('collectstatic', '--noinput', '--clear')
run('touch reload') # tell uwsgi to reload application code
# restart background celery workers
run('sudo /usr/bin/supervisorctl restart default-worker')
run('sudo /usr/bin/supervisorctl restart beat')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment