def pull(branch):
''' The git pull command. '''
run('git pull origin %s' % branch)
# TODO: Custom origin
python类run()的实例源码
def sync(branch):
''' Sync the current HEAD with the remote(origin)'s branch '''
run('git reset --hard origin/%s' % branch)
# TODO: Custom origin
def last_commit(remote=True, short=False):
'''
Get the last commit of the git repository.
Note: This assumes the current working directory (on remote or local host)
to be a git repository. So, make sure current directory is set before using this.
'''
cmd = 'git rev-parse{}HEAD'.format(' --short ' if short else ' ')
with hide('everything'):
result = run(cmd) if remote else local(cmd, capture=True)
return result.strip()
def show_last_commit():
''' Display the last commit. '''
run('git log -1')
def run(command, remote=True):
''' Run a command using fabric. '''
if remote:
return _run(command)
else:
return _local(command)
def get_fn():
"""
Returns the correct function call for the environment.
"""
return run if renv == 'prd' else local
def on_service(name):
"""
Define service where command should run
"""
global service_name
service_name = name
def make_doc():
with lcd('../docs'):
local('make html')
local('cp -rf _build/html/* /var/www/mendelmd_static/docs/')
#def backup():
# run(' mysqldump -u root -p mendelmd14 | gzip > db_backup/mendelmd151012.sql.gz ')
def create_sample_data():
#backup all users
# with cd('/projects/www/mendelmd14'):
#
# run('mysqldump -u root -p mendelmd14 auth_user account_account profiles_profile | gzip > db_backup/users.sql.gz')
# #get sample from individuals
# run('mysqldump -u root -p --where="individual_id < 16" mendelmd14 individuals_variant | gzip > db_backup/individual_variants_sample.sql.gz')
# run('mysqldump -u root -p --where="id < 16" mendelmd14 individuals_individual | gzip > db_backup/individuals_sample.sql.gz')
get('/projects/www/mendelmd14/db_backup/users.sql.gz', '/home/raony/sites/mendelmd14/db_backup/')
get('/projects/www/mendelmd14/db_backup/individual_variants_sample.sql.gz', '/home/raony/sites/mendelmd14/db_backup/')
get('/projects/www/mendelmd14/db_backup/individuals_sample.sql.gz', '/home/raony/sites/mendelmd14/db_backup/')
def loaddata():
#Load user and sample from individuals
local('gunzip < db_backup/users.sql.gz | mysql -u root -p mendelmd')
local('gunzip < db_backup/individual_variants_sample.sql.gz | mysql -u root -p mendelmd ')
local('gunzip < db_backup/individuals_sample.sql.gz | mysql -u root -p mendelmd ')
# run(' gunzip < db_backup/mendelmd151012.sql.gz | mysql -u root -p mendelmd14 ')
# local("""python manage.py loaddata db_backup/all_without_individuals.json.gz""")
def deploy(message="changes (fabric)"):
local('git add .; git commit -m "%s";git push' % (message))
with cd('/projects/www/mendelmd'):
# run('git reset --hard HEAD')
run('git pull')
# run('source virtualenvwrapper.sh && workon genome_research && python manage.py syncdb --noinput')
run('sudo /etc/init.d/apache2 restart')
def unlock():
"""os x servers need to be unlocked"""
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path):
run('security unlock-keychain')
def create_virtualenv():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with quiet():
env_vpath = os.path.join(env.envs_path, 'dprr-' + env.srvr)
if run('ls {}'.format(env_vpath)).succeeded:
print(
green('virtual environment at [{}] exists'.format(env_vpath)))
return
print(yellow('setting up virtual environment in [{}]'.format(env_vpath)))
run('virtualenv {}'.format(env_vpath))
def clone_repo():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with quiet():
if run('ls {}'.format(os.path.join(env.path, '.git'))).succeeded:
print(green(('repository at'
' [{}] exists').format(env.path)))
return
print(yellow('cloning repository to [{}]'.format(env.path)))
run('git clone --recursive {} {}'.format(REPOSITORY, env.path))
def makemigrations(app=None):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['dev', 'stg', 'liv']:
print(yellow('Do not run makemigrations on the servers'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py makemigrations {}'.format(app if app else ''))
def update_index():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py build_solr_schema > schema.xml')
run('mv schema.xml ../../solr/collection1/conf/')
sudo('service tomcat7-{} restart'.format(env.srvr))
run('./manage.py update_index')
def clear_cache():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py clear_cache')
def collect_static(process=False):
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
if env.srvr in ['local', 'vagrant']:
print(yellow('Do not run collect_static on local servers'))
return
with cd(env.path), prefix(env.within_virtualenv):
run('./manage.py collectstatic {process} --noinput'.format(
process=('--no-post-process' if not process else '')))
def install_requirements():
require('srvr', 'path', 'within_virtualenv', provided_by=env.servers)
reqs = 'requirements-{}.txt'.format(env.srvr)
try:
assert os.path.exists(reqs)
except AssertionError:
reqs = 'requirements.txt'
with cd(env.path), prefix(env.within_virtualenv):
run('pip install -U -r {}'.format(reqs))